mirror of https://github.com/boa-dev/boa.git
Browse Source
* Added a bunch more tests * Removed extra file * Fix tests * Increasing code coverage * Fix clippy lint * Removed ut_ prefix for testspull/2899/head
Iban Eguia Moraza
2 years ago
committed by
GitHub
17 changed files with 481 additions and 52 deletions
@ -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()); |
||||
} |
||||
|
@ -0,0 +1,166 @@
|
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn context() { |
||||
let result: ParseResult<String> = ParseResult::Err(Error::expected( |
||||
["testing".to_owned()], |
||||
"nottesting", |
||||
Span::new(Position::new(1, 1), Position::new(1, 1)), |
||||
"before", |
||||
)); |
||||
|
||||
assert_eq!(result.context(), Some("before")); |
||||
|
||||
let result = result.set_context("after"); |
||||
|
||||
assert_eq!(result.context(), Some("after")); |
||||
|
||||
let error = result.unwrap_err(); |
||||
if let Error::Expected { |
||||
expected, |
||||
found, |
||||
span, |
||||
context, |
||||
} = error |
||||
{ |
||||
assert_eq!(expected.as_ref(), &["testing".to_owned()]); |
||||
assert_eq!(found, "nottesting".into()); |
||||
assert_eq!(span, Span::new(Position::new(1, 1), Position::new(1, 1))); |
||||
assert_eq!(context, "after"); |
||||
} else { |
||||
unreachable!(); |
||||
} |
||||
|
||||
let err = Error::AbruptEnd; |
||||
assert!(err.context().is_none()); |
||||
let err = err.set_context("ignored"); |
||||
assert!(err.context().is_none()); |
||||
} |
||||
|
||||
#[test] |
||||
fn from_lex_error() { |
||||
let lex_err = LexError::syntax("testing", Position::new(1, 1)); |
||||
let parse_err: Error = lex_err.into(); |
||||
|
||||
assert!(matches!(parse_err, Error::Lex { .. })); |
||||
|
||||
let lex_err = LexError::syntax("testing", Position::new(1, 1)); |
||||
let parse_err = Error::lex(lex_err); |
||||
|
||||
assert!(matches!(parse_err, Error::Lex { .. })); |
||||
} |
||||
|
||||
#[test] |
||||
fn misplaced_function_declaration() { |
||||
let err = Error::misplaced_function_declaration(Position::new(1, 1), false); |
||||
if let Error::General { message, position } = err { |
||||
assert_eq!( |
||||
message.as_ref(), |
||||
"functions can only be declared at the top level or inside a block." |
||||
); |
||||
assert_eq!(position, Position::new(1, 1)); |
||||
} else { |
||||
unreachable!() |
||||
} |
||||
|
||||
let err = Error::misplaced_function_declaration(Position::new(1, 1), true); |
||||
if let Error::General { message, position } = err { |
||||
assert_eq!( |
||||
message.as_ref(), |
||||
"in strict mode code, functions can only be declared at the top level or inside a block." |
||||
); |
||||
assert_eq!(position, Position::new(1, 1)); |
||||
} else { |
||||
unreachable!() |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn wrong_labelled_function_declaration() { |
||||
let err = Error::wrong_labelled_function_declaration(Position::new(1, 1)); |
||||
if let Error::General { message, position } = err { |
||||
assert_eq!( |
||||
message.as_ref(), |
||||
"labelled functions can only be declared at the top level or inside a block" |
||||
); |
||||
assert_eq!(position, Position::new(1, 1)); |
||||
} else { |
||||
unreachable!() |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn display() { |
||||
let err = Error::expected( |
||||
["testing".to_owned()], |
||||
"nottesting", |
||||
Span::new(Position::new(1, 1), Position::new(1, 1)), |
||||
"context", |
||||
); |
||||
assert_eq!( |
||||
err.to_string(), |
||||
"expected token 'testing', got 'nottesting' in context at line 1, col 1" |
||||
); |
||||
|
||||
let err = Error::expected( |
||||
["testing".to_owned(), "more".to_owned()], |
||||
"nottesting", |
||||
Span::new(Position::new(1, 1), Position::new(1, 3)), |
||||
"context", |
||||
); |
||||
assert_eq!( |
||||
err.to_string(), |
||||
"expected one of 'testing' or 'more', got 'nottesting' in context at line 1, col 1" |
||||
); |
||||
|
||||
let err = Error::expected( |
||||
["testing".to_owned(), "more".to_owned(), "tokens".to_owned()], |
||||
"nottesting", |
||||
Span::new(Position::new(1, 1), Position::new(1, 3)), |
||||
"context", |
||||
); |
||||
assert_eq!( |
||||
err.to_string(), |
||||
"expected one of 'testing', 'more' or 'tokens', got 'nottesting' in context at line 1, col 1" |
||||
); |
||||
|
||||
let err = Error::expected( |
||||
[ |
||||
"testing".to_owned(), |
||||
"more".to_owned(), |
||||
"tokens".to_owned(), |
||||
"extra".to_owned(), |
||||
], |
||||
"nottesting", |
||||
Span::new(Position::new(1, 1), Position::new(1, 3)), |
||||
"context", |
||||
); |
||||
assert_eq!( |
||||
err.to_string(), |
||||
"expected one of 'testing', 'more', 'tokens' or 'extra', got 'nottesting' in context at line 1, col 1" |
||||
); |
||||
|
||||
let err = Error::unexpected( |
||||
"nottesting", |
||||
Span::new(Position::new(1, 1), Position::new(1, 3)), |
||||
"error message", |
||||
); |
||||
assert_eq!( |
||||
err.to_string(), |
||||
"unexpected token 'nottesting', error message at line 1, col 1" |
||||
); |
||||
|
||||
let err = Error::general("this is a general error message", Position::new(1, 1)); |
||||
assert_eq!( |
||||
err.to_string(), |
||||
"this is a general error message at line 1, col 1" |
||||
); |
||||
|
||||
let err = Error::AbruptEnd; |
||||
assert_eq!(err.to_string(), "abrupt end"); |
||||
|
||||
let lex_err = LexError::syntax("testing", Position::new(1, 1)); |
||||
let err = Error::lex(lex_err); |
||||
|
||||
assert_eq!(err.to_string(), "testing at line 1, col 1"); |
||||
} |
@ -1,7 +1,145 @@
|
||||
#![allow(clippy::cognitive_complexity)] |
||||
|
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn check_unicode_version() { |
||||
assert_eq!( |
||||
super::UNICODE_VERSION, |
||||
unicode_general_category::UNICODE_VERSION |
||||
); |
||||
assert_eq!(UNICODE_VERSION, unicode_general_category::UNICODE_VERSION); |
||||
} |
||||
|
||||
#[test] |
||||
fn is_id_start() { |
||||
// Sunny day
|
||||
for c in 'a'..='z' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
for c in 'A'..='Z' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
for c in '\u{00E0}'..='\u{00F6}' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
for c in '\u{02B0}'..='\u{02C1}' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
for c in '\u{05D0}'..='\u{05DE}' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
for c in '\u{1F88}'..='\u{1F89}' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
for c in '\u{0391}'..='\u{039F}' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
for c in '\u{2160}'..='\u{216F}' { |
||||
assert!(c.is_id_start()); |
||||
} |
||||
|
||||
// Rainy day
|
||||
for c in '0'..='9' { |
||||
assert!(!c.is_id_start()); |
||||
} |
||||
assert!(!' '.is_id_start()); |
||||
assert!(!'\n'.is_id_start()); |
||||
assert!(!'\t'.is_id_start()); |
||||
assert!(!'!'.is_id_start()); |
||||
assert!(!';'.is_id_start()); |
||||
assert!(!'-'.is_id_start()); |
||||
assert!(!'_'.is_id_start()); |
||||
assert!(!'='.is_id_start()); |
||||
assert!(!'+'.is_id_start()); |
||||
assert!(!'('.is_id_start()); |
||||
assert!(!')'.is_id_start()); |
||||
} |
||||
|
||||
#[test] |
||||
fn is_id_continue() { |
||||
// Sunny day
|
||||
for c in 'a'..='z' { |
||||
assert!(c.is_id_continue()); |
||||
} |
||||
for c in 'A'..='Z' { |
||||
assert!(c.is_id_continue()); |
||||
} |
||||
for c in '0'..='9' { |
||||
assert!(c.is_id_continue()); |
||||
} |
||||
for c in '\u{0300}'..='\u{036F}' { |
||||
assert!(c.is_id_continue()); |
||||
} |
||||
for c in '\u{093E}'..='\u{094F}' { |
||||
assert!(c.is_id_continue()); |
||||
} |
||||
for c in '\u{0660}'..='\u{0669}' { |
||||
assert!(c.is_id_continue()); |
||||
} |
||||
for c in ['_', '\u{203F}', '\u{2040}', '\u{2054}', '\u{FE33}'] { |
||||
assert!(c.is_id_continue()); |
||||
} |
||||
|
||||
// Rainy day
|
||||
for c in [' ', '\n', '\t', '!', ';', '-', '=', '+', '(', '('] { |
||||
assert!(!c.is_id_continue()); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn is_orther_id_start() { |
||||
// Sunny day
|
||||
for c in tables::OTHER_ID_START { |
||||
assert!(c.is_other_id_start()); |
||||
} |
||||
|
||||
// Rainy day
|
||||
for c in [' ', '\n', '='] { |
||||
assert!(!c.is_other_id_start()); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn is_orther_id_continue() { |
||||
// Sunny day
|
||||
for c in tables::OTHER_ID_CONTINUE { |
||||
assert!(c.is_other_id_continue()); |
||||
} |
||||
|
||||
// Rainy day
|
||||
for c in [' ', '\n', '='] { |
||||
assert!(!c.is_other_id_continue()); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn is_pattern_syntax() { |
||||
// Sunny day
|
||||
for c in tables::PATTERN_SYNTAX { |
||||
assert!(c.is_pattern_syntax()); |
||||
} |
||||
|
||||
// Rainy day
|
||||
for c in [' ', '\t', '\n', '\r'] { |
||||
assert!(!c.is_pattern_syntax()); |
||||
} |
||||
} |
||||
|
||||
#[test] |
||||
fn is_pattern_whitespace() { |
||||
// Sunny day
|
||||
for c in tables::PATTERN_WHITE_SPACE { |
||||
assert!(c.is_pattern_whitespace()); |
||||
} |
||||
|
||||
// Rainy day
|
||||
for c in ['+', '~', '`', '!', '@', '^', '='] { |
||||
assert!(!c.is_pattern_whitespace()); |
||||
} |
||||
for c in '0'..='9' { |
||||
assert!(!c.is_pattern_whitespace()); |
||||
} |
||||
for c in 'a'..='z' { |
||||
assert!(!c.is_pattern_whitespace()); |
||||
} |
||||
for c in 'A'..='Z' { |
||||
assert!(!c.is_pattern_whitespace()); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue