File: test-assertions.R

package info (click to toggle)
r-cran-assertthat 0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, forky, sid, trixie
  • size: 220 kB
  • sloc: sh: 9; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 3,951 bytes parent folder | download
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
context("Assertion assertions")

test_that("is.integerish works correctly", {
  expect_true(is.integerish(1L))
  expect_true(is.integerish(c(1L, 2L, 3L)))
  expect_true(is.integerish(c(1L, NA, 3L)))
  expect_false(is.integerish(c(1L, 2.1, 3L)))

  # base::.Machine holds info on machine numerical precision
  expect_false(is.integerish(1L + .Machine$double.eps))
  expect_false(is.integerish(1L - .Machine$double.neg.eps))

  # numbers larger than base::.Machine$integer.max shouldn't trip this up
  expect_true(is.integerish(Inf))
  expect_true(is.integerish(-Inf))

  expect_true(is.integerish(1e10))
  expect_true(is.integerish(-1e10))

  expect_false(is.integerish(1e10 + 0.0002))
  expect_false(is.integerish(1e10 - 0.0002))

  expect_false(is.integerish(NA))
  expect_false(is.integerish(NA_real_))
  expect_false(is.integerish(NULL))
  expect_false(is.integerish(NaN))
})

test_that("is.named works correctly", {
  expect_false(is.named(1))
  x <- 1:3
  expect_false(is.named(x))
  names(x) <- letters[1:3]
  expect_true(is.named(x))

  # Malformed or weird names
  names(x)[2] <- ""
  expect_false(is.named(x))
  names(x)[2] <- NA
  expect_false(is.named(x))
  names(x) <- NULL
  expect_false(is.named(x))

  expect_false(is.named(NA))
  expect_false(is.named(NULL))
})

test_that("has_attr works correctly", {
  x <- 1:3
  expect_false(has_attr(x, "names"))
  names(x) <- letters[1:3]
  expect_true(has_attr(x, "names"))
  expect_false(has_attr(x, "something else"))
  # not sure what else to test here
})

test_that("has_name works correctly", {
  x <- 1:3
  expect_false(has_name(x, "a"))
  names(x) <- letters[1:3]
  expect_true(has_name(x, letters[2]))
  expect_false(has_name(x, "something else"))
  expect_false(has_name(x, NA))
  expect_true(has_name(x, c("a", "b")))
  expect_true(has_name(x, c("a", "b", "c")))
  expect_false(has_name(x, c("a", "d")))
})

test_that("noNA works correctly", {
  expect_true(noNA("a"))
  expect_false(noNA(c(TRUE, NA)))
  x <- sample(c(1:10, NA), 100, rep = TRUE)
  expect_false(noNA(x))
  expect_true(noNA(1:1000))
})

test_that("are_equal works correctly", {
  x <- 2
  expect_false(are_equal(x, 1.9))
  expect_true(are_equal(x, 1.999, tol = 0.01))
  expect_true(are_equal(x, 2))
  expect_true(are_equal('a', 'a'))
  expect_false(are_equal('a', 'b'))

  expect_true(are_equal(NA, NA))
  expect_true(are_equal(NULL, NULL))
})

test_that("is.error works correctly", {
  x <- try(stop("!!"), silent=TRUE)
  expect_true(is.error(x))
  expect_false(is.error(1))

  expect_false(is.error(NA))
  expect_false(is.error(NULL))
})

test_that("is.time works correctly", {
  expect_true(is.time(Sys.time()))
  expect_false(is.time(Sys.Date()))
  expect_false(is.time(1))

  expect_false(is.time(NA))
  expect_false(is.time(NULL))
})

test_that("is.date works correctly", {
  expect_false(is.date(Sys.time()))
  expect_true(is.date(Sys.Date()))
  expect_false(is.date(1))

  expect_false(is.date(NA))
  expect_false(is.date(NULL))
})

test_that("has_args works correctly", {
  expect_error(1 %has_args% "x")
  expect_true(mean %has_args% "x")
  expect_false(mean %has_args% "y")

  expect_error(NA %has_args% "x")
  expect_error(NULL %has_args% "x")

  # should pass with exact = FALSE if you don't have all the args or you the order is different
  expect_true(has_args(rnorm, "n"))
  expect_true(has_args(rnorm, c("n", "mean")))
  expect_true(has_args(rnorm, c("mean", "sd", "n")))

  # should pass with exact = TRUE if you don't have all the args or you the order is different
  expect_false(has_args(rnorm, "n", exact = TRUE))
  expect_false(has_args(rnorm, c("n", "mean"), exact = TRUE))
  expect_false(has_args(rnorm, c("mean", "sd", "n"), exact = TRUE))
})

test_that("not_empty works correctly", {
  expect_true(not_empty(1))
  expect_false(not_empty(numeric()))
  expect_false(not_empty(mtcars[0, ]))
  expect_false(not_empty(mtcars[, 0]))

  expect_true(not_empty(NA))
  expect_false(not_empty(NULL))
})