Skip to content

Conversation

Copy link

Copilot AI commented Dec 22, 2025

The CSS parser had incomplete test coverage, particularly in core parser classes, DOM implementations, selector logic, and error handling paths.

Changes

Added 105 new tests (+3% total) across critical areas:

Parser Layer

  • CssCharStreamTest (19 tests): Buffer management, line/column tracking, wrap-around scenarios, backup operations
  • CSSErrorHandlingTest (15 tests): Malformed CSS recovery, invalid unicode escapes, buffer overflow, edge cases

DOM Layer

  • AbstractCSSRuleImplTest (11 tests): Parent relationship handling, circular reference prevention in equals/hashCode
  • Enhanced PropteryTest (+6 tests): Null handling, equality edge cases

Selector Layer

  • ComplexSelectorTest (15 tests): Combinator chains (>, +, ~), pseudo-classes, deeply nested selectors
  • Enhanced DescendantSelectorImplTest (+5 tests): Null ancestors, complex nesting
  • Enhanced SelectorSpecificityTest (+9 tests): :not()/:is()/:where() edge cases, multiple IDs

Condition & Media

  • Enhanced PseudoClassConditionTest (+5 tests): Double colon (::) vs single colon (:) handling
  • Enhanced MediaQueryTest (+9 tests): only/not modifiers, property combinations

Example Coverage

// CssCharStream now has comprehensive buffer wrap-around testing
@Test
public void getImageWrapAround() throws Exception {
    StringBuilder input = new StringBuilder();
    for (int i = 0; i < 3000; i++) {
        input.append((char) ('a' + (i % 26)));
    }
    
    CssCharStream stream = new CssCharStream(new StringReader(input.toString()), 1, 1, 100);
    stream.beginToken();
    for (int i = 0; i < 49; i++) {
        stream.readChar();
    }
    
    String image = stream.getImage();
    assertEquals(50, image.length());
}

// Error handling tests verify parser recovers from malformed CSS
@Test
public void malformedSelector() throws Exception {
    String css = "p { color: red; }\n"
               + "div# { font-size: 12px; }\n"  // malformed
               + "span { margin: 10px; }";
    
    CSSStyleSheetImpl sheet = parser.parseStyleSheet(new InputSource(new StringReader(css)), null);
    assertNotNull(sheet);  // Parser recovers and continues
}

All 730 tests pass. Zero security vulnerabilities detected.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkstyle.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/xml/tools/xml-extractor.jar --fileList=/home/REDACTED/work/htmlunit-cssparser/.codeql-scratch/dbs/java/working/files-to-index9820819314572264364.list --sourceArchiveDir=/home/REDACTED/work/htmlunit-cssparser/.codeql-scratch/dbs/java/src --outputDir=/home/REDACTED/work/htmlunit-cssparser/.codeql-scratch/dbs/java/trap/java (dns block)
  • www.puppycrawl.com
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.23.8/x64/codeql/xml/tools/xml-extractor.jar --fileList=/home/REDACTED/work/htmlunit-cssparser/.codeql-scratch/dbs/java/working/files-to-index9820819314572264364.list --sourceArchiveDir=/home/REDACTED/work/htmlunit-cssparser/.codeql-scratch/dbs/java/src --outputDir=/home/REDACTED/work/htmlunit-cssparser/.codeql-scratch/dbs/java/trap/java (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Problem Statement

The HtmlUnit CSS parser project currently has incomplete test coverage. This PR aims to add comprehensive unit tests to achieve 100% test coverage for the parser, focusing on:

Areas Requiring Additional Test Coverage

  1. Parser Classes - Missing comprehensive tests for:

    • org.htmlunit.cssparser.parser.CssCharStream - Character stream handling, buffer management, line/column tracking
    • org.htmlunit.cssparser.parser.javacc.CSS3Parser - Core parser functionality, error handling, edge cases
    • Parser error recovery and malformed CSS handling
    • Unicode escape sequence processing
    • Token boundary conditions
  2. DOM Classes - Incomplete coverage for:

    • org.htmlunit.cssparser.dom.Property - All setter/getter methods, equals/hashCode edge cases
    • org.htmlunit.cssparser.dom.CSSValueImpl - All value types and conversions
    • org.htmlunit.cssparser.dom.AbstractCSSRuleImpl - Parent relationship handling
    • Various CSS rule implementations
  3. Selector Classes - Missing tests for:

    • org.htmlunit.cssparser.parser.selector.DescendantSelector - Complex descendant relationships
    • org.htmlunit.cssparser.parser.selector.SelectorSpecificity - Edge cases in specificity calculation
    • Complex selector combinations
    • Pseudo-element and pseudo-class edge cases
  4. Condition Classes - Gaps in:

    • org.htmlunit.cssparser.parser.condition.PseudoClassCondition - Double colon handling
    • org.htmlunit.cssparser.parser.condition.SuffixAttributeCondition - Case sensitivity
    • All attribute condition variants (prefix, substring, begin-hyphen, one-of)
    • Complex condition combinations
  5. Media Query Support:

    • org.htmlunit.cssparser.parser.media.MediaQuery - All media types and features
    • Complex media query combinations
    • Media query error handling
  6. Error Handling & Edge Cases:

    • Malformed CSS recovery
    • Buffer overflow conditions
    • Invalid character handling
    • Null/empty input handling
    • Exception paths and error reporting

Test Coverage Goals

Create comprehensive test suites that cover:

  • All public methods in core parser classes
  • Edge cases and boundary conditions
  • Error handling and recovery
  • Unicode and special character handling
  • Complex CSS syntax combinations
  • Null safety and defensive programming
  • Equals/hashCode contracts
  • Serialization/deserialization if applicable
  • Thread safety where relevant

Expected Outcomes

  • Achieve 100% line coverage for parser core classes
  • Achieve 100% branch coverage for critical parsing logic
  • Comprehensive test documentation
  • Tests that serve as usage examples
  • Improved code quality through testing edge cases found during test development

Implementation Guidelines

  • Use JUnit 5 (Jupiter) as seen in existing tests
  • Follow existing test patterns and naming conventions
  • Include both positive and negative test cases
  • Test error messages and exception types
  • Add tests for CSS3 specific features
  • Ensure tests are isolated and don't depend on execution order
  • Add JavaDoc comments explaining complex test scenarios

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Dec 22, 2025
Copilot AI and others added 3 commits December 22, 2025 18:25
…or, PseudoClassCondition, and MediaQuery

Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
…on tests

Co-authored-by: rbri <2544132+rbri@users.noreply.github.com>
Copilot AI changed the title [WIP] Add comprehensive unit tests for CSS parser Add comprehensive unit tests for CSS parser core classes Dec 22, 2025
Copilot AI requested a review from rbri December 22, 2025 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants