diff --git a/tests/testthat/_snaps/c5.md b/tests/testthat/_snaps/c5.md new file mode 100644 index 0000000..26d6a98 --- /dev/null +++ b/tests/testthat/_snaps/c5.md @@ -0,0 +1,42 @@ +# check_args() works + + Code + spec <- C5_rules(trees = c(1, 2, 3)) %>% set_engine("C5.0") %>% set_mode( + "classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Error in `fit()`: + ! `tree` must be a whole number or `NULL`, not a double vector. + +--- + + Code + spec <- C5_rules(trees = 0) %>% set_engine("C5.0") %>% set_mode( + "classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Warning: + The number of trees should be `>= 1` and `<= 100` + Truncating to 1. + +--- + + Code + spec <- C5_rules(trees = 1000) %>% set_engine("C5.0") %>% set_mode( + "classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Warning: + The number of trees should be `>= 1` and `<= 100` + Truncating to 100. + +--- + + Code + spec <- C5_rules(min_n = c(1, 2, 3)) %>% set_engine("C5.0") %>% set_mode( + "classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + Condition + Error in `fit()`: + ! `min_n` must be a whole number or `NULL`, not a double vector. + diff --git a/tests/testthat/_snaps/cubist.md b/tests/testthat/_snaps/cubist.md new file mode 100644 index 0000000..09f5b4f --- /dev/null +++ b/tests/testthat/_snaps/cubist.md @@ -0,0 +1,64 @@ +# check_args() works + + Code + spec <- cubist_rules(committees = c(1, 2, 3)) %>% set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Error in `fit()`: + ! `committees` must be a whole number or `NULL`, not a double vector. + +--- + + Code + spec <- cubist_rules(committees = 0) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of committees should be `>= 1` and `<= 100`. + Truncating to 1. + +--- + + Code + spec <- cubist_rules(committees = 1000) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of committees should be `>= 1` and `<= 100`. + Truncating to 100. + +--- + + Code + spec <- cubist_rules(neighbors = c(1, 2, 3)) %>% set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Error in `fit()`: + ! `neighbors` must be a whole number or `NULL`, not a double vector. + +--- + + Code + spec <- cubist_rules(neighbors = -1) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of neighbors should be `>= 0` and `<= 9`. + Truncating to 0. + +--- + + Code + spec <- cubist_rules(neighbors = 1000) %>% set_engine("Cubist") %>% set_mode( + "regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + Condition + Warning: + The number of neighbors should be `>= 0` and `<= 9`. + Truncating to 9. + diff --git a/tests/testthat/test-c5.R b/tests/testthat/test-c5.R index 208744b..6bbe82a 100644 --- a/tests/testthat/test-c5.R +++ b/tests/testthat/test-c5.R @@ -412,3 +412,48 @@ test_that("tidy method", { expect_equal(nrow(tidy_2), term_nodes_2) }) + +test_that('check_args() works', { + skip_on_cran() + skip_if_not_installed("C50") + skip_if_not_installed("parsnip", "1.2.1.9001") + ad_data <- make_ad_data() + + expect_snapshot( + error = TRUE, + { + spec <- C5_rules(trees = c(1, 2, 3)) %>% + set_engine("C5.0") %>% + set_mode("classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) + + expect_snapshot( + { + spec <- C5_rules(trees = 0) %>% + set_engine("C5.0") %>% + set_mode("classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) + + expect_snapshot( + { + spec <- C5_rules(trees = 1000) %>% + set_engine("C5.0") %>% + set_mode("classification") + res <- fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) + + expect_snapshot( + error = TRUE, + { + spec <- C5_rules(min_n = c(1, 2, 3)) %>% + set_engine("C5.0") %>% + set_mode("classification") + fit(spec, Class ~ ., data = ad_data$ad_mod) + } + ) +}) diff --git a/tests/testthat/test-cubist.R b/tests/testthat/test-cubist.R index d90406d..8144678 100644 --- a/tests/testthat/test-cubist.R +++ b/tests/testthat/test-cubist.R @@ -705,3 +705,67 @@ test_that("mode specific package dependencies", { list(c("Cubist", "rules")) ) }) + + +test_that('check_args() works', { + skip_on_cran() + skip_if_not_installed("Cubist") + skip_if_not_installed("parsnip", "1.2.1.9001") + chi_data <- make_chi_data() + + expect_snapshot( + error = TRUE, + { + spec <- cubist_rules(committees = c(1, 2, 3)) %>% + set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(committees = 0) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(committees = 1000) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + error = TRUE, + { + spec <- cubist_rules(neighbors = c(1, 2, 3)) %>% + set_engine("Cubist") %>% + set_mode("regression") + fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(neighbors = -1) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) + + expect_snapshot( + { + spec <- cubist_rules(neighbors = 1000) %>% + set_engine("Cubist") %>% + set_mode("regression") + res <- fit(spec, ridership ~ ., data = chi_data$chi_mod) + } + ) +}) diff --git a/tests/testthat/test-rule_fit.R b/tests/testthat/test-rule_fit.R new file mode 100644 index 0000000..8666054 --- /dev/null +++ b/tests/testthat/test-rule_fit.R @@ -0,0 +1,6 @@ +test_that("check_args() works", { + skip_if_not_installed("parsnip", "1.2.1.9001") + + # Here for completeness, no checking is done + expect_true(TRUE) +})