-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-utils.R
More file actions
107 lines (93 loc) · 3.03 KB
/
test-utils.R
File metadata and controls
107 lines (93 loc) · 3.03 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
library(testit)
assert('var_type classifies correctly', {
(var_type(NULL) %==% 'none')
(var_type(1:5) %==% 'numeric')
(var_type(c(1.0, 2.0)) %==% 'numeric')
(var_type(c('a', 'b')) %==% 'categorical')
(var_type(factor(c('x', 'y'))) %==% 'categorical')
(var_type(c(TRUE, FALSE)) %==% 'categorical')
(var_type(Sys.Date()) %==% 'date')
(var_type(Sys.time()) %==% 'date')
})
assert('dropNulls removes NULL elements', {
(dropNulls(list(a = 1, b = NULL, c = 3)) %==% list(a = 1, c = 3))
})
assert('process_layout rejects bad lengths', {
(has_error(process_layout('padding', c(1, 2))))
})
assert('annotate_df() wraps data frames', {
x = list(data = data.frame(a = 1:3), children = list(
list(type = 'point', data = data.frame(b = 4:6))
))
res = annotate_df(x)
(res$data$type %==% 'column')
(is.data.frame(res$data$value))
(res$children[[1]]$data$type %==% 'column')
})
assert('extract_terms extracts + separated names', {
(extract_terms(quote(x)) %==% 'x')
(extract_terms(quote(x + y)) %==% c('x', 'y'))
(extract_terms(quote(x + y + z)) %==% c('x', 'y', 'z'))
})
assert('parse_formula: y ~ x', {
res = parse_formula(hp ~ mpg)
(res$aesthetics$x %==% 'mpg')
(res$aesthetics$y %==% 'hp')
(is.null(res$facet))
})
assert('parse_formula: ~ x', {
res = parse_formula(~ mpg)
(res$aesthetics$x %==% 'mpg')
(is.null(res$aesthetics$y))
})
assert('parse_formula: ~ x1 + x2 + x3', {
res = parse_formula(~ a + b + c)
(res$aesthetics$position %==% c('a', 'b', 'c'))
(is.null(res$aesthetics$x))
})
assert('parse_formula: y ~ x | z', {
res = parse_formula(hp ~ mpg | cyl)
(res$aesthetics$x %==% 'mpg')
(res$aesthetics$y %==% 'hp')
(res$facet$type %==% 'facetRect')
(res$facet$encode$x %==% 'cyl')
})
assert('parse_formula: y ~ x | z1 + z2', {
res = parse_formula(y ~ x | a + b)
(res$facet$encode$x %==% 'a')
(res$facet$encode$y %==% 'b')
})
assert('parse_formula: y ~ x | 0 + z (row facet)', {
res = parse_formula(hp ~ mpg | 0 + cyl)
(res$aesthetics$x %==% 'mpg')
(res$aesthetics$y %==% 'hp')
(res$facet$type %==% 'facetRect')
(is.null(res$facet$encode$x))
(res$facet$encode$y %==% 'cyl')
})
assert('parse_formula: y ~ x | 0 (bare 0, no facet)', {
res = parse_formula(hp ~ mpg | 0)
(is.null(res$facet))
})
assert('ts_to_df converts univariate ts', {
res = ts_to_df(sunspot.year)
(is.data.frame(res$data))
(names(res$data) %==% c('time', 'value'))
(nrow(res$data) %==% length(sunspot.year))
(res$data$time[1] %==% 1700)
(res$data$value[1] %==% as.numeric(sunspot.year[1]))
(res$aesthetics$x %==% 'time')
(res$aesthetics$y %==% 'value')
(is.null(res$aesthetics$color))
})
assert('ts_to_df converts multivariate ts', {
res = ts_to_df(EuStockMarkets)
(is.data.frame(res$data))
(names(res$data) %==% c('time', 'series', 'value'))
(nrow(res$data) %==% (nrow(EuStockMarkets) * ncol(EuStockMarkets)))
(is.factor(res$data$series))
(levels(res$data$series) %==% colnames(EuStockMarkets))
(res$aesthetics$x %==% 'time')
(res$aesthetics$y %==% 'value')
(res$aesthetics$color %==% 'series')
})