Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Improve code coverage #517

Merged
merged 5 commits into from Apr 30, 2022
Merged

refactor: Improve code coverage #517

merged 5 commits into from Apr 30, 2022

Conversation

fb55
Copy link
Collaborator

@fb55 fb55 commented Apr 20, 2022

Removes all low-hanging fruit for #515

@@ -17,9 +17,6 @@ export function parseChunked<T extends TreeAdapterTypeMap>(
parserStream.parser.tokenizer.preprocessor.bufferWaterline = 8;

for (let i = 0; i < chunks.length - 1; i++) {
if (typeof chunks[i] !== 'string') {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript validates this, we don't need this check.

@@ -317,6 +317,8 @@ generateTestsForEachTreeAdapter('open-element-stack', (treeAdapter) => {
test('Has numbered header in scope', () => {
const stack = new OpenElementStack(treeAdapter.createDocument(), treeAdapter, stackHandler);

assert.ok(stack.hasNumberedHeaderInScope());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty states won't be used in the parser, but we can test them here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Why does hasNumberedHeaderInScope exist if parse5 doesn’t use it? Should we deprecate it and remove it in a major then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these functions are used, can we instead add unit tests (input/output) of parsing behavior that depends on the result of these functions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasNumberedHeaderInScope is used by the parser. The only thing that the parser cannot reach is the default return value, eg. for an empty stack.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean this early return

causing to never run?

Could you change that early return to a break in that case, to fix the coverage?

Not sure about the algorithm: but it seems off that the default for that function i true? I’d expect, with such a function name, to exit early with true, and otherwise return false?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relevant part of the spec is https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-scope

The algorithm explicitly doesn't consider the possibility of an empty stack:

This will never fail, since the loop will always terminate in the previous step if the top of the stack — an html element — is reached.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I recommend using a c8 ignore (or similar), with this as an explanation:

/* Note: the algorithm <link> never fails */
/* c8 ignore next 123 */


await finished(parser);

assert.ok(foundText);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably a good idea to use an assert.equal instead, to check that the actual result makes sense and not, say, 'null' for example?

@@ -317,6 +317,8 @@ generateTestsForEachTreeAdapter('open-element-stack', (treeAdapter) => {
test('Has numbered header in scope', () => {
const stack = new OpenElementStack(treeAdapter.createDocument(), treeAdapter, stackHandler);

assert.ok(stack.hasNumberedHeaderInScope());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Why does hasNumberedHeaderInScope exist if parse5 doesn’t use it? Should we deprecate it and remove it in a major then?

@@ -317,6 +317,8 @@ generateTestsForEachTreeAdapter('open-element-stack', (treeAdapter) => {
test('Has numbered header in scope', () => {
const stack = new OpenElementStack(treeAdapter.createDocument(), treeAdapter, stackHandler);

assert.ok(stack.hasNumberedHeaderInScope());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these functions are used, can we instead add unit tests (input/output) of parsing behavior that depends on the result of these functions?

packages/parse5/lib/tokenizer/index.test.ts Show resolved Hide resolved
packages/parse5/lib/tokenizer/index.ts Show resolved Hide resolved
packages/parse5/lib/tokenizer/index.ts Show resolved Hide resolved
@fb55 fb55 enabled auto-merge (squash) April 30, 2022 11:06
@fb55 fb55 merged commit 6e57110 into master Apr 30, 2022
@fb55 fb55 deleted the increase-coverage branch November 16, 2022 14:31
jmbpwtw pushed a commit to jmbpwtw/parse5 that referenced this pull request Feb 16, 2023
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.

None yet

2 participants