Skip to content

Commit 8b0c186

Browse files
authored
Merge pull request #80 from cmu-delphi/km-tests-good
Updated tests to work for good.
2 parents 3dd7d65 + 66f807f commit 8b0c186

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

tests/testthat/test-assign_arg_list.R

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
test_that("First argument must be a list",{
2+
expect_error(assign_arg_list(c(1,2,3)))
3+
})
4+
test_that("All arguments should be named",{
5+
expect_error(assign_arg_list(list(1,2)))
6+
})
7+
test_that("assign_arg_list works as intended",{
8+
assign_arg_list(list(a="dog",b=2))
9+
expect_identical(a,"dog")
10+
expect_identical(b,2)
11+
})

tests/testthat/test-df_mat_mul.R

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
df <- data.frame(matrix(1:100, ncol = 5))
2+
mat <- matrix(1:4, ncol = 2)
3+
4+
test_that("First input must be a data frame and second
5+
input must be a matrix", {
6+
expect_error(df_mat_mul(df,20))
7+
expect_error(df_mat_mul(30,mat))
8+
})
9+
10+
test_that("Argument name is a character", {
11+
expect_error(df_mat_mul(df, mat, 100))
12+
})
13+
14+
test_that("The length of names does not differ from the length of the number
15+
of outputs" ,{
16+
expect_error(df_mat_mul(df, mat, c("a","b","c"), 2:3))
17+
})
18+
19+
test_that("The number of columns of the first data frame cannot differ from the
20+
number of rows of the second matrix, hence preventing incompatible
21+
matrix multiplication", {
22+
expect_error(df_mat_mul(df, mat, "z", 1:3))
23+
})
24+
25+
X <- df[c(1,4,5)]
26+
Z <- as.data.frame(as.matrix(df[2:3]) %*% mat)
27+
colnames(Z) <- c("z1","z2")
28+
output <- cbind(X,Z)
29+
30+
test_that("Names are being handled properly", {
31+
expect_identical(df_mat_mul(df, mat, "z", 2:3),output)
32+
expect_identical(df_mat_mul(df, mat, c("z1","z2"), 2:3),output)
33+
})
34+
35+
test_that("Other tidyselect functionalities are working", {
36+
mult <- df_mat_mul(df, mat, "z", dplyr::num_range("X", 2:3))
37+
expect_identical(mult,output)
38+
expect_identical(df_mat_mul(df, mat, "z", 2, 3),output)
39+
# Mismatched names should not work:
40+
expect_error(df_mat_mul(df, mat, "z", dplyr::num_range("Y", 2:3)))
41+
})

tests/testthat/test-grab_names.R

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
df <- data.frame(b=1,c=2,ca=3,cat=4)
2+
3+
test_that("Names are grabbed properly", {
4+
expect_identical(grab_names(df,dplyr::starts_with("ca")),
5+
subset(names(df),startsWith(names(df), "ca"))
6+
)
7+
})

0 commit comments

Comments
 (0)