1context("Creating aesthetic mappings")
2
3test_that("aes() captures input expressions", {
4  out <- aes(mpg, wt + 1)
5  expect_identical(out$x, quo(mpg))
6  expect_identical(out$y, quo(wt + 1))
7})
8
9test_that("aes_q() uses quoted calls and formulas", {
10  out <- aes_q(quote(mpg), ~ wt + 1)
11  expect_identical(out$x, quo(mpg))
12  expect_identical(out$y, quo(wt + 1))
13})
14
15test_that("aes_string() parses strings", {
16  expect_equal(aes_string("a + b")$x, quo(a + b))
17})
18
19test_that("aes_string() doesn't parse non-strings", {
20  old <- options(OutDec = ",")
21  on.exit(options(old))
22
23  expect_identical(aes_string(0.4)$x, 0.4)
24})
25
26test_that("aes_q() & aes_string() preserve explicit NULLs", {
27  expect_equal(aes_q(NULL), aes(NULL))
28  expect_equal(aes_q(x = NULL), aes(NULL))
29  expect_equal(aes_q(colour = NULL), aes(colour = NULL))
30
31  expect_equal(aes_string(NULL), aes(NULL))
32  expect_equal(aes_string(x = NULL), aes(NULL))
33  expect_equal(aes_string(colour = NULL), aes(colour = NULL))
34})
35
36test_that("aes_all() converts strings into mappings", {
37  expect_equal(
38    aes_all(c("x", "y", "col", "pch")),
39    aes(x, y, colour = col, shape = pch)
40  )
41})
42
43test_that("aes evaluated in environment where plot created", {
44  df <- data_frame(x = 1, y = 1)
45  p <- ggplot(df, aes(foo, y)) + geom_point()
46
47  # Accessing an undefined variable should result in error
48  expect_error(layer_data(p), "'foo' not found")
49
50  # Once it's defined we should get it back
51  foo <- 0
52  expect_equal(layer_data(p)$x, 0)
53
54  # And regular variable shadowing should work
55  f <- function() {
56    foo <- 10
57    ggplot(df, aes(foo, y)) + geom_point()
58  }
59  expect_equal(layer_data(f())$x, 10)
60})
61
62test_that("constants are not wrapped in quosures", {
63  aes <- aes(1L, "foo", 1.5)
64  expect_identical(unclass(aes), list(x = 1L, y = "foo", 1.5))
65})
66
67test_that("assignment methods wrap symbolic objects in quosures", {
68  mapping <- aes(a, b, c = c)
69  mapping[1] <- list(quote(foo))
70  expect_identical(mapping[[1]], new_quosure(quote(foo), globalenv()))
71
72  mapping[[2]] <- quote(bar)
73  expect_identical(mapping[[2]], new_quosure(quote(bar), globalenv()))
74
75  mapping$c <- quote(baz)
76  expect_identical(mapping[[3]], new_quosure(quote(baz), globalenv()))
77})
78
79test_that("assignment methods pull unwrap constants from quosures", {
80  mapping <- aes(a, b, c = c)
81  mapping[1] <- list(quo("foo"))
82  expect_identical(mapping[[1]], "foo")
83
84  mapping[[2]] <- quo("bar")
85  expect_identical(mapping[[2]], "bar")
86
87  mapping$c <- quo("baz")
88  expect_identical(mapping[[3]], "baz")
89})
90
91test_that("quosures are squashed when creating default label for a mapping", {
92  p <- ggplot(mtcars) + aes(!!quo(identity(!!quo(cyl))))
93  expect_identical(p$labels$x, "identity(cyl)")
94})
95
96test_that("labelling doesn't cause error if aesthetic is NULL", {
97  p <- ggplot(mtcars) + aes(x = NULL)
98  expect_identical(p$labels$x, "x")
99})
100
101test_that("aes standardises aesthetic names", {
102  # test a few common cases
103  expect_identical(aes(color = x), aes(colour = x))
104  expect_identical(aes(pch = x), aes(shape = x))
105
106  # US to British spelling in substrings
107  expect_identical(aes(point_color = x), aes(point_colour = x))
108  expect_identical(aes(color_point = x), aes(colour_point = x))
109
110  # warning when standardisation creates duplicates
111  expect_warning(aes(color = x, colour = y), "Duplicated aesthetics")
112})
113
114test_that("warn_for_aes_extract_usage() warns for discouraged uses of $ and [[ within aes()", {
115
116  df <- data_frame(x = 1:5, nested_df = data_frame(x = 6:10))
117
118  expect_warning(
119    warn_for_aes_extract_usage(aes(df$x), df),
120    "Use of `df\\$x` is discouraged"
121  )
122
123  expect_warning(
124    warn_for_aes_extract_usage(aes(df[["x"]]), df),
125    'Use of `df\\[\\["x"\\]\\]` is discouraged'
126  )
127})
128
129test_that("warn_for_aes_extract_usage() does not evaluate function calls", {
130  df <- data_frame(x = 1:5, nested_df = data_frame(x = 6:10))
131  returns_df <- function() df
132
133  expect_warning(warn_for_aes_extract_usage(aes(df$x), df))
134  expect_silent(warn_for_aes_extract_usage(aes(returns_df()$x), df))
135})
136
137test_that("warn_for_aes_extract_usage() does not warn for valid uses of $ and [[ within aes()", {
138  df <- data_frame(x = 1:5, nested_df = data_frame(x = 6:10))
139
140  # use of .data
141  expect_silent(warn_for_aes_extract_usage(aes(.data$x), df))
142  expect_silent(warn_for_aes_extract_usage(aes(.data[["x"]]), df))
143
144  # use of $ for a nested data frame column
145  expect_silent(warn_for_aes_extract_usage(aes(nested_df$x), df))
146  expect_silent(warn_for_aes_extract_usage(aes(nested_df[["x"]]), df))
147})
148
149test_that("Warnings are issued when plots use discouraged extract usage within aes()", {
150  df <- data_frame(x = 1:3, y = 1:3)
151  p <- ggplot(df, aes(df$x, y)) + geom_point()
152  expect_warning(ggplot_build(p), "Use of `df\\$x` is discouraged")
153})
154
155# Visual tests ------------------------------------------------------------
156
157test_that("aesthetics are drawn correctly", {
158  dat <- data_frame(xvar = letters[1:3], yvar = 7:9)
159
160  expect_doppelganger("stat='identity'",
161    ggplot(dat, aes(x = xvar, y = yvar)) + geom_bar(stat = "identity")
162  )
163  expect_doppelganger("stat='identity', width=0.5",
164    ggplot(dat, aes(x = xvar, y = yvar)) + geom_bar(stat = "identity", width = 0.5)
165  )
166  expect_doppelganger("stat='count'",
167    ggplot(dat, aes(x = xvar)) + geom_bar(stat = "count")
168  )
169  expect_doppelganger("stat='count', width=0.5",
170    ggplot(dat, aes(x = xvar)) + geom_bar(stat = "count", width = 0.5)
171  )
172})
173
174test_that("alpha is drawn correctly", {
175  expect_doppelganger("Alpha set in colour",
176    qplot(1, 1, color = I("#cc000044"), size = I(50))
177  )
178  expect_doppelganger("Alpha set in alpha",
179    qplot(1, 1, color = I("#cc0000"), size = I(50), alpha = I(0.27))
180  )
181})
182