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
|
strings <- list("foo", "", "111", "1", "-", "NA")
not_strings <- list(1, character(), NA_character_, NA,
c("foo", NA), c("1", "2"), NULL)
test_that("is_string", {
for (p in strings) {
expect_true(is_string(p))
expect_silent(assert_that(is_string(p)))
}
for (n in not_strings) {
expect_false(is_string(n))
expect_error(assert_that(is_string(n)), "is not a string")
}
})
test_that("is_string_or_null", {
for (p in strings) {
expect_true(is_string_or_null(p))
expect_silent(assert_that(is_string_or_null(p)))
}
expect_true(is_string_or_null(NULL))
expect_silent(assert_that(is_string_or_null(NULL)))
for (n in not_strings) {
if (!is.null(n)) {
expect_false(is_string_or_null(n))
expect_error(
assert_that(is_string_or_null(n)),
"must be a string .* NULL"
)
}
}
})
flags <- list(TRUE, FALSE)
not_flags <- list(1, character(), NA_character_, NA,
c("foo", NA), c("1", "2"), NULL)
test_that("is_flag", {
for (p in flags) {
expect_true(is_flag(p))
expect_silent(assert_that(is_flag(p)))
}
for (n in not_flags) {
expect_false(is_flag(n))
expect_error(assert_that(is_flag(n)), "is not a flag")
}
})
ints <- list(1, 0, -1, 1L, 0L, -1L, 1.0, 42.0)
not_ints <- list(1.2, 0.1, "foo", numeric(), integer(), NULL,
NA_integer_, NA_real_)
test_that("is_integerish_scalar", {
for (p in ints) {
expect_true(is_integerish_scalar(p))
expect_silent(assert_that(is_integerish_scalar(p)))
}
for (n in not_ints) {
expect_false(is_integerish_scalar(n))
expect_error(
assert_that(is_integerish_scalar(n)),
"is not a length 1 integer"
)
}
})
test_that("is_pid", {
for (p in ints) {
expect_true(is_pid(p))
expect_silent(assert_that(is_pid(p)))
}
for (n in not_ints) {
expect_false(is_pid(n))
expect_error(assert_that(is_pid(n)), "is not a process id")
}
})
test_that("is_flag_or_string", {
for (p in c(flags, strings)) {
expect_true(is_flag_or_string(p))
expect_silent(assert_that(is_flag_or_string(p)))
}
for (n in intersect(not_flags, not_strings)) {
expect_false(is_flag_or_string(n))
expect_error(
assert_that(is_flag_or_string(n)),
"is not a flag or a string"
)
}
})
test_that("is_existing_file", {
expect_false(is_existing_file(tempfile()))
expect_error(
assert_that(is_existing_file(tempfile())),
"File .* does not exist"
)
cat("foo\n", file = tmp <- tempfile())
on.exit(unlink(tmp), add = TRUE)
expect_true(is_existing_file(tmp))
expect_silent(assert_that(is_existing_file(tmp)))
})
|