Skip to content

Commit eba5762

Browse files
authored
expect_error(regexp = NA) -> expect_no_condition() (#1188)
1 parent 6699730 commit eba5762

30 files changed

+274
-390
lines changed

tests/testthat/test-boost_tree_C5.0.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@ test_that('C5.0 execution', {
1313

1414
skip_if_not_installed("C50")
1515

16-
expect_error(
16+
expect_no_condition(
1717
res <- fit(
1818
lc_basic,
1919
Class ~ log(funded_amnt) + int_rate,
2020
data = lending_club,
2121
control = ctrl
22-
),
23-
regexp = NA
22+
)
2423
)
2524

26-
expect_error(
25+
expect_no_condition(
2726
res <- fit_xy(
2827
lc_basic,
2928
x = lending_club[, num_pred],
3029
y = lending_club$Class,
3130
control = ctrl
32-
),
33-
regexp = NA
31+
)
3432
)
3533

3634
expect_true(has_multi_predict(res))

tests/testthat/test-boost_tree_xgboost.R

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,25 @@ test_that('xgboost execution, classification', {
1818
wts <- ifelse(runif(nrow(hpc)) < .1, 0, 1)
1919
wts <- importance_weights(wts)
2020

21-
expect_error({
21+
expect_no_condition({
2222
set.seed(1)
2323
res_f <- parsnip::fit(
2424
hpc_xgboost,
2525
class ~ compounds + input_fields,
2626
data = hpc,
2727
control = ctrl
2828
)
29-
},
30-
regexp = NA
31-
)
32-
expect_error({
29+
})
30+
expect_no_condition({
3331
set.seed(1)
3432
res_xy <- parsnip::fit_xy(
3533
hpc_xgboost,
3634
x = hpc[, c("compounds", "input_fields")],
3735
y = hpc$class,
3836
control = ctrl
3937
)
40-
},
41-
regexp = NA
42-
)
43-
expect_error({
38+
})
39+
expect_no_condition({
4440
set.seed(1)
4541
res_f_wts <- parsnip::fit(
4642
hpc_xgboost,
@@ -49,10 +45,8 @@ test_that('xgboost execution, classification', {
4945
control = ctrl,
5046
case_weights = wts
5147
)
52-
},
53-
regexp = NA
54-
)
55-
expect_error({
48+
})
49+
expect_no_condition({
5650
set.seed(1)
5751
res_xy_wts <- parsnip::fit_xy(
5852
hpc_xgboost,
@@ -61,9 +55,7 @@ test_that('xgboost execution, classification', {
6155
control = ctrl,
6256
case_weights = wts
6357
)
64-
},
65-
regexp = NA
66-
)
58+
})
6759

6860
expect_equal(res_f$fit$evaluation_log, res_xy$fit$evaluation_log)
6961
expect_equal(res_f_wts$fit$evaluation_log, res_xy_wts$fit$evaluation_log)
@@ -140,24 +132,22 @@ test_that('xgboost execution, regression', {
140132

141133
ctrl$verbosity <- 0L
142134

143-
expect_error(
135+
expect_no_condition(
144136
res <- parsnip::fit(
145137
car_basic,
146138
mpg ~ .,
147139
data = mtcars,
148140
control = ctrl
149-
),
150-
regexp = NA
141+
)
151142
)
152143

153-
expect_error(
144+
expect_no_condition(
154145
res <- parsnip::fit_xy(
155146
car_basic,
156147
x = mtcars[, num_pred],
157148
y = mtcars$mpg,
158149
control = ctrl
159-
),
160-
regexp = NA
150+
)
161151
)
162152

163153
expect_error(
@@ -285,32 +275,29 @@ test_that('validation sets', {
285275

286276
ctrl$verbosity <- 0L
287277

288-
expect_error(
278+
expect_no_condition(
289279
reg_fit <-
290280
boost_tree(trees = 20, mode = "regression") %>%
291281
set_engine("xgboost", validation = .1) %>%
292-
fit(mpg ~ ., data = mtcars[-(1:4), ]),
293-
regex = NA
282+
fit(mpg ~ ., data = mtcars[-(1:4), ])
294283
)
295284

296285
expect_equal(colnames(extract_fit_engine(reg_fit)$evaluation_log)[2], "validation_rmse")
297286

298-
expect_error(
287+
expect_no_condition(
299288
reg_fit <-
300289
boost_tree(trees = 20, mode = "regression") %>%
301290
set_engine("xgboost", validation = .1, eval_metric = "mae") %>%
302-
fit(mpg ~ ., data = mtcars[-(1:4), ]),
303-
regex = NA
291+
fit(mpg ~ ., data = mtcars[-(1:4), ])
304292
)
305293

306294
expect_equal(colnames(extract_fit_engine(reg_fit)$evaluation_log)[2], "validation_mae")
307295

308-
expect_error(
296+
expect_no_condition(
309297
reg_fit <-
310298
boost_tree(trees = 20, mode = "regression") %>%
311299
set_engine("xgboost", eval_metric = "mae") %>%
312-
fit(mpg ~ ., data = mtcars[-(1:4), ]),
313-
regex = NA
300+
fit(mpg ~ ., data = mtcars[-(1:4), ])
314301
)
315302

316303
expect_equal(colnames(extract_fit_engine(reg_fit)$evaluation_log)[2], "training_mae")
@@ -334,23 +321,21 @@ test_that('early stopping', {
334321
ctrl$verbosity <- 0L
335322

336323
set.seed(233456)
337-
expect_error(
324+
expect_no_condition(
338325
reg_fit <-
339326
boost_tree(trees = 200, stop_iter = 5, mode = "regression") %>%
340327
set_engine("xgboost", validation = .1) %>%
341-
fit(mpg ~ ., data = mtcars[-(1:4), ]),
342-
regex = NA
328+
fit(mpg ~ ., data = mtcars[-(1:4), ])
343329
)
344330

345331
expect_equal(extract_fit_engine(reg_fit)$niter - extract_fit_engine(reg_fit)$best_iteration, 5)
346332
expect_true(extract_fit_engine(reg_fit)$niter < 200)
347333

348-
expect_error(
334+
expect_no_condition(
349335
reg_fit <-
350336
boost_tree(trees = 20, mode = "regression") %>%
351337
set_engine("xgboost", validation = .1, eval_metric = "mae") %>%
352-
fit(mpg ~ ., data = mtcars[-(1:4), ]),
353-
regex = NA
338+
fit(mpg ~ ., data = mtcars[-(1:4), ])
354339
)
355340

356341
expect_warning(
@@ -380,39 +365,39 @@ test_that('xgboost data conversion', {
380365
mtcar_smat <- Matrix::Matrix(mtcar_mat, sparse = TRUE)
381366
wts <- 1:32
382367

383-
expect_error(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg), regexp = NA)
368+
expect_no_condition(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg))
384369
expect_true(inherits(from_df$data, "xgb.DMatrix"))
385370
expect_true(inherits(from_df$watchlist$training, "xgb.DMatrix"))
386371

387-
expect_error(from_mat <- parsnip:::as_xgb_data(mtcar_mat, mtcars$mpg), regexp = NA)
372+
expect_no_condition(from_mat <- parsnip:::as_xgb_data(mtcar_mat, mtcars$mpg))
388373
expect_true(inherits(from_mat$data, "xgb.DMatrix"))
389374
expect_true(inherits(from_mat$watchlist$training, "xgb.DMatrix"))
390375

391-
expect_error(from_sparse <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg), regexp = NA)
376+
expect_no_condition(from_sparse <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg))
392377
expect_true(inherits(from_mat$data, "xgb.DMatrix"))
393378
expect_true(inherits(from_mat$watchlist$training, "xgb.DMatrix"))
394379

395-
expect_error(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg, validation = .1), regexp = NA)
380+
expect_no_condition(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg, validation = .1))
396381
expect_true(inherits(from_df$data, "xgb.DMatrix"))
397382
expect_true(inherits(from_df$watchlist$validation, "xgb.DMatrix"))
398383
expect_true(nrow(from_df$data) > nrow(from_df$watchlist$validation))
399384

400-
expect_error(from_mat <- parsnip:::as_xgb_data(mtcar_mat, mtcars$mpg, validation = .1), regexp = NA)
385+
expect_no_condition(from_mat <- parsnip:::as_xgb_data(mtcar_mat, mtcars$mpg, validation = .1))
401386
expect_true(inherits(from_mat$data, "xgb.DMatrix"))
402387
expect_true(inherits(from_mat$watchlist$validation, "xgb.DMatrix"))
403388
expect_true(nrow(from_mat$data) > nrow(from_mat$watchlist$validation))
404389

405-
expect_error(from_sparse <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg, validation = .1), regexp = NA)
390+
expect_no_condition(from_sparse <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg, validation = .1))
406391
expect_true(inherits(from_mat$data, "xgb.DMatrix"))
407392
expect_true(inherits(from_mat$watchlist$validation, "xgb.DMatrix"))
408393
expect_true(nrow(from_sparse$data) > nrow(from_sparse$watchlist$validation))
409394

410395
# set event_level for factors
411396

412397
mtcars_y <- factor(mtcars$mpg < 15, levels = c(TRUE, FALSE), labels = c("low", "high"))
413-
expect_error(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars_y), regexp = NA)
398+
expect_no_condition(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars_y))
414399
expect_equal(xgboost::getinfo(from_df$data, name = "label")[1:5], rep(0, 5))
415-
expect_error(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars_y, event_level = "second"), regexp = NA)
400+
expect_no_condition(from_df <- parsnip:::as_xgb_data(mtcar_x, mtcars_y, event_level = "second"))
416401
expect_equal(xgboost::getinfo(from_df$data, name = "label")[1:5], rep(1, 5))
417402

418403
mtcars_y <- factor(mtcars$mpg < 15, levels = c(TRUE, FALSE, "na"), labels = c("low", "high", "missing"))
@@ -421,9 +406,13 @@ test_that('xgboost data conversion', {
421406
)
422407

423408
# case weights added
424-
expect_error(wted <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg, weights = wts), regexp = NA)
409+
expect_no_condition(
410+
wted <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg, weights = wts)
411+
)
425412
expect_equal(wts, xgboost::getinfo(wted$data, "weight"))
426-
expect_error(wted_val <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg, weights = wts, validation = 1/4), regexp = NA)
413+
expect_no_condition(
414+
wted_val <- parsnip:::as_xgb_data(mtcar_x, mtcars$mpg, weights = wts, validation = 1/4)
415+
)
427416
expect_true(all(xgboost::getinfo(wted_val$data, "weight") %in% wts))
428417
expect_null(xgboost::getinfo(wted_val$watchlist$validation, "weight"))
429418

@@ -461,9 +450,13 @@ test_that('xgboost data and sparse matrices', {
461450
expect_equal(extract_fit_engine(from_df), extract_fit_engine(from_sparse), ignore_function_env = TRUE)
462451

463452
# case weights added
464-
expect_error(wted <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg, weights = wts), regexp = NA)
453+
expect_no_condition(
454+
wted <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg, weights = wts)
455+
)
465456
expect_equal(wts, xgboost::getinfo(wted$data, "weight"))
466-
expect_error(wted_val <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg, weights = wts, validation = 1/4), regexp = NA)
457+
expect_no_condition(
458+
wted_val <- parsnip:::as_xgb_data(mtcar_smat, mtcars$mpg, weights = wts, validation = 1/4)
459+
)
467460
expect_true(all(xgboost::getinfo(wted_val$data, "weight") %in% wts))
468461
expect_null(xgboost::getinfo(wted_val$watchlist$validation, "weight"))
469462

tests/testthat/test-case-weights.R

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ test_that('case weights with xy method', {
1010
two_class_subset <- two_class_dat[wts != 0, ]
1111
wts <- importance_weights(wts)
1212

13-
expect_error({
13+
expect_no_condition({
1414
set.seed(1)
1515
C5_bst_wt_fit <-
1616
boost_tree(trees = 5) %>%
1717
set_engine("C5.0") %>%
1818
set_mode("classification") %>%
1919
fit(Class ~ ., data = two_class_dat, case_weights = wts)
20-
},
21-
regexp = NA)
20+
})
2221

2322
expect_output(
2423
print(C5_bst_wt_fit$fit$call),
2524
"weights = weights"
2625
)
2726

28-
expect_error({
27+
expect_no_condition({
2928
set.seed(1)
3029
C5_bst_wt_fit <-
3130
boost_tree(trees = 5) %>%
@@ -36,8 +35,7 @@ test_that('case weights with xy method', {
3635
y = two_class_dat$Class,
3736
case_weights = wts
3837
)
39-
},
40-
regexp = NA)
38+
})
4139

4240
expect_output(
4341
print(C5_bst_wt_fit$fit$call),
@@ -57,21 +55,20 @@ test_that('case weights with xy method - non-standard argument names', {
5755
two_class_subset <- two_class_dat[wts != 0, ]
5856
wts <- importance_weights(wts)
5957

60-
expect_error({
58+
expect_no_condition({
6159
set.seed(1)
6260
rf_wt_fit <-
6361
rand_forest(trees = 5) %>%
6462
set_mode("classification") %>%
6563
fit(Class ~ ., data = two_class_dat, case_weights = wts)
66-
},
67-
regexp = NA)
64+
})
6865

6966
# expect_output(
7067
# print(rf_wt_fit$fit$call),
7168
# "case\\.weights = weights"
7269
# )
7370

74-
expect_error({
71+
expect_no_condition({
7572
set.seed(1)
7673
rf_wt_fit <-
7774
rand_forest(trees = 5) %>%
@@ -81,8 +78,7 @@ test_that('case weights with xy method - non-standard argument names', {
8178
y = two_class_dat$Class,
8279
case_weights = wts
8380
)
84-
},
85-
regexp = NA)
81+
})
8682
})
8783

8884
test_that('case weights with formula method', {
@@ -97,11 +93,11 @@ test_that('case weights with formula method', {
9793
ames_subset <- ames[wts != 0, ]
9894
wts <- frequency_weights(wts)
9995

100-
expect_error(
96+
expect_no_condition(
10197
lm_wt_fit <-
10298
linear_reg() %>%
103-
fit(Sale_Price ~ Longitude + Latitude, data = ames, case_weights = wts),
104-
regexp = NA)
99+
fit(Sale_Price ~ Longitude + Latitude, data = ames, case_weights = wts)
100+
)
105101

106102
lm_sub_fit <-
107103
linear_reg() %>%
@@ -141,15 +137,14 @@ test_that('case weights with formula method that goes through `fit_xy()`', {
141137
ames_subset <- ames[wts != 0, ]
142138
wts <- frequency_weights(wts)
143139

144-
expect_error(
140+
expect_no_condition(
145141
lm_wt_fit <-
146142
linear_reg() %>%
147143
fit_xy(
148144
x = ames[c("Longitude", "Latitude")],
149145
y = ames$Sale_Price,
150146
case_weights = wts
151-
),
152-
regexp = NA)
147+
))
153148

154149
lm_sub_fit <-
155150
linear_reg() %>%

tests/testthat/test-descriptors.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,15 @@ test_that("can be temporarily overriden at evaluation time", {
199199

200200
test_that("system-level descriptor tests", {
201201
skip_if_not_installed("xgboost")
202-
expect_error(
202+
expect_no_condition(
203203
boost_tree(mode = "regression", mtry = .cols()) %>%
204204
set_engine("xgboost") %>%
205-
fit_xy(x = mtcars[, -1], y = mtcars$mpg),
206-
NA
205+
fit_xy(x = mtcars[, -1], y = mtcars$mpg)
207206
)
208-
expect_error(
207+
expect_no_condition(
209208
boost_tree(mode = "regression", mtry = .cols()) %>%
210209
set_engine("xgboost") %>%
211-
fit(mpg ~ ., data = mtcars),
212-
NA
210+
fit(mpg ~ ., data = mtcars)
213211
)
214212

215213
})

0 commit comments

Comments
 (0)