feat: 添加严格比较运算符与一元加法

This commit is contained in:
爱特每羊队
2026-06-06 20:08:10 +08:00
parent 33314265eb
commit e245ab347a
7 changed files with 263 additions and 0 deletions
+55
View File
@@ -1,5 +1,7 @@
use oxide_compiler::compiler::Compiler;
use oxide_parser::Allocator;
use oxide_types::value::JsValue;
use oxide_vm::coercion;
use oxide_vm::vm::Vm;
fn eval(source: &str) -> String {
@@ -33,3 +35,56 @@ fn eval_coercion_bool_equals_int() {
fn eval_not_falsy() {
assert_eq!(eval("!0"), "true");
}
#[test]
fn test_same_value_signed_zero() {
assert!(!coercion::same_value(
JsValue::float(0.0),
JsValue::float(-0.0)
));
}
#[test]
fn test_strict_equality_signed_zero() {
assert!(coercion::strict_equality(
JsValue::float(0.0),
JsValue::float(-0.0)
));
}
#[test]
fn test_same_value_nan() {
assert!(coercion::same_value(
JsValue::float(f64::NAN),
JsValue::float(f64::NAN)
));
}
#[test]
fn test_same_value_type_mismatch() {
assert!(!coercion::same_value(JsValue::int(1), JsValue::float(1.0)));
}
#[test]
fn test_strict_equality_nan() {
assert!(!coercion::strict_equality(
JsValue::float(f64::NAN),
JsValue::float(f64::NAN)
));
}
#[test]
fn test_strict_equality_type_mismatch() {
assert!(!coercion::strict_equality(
JsValue::int(1),
JsValue::float(1.0)
));
}
#[test]
fn test_strict_equality_null_undefined() {
assert!(!coercion::strict_equality(
JsValue::null(),
JsValue::undefined()
));
}
+64
View File
@@ -58,3 +58,67 @@ fn eval_comparison_gte() {
fn eval_string_number_eq() {
assert_eq!(eval("5 == 5"), "true");
}
#[test]
fn eval_strict_eq_true() {
assert_eq!(eval("1 === 1"), "true");
}
#[test]
fn eval_strict_eq_false_type() {
assert_eq!(eval("1 === '1'"), "false");
}
#[test]
fn eval_strict_eq_null_undefined() {
let source = r#"
var undef;
undef === null
"#;
assert_eq!(eval(source), "false");
}
#[test]
fn eval_strict_eq_bool_number() {
assert_eq!(eval("true === 1"), "false");
}
#[test]
fn eval_strict_eq_signed_zero() {
assert_eq!(eval("+0 === -0"), "true");
}
#[test]
fn eval_strict_neq_true() {
assert_eq!(eval("1 !== '1'"), "true");
}
#[test]
fn eval_strict_neq_false() {
assert_eq!(eval("1 !== 1"), "false");
}
#[test]
fn eval_unary_plus_number() {
assert_eq!(eval("+'42'"), "42");
}
#[test]
fn eval_unary_plus_bool() {
assert_eq!(eval("+true"), "1");
}
#[test]
fn eval_unary_plus_null() {
assert_eq!(eval("+null"), "0");
}
#[test]
fn eval_unary_plus_nan() {
assert_eq!(eval("+'hello'"), "NaN");
}
#[test]
fn eval_unary_plus_false() {
assert_eq!(eval("+false"), "0");
}