dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / parser / tests_css_value_parsing.lucb
#==============================================================================================
#
# tests_css_value_parsing - tests of the value and gradient parsers (region r32)
#
# DESCRIPTION:
# Ladybird has no unit tests for ValueParsing.cpp or GradientParsing.cpp. The oracle cases
# (tests_css_value_parsing_cases) run the value parsers on 427 inputs and compare what
# they print with what the local oracle prints from the reference build: the parsed
# value's serialization (or "none"), the tokens left in the stream and the
# ErrorReporter's errors. They cover <integer>, <number>, <percentage>, every dimension
# (with SVG presentation attributes' unitless numbers), anchor() and anchor-size() (with
# and without a property context), keywords, CSS-wide keywords, <custom-ident>,
# <dashed-ident>, <string>, <opentype-tag>, <family-name>, <url> and its request
# modifiers, every color syntax (named, hex, rgb()/hsl()/hwb() in both syntaxes, lab(),
# lch(), oklab(), oklch(), color(), color-mix(), light-dark()),
# <color-interpolation-method>, <image> (url(), image-set(), linear, conic and radial
# gradients and their repeating and -webkit- forms), <position> and the
# background-position form, <paint>, rect(), <ratio>, scroll(), view(), the view-timeline
# inset, fit-content(), <radial-size>, border radii, <urange> (both overloads),
# <declaration-value>, <counter-style-name>, <line-names>, the three grid track lists, the
# font-variant tuples and alternates, corner-shape keywords, <random-value-sharing> and
# comma-separated value lists.
# The focused tests below cover what the dumps cannot reach.
#
# The cases that waited for other regions (math functions, easing, transforms, basic
# shapes, font-style and counters for r35; superellipse() and symbols() for r33 and r35)
# are in the table since those regions merged. Quirks-mode parsing needs a document in
# quirks mode.
#
#==============================================================================================
# mark: The oracle dumps ========================================================================
## The index of the first `ch` in `text`.
func test_css_value_parsing_index_of(text: str, ch: u8) -> usize?:
for (index, byte) in text.bytes.indexed():
if byte == ch:
return index
return none
## The oracle's `value_type_named`: value_type_from_string, which knows neither `anchor`, `corner-shape` nor
## `font-style`.
func test_css_value_type_named(name: str) -> CssValueType:
if name == "anchor":
return CssValueType.anchor
if name == "corner-shape":
return CssValueType.corner_shape
if name == "font-style":
return CssValueType.font_style
return css_value_type_from_string(ak.sv(name)) else trap("unknown value type")
## The oracle's `value`: "value: <serialization>" or "none".
func test_css_value_line(out: ak.StringBuilder*, value: CssStyleValue*?):
if let parsed = value:
let serialized = css_style_value_to_string(parsed, css_syntax.SerializationMode.normal)
test_css_line(out, 1, css_string_formatted("value: {}", [css_arg_string(&serialized)]))
else:
test_css_line(out, 1, ak.string_from_str("none"))
## The oracle's `fly`: "'<string>'" or "none".
func test_css_fly_line(out: ak.StringBuilder*, value: ak.FlyString?):
if let string = value:
test_css_line(out, 1, css_string_formatted("'{}'", [css_arg_fly_string(&string)]))
else:
test_css_line(out, 1, ak.string_from_str("none"))
## A unicode range as "<min>-<max>".
func test_css_unicode_range_text(range: const web_fonts.UnicodeRange*) -> ak.String:
return css_string_formatted("{}-{}", [ak.arg_u64((u64)web_fonts.unicode_range_min_code_point(range)), ak.arg_u64((u64)web_fonts.unicode_range_max_code_point(range))])
## Upcasts of the parsers' typed results.
func test_css_as_style_value[T: IsCssStyleValue](value: T*?) -> CssStyleValue*?:
if let typed = value:
return typed.css_style_value()
return none
## The oracle's `[&](auto& it) { return parser.parse_number_value(it, infinite_range); }`.
func test_css_parse_one_number(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
return css_parser_parse_number_value((CssParser*)(context else trap("no parser")), tokens, css_infinite_range)
## Runs one oracle case (see the oracle's `run`) and returns its dump.
func test_css_value_parsing_run_case(full_mode: str, input: str) -> ak.String:
var out = ak.string_builder_new()
ak.hash_map_clear(&css_error_reporter_the().m_errors)
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms)
var mode = full_mode
var property: css_data.PropertyId? = none
while let plus = test_css_value_parsing_index_of(mode, '+'):
let option = (str)mode.bytes[0..<plus]
mode = (str)mode.bytes[plus + 1..]
if option == "svg":
css_parsing_params_construct_css_parsing_mode(¶ms, CssParsingMode.svg_presentation_attribute)
elif option.bytes.length > 5 and (str)option.bytes[0..<5] == "prop=":
property = css_data.property_id_from_string(ak.sv((str)option.bytes[5..]))
var argument = ""
if let colon = test_css_value_parsing_index_of(mode, ':'):
argument = (str)mode.bytes[colon + 1..]
mode = (str)mode.bytes[0..<colon]
var parser = css_parser_create(params, ak.sv(input))
let values = css_parser_parse_as_list_of_component_values(&parser)
var tokens = css_syntax.token_stream_create(values)
if let property_id = property:
ak.vector_append_t(&parser.m_value_context, CssValueParsingContext.property_id(property_id))
if mode == "type":
test_css_value_line(&out, css_parser_parse_value(&parser, test_css_value_type_named(argument), &tokens))
elif mode == "keyword":
test_css_value_line(&out, css_parser_parse_keyword_value(&parser, &tokens))
elif mode == "builtin":
test_css_value_line(&out, css_parser_parse_builtin_value(&parser, &tokens))
elif mode == "family":
test_css_value_line(&out, css_parser_parse_family_name_value(&parser, &tokens))
elif mode == "cim":
test_css_value_line(&out, test_css_as_style_value(css_parser_parse_color_interpolation_method_value(&parser, &tokens)))
elif mode == "radialsize":
test_css_value_line(&out, test_css_as_style_value(css_parser_parse_radial_size(&parser, &tokens)))
elif mode == "borderradius":
test_css_value_line(&out, test_css_as_style_value(css_parser_parse_border_radius_rect_value(&parser, &tokens)))
elif mode == "random":
test_css_value_line(&out, test_css_as_style_value(css_parser_parse_random_value_sharing(&parser, &tokens)))
elif mode == "numberlist":
let parse_number = ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssStyleValue*?](call = test_css_parse_one_number, context = &parser)
test_css_value_line(&out, test_css_as_style_value(css_parser_parse_comma_separated_value_list(&parser, &tokens, parse_number)))
elif mode == "urangevalue":
test_css_value_line(&out, test_css_as_style_value(css_parser_parse_unicode_range_value(&parser, &tokens)))
elif mode == "urange" or mode == "urangetext":
let range = css_parser_parse_unicode_range_token_stream(&parser, &tokens) if mode == "urange" else css_parser_parse_unicode_range_string_view(&parser, ak.sv(input))
if let parsed = range:
test_css_line(&out, 1, test_css_unicode_range_text(&parsed))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "uranges":
let ranges = css_parser_parse_unicode_ranges(&parser, &tokens)
var builder = ak.string_builder_new()
for range in ak.vector_span_const(&ranges):
let text = test_css_unicode_range_text(&range)
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&text))
ak.string_builder_append(&builder, " ")
test_css_line(&out, 1, css_string_formatted("{} ranges: {}", [ak.arg_u64((u64)ak.vector_size(&ranges)), ak.arg_string_view(ak.string_builder_string_view(&builder))]))
elif mode == "declvalue":
let end_token_type: css_syntax.TokenType? = none if argument == "" else css_syntax.TokenType.comma
if let result = css_parser_parse_declaration_value(&tokens, end_token_type):
var builder = ak.string_builder_new()
for component_value in ak.vector_span_const(&result):
let text = css_syntax.component_value_to_string(&component_value)
css_appendff(&builder, "[{}]", [css_arg_string(&text)])
test_css_line(&out, 1, css_string_formatted("{}: {}", [ak.arg_u64((u64)ak.vector_size(&result)), ak.arg_string_view(ak.string_builder_string_view(&builder))]))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "url":
if let url = css_parser_parse_url_function(&parser, &tokens):
let text = css_url_to_string(&url)
let raw = css_url_url(&url)
test_css_line(&out, 1, css_string_formatted("url: {} / {}", [css_arg_string(&text), css_arg_string(&raw)]))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "customident":
let blacklist: ak.StringView[1] = [ak.sv("none")]
test_css_fly_line(&out, css_parser_parse_custom_ident(&parser, &tokens, blacklist))
elif mode == "dashedident":
test_css_fly_line(&out, css_parser_parse_dashed_ident(&parser, &tokens))
elif mode == "counterstylename":
test_css_fly_line(&out, css_parser_parse_counter_style_name(&parser, &tokens))
elif mode == "linenames":
if let names = css_parser_parse_grid_line_names(&parser, &tokens):
let text = css_grid_line_names_to_string(&names)
test_css_line(&out, 1, css_string_formatted("names: '{}'", [css_arg_string(&text)]))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "tracklist" or mode == "autotracklist" or mode == "explicittracklist":
var list = CssGridTrackSizeList()
if mode == "tracklist":
list = css_parser_parse_grid_track_list(&parser, &tokens)
elif mode == "autotracklist":
list = css_parser_parse_grid_auto_track_list(&parser, &tokens)
else:
list = css_parser_parse_explicit_track_list(&parser, &tokens)
let text = css_grid_track_size_list_to_string(&list, css_syntax.SerializationMode.normal)
test_css_line(&out, 1, css_string_formatted("list: '{}'", [css_arg_string(&text)]))
else:
test_css_line(&out, 1, ak.string_from_str("unknown mode"))
test_css_dump_rest(&out, &tokens)
test_css_dump_errors(&out)
return ak.string_builder_to_string_without_validation(&out)
test "value and gradient parsing match the reference build on the oracle cases":
with memory.heap:
var failures = 0
for case_ in test_css_value_parsing_cases:
let actual = test_css_value_parsing_run_case(case_.mode, case_.input)
let actual_view = ak.string_bytes_as_string_view(&actual)
if not ak.string_view_equals(&actual_view, case_.expected):
failures += 1
print(f"case {case_.mode} {case_.input}\nexpected:\n{case_.expected}actual:\n{ak.string_view_text(actual_view)}")
assert(failures == 0)