Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add check_output test helper #1458

Merged
merged 8 commits into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
tests: add check_output test helper
  • Loading branch information
bartlomieju committed Aug 8, 2021
commit 800afa9f9b249efbebed9c44120d23d77a25dd77
17 changes: 17 additions & 0 deletions boa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,20 @@ pub(crate) fn exec<T: AsRef<[u8]>>(src: T) -> String {
Err(error) => error.display().to_string(),
}
}

/// Create a clean Context, optinally call `forward` if init script was provided,
/// call `forward` for each of provided test cases and assert output from `forward`
/// matches expected string.
#[cfg(test)]
#[track_caller]
pub(crate) fn check_output(maybe_init: Option<&str>, cases: &[(&str, &str)]) {
let mut context = Context::new();

if let Some(init) = maybe_init {
forward(&mut context, init);
}

for (i, (case, expected)) in cases.iter().enumerate() {
assert_eq!(&forward(&mut context, case), expected, "Test case {} ('{}')", i + 1, case);
}
}
108 changes: 49 additions & 59 deletions boa/src/value/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::float_cmp)]

use super::*;
use crate::{forward, forward_val, Context};
use crate::{check_output, forward, forward_val, Context};

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -64,63 +64,55 @@ fn number_is_true() {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
#[test]
fn abstract_equality_comparison() {
let mut context = Context::new();

assert_eq!(forward(&mut context, "undefined == undefined"), "true");
assert_eq!(forward(&mut context, "null == null"), "true");
assert_eq!(forward(&mut context, "true == true"), "true");
assert_eq!(forward(&mut context, "false == false"), "true");
assert_eq!(forward(&mut context, "'foo' == 'foo'"), "true");
assert_eq!(forward(&mut context, "0 == 0"), "true");
assert_eq!(forward(&mut context, "+0 == -0"), "true");
assert_eq!(forward(&mut context, "+0 == 0"), "true");
assert_eq!(forward(&mut context, "-0 == 0"), "true");
assert_eq!(forward(&mut context, "0 == false"), "true");
assert_eq!(forward(&mut context, "'' == false"), "true");
assert_eq!(forward(&mut context, "'' == 0"), "true");
assert_eq!(forward(&mut context, "'17' == 17"), "true");
assert_eq!(forward(&mut context, "[1,2] == '1,2'"), "true");
assert_eq!(forward(&mut context, "new String('foo') == 'foo'"), "true");
assert_eq!(forward(&mut context, "null == undefined"), "true");
assert_eq!(forward(&mut context, "undefined == null"), "true");
assert_eq!(forward(&mut context, "null == false"), "false");
assert_eq!(forward(&mut context, "[] == ![]"), "true");
assert_eq!(
forward(
&mut context,
"a = { foo: 'bar' }; b = { foo: 'bar'}; a == b"
check_output(None, &[
("undefined == undefined", "true"),
("null == null", "true"),
("true == true", "true"),
("false == false", "true"),
("'foo' == 'foo'", "true"),
("0 == 0", "true"),
("+0 == -0", "true"),
("+0 == 0", "true"),
("-0 == 0", "true"),
("0 == false", "true"),
("'' == false", "true"),
("'' == 0", "true"),
("'17' == 17", "true"),
("[1,2] == '1,2'", "true"),
("new String('foo') == 'foo'", "true"),
("null == undefined", "true"),
("undefined == null", "true"),
("null == false", "false"),
("[] == ![]", "true"),

(
"a = { foo: 'bar' }; b = { foo: 'bar'}; a == b",
"false",
),
"false"
);
assert_eq!(
forward(&mut context, "new String('foo') == new String('foo')"),
"false"
);
assert_eq!(forward(&mut context, "0 == null"), "false");
(
"new String('foo') == new String('foo')",
"false",
),
("0 == null", "false"),

assert_eq!(forward(&mut context, "0 == '-0'"), "true");
assert_eq!(forward(&mut context, "0 == '+0'"), "true");
assert_eq!(forward(&mut context, "'+0' == 0"), "true");
assert_eq!(forward(&mut context, "'-0' == 0"), "true");
("0 == '-0'", "true"),
("0 == '+0'", "true"),
("'+0' == 0", "true"),
("'-0' == 0", "true"),

assert_eq!(forward(&mut context, "0 == NaN"), "false");
assert_eq!(forward(&mut context, "'foo' == NaN"), "false");
assert_eq!(forward(&mut context, "NaN == NaN"), "false");
("0 == NaN", "false"),
("'foo' == NaN", "false"),
("NaN == NaN", "false"),

assert_eq!(
forward(
&mut context,
"Number.POSITIVE_INFINITY === Number.POSITIVE_INFINITY"
(
"Number.POSITIVE_INFINITY === Number.POSITIVE_INFINITY",
"true",
),
"true"
);
assert_eq!(
forward(
&mut context,
"Number.NEGAVIVE_INFINITY === Number.NEGAVIVE_INFINITY"
(
"Number.NEGAVIVE_INFINITY === Number.NEGAVIVE_INFINITY",
"true",
),
"true"
);
]);
}

/// Helper function to get the hash of a `Value`.
Expand Down Expand Up @@ -608,29 +600,27 @@ fn to_integer_or_infinity() {

#[test]
fn test_accessors() {
let mut context = Context::new();
let src = r#"
let arr = [];
let a = { get b() { return "c" }, set b(value) { arr = arr.concat([value]) }} ;
a.b = "a";
"#;
context.eval(src).unwrap();
assert_eq!(forward(&mut context, "a.b"), r#""c""#);
assert_eq!(forward(&mut context, "arr"), r#"[ "a" ]"#);
check_output(Some(src), &[
("a.b", r#""c""#),
("arr", r#"[ "a" ]"#),
]);
}

#[test]
fn to_primitive() {
let mut context = Context::new();
let src = r#"
let a = {};
a[Symbol.toPrimitive] = function() {
return 42;
};
let primitive = a + 0;
"#;
context.eval(src).unwrap();
assert_eq!(forward(&mut context, "primitive"), "42");
check_output(Some(src), &[("primitive", "42")]);
}

/// Test cyclic conversions that previously caused stack overflows
Expand Down