Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-foundation

Source of release 0.6.0

0.6.0 / src / ak / tests_json.lucb

#==============================================================================================
#
#   tests_json - tests of the JSON fragments (AK::JsonValue, JsonArray, JsonObject, JsonParser)
#
#   DESCRIPTION:
#       Port of Tests/AK/TestJSON.cpp (Ladybird 47c82b38d0). Lambdas passed to for_each and
#       try_for_each are functions with a context pointer.
#
#       Adapted: fallible_json_*_for_each check the ErrorOr<void> cases; the cases with C++
#       custom error types (CustomError, CustomErrorOr) test C++ return-type deduction, which
#       has no luce-base counterpart. json_object_move_from_value does not check that the
#       moved-from object is empty: a move is a copy in the port (DESIGN.md ยง3.3 rule 1).
#
#==============================================================================================

# mark: Helpers =================================================================================

## JsonValue::from_string(text).value() (MUST).
func parse_json(text: str) -> JsonValue2:
    return json_value2_from_string(sv(text)) else trap("MUST: JsonValue::from_string")

## Whether JsonValue::from_string(text) is an error.
func parse_json_fails(text: str) -> bool:
    return parse_json_fails_bytes(text.bytes)

## Whether JsonValue::from_string over `bytes` (which need not be UTF-8) is an error.
func parse_json_fails_bytes(bytes: const u8[]) -> bool:
    let result = json_value2_from_string(literalsv_u8(bytes.data, bytes.length)) catch:
        return true
    _ = result
    return false

## The text of a String.
func text_of_string(string: String) -> str:
    # A short String keeps its bytes inline, in this frame's copy: copy them out.
    let view = string_bytes_as_string_view(&string)
    let bytes = string_view_bytes(&view)
    let copy = new u8[bytes.length] --- else trap("out of memory")
    memory.copy(copy, bytes, bytes.length)
    return (str)copy

## A JsonValue holding the string `text`.
func json_string_value(text: str) -> JsonValue2:
    var value = json_value2_create()
    json_value2_construct_string_view(&value, sv(text))
    return value

let raw_form_json = """
    {
        "name": "Form1",
        "widgets": [
            {
                "enabled": true,
                "forecolor": "#000000ff",
                "ruler_visible": false,
                "autofill": false,
                "x": 155,
                "tooltip": null,
                "height": 121,
                "width": 126,
                "y": 10,
                "class": "GTextEditor",
                "text": "Hi",
                "backcolor": "#c0c0c0ff",
                "visible":true
            }
        ]
    }"""

## The widget lambda of load_form: reads the class and walks the members.
func load_form_widget(context: void*?, widget_value: const JsonValue2*):
    let widget_object = json_value2_as_object_const(widget_value)
    _ = json_object_get_string(widget_object, sv("class")) else trap("VERIFY_NOT_REACHED: no class")
    json_object_for_each_member(widget_object, load_form_member, context)

## The member lambda of load_form: does nothing.
func load_form_member(context: void*?, property_name: const String*, property_value: const JsonValue2*):
    return

# mark: TestJSON.cpp ============================================================================

test "load_form":
    var form_json = parse_json(raw_form_json)

    assert(json_value2_is_object(&form_json))

    let name = json_object_get_string(json_value2_as_object(&form_json), sv("name"))
    assert(name != none)

    assert(text_of_string(name else trap("VERIFY_NOT_REACHED")) == "Form1")

    let widgets = json_object_get_array(json_value2_as_object(&form_json), sv("widgets"))
    assert(widgets != none)

    json_array_for_each_const(widgets else trap("VERIFY_NOT_REACHED"), load_form_widget, none)

test "json_empty_string":
    let json = parse_json("\"\"")
    assert(json_value2_type(&json) == JsonValueType.string)
    let string = json_value2_as_string(&json)
    assert(string_is_empty(&string))

test "json_string":
    let json = parse_json("\"A\"")
    assert(json_value2_type(&json) == JsonValueType.string)
    assert(text_of_string(json_value2_as_string(&json)).length == 1)
    assert(text_of_string(json_value2_as_string(&json)) == "A")

test "json_utf8_character":
    let json = parse_json("\"\\u0041\"")
    assert(json_value2_type(&json) == JsonValueType.string)
    assert(text_of_string(json_value2_as_string(&json)).length == 1)
    assert(text_of_string(json_value2_as_string(&json)) == "A")

test "json_encoded_surrogates":
    let pair = parse_json("\"\\uD83E\\uDD13\"")
    assert(json_value2_type(&pair) == JsonValueType.string)
    assert(text_of_string(json_value2_as_string(&pair)).length == 4)
    assert(text_of_string(json_value2_as_string(&pair)) == "\u{1F913}")

    # A lone surrogate is kept as its (generalized) UTF-8 encoding.
    let high = parse_json("\"\\uD83E\"")
    assert(json_value2_type(&high) == JsonValueType.string)
    let high_string = json_value2_as_string(&high)
    let high_view = string_bytes_as_string_view(&high_string)
    let high_bytes = string_view_bytes(&high_view)
    assert(high_bytes.length == 3)
    assert(high_bytes[0] == 0xed and high_bytes[1] == 0xa0 and high_bytes[2] == 0xbe)

    let low = parse_json("\"\\uDD13\"")
    assert(json_value2_type(&low) == JsonValueType.string)
    let low_string = json_value2_as_string(&low)
    let low_view = string_bytes_as_string_view(&low_string)
    let low_bytes = string_view_bytes(&low_view)
    assert(low_bytes.length == 3)
    assert(low_bytes[0] == 0xed and low_bytes[1] == 0xb4 and low_bytes[2] == 0x93)

test "json_64_bit_value":
    let big_value: u64 = 0x12345678aabbccdd
    var big_json_value = json_value2_create()
    json_value2_construct_u64_2(&big_json_value, big_value)
    assert(json_value2_is_integer_u64(&big_json_value))
    assert(json_value2_as_integer_u64(&big_json_value) == big_value)

    let big_json_value_copy = json_value2_clone(&big_json_value)
    assert(json_value2_equals(&big_json_value, &big_json_value_copy))

test "json_64_bit_value_coerced_to_32_bit":
    let min_i64: i64 = -9223372036854775808
    let max_i64: i64 = 9223372036854775807

    var json = parse_json(formatted("{}", [arg_i64(min_i64)]))
    assert(json_value2_get_integer_i64(&json) == min_i64)
    assert(not json_value2_is_integer_i32(&json))

    json = parse_json(formatted("{}", [arg_i64(max_i64)]))
    assert(json_value2_get_integer_i64(&json) == max_i64)
    assert(not json_value2_is_integer_i32(&json))

    let min_u64: u64 = 0
    let max_u64: u64 = ~(u64)0

    json = parse_json(formatted("{}", [arg_u64(min_u64)]))
    assert(json_value2_get_integer_u64(&json) == min_u64)
    assert(json_value2_get_integer_u32(&json) == (u32)min_u64)

    json = parse_json(formatted("{}", [arg_u64(max_u64)]))
    assert(json_value2_get_integer_u64(&json) == max_u64)
    assert(not json_value2_is_integer_u32(&json))

test "json_duplicate_keys":
    var json: JsonObject
    json_object_construct(&json)
    json_object_set_string_view(&json, sv("test"), json_string_value("foo"))
    json_object_set_string_view(&json, sv("test"), json_string_value("bar"))
    json_object_set_string_view(&json, sv("test"), json_string_value("baz"))
    assert(text_of_string(json_object_serialized(&json)) == "{\"test\":\"baz\"}")

test "json_u64_roundtrip":
    let big_value: u64 = 0xffffffffffffffff
    var big = json_value2_create()
    json_value2_construct_u64_2(&big, big_value)
    let json = json_value2_serialized(&big)
    let value = json_value2_from_string(string_bytes_as_string_view(&json)) else trap("unexpected error")
    assert(json_value2_as_integer_u64(&value) == big_value)

test "json_parse_empty_string":
    assert(parse_json_fails(""))

test "json_parse_long_decimals":
    let value = parse_json("1644452550.6489999294281")
    assert(json_value2_get_double_with_precision_loss(&value) == 1644452550.6489999294281)

test "json_parse_number_with_exponent":
    let value_without_fraction = parse_json("10e5")
    assert(json_value2_get_double_with_precision_loss(&value_without_fraction) == 1000000.0)

    let value_with_fraction = parse_json("10.5e5")
    assert(json_value2_get_double_with_precision_loss(&value_with_fraction) == 1050000.0)

## EXPECT_TO_MATCH_NUMBER_BIT_WISE: `text` parses to a number with the bits of `expected`.
func expect_to_match_number_bit_wise(text: str, expected: f64):
    let value = parse_json(text)
    assert(json_value2_is_number(&value))
    let value_as_double = json_value2_get_double_with_precision_loss(&value) else trap("VERIFY_NOT_REACHED")
    assert(value_as_double.bits() == expected.bits())

test "json_parse_special_numbers":
    expect_to_match_number_bit_wise("-0", -0.0)
    expect_to_match_number_bit_wise("-0.0", -0.0)
    expect_to_match_number_bit_wise("-0.00", -0.0)
    expect_to_match_number_bit_wise("-0e0", -0.0)
    expect_to_match_number_bit_wise("-0e1", -0.0)
    expect_to_match_number_bit_wise("-0e2", -0.0)
    expect_to_match_number_bit_wise("-0e1000", -0.0)
    expect_to_match_number_bit_wise("-0e-1000", -0.0)

    expect_to_match_number_bit_wise("0", 0.0)
    expect_to_match_number_bit_wise("0.0", 0.0)
    expect_to_match_number_bit_wise("0.00", 0.0)
    expect_to_match_number_bit_wise("0e0", 0.0)
    expect_to_match_number_bit_wise("0e1", 0.0)
    expect_to_match_number_bit_wise("0e2", 0.0)
    expect_to_match_number_bit_wise("0e1000", 0.0)
    expect_to_match_number_bit_wise("0e-1000", 0.0)

    # These technically can be non zero, but not in doubles
    expect_to_match_number_bit_wise("-1e-2000", -0.0)
    expect_to_match_number_bit_wise("1e-2000", 0.0)

let invalid_numbers: str[73] = [
    "-", "00", "01", "-01", ".1", "-.1", "-,1", ".1e1", ".1e-1", "-.1e1", "-.1e-1", "1.e1",
    "1.e-1", "-1.e1", "-1.e-1", "1e", "1e+", "1e-", "1e-f", "1.e", "1.e+", "1.e-", "1.e-f",
    "1p2", "1.p2", "0x1.0p2", "0x1", "0x7", "0xA", "0x", "-0x", "0x", "1x", "100x",
    "1000000000000000000000x", "0e2x", "0.1e2x", "0.1x", "1e2x", "1.2x", "1.2e2x", ".0", ".e1",
    "-.0", "-.e1", "+0", "+0.0", "+0.00", "+0e0", "+0e1", "+0e2", "+0e1000", "+0e-1000", "+10",
    "+10e1", "+10.3", "+10.3e1", "0x1", "0x2", "0xA", "0xB", "0xF", "0Xf", "0X3", "10ee1",
    "1e1e1", "0,0", ",1", "10e1,", "10e,1", "10,e1", "1,0e1", ",10e1",
]

test "json_parse_fails_on_invalid_number":
    for text in invalid_numbers:
        assert(parse_json_fails(text))

test "json_parse_rejects_excessive_nesting_depth":
    var input: StringBuilder
    string_builder_construct(&input)
    let nesting_depth: usize = 4096

    string_builder_append_repeated_u8(&input, '[', nesting_depth)
    string_builder_append_u8(&input, '0')
    string_builder_append_repeated_u8(&input, ']', nesting_depth)

    assert(parse_json_fails(text_of(string_builder_string_view(&input))))

test "json_parse_accepts_reasonable_nesting_depth":
    var input: StringBuilder
    string_builder_construct(&input)
    let nesting_depth: usize = 64

    string_builder_append_repeated_u8(&input, '[', nesting_depth)
    string_builder_append_u8(&input, '0')
    string_builder_append_repeated_u8(&input, ']', nesting_depth)

    assert(not parse_json_fails(text_of(string_builder_string_view(&input))))

let raw_member_json = """
    {
        "name": "anon",
        "home": "/home/anon",
        "default_browser": "Ladybird"
    }"""

let raw_element_json = """
    [
        "anon",
        "/home/anon",
        "Ladybird"
    ]"""

## A try_for_each_member callback that succeeds.
func succeeding_member_callback(context: void*?, key: const String*, value: const JsonValue2*) -> !:
    return

## A try_for_each_member callback that fails.
func failing_member_callback(context: void*?, key: const String*, value: const JsonValue2*) -> !:
    error(ak_error, "nanananana")

## A try_for_each callback that succeeds.
func succeeding_element_callback(context: void*?, value: const JsonValue2*) -> !:
    return

## A try_for_each callback that fails.
func failing_element_callback(context: void*?, value: const JsonValue2*) -> !:
    error(ak_error, "nanananana")

test "fallible_json_object_for_each":
    let json = parse_json(raw_member_json)
    let object = json_value2_as_object_const(&json)

    json_object_try_for_each_member(object, succeeding_member_callback, none) else trap("MUST")

    var message = ""
    json_object_try_for_each_member(object, failing_member_callback, none) catch failure:
        message = failure.message
        recover ()
    assert(message == "nanananana")

test "fallible_json_array_for_each":
    let json = parse_json(raw_element_json)
    let array = json_value2_as_array_const(&json)

    json_array_try_for_each_const(array, succeeding_element_callback, none) else trap("MUST")

    var message = ""
    json_array_try_for_each_const(array, failing_element_callback, none) catch failure:
        message = failure.message
        recover ()
    assert(message == "nanananana")