Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jun 4, 2020
1 parent a700670 commit 0b0e0b5
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 7 deletions.
11 changes: 10 additions & 1 deletion boa/src/builtins/bigint/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::BigInt;

use crate::{builtins::value::Value, exec::Interpreter};
use crate::{
builtins::{Number, Value},
exec::Interpreter,
};
use num_traits::cast::{FromPrimitive, ToPrimitive};

use std::convert::TryFrom;
Expand Down Expand Up @@ -79,6 +82,12 @@ impl TryFrom<f64> for BigInt {
type Error = TryFromF64Error;

fn try_from(n: f64) -> Result<Self, Self::Error> {
// If the truncated version of the number is not the
// same as the non-truncated version then the floating-point
// number conains a fractional part.
if !Number::equal(n.trunc(), n) {
return Err(TryFromF64Error);
}
match num_bigint::BigInt::from_f64(n) {
Some(bigint) => Ok(BigInt(bigint)),
None => Err(TryFromF64Error),
Expand Down
84 changes: 79 additions & 5 deletions boa/src/builtins/bigint/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{forward, Interpreter, Realm};
use crate::{forward, forward_val, Interpreter, Realm};

#[test]
fn equality() {
Expand Down Expand Up @@ -55,7 +55,7 @@ fn equality() {
}

#[test]
fn bigint_function_conversion() {
fn bigint_function_conversion_from_integer() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

Expand All @@ -70,6 +70,83 @@ fn bigint_function_conversion() {
);
}

#[test]
fn bigint_function_conversion_from_rational() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

assert_eq!(forward(&mut engine, "BigInt(0.0)"), "0n");
assert_eq!(forward(&mut engine, "BigInt(1.0)"), "1n");
assert_eq!(forward(&mut engine, "BigInt(10000.0)"), "10000n");
}

#[test]
fn bigint_function_conversion_from_rational_with_fractional_part() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let scenario = r#"
var x = false;
try {
BigInt(0.1);
} catch (e) {
x = true;
}
"#;
forward_val(&mut engine, scenario).unwrap();
assert_eq!(forward(&mut engine, "x"), "true");
}

#[test]
fn bigint_function_conversion_from_null() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let scenario = r#"
var x = false;
try {
BigInt(null);
} catch (e) {
x = true;
}
"#;
forward_val(&mut engine, scenario).unwrap();
assert_eq!(forward(&mut engine, "x"), "true");
}

#[test]
fn bigint_function_conversion_from_undefined() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let scenario = r#"
var x = false;
try {
BigInt(undefined);
} catch (e) {
x = true;
}
"#;
forward_val(&mut engine, scenario).unwrap();
assert_eq!(forward(&mut engine, "x"), "true");
}

#[test]
fn bigint_function_conversion_from_string() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

assert_eq!(forward(&mut engine, "BigInt('')"), "0n");
assert_eq!(
forward(&mut engine, "BigInt('200000000000000000')"),
"200000000000000000n"
);
assert_eq!(
forward(&mut engine, "BigInt('1000000000000000000000000000000000')"),
"1000000000000000000000000000000000n"
);
}

#[test]
fn add() {
let realm = Realm::create();
Expand Down Expand Up @@ -138,10 +215,7 @@ fn to_string() {
let mut engine = Interpreter::new(realm);

assert_eq!(forward(&mut engine, "1000n.toString()"), "1000");

assert_eq!(forward(&mut engine, "1000n.toString(2)"), "1111101000");

assert_eq!(forward(&mut engine, "255n.toString(16)"), "ff");

assert_eq!(forward(&mut engine, "1000n.toString(36)"), "rs");
}
26 changes: 25 additions & 1 deletion boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{exec, exec::Interpreter, forward, realm::Realm};
use crate::{builtins::Value, exec, exec::Interpreter, forward, realm::Realm};

#[test]
fn empty_let_decl_undefined() {
Expand Down Expand Up @@ -753,3 +753,27 @@ fn function_decl_hoisting() {
"#;
assert_eq!(&exec(scenario), "5");
}

#[test]
fn to_bigint() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

assert!(engine.to_bigint(&Value::null()).is_err());
assert!(engine.to_bigint(&Value::undefined()).is_err());
assert!(engine.to_bigint(&Value::integer(55)).is_ok());
assert!(engine.to_bigint(&Value::rational(10.0)).is_ok());
assert!(engine.to_bigint(&Value::string("100")).is_ok());
}

#[test]
fn to_string() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

assert_eq!(engine.to_string(&Value::null()).unwrap(), "null");
assert_eq!(engine.to_string(&Value::undefined()).unwrap(), "undefined");
assert_eq!(engine.to_string(&Value::integer(55)).unwrap(), "55");
assert_eq!(engine.to_string(&Value::rational(55.0)).unwrap(), "55");
assert_eq!(engine.to_string(&Value::string("hello")).unwrap(), "hello");
}

0 comments on commit 0b0e0b5

Please sign in to comment.