1 use js_sys::*;
2 use wasm_bindgen::prelude::*;
3 use wasm_bindgen::JsCast;
4 use wasm_bindgen_test::*;
5
6 #[wasm_bindgen_test]
parse_array()7 fn parse_array() {
8 let js_array = JSON::parse("[1, 2, 3]").unwrap();
9 assert!(Array::is_array(&js_array));
10
11 let array = Array::from(&js_array);
12 assert_eq!(array.length(), 3);
13 assert_eq!(array.pop(), 3);
14 assert_eq!(array.pop(), 2);
15 assert_eq!(array.pop(), 1);
16 }
17
18 #[wasm_bindgen_test]
parse_object()19 fn parse_object() {
20 let js_object = JSON::parse("{\"x\": 5, \"y\": true, \"z\": [\"foo\", \"bar\"]}").unwrap();
21 assert!(js_object.is_object());
22
23 let obj = Object::from(js_object);
24 let keys = Object::keys(&obj);
25 assert_eq!(keys.length(), 3);
26 assert_eq!(keys.pop().as_string().unwrap(), "z");
27 assert_eq!(keys.pop().as_string().unwrap(), "y");
28 assert_eq!(keys.pop().as_string().unwrap(), "x");
29
30 let values = Object::values(&obj);
31 assert_eq!(values.length(), 3);
32
33 let z = values.pop();
34 assert!(Array::is_array(&z));
35 let z_array = Array::from(&z);
36 assert_eq!(z_array.length(), 2);
37
38 let y = values.pop();
39 assert_eq!(y.as_bool(), Some(true));
40
41 let x = values.pop();
42 assert_eq!(x.as_f64().unwrap(), 5.0);
43 }
44
45 #[wasm_bindgen_test]
parse_error()46 fn parse_error() {
47 let js_object = JSON::parse("invalid json");
48 assert!(js_object.is_err());
49 let err = js_object.unwrap_err();
50 assert!(err.is_instance_of::<Error>());
51 }
52
53 #[wasm_bindgen_test]
stringify()54 fn stringify() {
55 let arr = Array::new();
56 arr.push(&JsValue::from(1));
57 arr.push(&JsValue::from(true));
58 arr.push(&JsValue::from("hello"));
59
60 let str1: String = JSON::stringify(&JsValue::from(arr)).unwrap().into();
61 assert_eq!(str1, "[1,true,\"hello\"]");
62
63 let obj = Object::new();
64 Reflect::set(obj.as_ref(), &JsValue::from("foo"), &JsValue::from("bar")).unwrap();
65 let str2: String = JSON::stringify(&JsValue::from(obj)).unwrap().into();
66 assert_eq!(str2, "{\"foo\":\"bar\"}");
67 }
68
69 #[wasm_bindgen_test]
stringify_error()70 fn stringify_error() {
71 let func = Function::new_no_args("throw new Error(\"rust really rocks\")");
72 let obj = Object::new();
73 Reflect::set(obj.as_ref(), &JsValue::from("toJSON"), func.as_ref()).unwrap();
74
75 let result = JSON::stringify(&JsValue::from(obj));
76 assert!(result.is_err());
77 let err_obj = result.unwrap_err();
78 assert!(err_obj.is_instance_of::<Error>());
79 let err: &Error = err_obj.dyn_ref().unwrap();
80 let err_msg: String = From::from(err.message());
81 assert!(err_msg.contains("rust really rocks"));
82 }
83
84 #[wasm_bindgen_test]
stringify_with_replacer()85 fn stringify_with_replacer() {
86 let obj = Object::new();
87 Reflect::set(obj.as_ref(), &JsValue::from("foo"), &JsValue::from("bar")).unwrap();
88 Reflect::set(
89 obj.as_ref(),
90 &JsValue::from("hello"),
91 &JsValue::from("world"),
92 )
93 .unwrap();
94
95 let replacer_array = Array::new();
96 replacer_array.push(&JsValue::from("hello"));
97 let output1: String =
98 JSON::stringify_with_replacer(&JsValue::from(obj.clone()), &JsValue::from(replacer_array))
99 .unwrap()
100 .into();
101 assert_eq!(output1, "{\"hello\":\"world\"}");
102
103 let replacer_func =
104 Function::new_with_args("key, value", "return key === 'hello' ? undefined : value");
105 let output2: String =
106 JSON::stringify_with_replacer(&JsValue::from(obj), &JsValue::from(replacer_func))
107 .unwrap()
108 .into();
109 assert_eq!(output2, "{\"foo\":\"bar\"}");
110 }
111
112 #[wasm_bindgen_test]
stringify_with_replacer_error()113 fn stringify_with_replacer_error() {
114 let arr = Array::new();
115 arr.push(&JsValue::from(1));
116 arr.push(&JsValue::from(true));
117 arr.push(&JsValue::from("hello"));
118
119 let replacer = Function::new_no_args("throw new Error(\"rust really rocks\")");
120
121 let result = JSON::stringify_with_replacer(&JsValue::from(arr), &JsValue::from(replacer));
122 assert!(result.is_err());
123 let err_obj = result.unwrap_err();
124 assert!(err_obj.is_instance_of::<Error>());
125 let err: &Error = err_obj.dyn_ref().unwrap();
126 let err_msg: String = From::from(err.message());
127 assert!(err_msg.contains("rust really rocks"));
128 }
129
130 #[wasm_bindgen_test]
stringify_with_replacer_and_space()131 fn stringify_with_replacer_and_space() {
132 let arr = Array::new();
133 arr.push(&JsValue::from(1));
134 arr.push(&JsValue::from(true));
135 arr.push(&JsValue::from("hello"));
136
137 let output1: String = JSON::stringify_with_replacer_and_space(
138 &JsValue::from(arr),
139 &JsValue::NULL,
140 &JsValue::from(4),
141 )
142 .unwrap()
143 .into();
144 assert_eq!(output1, "[\n 1,\n true,\n \"hello\"\n]");
145
146 let obj = Object::new();
147 Reflect::set(obj.as_ref(), &JsValue::from("foo"), &JsValue::from("bar")).unwrap();
148 Reflect::set(
149 obj.as_ref(),
150 &JsValue::from("hello"),
151 &JsValue::from("world"),
152 )
153 .unwrap();
154
155 let replacer_array = Array::new();
156 replacer_array.push(&JsValue::from("hello"));
157 let output2: String = JSON::stringify_with_replacer_and_space(
158 &JsValue::from(obj.clone()),
159 &JsValue::from(replacer_array),
160 &JsValue::from(4),
161 )
162 .unwrap()
163 .into();
164 assert_eq!(output2, "{\n \"hello\": \"world\"\n}");
165
166 let replacer_func =
167 Function::new_with_args("key, value", "return key === 'hello' ? undefined : value");
168 let output3: String = JSON::stringify_with_replacer_and_space(
169 &JsValue::from(obj),
170 &JsValue::from(replacer_func),
171 &JsValue::from(4),
172 )
173 .unwrap()
174 .into();
175 assert_eq!(output3, "{\n \"foo\": \"bar\"\n}");
176 }
177
178 #[wasm_bindgen_test]
stringify_with_replacer_and_space_error()179 fn stringify_with_replacer_and_space_error() {
180 let arr = Array::new();
181 arr.push(&JsValue::from(1));
182 arr.push(&JsValue::from(true));
183 arr.push(&JsValue::from("hello"));
184
185 let replacer = Function::new_no_args("throw new Error(\"rust really rocks\")");
186
187 let result = JSON::stringify_with_replacer_and_space(
188 &JsValue::from(arr),
189 &JsValue::from(replacer),
190 &JsValue::from(4),
191 );
192 assert!(result.is_err());
193 let err_obj = result.unwrap_err();
194 assert!(err_obj.is_instance_of::<Error>());
195 let err: &Error = err_obj.dyn_ref().unwrap();
196 let err_msg: String = From::from(err.message());
197 assert!(err_msg.contains("rust really rocks"));
198 }
199