Test Fixtures Data
Relevant source files
The following files were used as context for generating this wiki page:
Test fixtures in cheerio-select provide the foundational data structures required to validate the CSS selector engine's accuracy across various DOM environments, including HTML and XML. These fixtures consist of static markup files and utility scripts that load, parse, and query these files to ensure the engine correctly handles jQuery-style positional selectors and standard CSS specifications.
The system relies on htmlparser2 for parsing these fixtures into a domhandler compatible format, which is then used by the cheerio-select suite for regression testing and feature validation.
Sources: test/tools/sizzle-testinit.ts:1-25, test/sizzle.spec.ts:15-30
Fixture Architecture and Loading Logic
The project uses a dedicated initialization tool to bridge static fixture files with the testing framework. The sizzle-testinit.ts utility handles the reading of files from the disk and provides a SimpleDocument interface that mimics standard browser DOM methods like getElementById and createElement.
Data Flow for Fixture Loading
The following diagram illustrates how a fixture file like sizzle.html is transformed into a queryable document object for tests.
Sources: test/tools/sizzle-testinit.ts:8-43
Key Initialization Functions
| Function | Purpose | Return Type |
|---|---|---|
getDOMFromPath | Reads a fixture file and parses it using htmlparser2. | AnyNode[] |
getDocument | Enhances a parsed DOM with helper methods for test convenience. | SimpleDocument |
loadDocument | Specifically loads the sizzle.html fixture for general testing. | SimpleDocument |
createWithFriesXML | Loads and returns the XML-specific fixture fries.xml. | Element[] |
Sources: test/tools/sizzle-testinit.ts:8-63
HTML Fixtures: sizzle.html
The sizzle.html file is an extensive document containing diverse HTML structures designed to stress-test selectors. It includes nested lists, complex forms with various input types, and elements with special characters in IDs or classes.
Structural Categories in sizzle.html
- Form Elements: Contains multiple forms (
#form,#testForm,#lengthtest) with inputs likeradio,checkbox,search, andhiddento test attribute selectors and positional pseudos. - Positional Testing: Uses structures like
#qunit-fixture pand#siblingTestto validate:first,:last, and sibling combinators (~,+). - Internationalization: Includes elements with UTF-8 IDs and classes (e.g.,
#台北Táiběi,.台北) to ensure encoding support. - Edge Cases: Elements with IDs containing backslashes, colons, and brackets (e.g.,
id="foo:bar",id="test.foo[5]bar") to test escaping logic.
Sources: test/fixtures/sizzle.html:45-385, test/sizzle.spec.ts:168-220
XML Fixtures: fries.xml
The fries.xml fixture is used to validate the engine in xmlMode. It focuses on namespaced elements and case-sensitive tag matching, which differs from standard HTML parsing.
XML Data Characteristics
The fixture includes SOAP-formatted XML with multiple namespaces and nested metadata properties:
- Namespaces: Uses
soap,xsd,xsi,ab, andcd. - Unique Tag Names: Elements like
<foo_bar>are used to test underscore support in tag selectors. - Attributes: Standard attributes like
idandclassare present within the XML structure to verify attribute selector parity with HTML.
Sources: test/fixtures/fries.xml:1, test/sizzle.spec.ts:133-157
Testing Utilities and Assertions
Tests consume fixtures through specialized assertion functions that map selector results back to element IDs for easy validation.
The t Assertion Function
The t function is the primary validation tool. It executes a select call and compares the resulting element IDs against an expected array of strings.
export function t(
selector: string,
expectedIds: string[],
context: AnyNode[] | AnyNode = document,
): void {
const actual = select(
selector,
context as Element | Element[],
) as Element[];
const actualIds = actual.map((element) => element.attribs["id"]);
expect(actualIds).toStrictEqual(expectedIds);
}
Sources: test/tools/sizzle-testinit.ts:65-80
Selector Validation Examples
The test suite validates various selector types against the fixtures:
- Attributes:
[name='action'],[data-comma='0,1']. - Pseudos:
:nth-child(even),:has(a),:contains(Google). - Traversals:
div ~ p,p > a.
Sources: test/sizzle.spec.ts:390-480, src/index.spec.ts:45-60
Summary
Test fixture data in cheerio-select serves as a comprehensive environment for verifying CSS selector compliance. By utilizing a mix of complex HTML (sizzle.html) and namespaced XML (fries.xml), supported by a custom DOM simulation layer (sizzle-testinit.ts), the project ensures robust selector evaluation across standard and edge-case scenarios.