-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathjson.rs
58 lines (51 loc) · 1.57 KB
/
json.rs
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
import core::*;
use std;
import option;
import std::json::*;
import option::{none, some};
#[test]
fn test_from_str_null() {
assert(from_str("null") == some(null));
}
#[test]
fn test_from_str_num() {
assert(from_str("3") == some(num(3f)));
assert(from_str("3.1") == some(num(3.1f)));
assert(from_str("-1.2") == some(num(-1.2f)));
assert(from_str(".4") == some(num(0.4f)));
}
#[test]
fn test_from_str_str() {
assert(from_str("\"foo\"") == some(string("foo")));
assert(from_str("\"\\\"\"") == some(string("\"")));
assert(from_str("\"lol") == none);
}
#[test]
fn test_from_str_bool() {
assert(from_str("true") == some(boolean(true)));
assert(from_str("false") == some(boolean(false)));
assert(from_str("truz") == none);
}
#[test]
fn test_from_str_list() {
assert(from_str("[]") == some(list(@[])));
assert(from_str("[true]") == some(list(@[boolean(true)])));
assert(from_str("[null]") == some(list(@[null])));
assert(from_str("[3, 1]") == some(list(@[num(3f), num(1f)])));
assert(from_str("[2, [4, 1]]") ==
some(list(@[num(2f), list(@[num(4f), num(1f)])])));
assert(from_str("[2, ]") == none);
assert(from_str("[5, ") == none);
assert(from_str("[6 7]") == none);
assert(from_str("[3") == none);
}
#[test]
fn test_from_str_dict() {
assert(from_str("{}") != none);
assert(from_str("{\"a\": 3}") != none);
assert(from_str("{\"a\": null}") != none);
assert(from_str("{\"a\": }") == none);
assert(from_str("{\"a\" }") == none);
assert(from_str("{\"a\"") == none);
assert(from_str("{") == none);
}