Skip to content

Commit

Permalink
Added a bunch more tests (#2885)
Browse files Browse the repository at this point in the history
* Added a bunch more tests

* Removed extra file

* Fix tests

* Increasing code coverage

* Fix clippy lint

* Removed ut_ prefix for tests
  • Loading branch information
Razican authored May 4, 2023
1 parent af10116 commit 7cb58de
Show file tree
Hide file tree
Showing 17 changed files with 481 additions and 52 deletions.
13 changes: 5 additions & 8 deletions boa_engine/src/builtins/array_buffer/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use super::*;

#[test]
fn ut_sunny_day_create_byte_data_block() {
assert!(create_byte_data_block(100).is_ok());
}
fn create_byte_data_block() {
// Sunny day
assert!(super::create_byte_data_block(100).is_ok());

#[test]
fn ut_rainy_day_create_byte_data_block() {
assert!(create_byte_data_block(u64::MAX).is_err());
// Rainy day
assert!(super::create_byte_data_block(u64::MAX).is_err());
}
4 changes: 2 additions & 2 deletions boa_engine/src/builtins/regexp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ fn no_panic_on_parse_fail() {
TestAction::assert_native_error(
r"var re = /]/u;",
ErrorKind::Syntax,
"Invalid regular expression literal: Unbalanced bracket at position: 1:10",
"Invalid regular expression literal: Unbalanced bracket at line 1, col 10",
),
TestAction::assert_native_error(
r"var re = /a{/u;",
ErrorKind::Syntax,
"Invalid regular expression literal: Invalid quantifier at position: 1:10",
"Invalid regular expression literal: Invalid quantifier at line 1, col 10",
),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/builtins/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ mod tests {

/// Checks that the `decode_byte()` function works as expected.
#[test]
fn ut_decode_byte() {
fn decode_byte() {
// Sunny day tests
assert_eq!(
decode_hex_byte(u16::from(b'2'), u16::from(b'0')).unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/tests/control_flow/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn test_invalid_break_target() {
}
"#},
ErrorKind::Syntax,
"undefined break target: nonexistent at position: 1:1",
"undefined break target: nonexistent at line 1, col 1",
)]);
}

Expand Down
6 changes: 3 additions & 3 deletions boa_engine/src/tests/control_flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn test_invalid_break() {
run_test_actions([TestAction::assert_native_error(
"break;",
ErrorKind::Syntax,
"illegal break statement at position: 1:1",
"illegal break statement at line 1, col 1",
)]);
}

Expand All @@ -21,7 +21,7 @@ fn test_invalid_continue_target() {
}
"#},
ErrorKind::Syntax,
"undefined continue target: nonexistent at position: 1:1",
"undefined continue target: nonexistent at line 1, col 1",
)]);
}

Expand All @@ -30,7 +30,7 @@ fn test_invalid_continue() {
run_test_actions([TestAction::assert_native_error(
"continue;",
ErrorKind::Syntax,
"illegal continue statement at position: 1:1",
"illegal continue statement at line 1, col 1",
)]);
}

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ fn strict_mode_dup_func_parameters() {
function f(a, b, b) {}
"#},
ErrorKind::Syntax,
"Duplicate parameter name not allowed in this context at position: 2:12",
"Duplicate parameter name not allowed in this context at line 2, col 12",
)]);
}

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fn strict_mode_octal() {
var n = 023;
"#},
ErrorKind::Syntax,
"implicit octal literals are not allowed in strict mode at position: 2:9",
"implicit octal literals are not allowed in strict mode at line 2, col 9",
)]);
}

Expand Down
38 changes: 19 additions & 19 deletions boa_engine/src/tests/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,22 @@ fn invalid_unary_access() {
TestAction::assert_native_error(
"++[]",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:1",
"Invalid left-hand side in assignment at line 1, col 1",
),
TestAction::assert_native_error(
"[]++",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:3",
"Invalid left-hand side in assignment at line 1, col 3",
),
TestAction::assert_native_error(
"--[]",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:1",
"Invalid left-hand side in assignment at line 1, col 1",
),
TestAction::assert_native_error(
"[]--",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:3",
"Invalid left-hand side in assignment at line 1, col 3",
),
]);
}
Expand All @@ -171,22 +171,22 @@ fn unary_operations_on_this() {
TestAction::assert_native_error(
"++this",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:1",
"Invalid left-hand side in assignment at line 1, col 1",
),
TestAction::assert_native_error(
"--this",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:1",
"Invalid left-hand side in assignment at line 1, col 1",
),
TestAction::assert_native_error(
"this++",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:5",
"Invalid left-hand side in assignment at line 1, col 5",
),
TestAction::assert_native_error(
"this--",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:5",
"Invalid left-hand side in assignment at line 1, col 5",
),
]);
}
Expand Down Expand Up @@ -306,7 +306,7 @@ fn assignment_to_non_assignable() {
TestAction::assert_native_error(
src,
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:3",
"Invalid left-hand side in assignment at line 1, col 3",
)
}),
);
Expand All @@ -331,7 +331,7 @@ fn assignment_to_non_assignable_ctd() {
TestAction::assert_native_error(
src,
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:13",
"Invalid left-hand side in assignment at line 1, col 13",
)
}),
);
Expand All @@ -345,7 +345,7 @@ fn multicharacter_assignment_to_non_assignable() {
TestAction::assert_native_error(
src,
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:3",
"Invalid left-hand side in assignment at line 1, col 3",
)
}));
}
Expand All @@ -359,7 +359,7 @@ fn multicharacter_assignment_to_non_assignable_ctd() {
TestAction::assert_native_error(
src,
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:13",
"Invalid left-hand side in assignment at line 1, col 13",
)
}),
);
Expand All @@ -374,7 +374,7 @@ fn multicharacter_bitwise_assignment_to_non_assignable() {
TestAction::assert_native_error(
src,
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:3",
"Invalid left-hand side in assignment at line 1, col 3",
)
}),
);
Expand All @@ -394,7 +394,7 @@ fn multicharacter_bitwise_assignment_to_non_assignable_ctd() {
TestAction::assert_native_error(
src,
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:13",
"Invalid left-hand side in assignment at line 1, col 13",
)
}),
);
Expand All @@ -406,22 +406,22 @@ fn assign_to_array_decl() {
TestAction::assert_native_error(
"[1] = [2]",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:5",
"Invalid left-hand side in assignment at line 1, col 5",
),
TestAction::assert_native_error(
"[3, 5] = [7, 8]",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:8",
"Invalid left-hand side in assignment at line 1, col 8",
),
TestAction::assert_native_error(
"[6, 8] = [2]",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:8",
"Invalid left-hand side in assignment at line 1, col 8",
),
TestAction::assert_native_error(
"[6] = [2, 9]",
ErrorKind::Syntax,
"Invalid left-hand side in assignment at position: 1:5",
"Invalid left-hand side in assignment at line 1, col 5",
),
]);
}
Expand Down Expand Up @@ -505,7 +505,7 @@ fn delete_variable_in_strict() {
delete x;
"#},
ErrorKind::Syntax,
"cannot delete variables in strict mode at position: 3:1",
"cannot delete variables in strict mode at line 3, col 1",
)]);
}

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/value/conversions/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod tests {
use crate::{string::utf16, JsValue};

#[test]
fn ut_json_conversions() {
fn json_conversions() {
const DATA: &str = indoc! {r#"
{
"name": "John Doe",
Expand Down
35 changes: 29 additions & 6 deletions boa_parser/src/error.rs → boa_parser/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Error and result implementation for the parser.
#[cfg(test)]
mod tests;

use crate::lexer::Error as LexError;
use boa_ast::{Position, Span};
use std::fmt;
Expand All @@ -8,12 +11,20 @@ use std::fmt;
pub type ParseResult<T> = Result<T, Error>;

pub(crate) trait ErrorContext {
fn context(self, context: &'static str) -> Self;
/// Sets the context of the error, if possible.
fn set_context(self, context: &'static str) -> Self;

/// Gets the context of the error, if any.
fn context(&self) -> Option<&'static str>;
}

impl<T> ErrorContext for ParseResult<T> {
fn context(self, context: &'static str) -> Self {
self.map_err(|e| e.context(context))
fn set_context(self, context: &'static str) -> Self {
self.map_err(|e| e.set_context(context))
}

fn context(&self) -> Option<&'static str> {
self.as_ref().err().and_then(Error::context)
}
}

Expand Down Expand Up @@ -75,7 +86,7 @@ pub enum Error {

impl Error {
/// Changes the context of the error, if any.
fn context(self, new_context: &'static str) -> Self {
fn set_context(self, new_context: &'static str) -> Self {
match self {
Self::Expected {
expected,
Expand All @@ -87,14 +98,26 @@ impl Error {
}
}

/// Gets the context of the error, if any.
const fn context(&self) -> Option<&'static str> {
if let Self::Expected { context, .. } = self {
Some(context)
} else {
None
}
}

/// Creates an `Expected` parsing error.
pub(crate) fn expected<E, F>(expected: E, found: F, span: Span, context: &'static str) -> Self
where
E: Into<Box<[String]>>,
F: Into<Box<str>>,
{
let expected = expected.into();
debug_assert_ne!(expected.len(), 0);

Self::Expected {
expected: expected.into(),
expected,
found: found.into(),
span,
context,
Expand Down Expand Up @@ -209,7 +232,7 @@ impl fmt::Display for Error {
position.line_number(),
position.column_number()
),
Self::Lex { err } => write!(f, "{err}"),
Self::Lex { err } => err.fmt(f),
}
}
}
Loading

0 comments on commit 7cb58de

Please sign in to comment.