Cotton code
223 lines · 8.0 KB · RustRaw
//! The JS semantics this module reproduces, pinned against what node answers.//!//! Everything here is a value I ran through the real built-in before writing//! the assertion. The two thresholds `Number::toString` switches at (1e21 and//! 1e-7) and the exact whitespace class `\s` matches are where a port drifts,//! and either drift silently changes a fingerprint.use super::*;use serde_json::json;#[test]fn number_to_string_matches_ecmascript() {    let cases: &[(f64, &str)] = &[        (0.0, "0"),        (-0.0, "0"),        (1.0, "1"),        (3.0, "3"),        (-1.0, "-1"),        (1.5, "1.5"),        (0.1, "0.1"),        (-0.25, "-0.25"),        (100.0, "100"),        (1e20, "100000000000000000000"),        (1e21, "1e+21"),        (1e22, "1e+22"),        (1e-6, "0.000001"),        (1e-7, "1e-7"),        (1.5e-7, "1.5e-7"),        (1.7976931348623157e308, "1.7976931348623157e+308"),        (5e-324, "5e-324"),        (0.1 + 0.2, "0.30000000000000004"),        (9007199254740991.0, "9007199254740991"),        (-12345678901.0, "-12345678901"),    ];    for (value, expected) in cases {        assert_eq!(&number_to_string(*value), expected, "String({value})");    }    assert_eq!(number_to_string(f64::NAN), "NaN");    assert_eq!(number_to_string(f64::INFINITY), "Infinity");    assert_eq!(number_to_string(f64::NEG_INFINITY), "-Infinity");}#[test]fn json_stringify_matches_ecmascript() {    // An integral float is `1` in JS and `1.0` in serde_json's own writer —    // the one difference that would change every fingerprint's material.    assert_eq!(json_stringify(&json!(1.0)), "1");    assert_eq!(json_stringify(&json!(1.5)), "1.5");    assert_eq!(json_stringify(&json!(1e21)), "1e+21");    assert_eq!(json_stringify(&json!(null)), "null");    assert_eq!(json_stringify(&json!(true)), "true");    assert_eq!(json_stringify(&json!([1, 2, 3])), "[1,2,3]");    assert_eq!(        json_stringify(&json!({"a":1,"b":[1,2],"c":null})),        r#"{"a":1,"b":[1,2],"c":null}"#    );    // Object keys keep INSERTION order, not sorted order.    assert_eq!(json_stringify(&json!({"z":1,"a":2})), r#"{"z":1,"a":2}"#);}#[test]fn json_stringify_escapes_exactly_what_ecmascript_escapes() {    assert_eq!(json_stringify(&json!("plain")), r#""plain""#);    assert_eq!(json_stringify(&json!("quote\"")), "\"quote\\\"\"");    assert_eq!(json_stringify(&json!("back\\slash")), r#""back\\slash""#);    assert_eq!(        json_stringify(&json!("tab\there\nnewline\r\u{8}\u{c}")),        r#""tab\there\nnewline\r\b\f""#    );    // Other C0 controls go to \u00XX, lower case, four digits.    assert_eq!(json_stringify(&json!("\u{1}\u{1f}")), "\"\\u0001\\u001f\"");    // Everything at or above U+0020 stays verbatim — including DEL, the emoji    // and the CJK that a naive escaper would mangle.    assert_eq!(json_stringify(&json!("\u{7f}")), "\"\u{7f}\"");    assert_eq!(        json_stringify(&json!("\u{1f9f5} \u{65e5}\u{672c}\u{8a9e}")),        "\"\u{1f9f5} \u{65e5}\u{672c}\u{8a9e}\""    );    // U+2028 and U+2029 are LineTerminators in JS SOURCE but JSON.stringify    // does not escape them.    assert_eq!(        json_stringify(&json!("\u{2028}\u{2029}")),        "\"\u{2028}\u{2029}\""    );}#[test]fn parses_the_date_time_string_format() {    assert_eq!(parse_date_ms("1970-01-01T00:00:00.000Z"), Some(0));    assert_eq!(parse_date_ms("1970-01-01"), Some(0));    assert_eq!(parse_date_ms("1970-01-01T00:00:00.001Z"), Some(1));    assert_eq!(parse_date_ms("1969-12-31T23:59:59.999Z"), Some(-1));    assert_eq!(        parse_date_ms("2026-08-23T14:30:00+05:30"),        parse_date_ms("2026-08-23T09:00:00Z")    );    assert_eq!(        parse_date_ms("2026-08-23T14:30:00+0530"),        parse_date_ms("2026-08-23T09:00:00Z")    );    // Extra fractional digits TRUNCATE, they do not round.    assert_eq!(        parse_date_ms("2026-08-23T09:00:00.123456Z"),        parse_date_ms("2026-08-23T09:00:00.123Z")    );    // One fractional digit is tenths.    assert_eq!(        parse_date_ms("2026-08-23T09:00:00.5Z"),        parse_date_ms("2026-08-23T09:00:00.500Z")    );    // A date-only form with an explicit `Z` is accepted — every engine takes    // it, and `toUtcInstant` DEPENDS on it: it appends a `Z` to a zone-less    // value, so `encodeValue("timestamp", "2026-08-23")` parses "2026-08-23Z".    assert_eq!(        parse_date_ms("2026-08-23Z"),        parse_date_ms("2026-08-23T00:00:00.000Z")    );    // Hour 24 is legal at exactly midnight and is the next day.    assert_eq!(        parse_date_ms("2026-08-23T24:00:00Z"),        parse_date_ms("2026-08-24T00:00:00Z")    );}#[test]fn an_out_of_range_day_rolls_over_rather_than_refusing() {    // `new Date("2026-02-30T00:00:00Z")` is 2026-03-02 in every engine — not    // Invalid Date. Refusing here would make the Rust store reject a timestamp    // the TypeScript happily normalises.    assert_eq!(        parse_date_ms("2026-02-30T00:00:00Z"),        parse_date_ms("2026-03-02T00:00:00Z")    );    assert_eq!(        parse_date_ms("2026-02-29T12:00:00Z"),        parse_date_ms("2026-03-01T12:00:00Z")    );    // 2024 IS a leap year, so the 29th is the 29th.    assert_eq!(        to_iso_string(parse_date_ms("2024-02-29T12:00:00Z").expect("parses")),        Some("2024-02-29T12:00:00.000Z".to_string())    );}#[test]fn refuses_what_the_grammar_refuses() {    for bad in [        "nope",        "garbageZ",        "not a date",        "2026-13-01",        "2026-00-01",        "2026-01-32",        "2026-01-00",        "2026-01-01T25:00:00Z",        "2026-01-01T24:00:01Z",        "2026-01-01T00:60:00Z",        "2026-01-01T00:00:60Z",        "2026-01-01T00:00:00.Z",        "2026-01-01T00:00:00+99:00",        "2026-01-01T00:00:00Q",        "2026-01-01T00:00:00Z ",        "2026-01-01+05:30",    ] {        assert_eq!(parse_date_ms(bad), None, "{bad} should not parse");    }}#[test]fn round_trips_through_to_iso_string() {    for iso in [        "1970-01-01T00:00:00.000Z",        "2026-08-23T09:00:00.000Z",        "2026-12-31T23:59:59.999Z",        "0001-01-01T00:00:00.000Z",        "1969-12-31T23:59:59.999Z",    ] {        let ms = parse_date_ms(iso).unwrap_or_else(|| panic!("{iso} parses"));        assert_eq!(to_iso_string(ms).as_deref(), Some(iso));    }}#[test]fn a_year_outside_four_digits_takes_the_expanded_form() {    // What `new Date(…).toISOString()` does at the edges.    let ms = parse_date_ms("+275760-09-13T00:00:00.000Z").expect("the maximum parses");    assert_eq!(        to_iso_string(ms).as_deref(),        Some("+275760-09-13T00:00:00.000Z")    );    assert!(        to_iso_string(ms + 1).is_none(),        "past the limit is a RangeError"    );    let ms = parse_date_ms("-000001-01-01T00:00:00.000Z").expect("a negative year parses");    assert_eq!(        to_iso_string(ms).as_deref(),        Some("-000001-01-01T00:00:00.000Z")    );    // `-000000` is explicitly not a year.    assert_eq!(parse_date_ms("-000000-01-01T00:00:00.000Z"), None);}#[test]fn collapses_the_ecmascript_whitespace_class_and_nothing_else() {    assert_eq!(        collapse_whitespace_and_trim("Hello\n  world "),        "Hello world"    );    assert_eq!(collapse_whitespace_and_trim("a\tb\r\nc"), "a b c");    assert_eq!(collapse_whitespace_and_trim("   "), "");    assert_eq!(collapse_whitespace_and_trim(""), "");    // U+FEFF (BOM/ZWNBSP) IS in JS's `\s`; Rust's `char::is_whitespace` says it    // is not, and the difference would change a fingerprint.    assert_eq!(collapse_whitespace_and_trim("\u{feff}bom\u{feff}"), "bom");    assert!(is_js_whitespace('\u{feff}'));    // U+0085 (NEL) is NOT in JS's `\s`, though Rust calls it whitespace.    assert!(!is_js_whitespace('\u{85}'));    assert_eq!(collapse_whitespace_and_trim("a\u{85}b"), "a\u{85}b");    // The ideographic and narrow no-break spaces are.    assert_eq!(collapse_whitespace_and_trim("trail\u{3000}"), "trail");    assert_eq!(collapse_whitespace_and_trim("a\u{202f}b"), "a b");}