|
| 1 | +acontext("Invalid selector character validation") |
| 2 | + |
| 3 | +# Test that selector names with special characters cause errors |
| 4 | +# Note: Selector names come from the VALUES in the data when using .variable/.value pattern |
| 5 | +test_that("selector name with # causes error in clickSelects", { |
| 6 | + viz <- list( |
| 7 | + plot1 = ggplot() + |
| 8 | + geom_point( |
| 9 | + aes(Sepal.Length, Petal.Length), |
| 10 | + data = data.frame( |
| 11 | + Sepal.Length = 1:10, |
| 12 | + Petal.Length = rnorm(10), |
| 13 | + regularization = "# nearest neighbors", # This VALUE becomes the selector name |
| 14 | + parameter = 1:10 |
| 15 | + ), |
| 16 | + clickSelects = c(regularization = "parameter") |
| 17 | + ) |
| 18 | + ) |
| 19 | + |
| 20 | + expect_error( |
| 21 | + animint2dir(viz, open.browser = FALSE), |
| 22 | + "Invalid character\\(s\\) in selector name\\(s\\)", |
| 23 | + info = "Selector names with '#' should cause an error" |
| 24 | + ) |
| 25 | + |
| 26 | + expect_error( |
| 27 | + animint2dir(viz, open.browser = FALSE), |
| 28 | + "# nearest neighbors", |
| 29 | + info = "Error message should mention the problematic selector name" |
| 30 | + ) |
| 31 | +}) |
| 32 | + |
| 33 | +test_that("selector name with @ causes error in clickSelects", { |
| 34 | + viz <- list( |
| 35 | + plot1 = ggplot() + |
| 36 | + geom_point( |
| 37 | + aes(Sepal.Length, Petal.Length), |
| 38 | + data = data.frame( |
| 39 | + Sepal.Length = 1:10, |
| 40 | + Petal.Length = rnorm(10), |
| 41 | + regularization = "model@version1", # This VALUE becomes selector name |
| 42 | + parameter = 1:10 |
| 43 | + ), |
| 44 | + clickSelects = c(regularization = "parameter") |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + expect_error( |
| 49 | + animint2dir(viz, open.browser = FALSE), |
| 50 | + "Invalid character\\(s\\) in selector name\\(s\\)", |
| 51 | + info = "Selector names with '@' should cause an error" |
| 52 | + ) |
| 53 | + |
| 54 | + expect_error( |
| 55 | + animint2dir(viz, open.browser = FALSE), |
| 56 | + "model@version", |
| 57 | + info = "Error message should mention the problematic selector" |
| 58 | + ) |
| 59 | +}) |
| 60 | + |
| 61 | +test_that("selector name with ! causes error", { |
| 62 | + viz <- list( |
| 63 | + plot1 = ggplot() + |
| 64 | + geom_point( |
| 65 | + aes(x=1:10, y=rnorm(10)), |
| 66 | + data = data.frame( |
| 67 | + model = "model!important", |
| 68 | + parameter = 1:10 |
| 69 | + ), |
| 70 | + clickSelects = c(model = "parameter") |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + expect_error( |
| 75 | + animint2dir(viz, open.browser = FALSE), |
| 76 | + "Invalid character\\(s\\) in selector name\\(s\\)", |
| 77 | + info = "Selector names with '!' should cause an error" |
| 78 | + ) |
| 79 | +}) |
| 80 | + |
| 81 | +test_that("valid selector names work with clickSelects", { |
| 82 | + viz <- list( |
| 83 | + plot1 = ggplot() + |
| 84 | + geom_point( |
| 85 | + aes(x=1:10, y=rnorm(10)), |
| 86 | + data = data.frame( |
| 87 | + regularization = "polynomial_degree", # Valid name |
| 88 | + parameter = 0:9 |
| 89 | + ), |
| 90 | + clickSelects = c(regularization = "parameter") |
| 91 | + ) |
| 92 | + ) |
| 93 | + |
| 94 | + # Should NOT error |
| 95 | + info <- animint2dir(viz, open.browser = FALSE) |
| 96 | + expect_true(TRUE, info = "Valid selector names should not cause errors") |
| 97 | +}) |
| 98 | + |
| 99 | +test_that("selector names with spaces work", { |
| 100 | + viz <- list( |
| 101 | + plot1 = ggplot() + |
| 102 | + geom_point( |
| 103 | + aes(x=1:10, y=rnorm(10)), |
| 104 | + data = data.frame( |
| 105 | + regularization = "nearest neighbors", # spaces are OK |
| 106 | + parameter = 1:10 |
| 107 | + ), |
| 108 | + clickSelects = c(regularization = "parameter") |
| 109 | + ) |
| 110 | + ) |
| 111 | + |
| 112 | + # Should NOT error - spaces are fine |
| 113 | + info <- animint2dir(viz, open.browser = FALSE) |
| 114 | + expect_true(TRUE, info = "Selector names with spaces should work") |
| 115 | +}) |
| 116 | + |
| 117 | +test_that("multiple values with invalid characters all reported", { |
| 118 | + viz <- list( |
| 119 | + plot1 = ggplot() + |
| 120 | + geom_point( |
| 121 | + aes(x=1:10, y=rnorm(10)), |
| 122 | + data = data.frame( |
| 123 | + regularization = rep(c("#bad", "!worse"), 5), # Both have invalid chars |
| 124 | + parameter = 1:10 |
| 125 | + ), |
| 126 | + clickSelects = c(regularization = "parameter") |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + error_msg <- tryCatch( |
| 131 | + animint2dir(viz, open.browser = FALSE), |
| 132 | + error = function(e) as.character(e$message) |
| 133 | + ) |
| 134 | + |
| 135 | + # Should catch both bad selector names |
| 136 | + expect_match(error_msg, "Invalid character", info = "Should report invalid characters") |
| 137 | + expect_match(error_msg, "#bad|!worse", info = "Should mention at least one problematic selector") |
| 138 | +}) |
0 commit comments