Skip to content

Commit

Permalink
test(cases): refactor test cases and move html data to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Nov 3, 2019
1 parent 8e30784 commit e4fcb09
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 32 deletions.
178 changes: 178 additions & 0 deletions test/cases/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
module.exports = [
// high-level tags
{
name: 'empty html',
data: '<html></html>'
},
{
name: 'html with attribute',
data: '<html lang="en"></html>'
},
{
name: 'html with empty head and body',
data: '<html><head></head><body></body></html>'
},
{
name: 'html with empty head',
data: '<html><head></head></html>'
},
{
name: 'html with empty body',
data: '<html><body></body></html>'
},

{
name: 'empty head',
data: '<head></head>'
},
{
name: 'head with title',
data: '<head><title>Page</title></head>'
},
{
name: 'empty head and body',
data: '<head></head><body></body>'
},
{
name: 'empty body',
data: '<body></body>'
},
{
name: 'capitalized body',
data: '<BODY></BODY>'
},
{
name: 'body with paragraph',
data: '<body><p>text</p></body>'
},

// low-level tags
{
name: 'empty div',
data: '<div></div>'
},
{
name: 'empty paragraph',
data: '<p></p>'
},
{
name: 'paragraph with text',
data: '<p>text</p>'
},
{
name: 'meta with attribute',
data: '<meta charset="utf-8">'
},
{
name: 'textarea with value',
data: '<textarea>value</textarea>'
},
{
name: 'multiple spans',
data: '<span>1</span><span>2</span>'
},

// void (self-closing) tags
{
name: 'void',
data: '<br>'
},
{
name: 'self-closing void',
data: '<hr/>'
},
{
name: 'input with attributes',
data: '<input type="text" value="value">'
},
{
name: 'image',
data: '<img src="https://httpbin.org/image/png" alt="Image">'
},
{
name: 'multiple void',
data: '<link /><meta/><hr><input type="radio" checked />'
},

// tag attributes
{
name: 'h1 with id attribute',
data: '<h1 id="heading"></h1>'
},
{
name: 'h2 with class attribute',
data: '<h2 class="heading"></h2>'
},
{
name: 'em with style attribute',
data: '<em style="color: white; z-index: 1; -webkit-appearance: none"></em>'
},
{
name: 'data attribute',
data: '<div data-attribute="value"></div>'
},
{
name: 'event attribute',
data: '<div onclick="alert();"></div>'
},
{
name: 'span with multiple attributes',
data:
'<span id="button" class="big" style="border: 1px solid #000; -moz-appearance: button;" aria-label="Back" />'
},
{
name: 'hr with multiple attributes',
data:
'<hr id="foo" class="bar baz" style="background: #fff; text-align: center;" data-foo="bar">'
},

// adjacent tags
{
name: 'sibling',
data: '<li>brother</li><li>sister</li>'
},

// nested tags
{
name: 'nested definition list',
data: '<dl><dt>foo</dt><dd>bar<span>baz</span></dd></dl>'
},
{
name: 'nested unordered list',
data: '<ul><li>foo<span>bar</span></li><li>baz</li></ul>'
},

// script tag
{
name: 'script',
data: '<script>console.log(1 < 2);</script>'
},

// style tag
{
name: 'style',
data: '<style>body > .foo { color: #f00; }</style>'
},

// special
{
name: 'directive',
data: '<!doctype html>'
},
{
name: 'directive with html',
data: '<!DOCTYPE html><html></html>'
},
{
name: 'comment',
data: '<!-- comment -->'
},
{
name: 'text',
data: 'text'
},
{
name: 'closing tag',
data: '</div>'
}
];
44 changes: 16 additions & 28 deletions test/cases/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs');
const path = require('path');
const { minify } = require('html-minifier');
const htmlCases = require('./html');

/**
* Reads file (helper for `readFileSync`).
Expand All @@ -16,35 +17,22 @@ function read(filepath) {
}
}

const html = {
directive: '<!DOCTYPE html>',
head: '<head></head>',
'head with title': '<head><title>content</title></head>',
body: '<body></body>',
'body with div': '<body><div>hello</div></body>',
'capitalized body': '<BODY></BODY>',
'head and body': '<head></head><body></body>',
'empty paragraph': '<p></p>',
'paragraph with text': '<p>text</p>',
'multiple spans': '<span>1</span><span>2</span>',
nested: '<ul><li>foo<span>bar</span></li><li>baz</li></ul>',
attributes:
'<hr id="foo" class="bar baz" style="background: #fff; text-align: center;" data-foo="bar" />',
textarea: '<textarea>foo</textarea>',
script: '<script>console.log(1 < 2);</script>',
style: '<style>body > .foo { color: #f00; }</style>',
img: '<img src="http://stat.ic/img.jpg" alt="Image"/>',
void: '<link/><meta/><img/><br/><hr/><input/>',
comment: '<!-- comment -->',
'closing tag': '</div>',
'complex html': minify(read('./complex.html'), {
collapseWhitespace: true
})
};
const html = [
...htmlCases,
{
name: 'complex html',
data: minify(read('./complex.html'), {
collapseWhitespace: true
})
}
];

const svg = {
'complex svg': read('./complex.svg')
};
const svg = [
{
name: 'complex svg',
data: read('./complex.svg')
}
];

module.exports = {
html,
Expand Down
11 changes: 7 additions & 4 deletions test/helpers/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ function runTests(testCases, expectedParser, actualParser, assert) {
throw new TypeError('Missing or invalid actual parser');
}

Object.keys(testCases).forEach(function(type) {
var testCase = testCases[type];
testCases.forEach(function(testCase) {
var _it = testCase.only ? it.only : testCase.skip ? it.skip : it;

it(type, function() {
assert.deepEqual(expectedParser(testCase), actualParser(testCase));
_it('parses ' + testCase.name, function() {
assert.deepEqual(
expectedParser(testCase.data),
actualParser(testCase.data)
);
});
});
}
Expand Down

0 comments on commit e4fcb09

Please sign in to comment.