Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / style_values / tests_style_values_1_2.lucb

#==============================================================================================
#
#   tests_style_values_1_2 - region r34's oracle dump, part 2, and the test that compares it
#
#   DESCRIPTION:
#       The `# helpers` and `# geometry` sections of the dump (the free helpers, the statics,
#       KeywordStyleValue::is_color over every keyword, HTML::is_supported_image_type, lists,
#       shorthand longhands, edges, gradient angles and radial sizes), and the test that builds
#       the whole dump and compares it with the oracle's output line by line.
#
#==============================================================================================

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

## The `# helpers` section.
func test_dump_style_value_helpers(out: ak.StringBuilder*):
    let px = css_data.LengthUnit.px
    let em = css_data.LengthUnit.em
    tw(out, "# helpers\nint ")
    tn(out, (i64)css_int_from_style_value(css_integer_style_value_create(-12).css_style_value()))
    tw(out, "\nnumber ")
    td(out, css_number_from_style_value(tsv_num(2.5), none))
    tw(out, " ")
    td(out, css_number_from_style_value(tsv_pct(25.0), 200.0))
    tw(out, "\nstring ")
    tfly(out, css_string_from_style_value(css_string_style_value_create(ak.fly_string_from_str("s")).css_style_value()))
    tw(out, " ")
    tfly(out, css_string_from_style_value(css_custom_ident_style_value_create(ak.fly_string_from_str("c")).css_style_value()))
    tw(out, "\nstatics ")
    tb(out, tsv_len(0.0, px) == tsv_len(0.0, px))
    tb(out, tsv_len(1.0, px) == tsv_len(1.0, px))
    tb(out, tsv_len(2.0, px) == tsv_len(2.0, px))
    tb(out, tsv_len(0.0, em) == tsv_len(0.0, em))
    tb(out, tsv_kw(css_data.Keyword.inherit) == tsv_kw(css_data.Keyword.inherit))
    tb(out, tsv_kw(css_data.Keyword.revert_layer) == tsv_kw(css_data.Keyword.revert_layer))
    tb(out, tsv_kw(css_data.Keyword.auto) == tsv_kw(css_data.Keyword.auto))
    tb(out, css_guaranteed_invalid_style_value_create() == css_guaranteed_invalid_style_value_create())
    tw(out, "\nis_color ")
    for keyword: u16 in 0..=849:
        tb(out, css_keyword_style_value_is_color((css_data.Keyword)keyword))
    tw(out, "\nsupported image types ")
    tb(out, html_is_supported_image_type(ak.sv("")))
    tb(out, html_is_supported_image_type(ak.sv("image/PNG")))
    tb(out, html_is_supported_image_type(ak.sv("image/svg+xml")))
    tb(out, html_is_supported_image_type(ak.sv("IMAGE/webp")))
    tb(out, html_is_supported_image_type(ak.sv("text/html")))
    tb(out, html_is_supported_image_type(ak.sv("image/x")))
    let list = tsv_list([tsv_num(1.0), tsv_num(2.0), tsv_num(3.0)], CssStyleValueListSeparator.comma)
    tw(out, "\nvalue_at ")
    ts(out, css_style_value_to_string(css_style_value_list_value_at(list, 4, true), css_syntax.SerializationMode.normal))
    tw(out, " ")
    ts(out, css_style_value_to_string(css_style_value_list_value_at(list, 2, false), css_syntax.SerializationMode.normal))
    tw(out, " ")
    tn(out, (i64)css_style_value_list_size(list))
    tw(out, "\nsubdivide ")
    let background_image = css_property_name_and_id_from_id(css_data.PropertyId.background_image)
    let color = css_property_name_and_id_from_id(css_data.PropertyId.color)
    let iterations = css_style_value_subdivide_into_iterations(list.css_style_value(), background_image)
    tn(out, (i64)ak.vector_size(&iterations))
    tw(out, " ")
    let color_iterations = css_style_value_subdivide_into_iterations(list.css_style_value(), color)
    tn(out, (i64)ak.vector_size(&color_iterations))
    tw(out, " ")
    let number_iterations = css_style_value_subdivide_into_iterations(tsv_num(1.0), background_image)
    tn(out, (i64)ak.vector_size(&number_iterations))
    let shorthand = css_style_value_as_shorthand(test_shorthand(css_data.PropertyId.margin, [css_data.PropertyId.margin_top, css_data.PropertyId.margin_right], [tsv_len(1.0, px), tsv_len(2.0, px)]))
    tw(out, "\nlonghand ")
    ts(out, css_style_value_to_string(css_shorthand_style_value_longhand(shorthand, css_data.PropertyId.margin_right) else trap("a margin-right longhand"), css_syntax.SerializationMode.normal))
    tw(out, " ")
    tb(out, css_shorthand_style_value_longhand(shorthand, css_data.PropertyId.margin_left) == none)
    tw(out, "\nradius ")
    tb(out, css_border_radius_style_value_is_elliptical(css_border_radius_style_value_create(tsv_len(1.0, px), tsv_len(1.0, px))))
    tb(out, css_border_radius_style_value_is_elliptical(css_border_radius_style_value_create(tsv_len(1.0, px), tsv_len(2.0, px))))
    tw(out, "\nedges ")
    tb(out, css_edge_style_value_is_center(tsv_edge(css_data.PositionEdge.center, none), css_syntax.SerializationMode.normal))
    tb(out, css_edge_style_value_is_center(tsv_edge(none, tsv_pct(50.0)), css_syntax.SerializationMode.normal))
    tb(out, css_edge_style_value_is_center(tsv_edge(css_data.PositionEdge.left, tsv_pct(50.0)), css_syntax.SerializationMode.normal))
    tb(out, css_edge_style_value_is_center(tsv_edge(css_data.PositionEdge.left, none), css_syntax.SerializationMode.normal))
    tl(out)
    let edges: CssEdgeStyleValue*[6] = [tsv_edge(css_data.PositionEdge.center, none), tsv_edge(css_data.PositionEdge.left, none), tsv_edge(css_data.PositionEdge.right, none), tsv_edge(css_data.PositionEdge.top, tsv_len(1.0, px)), tsv_edge(css_data.PositionEdge.bottom, none), tsv_edge(none, tsv_pct(7.0))]
    for e in edges:
        tw(out, "resolved edge ")
        ts(out, css_style_value_to_string(css_edge_style_value_with_resolved_keywords(e).css_style_value(), css_syntax.SerializationMode.normal))
        tl(out)

# mark: Geometry ================================================================================

## The `# geometry` section.
func test_dump_style_value_geometry(out: ak.StringBuilder*):
    tw(out, "# geometry\n")
    let sides: f64[8] = [100.0, 50.0, 50.0, 100.0, 0.0, 40.0, 33.5, 77.25]
    let side_or_corners: CssSideOrCorner[8] = [CssSideOrCorner.top, CssSideOrCorner.bottom, CssSideOrCorner.left, CssSideOrCorner.right, CssSideOrCorner.top_left, CssSideOrCorner.top_right, CssSideOrCorner.bottom_left, CssSideOrCorner.bottom_right]
    for i in 0..<4:
        let gradient_size = gfx.CssPixelSize(m_width = gfx.css_pixels_from_f64(sides[i * 2]), m_height = gfx.css_pixels_from_f64(sides[i * 2 + 1]))
        tw(out, "angles ")
        tpx(out, gradient_size.m_width)
        tw(out, "x")
        tpx(out, gradient_size.m_height)
        for side in side_or_corners:
            tw(out, " ")
            tf(out, css_linear_gradient_style_value_angle_degrees(test_linear_one(.css_side_or_corner(side), CssLinearGradientStyleValueGradientType.standard), gradient_size))
            tw(out, "/")
            tf(out, css_linear_gradient_style_value_angle_degrees(test_linear_one(.css_side_or_corner(side), CssLinearGradientStyleValueGradientType.web_kit), gradient_size))
        tw(out, " ")
        tf(out, css_linear_gradient_style_value_angle_degrees(test_linear_one(.css_style_value(css_angle_style_value_create(css_angle_new(1.0, css_data.AngleUnit.rad)).css_style_value()), CssLinearGradientStyleValueGradientType.standard), gradient_size))
        tl(out)
    let one_stop = tsv_stops([tsv_stop(tsv_rgb(1.0, 1.0, 1.0))])
    tw(out, "conic ")
    tf(out, css_conic_gradient_style_value_angle_degrees(css_conic_gradient_style_value_create(none, css_position_style_value_create_center(), one_stop, CssGradientRepeating.no, none)))
    tw(out, " ")
    tf(out, css_conic_gradient_style_value_angle_degrees(css_conic_gradient_style_value_create(css_angle_style_value_create(css_angle_new(0.5, css_data.AngleUnit.turn)).css_style_value(), css_position_style_value_create_center(), one_stop, CssGradientRepeating.no, none)))
    tl(out)

    # Radial sizes for extents only (the length-percentage branch needs a layout node).
    var boxes: gfx.CssPixelRect[4] = ---
    gfx.css_pixel_rect_construct_int(&boxes[0], 0, 0, 200, 100)
    gfx.css_pixel_rect_construct_int(&boxes[1], 10, 20, 30, 300)
    gfx.css_pixel_rect_construct_int(&boxes[2], 0, 0, 1, 50)
    gfx.css_pixel_rect_construct_int(&boxes[3], 5, 5, 64, 0)
    let centers: gfx.CssPixelPoint[4] = [
        gfx.CssPixelPoint(m_x = gfx.css_pixels_from_int(100), m_y = gfx.css_pixels_from_int(50)),
        gfx.CssPixelPoint(m_x = gfx.css_pixels_from_int(0), m_y = gfx.css_pixels_from_int(0)),
        gfx.CssPixelPoint(m_x = gfx.css_pixels_from_int(15), m_y = gfx.css_pixels_from_int(200)),
        gfx.CssPixelPoint(m_x = gfx.css_pixels_from_f64(33.25), m_y = gfx.css_pixels_from_f64(-7.5)),
    ]
    # NB: as the oracle's, the layout node is never read for extents.
    let node = (LayoutNode*)&boxes[0]
    let extents: css_data.RadialExtent[4] = [css_data.RadialExtent.closest_side, css_data.RadialExtent.farthest_side, css_data.RadialExtent.closest_corner, css_data.RadialExtent.farthest_corner]
    for extent in extents:
        let radial_size = tsv_extent(extent)
        tw(out, "radial ")
        tv(out, css_data.to_string_radial_extent(extent))
        for box in boxes:
            for center in centers:
                let ellipse = css_radial_size_style_value_resolve_ellipse_size(radial_size, center, box, node)
                tw(out, " ")
                tpx(out, css_radial_size_style_value_resolve_circle_size(radial_size, center, box, node))
                tw(out, ":")
                tpx(out, ellipse.m_width)
                tw(out, ",")
                tpx(out, ellipse.m_height)
        tl(out)

# mark: The test ================================================================================

test "the style values print what the reference build prints":
    with memory.heap:
        let values = test_build_style_values()
        var out = ak.string_builder_new()
        test_dump_style_values(&out, &values)
        test_dump_equals(&out, &values)
        test_dump_tokenize(&out, &values)
        test_dump_style_value_helpers(&out)
        test_dump_style_value_geometry(&out)

        let actual_view = ak.string_builder_string_view(&out)
        let actual = (str)ak.string_view_bytes(&actual_view)
        var actual_lines = test_split_lines(actual)
        var expected_lines = test_split_lines(test_style_values_1_expected)
        var line: usize = 1
        var mismatches: usize = 0
        while true:
            let actual_line = actual_lines.next()
            let expected_line = expected_lines.next()
            if actual_line == none and expected_line == none:
                break
            let actual_text = actual_line else "(end)"
            let expected_text = expected_line else "(end)"
            if actual_text != expected_text:
                if mismatches < 30:
                    print(f"line {line}:\n  expected: {expected_text}\n  actual:   {actual_text}")
                mismatches += 1
            line += 1
        assert(mismatches == 0, "the dump differs from the oracle's")