Browse Source

reorganising unit tests into their respective files

pull/18/head
Jason Williams 6 years ago
parent
commit
5ef57ffb90
  1. 11
      src/lib/js/string.rs
  2. 36
      src/lib/js/value.rs
  3. 69
      src/lib/syntax/lexer.rs
  4. 70
      tests/lexer_test.rs
  5. 11
      tests/string_test.rs
  6. 32
      tests/value_test.rs

11
src/lib/js/string.rs

@ -51,3 +51,14 @@ pub fn _create(global: Value) -> Value {
pub fn init(global: Value) {
global.set_field_slice("String", _create(global.clone()));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_string_constructor_is_function() {
let global = ValueData::new_obj(None);
let string_constructor = _create(global);
assert_eq!(string_constructor.is_function(), true);
}
}

36
src/lib/js/value.rs

@ -763,3 +763,39 @@ pub fn from_value<A: FromValue>(v: Value) -> Result<A, &'static str> {
pub fn to_value<A: ToValue>(v: A) -> Value {
v.to_value()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_is_object() {
let val = ValueData::new_obj(None);
assert_eq!(val.is_object(), true);
}
#[test]
fn check_string_to_value() {
let s = String::from("Hello");
let v = s.to_value();
assert_eq!(v.is_string(), true);
assert_eq!(v.is_null(), false);
}
#[test]
fn check_undefined() {
let u = ValueData::Undefined;
assert_eq!(u.get_type(), "undefined");
assert_eq!(u.to_string(), "undefined");
}
#[test]
fn check_get_set_field() {
let obj = ValueData::new_obj(None);
// Create string and convert it to a Value
let s = String::from("bar").to_value();
obj.set_field_slice("foo", s);
assert_eq!(obj.get_field_slice("foo").to_string(), "bar");
}
}

69
src/lib/syntax/lexer.rs

@ -441,3 +441,72 @@ impl<'a> Lexer<'a> {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::syntax::ast::keyword::Keyword;
#[test]
fn check_variable_definition_tokens() {
let s = &String::from("let a = 'hello';");
let mut lexer = Lexer::new(s);
lexer.lex().expect("finished");
assert_eq!(lexer.tokens[0].data, TokenData::Keyword(Keyword::Let));
assert_eq!(lexer.tokens[1].data, TokenData::Identifier("a".to_string()));
assert_eq!(
lexer.tokens[2].data,
TokenData::Punctuator(Punctuator::Assign)
);
assert_eq!(
lexer.tokens[3].data,
TokenData::StringLiteral("hello".to_string())
);
}
#[test]
fn check_positions() {
let s = &String::from("console.log(\"hello world\");");
// -------------------123456789
let mut lexer = Lexer::new(s);
lexer.lex().expect("finished");
// The first column is 1 (not zero indexed)
assert_eq!(lexer.tokens[0].pos.column_number, 1);
assert_eq!(lexer.tokens[0].pos.line_number, 1);
// Dot Token starts on line 7
assert_eq!(lexer.tokens[1].pos.column_number, 8);
assert_eq!(lexer.tokens[1].pos.line_number, 1);
// Log Token starts on line 7
assert_eq!(lexer.tokens[2].pos.column_number, 9);
assert_eq!(lexer.tokens[2].pos.line_number, 1);
// Open parenthesis token starts on line 12
assert_eq!(lexer.tokens[3].pos.column_number, 12);
assert_eq!(lexer.tokens[3].pos.line_number, 1);
// String token starts on line 13
assert_eq!(lexer.tokens[4].pos.column_number, 13);
assert_eq!(lexer.tokens[4].pos.line_number, 1);
// Close parenthesis token starts on line 26
assert_eq!(lexer.tokens[5].pos.column_number, 26);
assert_eq!(lexer.tokens[5].pos.line_number, 1);
// Semi Colon token starts on line 27
assert_eq!(lexer.tokens[6].pos.column_number, 27);
assert_eq!(lexer.tokens[6].pos.line_number, 1);
}
// Increment/Decrement
#[test]
fn check_decrement_advances_lexer_2_places() {
// Here we want an example of decrementing an integer
let s = &String::from("let a = b--;");
let mut lexer = Lexer::new(s);
lexer.lex().expect("finished");
assert_eq!(lexer.tokens[4].data, TokenData::Punctuator(Punctuator::Dec));
// Decrementing means adding 2 characters '--', the lexer should consume it as a single token
// and move the curser forward by 2, meaning the next token should be a semicolon
assert_eq!(
lexer.tokens[5].data,
TokenData::Punctuator(Punctuator::Semicolon)
);
}
}

70
tests/lexer_test.rs

@ -1,70 +0,0 @@
extern crate boa;
use boa::syntax::ast::keyword::Keyword;
use boa::syntax::ast::punc::Punctuator;
use boa::syntax::ast::token::TokenData;
use boa::syntax::lexer::Lexer;
#[test]
/// Check basic variable definition tokens
fn check_variable_definition_tokens() {
let s = &String::from("let a = 'hello';");
let mut lexer = Lexer::new(s);
lexer.lex().expect("finished");
assert_eq!(lexer.tokens[0].data, TokenData::Keyword(Keyword::Let));
assert_eq!(lexer.tokens[1].data, TokenData::Identifier("a".to_string()));
assert_eq!(
lexer.tokens[2].data,
TokenData::Punctuator(Punctuator::Assign)
);
assert_eq!(
lexer.tokens[3].data,
TokenData::StringLiteral("hello".to_string())
);
}
#[test]
/// Check positions are correct
fn check_positions() {
let s = &String::from("console.log(\"hello world\");");
// -------------------123456789
let mut lexer = Lexer::new(s);
lexer.lex().expect("finished");
// The first column is 1 (not zero indexed)
assert_eq!(lexer.tokens[0].pos.column_number, 1);
assert_eq!(lexer.tokens[0].pos.line_number, 1);
// Dot Token starts on line 7
assert_eq!(lexer.tokens[1].pos.column_number, 8);
assert_eq!(lexer.tokens[1].pos.line_number, 1);
// Log Token starts on line 7
assert_eq!(lexer.tokens[2].pos.column_number, 9);
assert_eq!(lexer.tokens[2].pos.line_number, 1);
// Open parenthesis token starts on line 12
assert_eq!(lexer.tokens[3].pos.column_number, 12);
assert_eq!(lexer.tokens[3].pos.line_number, 1);
// String token starts on line 13
assert_eq!(lexer.tokens[4].pos.column_number, 13);
assert_eq!(lexer.tokens[4].pos.line_number, 1);
// Close parenthesis token starts on line 26
assert_eq!(lexer.tokens[5].pos.column_number, 26);
assert_eq!(lexer.tokens[5].pos.line_number, 1);
// Semi Colon token starts on line 27
assert_eq!(lexer.tokens[6].pos.column_number, 27);
assert_eq!(lexer.tokens[6].pos.line_number, 1);
}
// Increment/Decrement
#[test]
fn check_decrement_advances_lexer_2_places() {
// Here we want an example of decrementing an integer
let s = &String::from("let a = b--;");
let mut lexer = Lexer::new(s);
lexer.lex().expect("finished");
assert_eq!(lexer.tokens[4].data, TokenData::Punctuator(Punctuator::Dec));
// Decrementing means adding 2 characters '--', the lexer should consume it as a single token
// and move the curser forward by 2, meaning the next token should be a semicolon
assert_eq!(
lexer.tokens[5].data,
TokenData::Punctuator(Punctuator::Semicolon)
);
}

11
tests/string_test.rs

@ -1,11 +0,0 @@
extern crate boa;
use boa::js::string::_create;
use boa::js::object::PROTOTYPE;
use boa::js::value::{ValueData};
#[test]
fn check_string_constructor_is_function() {
let global = ValueData::new_obj(None);
let string_constructor = _create(global);
assert_eq!(string_constructor.is_function(), true);
}

32
tests/value_test.rs

@ -1,32 +0,0 @@
extern crate boa;
use boa::js::value::{ToValue, ValueData};
#[test]
fn check_is_object() {
let val = ValueData::new_obj(None);
assert_eq!(val.is_object(), true);
}
#[test]
fn check_string_to_value() {
let s = String::from("Hello");
let v = s.to_value();
assert_eq!(v.is_string(), true);
assert_eq!(v.is_null(), false);
}
#[test]
fn check_undefined() {
let u = ValueData::Undefined;
assert_eq!(u.get_type(), "undefined");
assert_eq!(u.to_string(), "undefined");
}
#[test]
fn check_get_set_field() {
let obj = ValueData::new_obj(None);
// Create string and convert it to a Value
let s = String::from("bar").to_value();
obj.set_field_slice("foo", s);
assert_eq!(obj.get_field_slice("foo").to_string(), "bar");
}
Loading…
Cancel
Save