1context("Utilities")
2
3test_that("finite_cases.data.frame", {
4  finite_cases <- function(x) cases(x, is_finite)
5
6  # All finite --------------------------------------------------------------
7  expect_identical(finite_cases(data_frame(x = 4)),              TRUE)          # 1x1
8  expect_identical(finite_cases(data_frame(x = 4, y = 11)),      TRUE)          # 1x2
9  expect_identical(finite_cases(data_frame(x = 4:5)),            c(TRUE, TRUE)) # 2x1
10  expect_identical(finite_cases(data_frame(x = 4:5, y = 11:12)), c(TRUE, TRUE)) # 2x2
11
12  # Has one NA --------------------------------------------------------------
13  expect_identical(finite_cases(data_frame(x = NA)),                      FALSE)           # 1x1
14  expect_identical(finite_cases(data_frame(x = 4, y = NA)),               FALSE)           # 1x2
15  expect_identical(finite_cases(data_frame(x = c(4, NA))),                c(TRUE,  FALSE)) # 2x1
16  expect_identical(finite_cases(data_frame(x = c(4, NA), y = c(11, NA))), c(TRUE,  FALSE)) # 2x2
17  expect_identical(finite_cases(data_frame(x = c(4, NA), y = c(NA, 12))), c(FALSE, FALSE)) # 2x2
18  expect_identical(finite_cases(data_frame(x = c(4, 5),  y = c(NA, 12))), c(FALSE, TRUE))  # 2x2
19
20  # Testing NaN and Inf, using miscellaneous data shapes --------------------
21  expect_identical(finite_cases(data_frame(x = c(4, NaN))),                c(TRUE, FALSE))
22  expect_identical(finite_cases(data_frame(x = Inf)),                      FALSE)
23  expect_identical(finite_cases(data_frame(x = c(4, 5), y = c(-Inf, 12))), c(FALSE, TRUE))
24})
25
26test_that("add_group", {
27  data <- data_frame(f=letters[7:9], x=1:3, y=4:6, group=c(1, -1, 1))
28  expect_true(has_groups(add_group(data[2:4])))  # explicit group column
29  expect_true(has_groups(add_group(data[1:3])))  # discrete column
30  expect_false(has_groups(add_group(data[2:3]))) # no group or discrete column
31})
32
33test_that("find_args behaves correctly", {
34  test_fun <- function(arg1, arg2 = FALSE, ...) {
35    find_args(...)
36  }
37  # Missing args are removed
38  expect_false("arg1" %in% names(test_fun()))
39  # Ellipsis is not an element
40  expect_false("..." %in% names(test_fun()))
41  # Args are added
42  expect_true(all(c("arg1", "arg2", "arg3") %in% names(test_fun(arg1 = 1, arg2 = 1, arg3 = 1))))
43  # Defaults are overwritten
44  expect_true(test_fun(arg2 = TRUE)$arg2)
45})
46
47test_that("parse_safe works with simple expressions", {
48  expect_equal(
49    parse_safe(c("", " ", "     ")),
50    expression(NA, NA, NA)
51  )
52
53  expect_equal(
54    parse_safe(c("A", "B", "C")),
55    expression(A, B, C)
56  )
57
58  expect_equal(
59    parse_safe(c("alpha", "", "gamma", " ")),
60    expression(alpha, NA, gamma, NA)
61  )
62
63  expect_equal(
64    parse_safe(c(NA, "a", NA, "alpha")),
65    expression(NA, a, NA, alpha)
66  )
67})
68
69test_that("parse_safe works with multi expressions", {
70  expect_equal(
71    parse_safe(c(" \n", "\n ", " \n  \n  ")),
72    expression(NA, NA, NA)
73  )
74
75  expect_equal(
76    parse_safe(c("alpha ~ beta", "beta \n gamma", "")),
77    expression(alpha ~ beta, beta, NA)
78  )
79
80  expect_equal(
81    parse_safe(c("alpha ~ beta", " ", "integral(f(x) * dx, a, b)")),
82    expression(alpha ~ beta, NA, integral(f(x) * dx, a, b))
83  )
84
85  expect_equal(
86    parse_safe(c(NA, 1, 2, "a \n b")),
87    expression(NA, 1, 2, a)
88  )
89})
90
91test_that("x and y aesthetics have the same length", {
92  expect_equal(length(ggplot_global$x_aes), length(ggplot_global$y_aes))
93})
94