dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / css / parser / tests_css_parser.lucb
#==============================================================================================
#
# tests_css_parser - tests of the CSS parser core (region r30b)
#
# DESCRIPTION:
# Ladybird has no unit tests for Parser.cpp, Helpers.cpp, ErrorReporter, RuleContext or
# Serialize's engine half. The oracle cases (tests_css_parser_cases) run the parser's
# consumers on 111 inputs and compare dumps made the way the local oracle makes them
# from the reference build: component values (their debug strings and serialization),
# raw rules and declarations of stylesheets and blocks, single rules, declarations and
# component values, comma-separated lists, a declaration's original text, ignored vendor
# prefixes, <custom-ident> validity, declaration serialization and CSS decode bytes. The
# focused tests below pin the rest to the C++ (values read from the donor's code).
#
# Paths that reach other regions' stubs (CSSOM rules, style values, selectors, media
# queries, top-level at-rules through is_margin_rule_name) are not exercised yet.
#
#==============================================================================================
# mark: The oracle dumps ========================================================================
## One line of a dump: two spaces per indent level, the text, a newline.
func test_css_line(out: ak.StringBuilder*, indent: usize, text: ak.String):
for _unused in 0..<indent:
ak.string_builder_append(out, " ")
ak.string_builder_append_string_view(out, ak.string_bytes_as_string_view(&text))
ak.string_builder_append_u8(out, '\n')
## serialize_a_series_of_component_values of a vector.
func test_css_serialized(values: const ak.Vector[css_syntax.ComponentValue]*) -> ak.String:
return css_serialize_a_series_of_component_values(ak.vector_span_const(values))
## "name: [value] important:0|1 original:text|-".
func test_css_dump_declaration(out: ak.StringBuilder*, declaration: const css_syntax.Declaration*, indent: usize):
let value = test_css_serialized(&declaration.value)
let important = 1 if declaration.important == css_syntax.Important.yes else 0
let original = declaration.original_value_text else ak.string_from_str("-")
test_css_line(out, indent, css_string_formatted("{}: [{}] important:{} original:{}",
[css_arg_fly_string(&declaration.name), css_arg_string(&value), ak.arg_u64((u64)important), css_arg_string(&original)]))
## A rule or a list of declarations.
func test_css_dump_rule_or_list(out: ak.StringBuilder*, item: const css_syntax.RuleOrListOfDeclarations*, indent: usize):
match *item:
.rule(rule):
test_css_dump_rule(out, &rule, indent)
.vector_declaration(declarations):
test_css_line(out, indent, ak.string_from_str("declarations:"))
let list = ak.vector_span_const(&declarations)
for declaration in &list:
test_css_dump_declaration(out, declaration, indent + 1)
## "@name prelude:[...] block:0|1" or "qualified prelude:[...]", then the children.
func test_css_dump_rule(out: ak.StringBuilder*, rule: const css_syntax.Rule*, indent: usize):
match *rule:
.at_rule(at_rule):
let prelude = test_css_serialized(&at_rule.prelude)
let block = 1 if at_rule.is_block_rule else 0
test_css_line(out, indent, css_string_formatted("@{} prelude:[{}] block:{}", [css_arg_fly_string(&at_rule.name), css_arg_string(&prelude), ak.arg_u64((u64)block)]))
let children = ak.vector_span_const(&at_rule.child_rules_and_lists_of_declarations)
for child in &children:
test_css_dump_rule_or_list(out, child, indent + 1)
.qualified_rule(qualified_rule):
let prelude = test_css_serialized(&qualified_rule.prelude)
test_css_line(out, indent, css_string_formatted("qualified prelude:[{}]", [css_arg_string(&prelude)]))
let declarations = ak.vector_span_const(&qualified_rule.declarations)
for declaration in &declarations:
test_css_dump_declaration(out, declaration, indent + 1)
let children = ak.vector_span_const(&qualified_rule.child_rules)
for child in &children:
test_css_dump_rule_or_list(out, child, indent + 1)
## Each value's debug string, then "= " and their serialization.
func test_css_dump_values(out: ak.StringBuilder*, values: const ak.Vector[css_syntax.ComponentValue]*, indent: usize):
let list = ak.vector_span_const(values)
for value in &list:
test_css_line(out, indent, css_syntax.component_value_to_debug_string(value))
let serialized = test_css_serialized(values)
test_css_line(out, indent, css_string_formatted("= {}", [css_arg_string(&serialized)]))
## A parser over `input`, with a style rule as the context when `style`.
func test_css_parser(input: str, style: bool) -> CssParser:
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms)
if style:
ak.vector_append_t(¶ms.rule_context, CssRuleContext.style)
return css_parser_create(params, ak.sv(input))
## "true" or "false", as AK formats a bool.
func test_css_bool(value: bool) -> ak.String:
return ak.string_from_str("true" if value else "false")
## One error of each kind, with the oracle's values; the C++ defaults ("style", "@media") are
## written out where the C++ leaves them to the member initializers.
func test_css_parsing_errors(name: ak.FlyString, text: ak.String) -> CssParsingError[11]:
let style = ak.fly_string_from_str("style")
let description = ak.string_from_str("desc")
return [
CssParsingError.css_unknown_property_error(CssUnknownPropertyError(rule_name = style, property_name = name)),
CssParsingError.css_unknown_rule_error(CssUnknownRuleError(rule_name = name)),
CssParsingError.css_unknown_media_feature_error(CssUnknownMediaFeatureError(media_feature_name = name)),
CssParsingError.css_unknown_pseudo_class_or_element_error(CssUnknownPseudoClassOrElementError(rule_name = style, name = name)),
CssParsingError.css_invalid_property_error(CssInvalidPropertyError(rule_name = style, property_name = name, value_string = text, description = description)),
CssParsingError.css_invalid_value_error(CssInvalidValueError(value_type = name, value_string = text, description = description)),
CssParsingError.css_invalid_rule_error(CssInvalidRuleError(rule_name = name, prelude = text, description = description)),
CssParsingError.css_invalid_query_error(CssInvalidQueryError(query_type = ak.fly_string_from_str("@media"), value_string = text, description = description)),
CssParsingError.css_invalid_selector_error(CssInvalidSelectorError(rule_name = style, value_string = text, description = description)),
CssParsingError.css_invalid_pseudo_class_or_element_error(CssInvalidPseudoClassOrElementError(name = name, value_string = text, description = description)),
CssParsingError.css_invalid_rule_location_error(CssInvalidRuleLocationError(outer_rule_name = name, inner_rule_name = ak.fly_string_from_str("@inner"))),
]
## Runs one oracle case and answers its dump.
noinline func test_css_run_case(mode: str, input: str) -> ak.String:
var out = ak.string_builder_new()
if mode == "cv":
var parser = test_css_parser(input, false)
let values = css_parser_parse_as_list_of_component_values(&parser)
test_css_dump_values(&out, &values, 1)
elif mode == "one":
var parser = test_css_parser(input, false)
if let value = css_parser_parse_as_component_value(&parser):
test_css_line(&out, 1, css_syntax.component_value_to_debug_string(&value))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "csv":
var parser = test_css_parser(input, false)
let groups = css_parser_parse_a_comma_separated_list_of_component_values(&parser, &parser.m_token_stream)
let list = ak.vector_span_const(&groups)
for group in &list:
test_css_line(&out, 1, ak.string_from_str("group:"))
test_css_dump_values(&out, group, 2)
elif mode == "sheet":
var parser = test_css_parser(input, false)
let rules = css_parser_parse_a_stylesheets_contents(&parser, &parser.m_token_stream)
let list = ak.vector_span_const(&rules)
for rule in &list:
test_css_dump_rule(&out, rule, 1)
elif mode == "block" or mode == "style":
var parser = test_css_parser(input, mode == "style")
let items = css_parser_parse_a_blocks_contents(&parser, &parser.m_token_stream)
let list = ak.vector_span_const(&items)
for item in &list:
test_css_dump_rule_or_list(&out, item, 1)
elif mode == "rule":
var parser = test_css_parser(input, false)
if let rule = css_parser_parse_a_rule(&parser, &parser.m_token_stream):
test_css_dump_rule(&out, &rule, 1)
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "decl":
var parser = test_css_parser(input, true)
if let declaration = css_parser_parse_a_declaration(&parser, &parser.m_token_stream):
test_css_dump_declaration(&out, &declaration, 1)
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "fulltext":
var parser = test_css_parser(input, true)
if let declaration = css_parser_consume_a_declaration(&parser, &parser.m_token_stream, CssParserNested.no, CssParserSaveOriginalText.yes):
test_css_line(&out, 1, declaration.original_full_text else ak.string_from_str("-"))
else:
test_css_line(&out, 1, ak.string_from_str("none"))
elif mode == "vendor":
test_css_line(&out, 1, test_css_bool(css_parser_has_ignored_vendor_prefix(ak.sv(input))))
elif mode == "ident":
let blacklist: ak.StringView[2] = [ak.sv("auto"), ak.sv("none")]
test_css_line(&out, 1, test_css_bool(is_valid_custom_ident(ak.fly_string_from_str(input), blacklist)))
elif mode == "decldecl":
test_css_line(&out, 1, css_serialize_a_css_declaration(ak.sv(input), ak.sv(" red "), css_syntax.Important.no))
test_css_line(&out, 1, css_serialize_a_css_declaration(ak.sv(input), ak.sv(" "), css_syntax.Important.yes))
elif mode == "decode":
let bytes = ak.byte_buffer_copy_span_u8(input.bytes) else trap("out of memory")
let decoded: ak.String? = css_decode_bytes(none, none, &bytes) catch: recover none
test_css_line(&out, 1, decoded else ak.string_from_str("error"))
elif mode == "math":
ak.hash_map_clear(&css_error_reporter_the().m_errors)
var parser = test_css_parser(input, false)
let value = css_parser_parse_as_component_value(&parser) else trap("a component value")
let function = css_syntax.component_value_function(&value)
let context = CssCalculationContext()
let node = css_parser_parse_math_function(&parser, &function, &context)
test_css_line(&out, 1, ak.string_from_str("node" if node != none else "none"))
for entry in ak.hash_map_begin(&css_error_reporter_the().m_errors):
let message = css_serialize_parsing_error(&entry.key)
test_css_line(&out, 1, css_string_formatted("{} ({})", [css_arg_string(&message), ak.arg_u64((u64)entry.value.occurrences)]))
elif mode == "atctx":
test_css_line(&out, 1, ak.string_number_u64((u64)css_rule_context_type_for_at_rule(ak.fly_string_from_str(input))))
elif mode == "errors":
let errors = test_css_parsing_errors(ak.fly_string_from_str(input), ak.string_from_str(input))
for error_ in &errors:
let message = css_serialize_parsing_error(error_)
test_css_line(&out, 1, css_string_formatted("{} {}", [css_arg_string(&message), ak.arg_u64((u64)css_parsing_error_hash(error_))]))
else:
trap("unknown oracle case mode")
return ak.string_builder_to_string_without_validation(&out)
test "the CSS parser's consumers match the reference build on the oracle cases":
with memory.heap:
var failures = 0
for case_ in test_css_parser_cases:
let actual = test_css_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)