File: test_assert.R

package info (click to toggle)
r-cran-checkmate 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,512 kB
  • sloc: ansic: 2,211; sh: 9; makefile: 8
file content (66 lines) | stat: -rw-r--r-- 2,450 bytes parent folder | download | duplicates (3)
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
context("assert")

test_that("assert w/ check*", {
  x = NULL
  expect_true(assert(checkNull(x), checkDataFrame(x)))
  expect_true(assert(checkNull(x)))
  grepme = iris
  expect_true(assert(checkNull(grepme), checkDataFrame(grepme)))
  expect_error(assert(checkNull(grepme), checkNumeric(grepme)), "One of")
  expect_error(assert(checkNull(grepme), checkNumeric(grepme)), "grepme")

  x = 1
  expect_true(assert(checkNumeric(x), checkCount(x)))
  expect_true(assert(checkNumeric(x), checkCount(x), combine = "or"))
  expect_true(assert(checkNumeric(x), checkCount(x), combine = "and"))

  x = 1.1
  expect_true(assert(checkNumeric(x), checkCount(x), combine = "or"))
  expect_error(assert(checkNumeric(x), checkCount(x), combine = "and"))

  x = "a"
  expect_true(assert(checkString(x)))
  expect_error(assert(checkNumeric(x), checkCount(x), combine = "or"))
  expect_error(assert(checkNumeric(x), checkCount(x), combine = "and"))
})

test_that("bug #69", {
  sub = subset = 1:150
  res = assert(checkIntegerish(subset), checkLogical(subset, len = 150))
  expect_true(res)
  res = assert(checkIntegerish(sub), checkLogical(sub, len = 150))
  expect_true(res)
})

test_that("correct variable is reported (#182)", {
  f = function(a, b) assert(checkFunction(a), checkNumeric(b), combine = "and")
  expect_error(f(identity, "a"), "'b'")
})

test_that("can push to collection", {
  coll = makeAssertCollection()
  x = NULL
  expect_error(assert(checkNull(x), .var.name = c("a", "b"), add = coll), "length")
  expect_error(assert(checkNull(x), add = "not_assert_coll"), "AssertCollection")
  expect_true(assert(checkNull(x), add = coll))
  expect_true(coll$isEmpty())
  expect_true(assert(checkNull(x), checkDataFrame(x), add = coll))
  expect_true(coll$isEmpty())
  grepme = iris
  expect_true(assert(checkNull(grepme), checkDataFrame(grepme), add = coll))
  expect_true(coll$isEmpty())
  assert(checkNull(grepme), checkNumeric(grepme), add = coll)
  expect_match(
    coll$getMessages(), "Variable.+One of")
  assert(checkNull(grepme), checkNumeric(x), add = coll)
  expect_match(
    coll$getMessages()[2], "Variables.+grepme.+x.+One of")
  assert(
    checkNull(grepme), checkNumeric(x), .var.name = "TEST", add = coll)
  expect_match(
    coll$getMessages()[3], "Variable.+TEST.+One of")
  assert(
    checkNull(grepme), checkNumeric(x), .var.name = c("V1", "V2"), add = coll)
  expect_match(
    coll$getMessages()[4], "Variable.+V1.+V2.+One of")
})