dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / parser / tests_css_property_parsing.lucb
#==============================================================================================
#
# tests_css_property_parsing - tests of property parsing, the arbitrary substitution
# functions and the generated property data
#
# DESCRIPTION:
# Ladybird has no unit tests for PropertyParsing.cpp, ArbitrarySubstitutionFunctions.cpp
# or the value-dependent part of the generated PropertyID.cpp. The oracle cases
# (tests_css_property_parsing_cases) run the reference build on the same inputs
# (luce-browser-tools, oracles/luce-browser-engine/css_property_parsing); the test below
# runs each through the port and compares the output: every property's accepted value
# types, numeric ranges, percentage basis and range checks, every environment variable's
# value type, every initial value, parse_css_value (longhands, shorthand expansion, the
# special-cased properties and their errors), parse_css_value_for_properties, the
# arbitrary substitution function names and their argument grammars. The focused tests
# after it cover substitution contexts and the substitution of values without an element.
#
#==============================================================================================
# mark: The oracle cases ========================================================================
## The parsing parameters of the cases: the realm only, as the oracle's internal CSS realm.
func test_css_property_parsing_params(realm: Realm*) -> CssParsingParams:
var params: CssParsingParams = ---
css_parsing_params_construct_realm_css_parsing_mode(¶ms, realm)
return params
## Append `text` to `out`, indented by `indent` levels of two spaces, and a line feed (the oracle's `line`).
func test_property_line(out: ak.StringBuilder*, indent: i32, text: ak.StringView):
for _ in 0..<indent:
ak.string_builder_append(out, " ")
ak.string_builder_append_string_view(out, text)
ak.string_builder_append_u8(out, '\n')
## The oracle's `d`: a double as AK formats it with `{}`.
func test_property_double(builder: ak.StringBuilder*, value: f64):
css_appendff(builder, "{}", [ak.arg_f64(value)])
## The type's snake-case name, as ENUMERATE_CSS_STYLE_VALUE_TYPES spells it (every type).
func test_property_type_name(type_: CssStyleValueType) -> str:
match type_:
.anchor: return "anchor"
.anchor_size: return "anchor_size"
.angle: return "angle"
.background_size: return "background_size"
.basic_shape: return "basic_shape"
.border_image_slice: return "border_image_slice"
.border_radius: return "border_radius"
.border_radius_rect: return "border_radius_rect"
.calculated: return "calculated"
.color_interpolation_method: return "color_interpolation_method"
.color_scheme: return "color_scheme"
.color: return "color"
.conic_gradient: return "conic_gradient"
.content: return "content"
.counter: return "counter"
.counter_style: return "counter_style"
.counter_definitions: return "counter_definitions"
.counter_style_system: return "counter_style_system"
.cursor: return "cursor"
.custom_ident: return "custom_ident"
.display: return "display"
.easing: return "easing"
.edge: return "edge"
.filter_value_list: return "filter_value_list"
.function: return "function"
.flex: return "flex"
.font_source: return "font_source"
.font_style: return "font_style"
.frequency: return "frequency"
.grid_auto_flow: return "grid_auto_flow"
.grid_template_area: return "grid_template_area"
.grid_track_placement: return "grid_track_placement"
.grid_track_size_list: return "grid_track_size_list"
.guaranteed_invalid: return "guaranteed_invalid"
.image: return "image"
.image_set: return "image_set"
.integer: return "integer"
.keyword: return "keyword"
.length: return "length"
.linear_gradient: return "linear_gradient"
.number: return "number"
.opacity_value: return "opacity_value"
.open_type_tagged: return "open_type_tagged"
.pending_substitution: return "pending_substitution"
.percentage: return "percentage"
.position: return "position"
.radial_gradient: return "radial_gradient"
.radial_size: return "radial_size"
.random_value_sharing: return "random_value_sharing"
.ratio: return "ratio"
.rect: return "rect"
.repeat_style: return "repeat_style"
.resolution: return "resolution"
.scrollbar_color: return "scrollbar_color"
.scrollbar_gutter: return "scrollbar_gutter"
.shadow: return "shadow"
.shorthand: return "shorthand"
.string: return "string"
.superellipse: return "superellipse"
.text_indent: return "text_indent"
.text_underline_position: return "text_underline_position"
.time: return "time"
.transformation: return "transformation"
.tree_counting_function: return "tree_counting_function"
.tuple: return "tuple"
.unicode_range: return "unicode_range"
.unresolved: return "unresolved"
.url: return "url"
.value_list: return "value_list"
_: return "?"
## The oracle's `print_value`: a value's type and serialization; a shorthand's longhands and a list's items, recursively.
func test_property_print_value(out: ak.StringBuilder*, indent: i32, value: CssStyleValue*):
if css_style_value_is_shorthand(value):
let shorthand = css_style_value_as_shorthand(value)
let name = css_data.string_from_property_id(shorthand.m_properties.shorthand_property)
var text = ak.string_builder_new()
css_appendff(&text, "shorthand {}", [css_arg_fly_string(&name)])
test_property_line(out, indent, ak.string_builder_string_view(&text))
let sub_properties = css_shorthand_style_value_sub_properties(shorthand)
let values = css_shorthand_style_value_values(shorthand)
for i in 0..<ak.vector_size(&sub_properties):
let sub_name = css_data.string_from_property_id(*ak.vector_at_const(&sub_properties, i))
test_property_line(out, indent + 1, ak.fly_string_bytes_as_string_view(&sub_name))
test_property_print_value(out, indent + 2, *ak.vector_at_const(&values, i))
return
if css_style_value_is_value_list(value):
let list = css_style_value_as_value_list(value)
let items = css_style_value_list_values(list)
var has_shorthand = false
for item in ak.vector_span_const(&items):
if css_style_value_is_shorthand(item):
has_shorthand = true
var header = ak.string_builder_new()
ak.string_builder_append(&header, "value_list comma " if css_style_value_list_separator(list) == CssStyleValueListSeparator.comma else "value_list space ")
css_appendff(&header, "{}", [ak.arg_u64((u64)css_style_value_list_size(list))])
if not has_shorthand:
let serialized = css_style_value_to_string(value, .normal)
css_appendff(&header, " {}", [css_arg_string(&serialized)])
test_property_line(out, indent, ak.string_builder_string_view(&header))
for item in ak.vector_span_const(&items):
test_property_print_value(out, indent + 1, item)
return
let serialized = css_style_value_to_string(value, .normal)
var text = ak.string_builder_new()
css_appendff(&text, "{} {}", [ak.arg_str(test_property_type_name(css_style_value_type(value))), css_arg_string(&serialized)])
test_property_line(out, indent, ak.string_builder_string_view(&text))
## The oracle's `property_named`: `custom` is PropertyID::Custom.
func test_property_named(name: str) -> css_data.PropertyId?:
if name == "custom":
return css_data.PropertyId.custom
return css_data.property_id_from_string(ak.sv(name))
## The sample values of the range checks.
let test_property_samples: f64[6] = [-1.0, 0.0, 0.5, 1.0, 100.0, 1000.0]
## ` <name>=<one 0/1 per sample>` for the range check `check`.
func test_property_bounds(builder: ak.StringBuilder*, name: str, id: css_data.PropertyId, check: func(css_data.PropertyId, f64) -> bool):
ak.string_builder_append(builder, " ")
ak.string_builder_append(builder, name)
ak.string_builder_append(builder, "=")
for sample in test_property_samples:
ak.string_builder_append(builder, "1" if check(id, sample) else "0")
func test_property_accepts_angle(id: css_data.PropertyId, value: f64) -> bool:
return css_property_accepts_angle(id, css_angle_make_degrees(value))
func test_property_accepts_flex(id: css_data.PropertyId, value: f64) -> bool:
return css_property_accepts_flex(id, css_flex_make_fr(value))
func test_property_accepts_frequency(id: css_data.PropertyId, value: f64) -> bool:
return css_property_accepts_frequency(id, css_frequency_make_hertz(value))
func test_property_accepts_length(id: css_data.PropertyId, value: f64) -> bool:
return css_property_accepts_length(id, css_length_make_px_f64(value))
func test_property_accepts_percentage(id: css_data.PropertyId, value: f64) -> bool:
return css_property_accepts_percentage(id, css_percentage_new(value))
func test_property_accepts_resolution(id: css_data.PropertyId, value: f64) -> bool:
return css_property_accepts_resolution(id, css_resolution_make_dots_per_pixel(value))
func test_property_accepts_time(id: css_data.PropertyId, value: f64) -> bool:
return css_property_accepts_time(id, css_time_make_seconds(value))
## The oracle's `print_property`: the generated data of one property.
func test_property_print_property(out: ak.StringBuilder*, id: css_data.PropertyId):
var accepts = ak.string_builder_new()
var ranges = ak.string_builder_new()
ak.string_builder_append(&accepts, "accepts")
ak.string_builder_append(&ranges, "ranges")
let accepted_ranges = css_property_accepted_ranges_by_value_type(id)
for type_index in 0..=(u8)CssValueType.view_timeline_inset:
let value_type = (CssValueType)type_index
if css_property_accepts_type(id, value_type):
ak.string_builder_append(&accepts, " ")
ak.string_builder_append_string_view(&accepts, css_value_type_to_string(value_type))
if let range = ak.hash_map_get_k_const(&accepted_ranges, value_type):
ak.string_builder_append(&ranges, " ")
ak.string_builder_append_string_view(&ranges, css_value_type_to_string(value_type))
ak.string_builder_append(&ranges, "=")
test_property_double(&ranges, range.min)
ak.string_builder_append(&ranges, ",")
test_property_double(&ranges, range.max)
test_property_line(out, 1, ak.string_builder_string_view(&accepts))
test_property_line(out, 1, ak.string_builder_string_view(&ranges))
var percentages = ak.string_builder_new()
ak.string_builder_append(&percentages, "percentages ")
if let basis = css_property_resolves_percentages_relative_to(id):
ak.string_builder_append_string_view(&percentages, css_value_type_to_string(basis))
else:
ak.string_builder_append(&percentages, "none")
test_property_line(out, 1, ak.string_builder_string_view(&percentages))
var bounds = ak.string_builder_new()
ak.string_builder_append(&bounds, "bounds")
test_property_bounds(&bounds, "angle", id, test_property_accepts_angle)
test_property_bounds(&bounds, "flex", id, test_property_accepts_flex)
test_property_bounds(&bounds, "frequency", id, test_property_accepts_frequency)
test_property_bounds(&bounds, "length", id, test_property_accepts_length)
test_property_bounds(&bounds, "percentage", id, test_property_accepts_percentage)
test_property_bounds(&bounds, "resolution", id, test_property_accepts_resolution)
test_property_bounds(&bounds, "time", id, test_property_accepts_time)
test_property_line(out, 1, ak.string_builder_string_view(&bounds))
## `<label><number>` as a line.
func test_property_labeled_number(out: ak.StringBuilder*, label: str, value: u64):
var text = ak.string_builder_new()
ak.string_builder_append(&text, label)
css_appendff(&text, "{}", [ak.arg_u64(value)])
test_property_line(out, 1, ak.string_builder_string_view(&text))
## `[` + the component values, each serialized, concatenated (`String::join(""sv, values)`) + `]`.
func test_property_joined(builder: ak.StringBuilder*, values: const ak.Vector[css_syntax.ComponentValue]*):
ak.string_builder_append(builder, "[")
for value in ak.vector_span_const(values):
let text = css_syntax.component_value_to_string(&value)
ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&text))
ak.string_builder_append(builder, "]")
## The at-rule named `name` (`@font-face`).
func test_property_at_rule_named(name: str) -> css_data.AtRuleId?:
for index: u8 in 0..<5:
let at_rule = (css_data.AtRuleId)index
let at_rule_name = css_data.to_string_at_rule_id(at_rule)
if ak.fly_string_eq_string_view(&at_rule_name, ak.sv(name)):
return at_rule
return none
## Run one oracle case and answer its output (the oracle's `run`).
func test_css_property_parsing_run(realm: Realm*, test_case: const TestCssPropertyParsingCase*) -> ak.String:
var out = ak.string_builder_new()
let mode = test_case.mode
if mode == "property":
if let id = test_property_named(test_case.arguments[0]):
test_property_print_property(&out, id)
else:
test_property_line(&out, 1, ak.sv("unknown property"))
elif mode == "envvar":
let variable = css_data.environment_variable_from_string(ak.sv(test_case.arguments[0])) else trap("unknown environment variable")
var text = ak.string_builder_new()
ak.string_builder_append(&text, "type ")
ak.string_builder_append_string_view(&text, css_value_type_to_string(css_environment_variable_type(variable)))
test_property_line(&out, 1, ak.string_builder_string_view(&text))
elif mode == "asf":
if let function = css_to_arbitrary_substitution_function(ak.fly_string_from_str(test_case.arguments[0])):
test_property_labeled_number(&out, "function ", (u64)(u8)function)
else:
test_property_line(&out, 1, ak.sv("none"))
elif mode == "value":
let id = test_property_named(test_case.arguments[0]) else trap("unknown property")
var parser = css_parser_create(test_css_property_parsing_params(realm), ak.sv(test_case.arguments[1]))
let values = css_parser_parse_a_list_of_component_values(&parser, &parser.m_token_stream)
var tokens = css_syntax.token_stream_create(values)
let result: CssStyleValue*? = css_parser_parse_css_value(&parser, id, &tokens) catch failure:
if failure.code == css_parser_parse_error_syntax_error:
test_property_line(&out, 1, ak.sv("error SyntaxError"))
else:
test_property_line(&out, 1, ak.sv("error IncludesIgnoredVendorPrefix"))
recover none
if let value = result:
test_property_print_value(&out, 1, value)
test_property_labeled_number(&out, "rest ", (u64)css_syntax.token_stream_remaining_token_count(&tokens))
elif mode == "for":
var ids = ak.Vector[css_data.PropertyId]()
let names_view = ak.sv(test_case.arguments[0])
let names = ak.string_view_split_view_u8(&names_view, ' ')
for name in ak.vector_span_const(&names):
ak.vector_append_t(&ids, test_property_named(ak.string_view_text(name)) else trap("unknown property"))
var parser = css_parser_create(test_css_property_parsing_params(realm), ak.sv(test_case.arguments[1]))
let values = css_parser_parse_a_list_of_component_values(&parser, &parser.m_token_stream)
var tokens = css_syntax.token_stream_create(values)
if let result = css_parser_parse_css_value_for_properties(&parser, ak.vector_span_const(&ids), &tokens):
let name = css_data.string_from_property_id(result.property)
var text = ak.string_builder_new()
css_appendff(&text, "property {}", [css_arg_fly_string(&name)])
test_property_line(&out, 1, ak.string_builder_string_view(&text))
test_property_print_value(&out, 2, result.style_value else trap("no value"))
else:
test_property_line(&out, 1, ak.sv("none"))
test_property_labeled_number(&out, "rest ", (u64)css_syntax.token_stream_remaining_token_count(&tokens))
elif mode == "initial":
test_property_print_value(&out, 1, css_property_initial_value(test_property_named(test_case.arguments[0]) else trap("unknown property")))
elif mode == "descriptor_initial":
let at_rule = test_property_at_rule_named(test_case.arguments[0]) else trap("unknown at-rule")
let descriptor = css_data.descriptor_id_from_string(at_rule, ak.sv(test_case.arguments[1])) else trap("unknown descriptor")
if let value = css_descriptor_initial_value(at_rule, descriptor):
test_property_print_value(&out, 1, value)
else:
test_property_line(&out, 1, ak.sv("null"))
elif mode == "grammar":
let function = css_to_arbitrary_substitution_function(ak.fly_string_from_str(test_case.arguments[0])) else trap("unknown function")
var parser = css_parser_create(test_css_property_parsing_params(realm), ak.sv(test_case.arguments[1]))
let values = css_parser_parse_a_list_of_component_values(&parser, &parser.m_token_stream)
if let arguments = css_parse_according_to_argument_grammar(function, values):
match arguments:
.vector_vector_component_value(list):
test_property_labeled_number(&out, "declaration values ", (u64)ak.vector_size(&list))
for value in ak.vector_span_const(&list):
var text = ak.string_builder_new()
test_property_joined(&text, &value)
test_property_line(&out, 2, ak.string_builder_string_view(&text))
.vector_css_if_args_branch(branches):
test_property_labeled_number(&out, "if branches ", (u64)ak.vector_size(&branches))
for branch in ak.vector_span_const(&branches):
var text = ak.string_builder_new()
test_property_joined(&text, &branch.condition)
ak.string_builder_append(&text, " : ")
if let branch_value = branch.value:
test_property_joined(&text, &branch_value)
else:
ak.string_builder_append(&text, "none")
test_property_line(&out, 2, ak.string_builder_string_view(&text))
else:
test_property_line(&out, 1, ak.sv("failure"))
else:
test_property_line(&out, 1, ak.sv("unknown mode"))
return ak.string_builder_to_string_without_validation(&out)
test "property parsing and the generated property data match the reference build on the oracle cases":
let (vm, realm, _) = test_html_setup()
defer web_test_destroy_vm(vm)
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
var failures = 0
for test_case in &test_css_property_parsing_cases:
let actual = test_css_property_parsing_run(realm, test_case)
if not ak.string_eq_string_view(&actual, ak.sv(test_case.expected)):
let actual_view = ak.string_bytes_as_string_view(&actual)
print(f"case {test_case.mode} {test_case.arguments[0]} | {test_case.arguments[1]}\nexpected:\n{test_case.expected}actual:\n{(str)ak.string_view_bytes(&actual_view)}")
failures += 1
assert(failures == 0, "property parsing oracle cases differ")
# mark: Focused tests ===========================================================================
test "SubstitutionContext: equality ignores is_cyclic, to_string prints an absent second as None (ArbitrarySubstitutionFunctions.cpp:20, 27)":
let a = CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.property, first = ak.string_from_str("--a"))
let a_cyclic = CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.property, first = ak.string_from_str("--a"), is_cyclic = true)
let b = CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.attribute, first = ak.string_from_str("--a"))
let c = CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.function, first = ak.string_from_str("f"), second = ak.string_from_str("g"))
assert(css_substitution_context_eq(&a, a_cyclic))
assert(not css_substitution_context_eq(&a, b))
assert(not css_substitution_context_eq(&c, CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.function, first = ak.string_from_str("f"))))
let a_text = css_substitution_context_to_string(&a)
let b_text = css_substitution_context_to_string(&b)
let c_text = css_substitution_context_to_string(&c)
assert(ak.string_eq_string_view(&a_text, ak.sv("Property --a None")))
assert(ak.string_eq_string_view(&b_text, ak.sv("Attribute --a None")))
assert(ak.string_eq_string_view(&c_text, ak.sv("Function f g")))
test "GuardedSubstitutionContexts: guarding an equal context marks both cyclic and keeps one entry; unguard removes it (ArbitrarySubstitutionFunctions.cpp:43, 56)":
var guarded = CssGuardedSubstitutionContexts()
var outer = CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.property, first = ak.string_from_str("--a"))
var other = CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.property, first = ak.string_from_str("--b"))
var inner = CssSubstitutionContext(dependency_type = CssSubstitutionContextDependencyType.property, first = ak.string_from_str("--a"))
css_guarded_substitution_contexts_guard(&guarded, &outer)
css_guarded_substitution_contexts_guard(&guarded, &other)
assert(ak.vector_size(&guarded.m_contexts) == 2 and not outer.is_cyclic and not other.is_cyclic)
css_guarded_substitution_contexts_guard(&guarded, &inner)
assert(ak.vector_size(&guarded.m_contexts) == 2)
assert(outer.is_cyclic and inner.is_cyclic and not other.is_cyclic)
css_guarded_substitution_contexts_unguard(&guarded, inner)
assert(ak.vector_size(&guarded.m_contexts) == 1)
assert(*ak.vector_at(&guarded.m_contexts, 0) == &other)
css_guarded_substitution_contexts_unguard(&guarded, other)
assert(ak.vector_is_empty(&guarded.m_contexts))
test "guaranteed-invalid and attr-tainted values: contains_* look inside functions and blocks; mark_as_attr_tainted marks a copy (ArbitrarySubstitutionFunctions.cpp:79-102)":
var parser = css_parser_create(css_default_parsing_params_for_tests(), ak.sv("a f(b) [c]"))
let values = css_parser_parse_as_list_of_component_values(&parser)
assert(not css_contains_guaranteed_invalid_value(values))
assert(not css_contains_attr_tainted_value(values))
let marked = css_mark_as_attr_tainted(values)
assert(css_contains_attr_tainted_value(marked))
assert(not css_contains_attr_tainted_value(values))
var with_invalid = ak.vector_clone(&values)
ak.vector_append_t(&with_invalid, css_syntax.component_value_from_guaranteed_invalid_value(css_syntax.GuaranteedInvalidValue()))
assert(css_contains_guaranteed_invalid_value(with_invalid))
assert(css_contains_guaranteed_invalid_value(css_guaranteed_invalid_component_values()))
## Default ParsingParams for the focused tests.
func css_default_parsing_params_for_tests() -> CssParsingParams:
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms)
return params