Add a proper test.

And fix loads of bugs.
This commit is contained in:
Richard Dodd
2019-01-12 10:45:38 +00:00
parent f3dd38690a
commit 5bffc28631
3 changed files with 42 additions and 5 deletions

View File

@ -377,7 +377,7 @@ impl<'a> Context<'a> {
} }
if (type == 'function') { if (type == 'function') {
const name = val.name; const name = val.name;
if (typeof name == 'string') { if (typeof name == 'string' && name.length > 0) {
return `Function(${name})`; return `Function(${name})`;
} else { } else {
return 'Function'; return 'Function';
@ -391,16 +391,16 @@ impl<'a> Context<'a> {
debug += debug_str(val[0]); debug += debug_str(val[0]);
} }
for(let i = 1; i < length; i++) { for(let i = 1; i < length; i++) {
debug += debug_str(val[i]) + ', '; debug += ', ' + debug_str(val[i]);
} }
debug += ']'; debug += ']';
return debug; return debug;
} }
// Test for built-in // Test for built-in
const builtInMatches = /\\[object ([^])+\\]/.exec(toString.call(val)); const builtInMatches = /\\[object ([^\\]]+)\\]/.exec(toString.call(val));
let className; let className;
if (builtInMatches.len > 0) { if (builtInMatches.length > 1) {
className = builtInMatches[0]; className = builtInMatches[1];
} else { } else {
// Failed to match the standard '[object ClassName]' // Failed to match the standard '[object ClassName]'
return toString.call(val); return toString.call(val);
@ -414,6 +414,7 @@ impl<'a> Context<'a> {
} catch (_) { } catch (_) {
return 'Object'; return 'Object';
} }
// TODO we could test for more things here, like `Set`s and `Map`s.
} else { } else {
return className; return className;
} }

View File

@ -41,3 +41,17 @@ exports.js_eq_works = () => {
assert.strictEqual(wasm.eq_test(x, x), true); assert.strictEqual(wasm.eq_test(x, x), true);
assert.strictEqual(wasm.eq_test1(x), true); assert.strictEqual(wasm.eq_test1(x), true);
}; };
exports.debug_values = () => ([
null,
undefined,
0,
1.0,
true,
[1,2,3],
"string",
{test: "object"},
[1.0, [2.0, 3.0]],
() => (null),
new Set(),
]);

View File

@ -8,6 +8,7 @@ extern "C" {
fn js_works(); fn js_works();
fn js_eq_works(); fn js_eq_works();
fn assert_null(v: JsValue); fn assert_null(v: JsValue);
fn debug_values() -> JsValue;
} }
#[wasm_bindgen_test] #[wasm_bindgen_test]
@ -145,3 +146,24 @@ fn memory_accessor_appears_to_work() {
.for_each(&mut |val, _, _| v.push(val)); .for_each(&mut |val, _, _| v.push(val));
assert_eq!(v, [3, 0, 0, 0]); assert_eq!(v, [3, 0, 0, 0]);
} }
#[wasm_bindgen_test]
fn debug_output() {
let test_iter = debug_values().dyn_into::<js_sys::Array>().unwrap().values().into_iter();
let expecteds = vec![
"JsValue(null)",
"JsValue(undefined)",
"JsValue(0)",
"JsValue(1)",
"JsValue(true)",
"JsValue([1, 2, 3])",
"JsValue(\"string\")",
"JsValue(Object({\"test\":\"object\"}))",
"JsValue([1, [2, 3]])",
"JsValue(Function)",
"JsValue(Set)",
];
for (test, expected) in test_iter.zip(expecteds) {
assert_eq!(format!("{:?}", test.unwrap()), expected);
}
}