Skip to content

Commit d7a083c

Browse files
committed
Reorganize tests, closes #69
1 parent 7b45cf4 commit d7a083c

File tree

6 files changed

+1410
-1411
lines changed

6 files changed

+1410
-1411
lines changed

src/number.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ const NAN_MASK: u8 = !1;
3232
#[derive(Copy, Clone, Debug)]
3333
pub struct Number {
3434
// A byte describing the sign and NaN-ness of the number.
35-
// category == 0 -> negative sign
36-
// category == 1 -> positive sign
37-
// category > 1 -> NaN
35+
//
36+
// category == 0 (NEGATIVE constant) -> negative sign
37+
// category == 1 (POSITIVE constnat) -> positive sign
38+
// category > 1 (matches NAN_MASK constant) -> NaN
3839
category: u8,
3940

4041
// Decimal exponent, analog to `e` notation in string form.

src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use object::Object;
2222
use number::Number;
2323
use { JsonValue, Error, Result };
2424

25-
// This is not actual max precision, but a treshold at which number parsing
25+
// This is not actual max precision, but a threshold at which number parsing
2626
// kicks into checked math.
2727
const MAX_PRECISION: u64 = 576460752303423500;
2828

@@ -95,7 +95,7 @@ macro_rules! expect_sequence {
9595

9696

9797
// A drop in macro for when we expect to read a byte, but we don't care
98-
// about any whitespace characters that might occure before it.
98+
// about any whitespace characters that might occur before it.
9999
macro_rules! expect_byte_ignore_whitespace {
100100
($parser:ident) => ({
101101
let mut ch = expect_byte!($parser);

tests/json_checker.rs

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
#[macro_use]
2+
extern crate json;
3+
4+
mod json_checker_fail {
5+
use super::json::parse;
6+
7+
#[test]
8+
fn unclosed_array() {
9+
assert!(parse(r#"["Unclosed array""#).is_err());
10+
}
11+
12+
#[test]
13+
fn unquoted_key() {
14+
assert!(parse(r#"{unquoted_key: "keys must be quoted"}"#).is_err());
15+
}
16+
17+
#[test]
18+
fn extra_comma_arr() {
19+
assert!(parse(r#"["extra comma",]"#).is_err());
20+
}
21+
22+
#[test]
23+
fn double_extra_comma() {
24+
assert!(parse(r#"["double extra comma",,]"#).is_err());
25+
}
26+
27+
#[test]
28+
fn missing_value() {
29+
assert!(parse(r#"[ , "<-- missing value"]"#).is_err());
30+
}
31+
32+
#[test]
33+
fn comma_after_close() {
34+
assert!(parse(r#"["Comma after the close"],"#).is_err());
35+
}
36+
37+
#[test]
38+
fn extra_close() {
39+
assert!(parse(r#"["Extra close"]]"#).is_err());
40+
}
41+
42+
#[test]
43+
fn extra_comma_obj() {
44+
assert!(parse(r#"{"Extra comma": true,}"#).is_err());
45+
}
46+
47+
#[test]
48+
fn extra_value_after_close() {
49+
assert!(parse(r#"{"Extra value after close": true} "misplaced quoted value""#).is_err());
50+
}
51+
52+
#[test]
53+
fn illegal_expression() {
54+
assert!(parse(r#"{"Illegal expression": 1 + 2}"#).is_err());
55+
}
56+
57+
#[test]
58+
fn illegal_invocation() {
59+
assert!(parse(r#"{"Illegal invocation": alert()}"#).is_err());
60+
}
61+
62+
#[test]
63+
fn numbers_cannot_have_leading_zeroes() {
64+
assert!(parse(r#"{"Numbers cannot have leading zeroes": 013}"#).is_err());
65+
}
66+
67+
#[test]
68+
fn numbers_cannot_be_hex() {
69+
assert!(parse(r#"{"Numbers cannot be hex": 0x14}"#).is_err());
70+
}
71+
72+
#[test]
73+
fn illegal_backslash_escape() {
74+
assert!(parse(r#"["Illegal backslash escape: \x15"]"#).is_err());
75+
}
76+
77+
#[test]
78+
fn naked() {
79+
assert!(parse(r#"[\naked]"#).is_err());
80+
}
81+
82+
#[test]
83+
fn illegal_backslash_escape_2() {
84+
assert!(parse(r#"["Illegal backslash escape: \017"]"#).is_err());
85+
}
86+
87+
#[test]
88+
fn missing_colon() {
89+
assert!(parse(r#"{"Missing colon" null}"#).is_err());
90+
}
91+
92+
#[test]
93+
fn double_colon() {
94+
assert!(parse(r#"{"Double colon":: null}"#).is_err());
95+
}
96+
97+
#[test]
98+
fn comma_instead_of_colon() {
99+
assert!(parse(r#"{"Comma instead of colon", null}"#).is_err());
100+
}
101+
102+
#[test]
103+
fn colon_instead_of_comma() {
104+
assert!(parse(r#"["Colon instead of comma": false]"#).is_err());
105+
}
106+
107+
#[test]
108+
fn bad_value() {
109+
assert!(parse(r#"["Bad value", truth]"#).is_err());
110+
}
111+
112+
#[test]
113+
fn single_quote() {
114+
assert!(parse(r#"['single quote']"#).is_err());
115+
}
116+
117+
#[test]
118+
fn tab_character_in_string() {
119+
assert!(parse("[\"\ttab\tcharacter\tin\tstring\t\"]").is_err());
120+
}
121+
122+
#[test]
123+
fn tab_character_in_string_esc() {
124+
assert!(parse("[\"tab\\\tcharacter\\\tin\\\tstring\\\t\"]").is_err());
125+
}
126+
127+
#[test]
128+
fn line_break() {
129+
assert!(parse("[\"line\nbreak\"]").is_err());
130+
}
131+
132+
#[test]
133+
fn line_break_escaped() {
134+
assert!(parse("[\"line\\\nbreak\"]").is_err());
135+
}
136+
137+
#[test]
138+
fn no_exponent() {
139+
assert!(parse(r#"[0e]"#).is_err());
140+
}
141+
142+
#[test]
143+
fn no_exponent_plus() {
144+
assert!(parse(r#"[0e+]"#).is_err());
145+
}
146+
147+
#[test]
148+
fn exponent_both_signs() {
149+
assert!(parse(r#"[0e+-1]"#).is_err());
150+
}
151+
152+
#[test]
153+
fn comma_instead_of_closing_brace() {
154+
assert!(parse(r#"{"Comma instead if closing brace": true,"#).is_err());
155+
}
156+
157+
#[test]
158+
fn missmatch() {
159+
assert!(parse(r#"["mismatch"}"#).is_err());
160+
}
161+
}
162+
163+
mod json_checker_pass {
164+
use super::json::parse;
165+
166+
#[test]
167+
fn pass_1() {
168+
assert!(parse(r##"
169+
170+
[
171+
"JSON Test Pattern pass1",
172+
{"object with 1 member":["array with 1 element"]},
173+
{},
174+
[],
175+
-42,
176+
true,
177+
false,
178+
null,
179+
{
180+
"integer": 1234567890,
181+
"real": -9876.543210,
182+
"e": 0.123456789e-12,
183+
"E": 1.234567890E+34,
184+
"": 23456789012E66,
185+
"zero": 0,
186+
"one": 1,
187+
"space": " ",
188+
"quote": "\"",
189+
"backslash": "\\",
190+
"controls": "\b\f\n\r\t",
191+
"slash": "/ & \/",
192+
"alpha": "abcdefghijklmnopqrstuvwyz",
193+
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
194+
"digit": "0123456789",
195+
"0123456789": "digit",
196+
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
197+
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
198+
"true": true,
199+
"false": false,
200+
"null": null,
201+
"array":[ ],
202+
"object":{ },
203+
"address": "50 St. James Street",
204+
"url": "http://www.JSON.org/",
205+
"comment": "// /* <!-- --",
206+
"# -- --> */": " ",
207+
" s p a c e d " :[1,2 , 3
208+
209+
,
210+
211+
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
212+
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
213+
"quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
214+
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
215+
: "A key can be any string"
216+
},
217+
0.5 ,98.6
218+
,
219+
99.44
220+
,
221+
222+
1066,
223+
1e1,
224+
0.1e1,
225+
1e-1,
226+
1e00,2e+00,2e-00
227+
,"rosebud"]
228+
229+
"##).is_ok());
230+
}
231+
232+
#[test]
233+
fn pass_2() {
234+
assert!(parse(r#"[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]"#).is_ok());
235+
}
236+
237+
#[test]
238+
fn pass_3() {
239+
assert!(parse(r#"
240+
241+
{
242+
"JSON Test Pattern pass3": {
243+
"The outermost value": "must be an object or array.",
244+
"In this test": "It is an object."
245+
}
246+
}
247+
248+
"#).is_ok());
249+
}
250+
}

0 commit comments

Comments
 (0)