Skip to content

Commit 357c73c

Browse files
committed
Refactor and update tests for testthat v3
1 parent 647d67c commit 357c73c

File tree

5 files changed

+70
-57
lines changed

5 files changed

+70
-57
lines changed

tests/testthat/test-labelled_light.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11

22
test_that("threadbare() works",{
3+
34
# Data
45
attach(test_data_labelled_light)
6+
withr::defer(detach(test_data_labelled_light))
57

68
# Test regular function
79
expect_equal(threadbare(zulu_vec), letters)
810

911
# Lists should throw errors
1012
expect_error(threadbare(cars))
13+
1114
})
1215

1316
test_that("labelled_light ll_labelled() works", {
1417

1518
# Data
1619
attach(test_data_labelled_light)
20+
withr::defer(detach(test_data_labelled_light))
1721

1822
# Empty constructor
1923
expect_silent(ll_labelled(letters))
@@ -37,6 +41,7 @@ test_that("labelled_light ll_assert_*() works", {
3741

3842
# Data
3943
attach(test_data_labelled_light)
44+
withr::defer(detach(test_data_labelled_light))
4045

4146
# Test valid labelled variables
4247
expect_silent(ll_assert_labelled(fruit_lbl))
@@ -47,13 +52,15 @@ test_that("labelled_light ll_assert_*() works", {
4752

4853
# Test invalid labelled variables
4954
expect_error(ll_assert_labelled(fruit_fct))
55+
5056
})
5157

5258

5359
test_that("labelled_light ll_var_label() get/set works", {
5460

5561
# Data
5662
attach(test_data_labelled_light)
63+
withr::defer(detach(test_data_labelled_light))
5764

5865
# Get label
5966
expect_null(ll_var_label(fruit_lbl))
@@ -73,6 +80,9 @@ test_that("labelled_light ll_val_labels() get/set works", {
7380

7481
# Data
7582
attach(test_data_labelled_light)
83+
withr::defer(detach(test_data_labelled_light))
84+
85+
# Set some variables
7686
letters_lbl <- ll_labelled(letters)
7787
char_veggie_labels <- c(Carrot = 2, Potato = 3,
7888
Tomato = 5, Cucumber = 7, Broccoli = 11 )
@@ -100,6 +110,9 @@ test_that("labelled_light ll_to_character() works", {
100110

101111
# Data
102112
attach(test_data_labelled_light)
113+
withr::defer(detach(test_data_labelled_light))
114+
115+
# Set some variables
103116
result_fruit_lbl_default <-
104117
c("Lime", "Peach", "Orange", "Lime", "Apple", "Banana", "Apple",
105118
"Orange", "Apple", "Apple", "Banana", "Apple", "Peach", "Orange",

tests/testthat/test-utils_ddply_helper.R

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ test_that("ddply_helper works correctly", {
1111
result_mean <- ddply_helper(df, mean)
1212
result_sqrt <- ddply_helper(df, sqrt)
1313

14-
expect_is(result_sum, "data.frame")
15-
expect_is(result_mean, "data.frame")
16-
expect_is(result_sqrt, "data.frame")
17-
expect_error(ddply_helper(df, unique)) # Mismatched column lengths
14+
result_sum |> expect_s3_class("data.frame")
15+
result_mean |> expect_s3_class("data.frame")
16+
result_sqrt |> expect_s3_class("data.frame")
17+
18+
ddply_helper(df, unique) |>expect_error() # Mismatched column lengths
1819

1920
# Test with a tibble (if tibble package is available)
2021
if (requireNamespace("tibble", quietly = TRUE)) {
@@ -24,10 +25,11 @@ test_that("ddply_helper works correctly", {
2425
result_mean_tb <- ddply_helper(tb, mean)
2526
result_sqrt_tb <- ddply_helper(tb, sqrt)
2627

27-
expect_is(result_sum_tb, "tbl_df")
28-
expect_is(result_mean_tb, "tbl_df")
29-
expect_is(result_sqrt_tb, "tbl_df")
30-
expect_error(ddply_helper(tb, unique)) # Mismatched column lengths
28+
result_sum_tb |> expect_s3_class("tbl_df")
29+
result_mean_tb |> expect_s3_class("tbl_df")
30+
result_sqrt_tb |> expect_s3_class("tbl_df")
31+
32+
ddply_helper(tb, unique) |> expect_error() # Mismatched column lengths
3133

3234
expect_equal(tibble::as_tibble(result_sum), result_sum_tb)
3335
expect_equal(tibble::as_tibble(result_sqrt), result_sqrt_tb)
@@ -38,8 +40,8 @@ test_that("ddply_helper works correctly", {
3840
result_zero_sum <- ddply_helper(df_zero_row, sum)
3941
result_zero_sqrt <- ddply_helper(df_zero_row, sqrt)
4042

41-
expect_is(result_zero_sum, "data.frame")
42-
expect_is(result_zero_sqrt, "data.frame")
43+
expect_s3_class(result_zero_sum, "data.frame")
44+
expect_s3_class(result_zero_sqrt, "data.frame")
4345
expect_equal(ncol(result_zero_sum), 1) # columns should be maintained
4446
expect_equal(ncol(result_zero_sqrt), 1) # columns should be maintained
4547
expect_equal(nrow(result_zero_sum), 1) # aggregate fun
@@ -49,7 +51,7 @@ test_that("ddply_helper works correctly", {
4951
df_zero_col <- data.frame()
5052
result_zero_col <- ddply_helper(df_zero_col, sum)
5153

52-
expect_is(result_zero_col, "data.frame")
54+
expect_s3_class(result_zero_col, "data.frame")
5355
expect_equal(ncol(result_zero_col), 0)
5456
expect_equal(nrow(result_zero_col), 0)
5557

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22

33
#### Test zample() ####
4-
context("zample")
54
test_that("zample works", {
65

76
# Expected first ten samples with seed at 1
@@ -42,49 +41,4 @@ test_that("zample works", {
4241
})
4342

4443

45-
#### Test zeq() ####
46-
context("zeq")
47-
test_that("zeq works", {
4844

49-
# Positive intervals should equal seq
50-
expect_equal( zeq(1,2), seq(1,2) )
51-
expect_equal( zeq(1,10), seq(1,10) )
52-
expect_equal( zeq(5,10), seq(5,10) )
53-
expect_equal( zeq(20,20), seq(20,20) )
54-
55-
# Identity should have length one
56-
expect_equal( zeq(1,1), 1 )
57-
58-
# End one less than start gives empty sequence
59-
expect_equal( zeq(1,0), numeric(0) )
60-
61-
# End lower than start by two or more gives error
62-
expect_error( zeq(2,0) )
63-
})
64-
65-
66-
#### Test zingle() ####
67-
context("zingle")
68-
test_that("zingle works", {
69-
70-
# Check that if all equal, return first item
71-
expect_equal( zingle(rep(10,10)), 10 )
72-
expect_equal( zingle(rep("a",20)), "a" )
73-
expect_equal( zingle(as.factor(rep("a",20))), as.factor("a") )
74-
75-
# Check that if not all equal, an error is thrown
76-
expect_error( zingle(1:2) )
77-
78-
# Check that NAs behave as expected with na.rm
79-
expect_equal( zingle(c( 1, 1,NA, 1, 1), na.rm=TRUE), 1 )
80-
expect_equal( zingle(c(NA,NA,NA, 1,NA), na.rm=TRUE), 1 )
81-
expect_equal( zingle(c(NA,NA,NA,NA,NA), na.rm=TRUE), NA )
82-
83-
# Check that without na.rm=TRUE, NAs cause an error
84-
expect_error( zingle(c( 1, 1,NA, 1, 1)) )
85-
expect_error( zingle(c(NA,NA,NA, 1,NA)) )
86-
expect_error( zingle(c(NA,NA,NA,NA,NA)) )
87-
88-
# Check that the result never has names
89-
expect_named( zingle(c(a=1,b=1)), NULL)
90-
})

tests/testthat/test-zeq.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#### Test zeq() ####
3+
test_that("zeq works", {
4+
5+
# Positive intervals should equal seq
6+
expect_equal( zeq(1,2), seq(1,2) )
7+
expect_equal( zeq(1,10), seq(1,10) )
8+
expect_equal( zeq(5,10), seq(5,10) )
9+
expect_equal( zeq(20,20), seq(20,20) )
10+
11+
# Identity should have length one
12+
expect_equal( zeq(1,1), 1 )
13+
14+
# End one less than start gives empty sequence
15+
expect_equal( zeq(1,0), numeric(0) )
16+
17+
# End lower than start by two or more gives error
18+
expect_error( zeq(2,0) )
19+
})

tests/testthat/test-zingle.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
#### Test zingle() ####
3+
test_that("zingle works", {
4+
5+
# Check that if all equal, return first item
6+
expect_equal( zingle(rep(10,10)), 10 )
7+
expect_equal( zingle(rep("a",20)), "a" )
8+
expect_equal( zingle(as.factor(rep("a",20))), as.factor("a") )
9+
10+
# Check that if not all equal, an error is thrown
11+
expect_error( zingle(1:2) )
12+
13+
# Check that NAs behave as expected with na.rm
14+
expect_equal( zingle(c( 1, 1,NA, 1, 1), na.rm=TRUE), 1 )
15+
expect_equal( zingle(c(NA,NA,NA, 1,NA), na.rm=TRUE), 1 )
16+
expect_equal( zingle(c(NA,NA,NA,NA,NA), na.rm=TRUE), NA )
17+
18+
# Check that without na.rm=TRUE, NAs cause an error
19+
expect_error( zingle(c( 1, 1,NA, 1, 1)) )
20+
expect_error( zingle(c(NA,NA,NA, 1,NA)) )
21+
expect_error( zingle(c(NA,NA,NA,NA,NA)) )
22+
23+
# Check that the result never has names
24+
expect_named( zingle(c(a=1,b=1)), NULL)
25+
})

0 commit comments

Comments
 (0)