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