1
1
context(" Facetting" )
2
2
3
3
test_that(" as_facets_list() coerces formulas" , {
4
- expect_identical(as_facets_list(~ foo ), list (quos(foo = foo )))
5
- expect_identical(as_facets_list(~ foo + bar ), list (quos(foo = foo , bar = bar )))
6
-
4
+ expect_identical(as_facets_list(~ foo ), list (quos(), quos(foo = foo )))
5
+ expect_identical(as_facets_list(~ foo + bar ), list (quos(), quos(foo = foo , bar = bar )))
7
6
expect_identical(as_facets_list(foo ~ bar ), list (quos(foo = foo ), quos(bar = bar )))
8
7
9
8
exp <- list (quos(foo = foo , bar = bar ), quos(baz = baz , bam = bam ))
@@ -18,8 +17,13 @@ test_that("as_facets_list() coerces strings containing formulas", {
18
17
})
19
18
20
19
test_that(" as_facets_list() coerces character vectors" , {
21
- expect_identical(as_facets_list(" foo" ), as_facets_list(local(~ foo , globalenv())))
22
- expect_identical(as_facets_list(c(" foo" , " bar" )), as_facets_list(local(foo ~ bar , globalenv())))
20
+ foo <- rlang :: new_quosure(quote(foo ), globalenv())
21
+ bar <- rlang :: new_quosure(quote(bar ), globalenv())
22
+ foobar <- rlang :: as_quosures(list (foo , bar ), named = TRUE )
23
+
24
+ expect_identical(as_facets_list(" foo" ), list (foobar [1 ]))
25
+ expect_identical(as_facets_list(c(" foo" , " bar" )), list (foobar [1 ], foobar [2 ]))
26
+ expect_identical(wrap_as_facets_list(c(" foo" , " bar" )), foobar )
23
27
})
24
28
25
29
test_that(" as_facets_list() coerces lists" , {
@@ -36,17 +40,39 @@ test_that("as_facets_list() coerces lists", {
36
40
expect_identical(out , exp )
37
41
})
38
42
39
- test_that(" as_facets_list() errors with empty specs" , {
40
- expect_error(as_facets_list(list ()), " at least one variable to facet by" )
41
- expect_error(as_facets_list(. ~ . ), " at least one variable to facet by" )
42
- expect_error(as_facets_list(list (. ~ . )), " at least one variable to facet by" )
43
- expect_error(as_facets_list(list (NULL )), " at least one variable to facet by" )
43
+ test_that(" as_facets_list() coerces quosures objectss" , {
44
+ expect_identical(as_facets_list(vars(foo )), list (quos(foo = foo )))
45
+ })
46
+
47
+ test_that(" facets reject aes()" , {
48
+ expect_error(facet_wrap(aes(foo )), " Please use `vars()` to supply facet variables" , fixed = TRUE )
49
+ expect_error(facet_grid(aes(foo )), " Please use `vars()` to supply facet variables" , fixed = TRUE )
44
50
})
45
51
46
- test_that(" as_facets_list() coerces quosure lists" , {
47
- expect_identical(as_facets_list(vars(foo )), list (rlang :: quos(foo = foo )))
52
+ test_that(" wrap_as_facets_list() returns a quosures object with compacted" , {
53
+ expect_identical(wrap_as_facets_list(vars(foo )), quos(foo = foo ))
54
+ expect_identical(wrap_as_facets_list(~ foo + bar ), quos(foo = foo , bar = bar ))
55
+ expect_identical(wrap_as_facets_list(vars(foo , NULL , bar )), quos(foo = foo , bar = bar ))
48
56
})
49
57
58
+ test_that(" grid_as_facets_list() returns a list of quosures objects with compacted" , {
59
+ expect_identical(grid_as_facets_list(vars(foo ), NULL ), list (rows = quos(foo = foo ), cols = quos()))
60
+ expect_identical(grid_as_facets_list(~ foo , NULL ), list (rows = quos(), cols = quos(foo = foo )))
61
+ expect_identical(grid_as_facets_list(vars(foo , NULL , bar ), NULL ), list (rows = quos(foo = foo , bar = bar ), cols = quos()))
62
+ })
63
+
64
+ test_that(" wrap_as_facets_list() and grid_as_facets_list() accept empty specs" , {
65
+ expect_identical(wrap_as_facets_list(NULL ), quos())
66
+ expect_identical(wrap_as_facets_list(list ()), quos())
67
+ expect_identical(wrap_as_facets_list(. ~ . ), quos())
68
+ expect_identical(wrap_as_facets_list(list (. ~ . )), quos())
69
+ expect_identical(wrap_as_facets_list(list (NULL )), quos())
70
+
71
+ expect_identical(grid_as_facets_list(list (), NULL ), list (rows = quos(), cols = quos()))
72
+ expect_identical(grid_as_facets_list(. ~ . , NULL ), list (rows = quos(), cols = quos()))
73
+ expect_identical(grid_as_facets_list(list (. ~ . ), NULL ), list (rows = quos(), cols = quos()))
74
+ expect_identical(grid_as_facets_list(list (NULL ), NULL ), list (rows = quos(), cols = quos()))
75
+ })
50
76
51
77
df <- data_frame(x = 1 : 3 , y = 3 : 1 , z = letters [1 : 3 ])
52
78
@@ -110,6 +136,23 @@ test_that("vars() accepts optional names", {
110
136
expect_named(wrap $ params $ facets , c(" A" , " b" ))
111
137
})
112
138
139
+ test_that(" facets_wrap() compacts the facet spec and accept empty spec" , {
140
+ p <- ggplot(df , aes(x , y )) + geom_point() + facet_wrap(vars(NULL ))
141
+ d <- layer_data(p )
142
+
143
+ expect_equal(d $ PANEL , c(1L , 1L , 1L ))
144
+ expect_equal(d $ group , c(- 1L , - 1L , - 1L ))
145
+ })
146
+
147
+ test_that(" facets_grid() compacts the facet spec and accept empty spec" , {
148
+ p <- ggplot(df , aes(x , y )) + geom_point() + facet_grid(vars(NULL ))
149
+ d <- layer_data(p )
150
+
151
+ expect_equal(d $ PANEL , c(1L , 1L , 1L ))
152
+ expect_equal(d $ group , c(- 1L , - 1L , - 1L ))
153
+ })
154
+
155
+
113
156
test_that(" facets with free scales scale independently" , {
114
157
l1 <- ggplot(df , aes(x , y )) + geom_point() +
115
158
facet_wrap(~ z , scales = " free" )
0 commit comments