-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathtest-json.R
190 lines (157 loc) · 4.59 KB
/
test-json.R
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
test_that("JSON is standalone", {
## baseenv() makes sure that the remotes package env is not used
env <- new.env(parent = baseenv())
env$json <- json
stenv <- env$json$.internal
objs <- ls(stenv, all.names = TRUE)
funs <- Filter(function(x) is.function(stenv[[x]]), objs)
funobjs <- mget(funs, stenv)
expect_message(
mapply(codetools::checkUsage, funobjs, funs,
MoreArgs = list(report = message)),
NA)
})
test_that("JSON parser scalars", {
expect_equal(json$parse('"foobar"'), "foobar" )
expect_equal(json$parse('""'), "")
expect_equal(json$parse("42"), 42)
expect_equal(json$parse("-42"), -42)
expect_equal(json$parse("42.42"), 42.42)
expect_equal(json$parse("1e2"), 1e2)
expect_equal(json$parse("-0.1e-2"), -0.1e-2)
expect_equal(json$parse('null'), NULL)
expect_equal(json$parse('true'), TRUE)
expect_equal(json$parse('false'), FALSE)
})
test_that("JSON parser arrays", {
cases <- list(
list("[1,2,3]", list(1,2,3)),
list("[1]", list(1)),
list("[]", list()),
list('["foo"]', list("foo")),
list('["foo", 1, "bar", true]', list("foo", 1, "bar", TRUE))
)
for (c in cases) {
r <- json$parse(c[[1]])
expect_equal(r, c[[2]], info = c[[1]])
}
})
test_that("JSON parser nested arrays", {
cases <- list(
list('[1,2, ["foo", "bar"], 3]', list(1,2, list("foo","bar"), 3)),
list('[ [ [ 1 ] ] ]', list(list(list(1)))),
list('[ [ [ ] ] ]', list(list(list())))
)
for (c in cases) {
r <- json$parse(c[[1]])
expect_equal(r, c[[2]], info = c[[1]])
}
})
test_that("JSON parser, real examples", {
inp <- '
{
"sha": "e183ccdc515bbb8e7f32d8d16586aed9eea6de0b",
"commit": {
"author": {
"name": "Hadley Wickham",
"email": "h.wickham@gmail.com",
"date": "2015-03-30T13:55:18Z"
},
"committer": {
"name": "Hadley Wickham",
"email": "h.wickham@gmail.com",
"date": "2015-03-30T13:55:18Z"
},
"message": "Merge pull request #22 from paulstaab/HEAD\\n\\nImprove error message for assertions of length 0",
"tree": {
"sha": "f2e840b7a134fbc118597842992aa50048e0fa04",
"url": "https://api.github.com/repos/hadley/assertthat/git/trees/f2e840b7a134fbc118597842992aa50048e0fa04"
},
"url": "https://api.github.com/repos/hadley/assertthat/git/commits/e183ccdc515bbb8e7f32d8d16586aed9eea6de0b",
"comment_count": 0
}
}'
exp <- list(
sha = "e183ccdc515bbb8e7f32d8d16586aed9eea6de0b",
commit = list(
author = list(
name = "Hadley Wickham",
email = "h.wickham@gmail.com",
date = "2015-03-30T13:55:18Z"),
committer = list(
name = "Hadley Wickham",
email = "h.wickham@gmail.com",
date = "2015-03-30T13:55:18Z"),
message = "Merge pull request #22 from paulstaab/HEAD\\n\\nImprove error message for assertions of length 0",
tree = list(
sha = "f2e840b7a134fbc118597842992aa50048e0fa04",
url = "https://api.github.com/repos/hadley/assertthat/git/trees/f2e840b7a134fbc118597842992aa50048e0fa04"
),
url = "https://api.github.com/repos/hadley/assertthat/git/commits/e183ccdc515bbb8e7f32d8d16586aed9eea6de0b",
comment_count = 0
)
)
expect_equal(json$parse(inp), exp)
})
test_that("JSON parser, errors", {
expect_error(
json$parse("[1,2,3,"),
"EXPECTED value GOT EOF"
)
expect_error(
json$parse('{ 123: "foo" }'),
"EXPECTED string GOT 123"
)
expect_error(
json$parse('{ "foo" "foobar" }'),
'EXPECTED : GOT "foobar"'
)
expect_error(
json$parse('{ "foo": "foobar" "foo2": "foobar2" }'),
'EXPECTED , or } GOT "foo2"'
)
expect_error(
json$parse('[1,2,3 4]'),
'EXPECTED , GOT 4'
)
})
test_that("get_json_sha", {
inp <- '
{
"sha": "e183ccdc515bbb8e7f32d8d16586aed9eea6de0b",
"author": {
"name": "Hadley Wickham",
"email": "h.wickham@gmail.com",
"date": "2015-03-30T13:55:18Z"
},
}'
expect_identical(
get_json_sha(inp),
"e183ccdc515bbb8e7f32d8d16586aed9eea6de0b")
inp2 <- '
{
"sha":
"e183ccdc515bbb8e7f32d8d16586aed9eea6de0b",
"author": {
"name": "Hadley Wickham",
"email": "h.wickham@gmail.com",
"date": "2015-03-30T13:55:18Z"
},
}'
expect_identical(
get_json_sha(inp2),
"e183ccdc515bbb8e7f32d8d16586aed9eea6de0b")
inp3 <- '
{
"nosha":
"e183ccdc515bbb8e7f32d8d16586aed9eea6de0b",
"author": {
"name": "Hadley Wickham",
"email": "h.wickham@gmail.com",
"date": "2015-03-30T13:55:18Z"
},
}'
expect_identical(
get_json_sha(inp3),
NA_character_)
})