/
test-zfit_base.R
189 lines (139 loc) · 5.15 KB
/
test-zfit_base.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
context("zfunction")
test_that("zfunction works", {
# Define the zgrep function, which is our test case
zgrep <- zfunction(grep, x)
# Run grep and zgrep on the same input (apart from order)
char_vector <- rownames(mtcars)
r.grep <- grep("ll", char_vector, value=TRUE)
r.zgrep <- zgrep(char_vector, "ll", value=TRUE)
expect_equal(r.zgrep, r.grep)
})
test_that("zfunction curly notation works", {
# Define a parameter name
the_param_name <- "x"
# Define the zgrep function, which is our test case
zgrep <- zfunction(grep, x)
zgrep_constant <- zfunction(grep, "x")
zgrep_constant_named <- zfunction(grep, x = "x")
zgrep_curly <- zfunction(grep, {the_param_name})
zgrep_curly_named <- zfunction(grep, x = {the_param_name})
# These shouls all be equal
expect_equal(zgrep, zgrep_constant)
expect_equal(zgrep, zgrep_constant_named)
expect_equal(zgrep, zgrep_curly)
expect_equal(zgrep, zgrep_curly_named)
})
context("zfold")
test_that("zfold works", {
# Check that the abc() helper works as expected
abc() |> expect_output(".*a.*b.*c")
"hi" |> abc() |> expect_output("a.*hi.*b")
# Define alternative functions
bac_function <- zfunction(abc, b)
bac_fold <- zfold(abc, b)
# function first, constant and variable
hi <- "hi"
"hi" |> bac_function() |> expect_output("b.*hi.*c")
hi |> bac_function() |> expect_output("b.*hi.*c")
# then fold, constant and variable
"hi" |> bac_fold() |> expect_output("b.*hi.*c")
hi |> bac_fold() |> expect_output("b.*hi.*c")
# Define the zgrep function
zgrep <- zfold(grep, x)
# Run grep and zgrep on the same input (apart from order)
carnames <- rownames(mtcars)
r.grep <- grep("ll", carnames, value=TRUE)
r.zgrep <- zgrep(carnames, "ll", value=TRUE)
expect_equal(r.zgrep, r.grep)
})
test_that("zfold curly notation works", {
# Define parameter
the_param_name <- "x"
# Define the zgrep function
zgrep <- zfold(grep, x)
zgrep_constant <- zfold(grep, "x")
zgrep_constant_named <- zfold(grep, x = "x")
zgrep_curly <- zfold(grep, {the_param_name})
zgrep_curly_named <- zfold(grep, x = {the_param_name})
# These shouls all be equal
expect_equal(zgrep, zgrep_constant)
expect_equal(zgrep, zgrep_constant_named)
expect_equal(zgrep, zgrep_curly)
expect_equal(zgrep, zgrep_curly_named)
})
context("zfold generics")
test_that("zfold on S3 generic print works", {
if (requireNamespace("tibble") && getRversion() >= "4.1.0") {
# Flip order of print generic, but still dispatch to print.tbl_df
ztbl_print <- zfold(print, "n", x_not_found = "ok")
cartibble <- tibble::tibble(cars)
# Print 7 rows, leaving 43 unprinted
7 |> ztbl_print(cartibble) |>
expect_output("43 more rows")
# Print 7 rows, leaving 43 unprinted
13 |> ztbl_print(cartibble) |>
expect_output("37 more rows")
}
})
test_that("zfold on well-behaved S3 generics works", {
if (getRversion() >= "4.1.0") {
# Define dispatch functions
dispatch <- function(x, y) { UseMethod("dispatch") }
dispatch.default <- function(x, y) { paste("default", x, y) }
dispatch.numeric <- function(x, y) { paste("numeric", x, y) }
dispatch.character <- function(x, y) { paste("character", x, y) }
# dispatch(1, "b")
# dispatch("a", 2)
zdispatch_fun <- zfunction(dispatch, y)
zdispatch_fld <- zfold(dispatch, y)
# Incorrect dispatch to numeric (1:3 first arg)
expect_match( 1:3 |> zdispatch_fun("a"), "numeric")
# Correct dispatch to character ("a" first arg)
expect_match( 1:3 |> zdispatch_fld("a"), "character")
}
})
test_that("zfold on poorly-behaved S3 generics doesn't work", {
if (getRversion() >= "4.1.0") {
# Flip order of t.test generic, but still dispatch t.test.formula
zgt.test <- zfold(t.test, "data", x_not_found = "ok")
# t.test.formula changes the name of the first argument,
# whics breaks even folded dispatch.
t.test(mpg ~ am, data = mtcars)
expect_error(
mtcars |> zgt.test(mpg ~ am),
"'formula' missing or incorrect")
}
})
context("zfitter")
test_that("zfitter works", {
# Create a custom version of zlm, using zfitter
zzlm <- zfitter(lm)
zzlm_stats <- zfitter(stats::lm)
# Test usage in the context of regular parameters
m.lm <- lm(dist~speed, cars)
m.zzlm <- zzlm(cars, dist~speed)
m.zzlm_stats <- zzlm_stats(cars, dist~speed)
expect_equal(m.zzlm, m.lm)
expect_equal(m.zzlm_stats, m.lm)
# Test usage in the context of dplyr pipes
if ( require("dplyr", warn.conflicts=FALSE) ) {
m.lm.p <- cars %>% lm(dist~speed, data=.)
m.zzlm.p <- cars %>% zlm(dist~speed)
expect_equal(m.zzlm.p, m.lm.p)
}
# Test usage in the context of native pipes
if ( getRversion() >= "4.1.0" ) {
m.zzlm.np <- cars |> zzlm(dist~speed)
expect_equal(m.zzlm.np, m.lm)
}
})
test_that("zfitter error checking works",{
# These should all be errors
expect_error(zfitter())
expect_error(zfitter(""))
expect_error(zfitter("lm"))
expect_error(zfitter(a_missing_function))
# The target function must have both function and data parameters
expect_error(zfitter(grep))
expect_error(zfitter(within))
})