Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / style_values / tests_style_values_2_focused.lucb

#==============================================================================================
#
#   tests_style_values_2_focused - region r35's members the oracle dumps do not reach
#
#   DESCRIPTION:
#       Focused tests, each pinned to the donor's code it names: the FilterOperation accessors,
#       CounterStyleSystemStyleValue::algorithm_differs_from, the CalculationResult arithmetic
#       (including divide_by's use of the divisor's uninverted type), GridTemplateAreaStyleValue's
#       cell names, TransformationStyleValue::identity_transformation and the counter value
#       accessors. Paths into layout (BasicShapeStyleValue::to_path, CounterStyleValue::resolve)
#       stop in other regions' stubs and are not covered here.
#
#==============================================================================================

## `property: input` parsed with parse_css_value, or a trap.
func test_sv2_parse_property(property: css_data.PropertyId, input: str) -> CssStyleValue*:
    var params: CssParsingParams = ---
    css_parsing_params_construct_css_parsing_mode(&params)
    var (parser, values) = test_sv2_parser(params, input)
    var tokens = css_syntax.token_stream_create(values)
    return css_parser_parse_css_value(&parser, property, &tokens) else trap("does not parse")

## A style value's normal serialization as a str for comparisons.
func test_sv2_text(value: CssStyleValue*) -> ak.String:
    return css_style_value_to_string(value, css_syntax.SerializationMode.normal)

test "FilterOperation accessors resolve radius, angle and amount (FilterValueListStyleValue.cpp:13-26)":
    with memory.heap:
        let list = css_style_value_as_filter_value_list(test_sv2_parse_property(css_data.PropertyId.filter, "blur(2px) hue-rotate(0.5turn) brightness(50%) contrast(3)"))
        assert(css_filter_value_list_style_value_size(list) == 4)
        assert(not css_filter_value_list_style_value_contains_url(list))
        let filters = css_filter_value_list_style_value_filter_value_list(list)
        match *ak.vector_at_const(&filters, 0):
            .css_blur(blur): assert(css_blur_resolved_radius(&blur) == 2.0)
            _: trap("not a blur")
        match *ak.vector_at_const(&filters, 1):
            .css_hue_rotate(hue_rotate): assert(css_hue_rotate_angle_degrees(&hue_rotate) == 180.0)
            _: trap("not a hue-rotate")
        match *ak.vector_at_const(&filters, 2):
            .css_color(color):
                assert(color.operation == gfx.ColorFilterType.brightness)
                assert(css_color_resolved_amount(&color) == 0.5)
            _: trap("not a color filter")
        match *ak.vector_at_const(&filters, 3):
            .css_color(color): assert(css_color_resolved_amount(&color) == 3.0)
            _: trap("not a color filter")
        let with_url = css_style_value_as_filter_value_list(test_sv2_parse_property(css_data.PropertyId.filter, "url(#a) blur(1px)"))
        assert(css_filter_value_list_style_value_contains_url(with_url))

test "CounterStyleSystemStyleValue::algorithm_differs_from compares the alternative, the system and the extended name":
    with memory.heap:
        let cyclic = css_style_value_as_counter_style_system(css_counter_style_system_style_value_create(css_data.CounterStyleSystem.cyclic))
        let numeric = css_style_value_as_counter_style_system(css_counter_style_system_style_value_create(css_data.CounterStyleSystem.numeric))
        let fixed = css_style_value_as_counter_style_system(css_counter_style_system_style_value_create_fixed(none))
        let fixed_3 = css_style_value_as_counter_style_system(css_counter_style_system_style_value_create_fixed(css_integer_style_value_create(3).css_style_value()))
        let extends_a = css_style_value_as_counter_style_system(css_counter_style_system_style_value_create_extends(ak.fly_string_from_str("a")))
        let extends_b = css_style_value_as_counter_style_system(css_counter_style_system_style_value_create_extends(ak.fly_string_from_str("b")))
        assert(not css_counter_style_system_style_value_algorithm_differs_from(cyclic, cyclic))
        assert(css_counter_style_system_style_value_algorithm_differs_from(cyclic, numeric))
        assert(css_counter_style_system_style_value_algorithm_differs_from(cyclic, fixed))
        assert(not css_counter_style_system_style_value_algorithm_differs_from(fixed, fixed_3))
        assert(css_counter_style_system_style_value_algorithm_differs_from(extends_a, extends_b))
        assert(not css_counter_style_system_style_value_algorithm_differs_from(extends_a, extends_a))
        assert(css_counter_style_system_style_value_algorithm_differs_from(extends_a, fixed))
        # Fixed with and without a first symbol are different values but the same algorithm.
        assert(not css_style_value_equals(fixed.css_style_value(), fixed_3.css_style_value()))

test "CalculationResult arithmetic: add, subtract, multiply_by, divide_by, negate, invert (CalculatedStyleValue.cpp:2933-2969)":
    let length = css_numeric_type_new(.length, 1)
    var a = css_calculated_style_value_calculation_result_new(2.0, length)
    let b = css_calculated_style_value_calculation_result_new(3.0, length)
    css_calculated_style_value_calculation_result_add(&a, &b)
    assert(a.m_value == 5.0 and a.m_type == length)
    css_calculated_style_value_calculation_result_subtract(&a, &b)
    assert(a.m_value == 2.0 and a.m_type == length)
    var product = css_calculated_style_value_calculation_result_new(2.0, length)
    css_calculated_style_value_calculation_result_multiply_by(&product, &b)
    assert(product.m_value == 6.0 and product.m_type == css_numeric_type_new(.length, 2))
    # divide_by multiplies by the inverted value but by the divisor's own (uninverted) type.
    var quotient = css_calculated_style_value_calculation_result_new(6.0, length)
    css_calculated_style_value_calculation_result_divide_by(&quotient, &b)
    assert(quotient.m_value == 2.0 and quotient.m_type == css_numeric_type_new(.length, 2))
    var untyped = css_calculated_style_value_calculation_result_new(1.0, none)
    css_calculated_style_value_calculation_result_add(&untyped, &b)
    assert(untyped.m_value == 4.0 and untyped.m_type == none)
    css_calculated_style_value_calculation_result_negate(&a)
    assert(a.m_value == -2.0)
    css_calculated_style_value_calculation_result_invert(&a)
    assert(a.m_value == -0.5 and a.m_type == css_numeric_type_new(.length, -1))

test "GridTemplateAreaStyleValue::cell_name_at answers the area's name or \".\" (GridTemplateAreaStyleValue.cpp:20)":
    with memory.heap:
        let areas = css_style_value_as_grid_template_area(test_sv2_parse_property(css_data.PropertyId.grid_template_areas, "\"a b\" \"c .\""))
        assert(css_grid_template_area_style_value_row_count(areas) == 2)
        assert(css_grid_template_area_style_value_column_count(areas) == 2)
        let names: str[5] = ["a", "b", "c", ".", "."]
        let cells: (usize, usize)[5] = [(0, 0), (0, 1), (1, 0), (1, 1), (5, 5)]
        for i in 0..<5:
            let name = css_grid_template_area_style_value_cell_name_at(areas, cells[i].0, cells[i].1)
            assert(ak.string_eq_string_view(&name, ak.sv(names[i])))

test "TransformationStyleValue::identity_transformation gives each function's identity arguments and an identity matrix":
    with memory.heap:
        let functions: css_data.TransformFunction[8] = [.matrix, .perspective, .rotate3_d, .translate3_d, .scale, .scale3_d, .skew, .rotate_x]
        let expected: str[8] = ["matrix(1, 0, 0, 1, 0, 0)", "perspective(none)", "rotate3d(1, 1, 1, 0deg)", "translate3d(0px, 0px, 0px)", "scale(1)", "scale3d(1, 1, 1)", "skew(0px)", "rotateX(0deg)"]
        for i in 0..<8:
            let identity = css_transformation_style_value_identity_transformation(functions[i])
            let text = test_sv2_text(identity.css_style_value())
            assert(ak.string_eq_string_view(&text, ak.sv(expected[i])))
            assert(css_transformation_style_value_transform_function(identity) == functions[i])
            if functions[i] != css_data.TransformFunction.skew:
                let matrix = css_transformation_style_value_to_matrix(identity, none) else trap("to_matrix failed")
                assert(gfx.float_matrix4_x4_is_identity(&matrix))

test "CounterStyleValue accessors and CounterDefinitionsStyleValue's definitions":
    with memory.heap:
        let counters = css_counter_style_value_create_counters(ak.fly_string_from_str("item"), ak.fly_string_from_str("."), css_keyword_style_value_create(css_data.Keyword.decimal).css_style_value())
        assert(css_counter_style_value_function_type(counters) == CssCounterStyleValueCounterFunction.counters)
        let name = css_counter_style_value_counter_name(counters)
        assert(ak.fly_string_eq_string_view(&name, ak.sv("item")))
        let join = css_counter_style_value_join_string(counters)
        assert(ak.fly_string_eq_string_view(&join, ak.sv(".")))
        let text = test_sv2_text(counters.css_style_value())
        assert(ak.string_eq_string_view(&text, ak.sv("counters(item, \".\")")))
        let parsed = css_style_value_as_counter_definitions(test_sv2_parse_property(css_data.PropertyId.counter_reset, "a 2 b"))
        let list = css_counter_definitions_style_value_counter_definitions(parsed)
        assert(ak.vector_size(&list) == 2)
        assert(not ak.vector_at_const(&list, 1).is_reversed)
        # The parser does not make reversed() counters here; build one to serialize it (CounterDefinitionsStyleValue.cpp:13).
        var definitions = ak.Vector[CssCounterDefinition]()
        ak.vector_append_t(&definitions, CssCounterDefinition(name = ak.fly_string_from_str("foo"), is_reversed = true, value = css_integer_style_value_create(3).css_style_value()))
        ak.vector_append_t(&definitions, CssCounterDefinition(name = ak.fly_string_from_str("bar"), is_reversed = false, value = none))
        let built = css_counter_definitions_style_value_create(definitions)
        let built_text = test_sv2_text(built.css_style_value())
        assert(ak.string_eq_string_view(&built_text, ak.sv("reversed(foo) 3 bar")))
        assert(css_style_value_is_computationally_independent(built.css_style_value()))