File: test_makeFunction.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 (69 lines) | stat: -rw-r--r-- 2,357 bytes parent folder | download | duplicates (4)
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
context("makeXFunction")


test_that("makeAssertion", {
  x = assertFlag
  y = makeAssertionFunction(checkFlag, c.fun = "c_check_flag", use.namespace = FALSE)
  expect_identical(formals(x), formals(y))
  if (!isNamespaceLoaded("covr"))
    expect_equal(body(x), body(y))

  x = assertList
  y = makeAssertionFunction(checkList, use.namespace = FALSE)
  expect_identical(formals(x), formals(y))
  if (!isNamespaceLoaded("covr"))
    expect_equal(body(x), body(y))
})

test_that("makeTest", {
  x = testFlag
  y = makeTestFunction(checkFlag, c.fun = "c_check_flag")
  expect_identical(formals(x), formals(y))
  if (!isNamespaceLoaded("covr"))
    expect_equal(body(x), body(y))

  x = testList
  y = makeTestFunction(checkList)
  expect_identical(formals(x), formals(y))
  if (!isNamespaceLoaded("covr"))
    expect_equal(body(x), body(y))

  x = testFlag
  y = function(x) makeTest(checkFlag(x))
  expect_equal(x(TRUE), y(TRUE))
  expect_equal(x(FALSE), y(FALSE))
})

test_that("makeExpectation", {
  x = expect_flag
  y = makeExpectationFunction(checkFlag, c.fun = "c_check_flag", use.namespace = FALSE)
  expect_identical(formals(x), formals(y))
  if (!isNamespaceLoaded("covr"))
    expect_equal(body(x), body(y))

  x = expect_list
  y = makeExpectationFunction(checkList, use.namespace = FALSE)
  expect_identical(formals(x), formals(y))
  if (!isNamespaceLoaded("covr"))
    expect_equal(body(x), body(y))
})

test_that("makeX with name for 'x' not 'x'", {
  checker = function(foo, bar = TRUE) check_numeric(foo)

  achecker = makeAssertionFunction(checker)
  expect_identical(names(formals(achecker)), c("foo", "bar", ".var.name", "add"))
  expect_identical(as.character(formals(achecker)$.var.name)[2], "foo")
  expect_equal(sum(grepl("foo", as.character(body(achecker)))), 3L)
  expect_equal(sum(grepl("bar", as.character(body(achecker)))), 1L)

  tchecker = makeTestFunction(checker)
  expect_identical(names(formals(tchecker)), c("foo", "bar"))
  expect_equal(sum(grepl("foo", as.character(body(tchecker)))), 1L)
  expect_equal(sum(grepl("bar", as.character(body(tchecker)))), 1L)

  echecker = makeExpectationFunction(checker)
  expect_identical(names(formals(echecker)), c("foo", "bar", "info", "label"))
  expect_equal(sum(grepl("foo", as.character(body(echecker)))), 3L)
  expect_equal(sum(grepl("bar", as.character(body(echecker)))), 1L)
})