Skip to content

Commit

Permalink
describe tests in test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfortis committed Jan 20, 2024
1 parent 1dd5b54 commit 4ee7604
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 125 deletions.
3 changes: 2 additions & 1 deletion static-pages-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"node": ">=18"
},
"scripts": {
"cloc": "cloc . --not-match-d=./ --fullpath"
"cloc": "cloc . --not-match-d=./ --fullpath",
"test": "node --test"
}
}
25 changes: 14 additions & 11 deletions static-pages-bundler/tests/media-remaper.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import test from 'node:test'
import { describe, test } from 'node:test'
import { equal, throws } from 'node:assert/strict'
import { remapMediaInHTML } from '../media-remaper.js'


const mHashes = new Map([
['alpha.png', '0xFA.png'],
['beta.png', '0xFB.png'],
['chi.png', '0xFC.png']
])
describe('Media Remapper', () => {
const mHashes = new Map([
['alpha.png', '0xFA.png'],
['beta.png', '0xFB.png'],
['chi.png', '0xFC.png']
])

test('Throws when the file does not exist', () =>
throws(() => remapMediaInHTML(mHashes, `<video src="media/missing.mp4">`)))
test('Throws when the file does not exist', () =>
throws(() => remapMediaInHTML(mHashes, `<video src="media/missing.mp4">`)))

test('Acceptance', () =>
equal(remapMediaInHTML(mHashes, `
test('Acceptance', () =>
equal(remapMediaInHTML(mHashes, `
<img src="media/alpha.png">
<img src="media/alpha.png">
<img src="media/beta.png">
<video poster="media/chi.png">`),
`
`
<img src="media/0xFA.png">
<img src="media/0xFA.png">
<img src="media/0xFB.png">
<video poster="media/0xFC.png">`))


})
136 changes: 69 additions & 67 deletions static-pages-bundler/tests/minifyCSS.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import test from 'node:test'
import { describe, test } from 'node:test'
import { equal, deepEqual, throws } from 'node:assert/strict'
import { minifyCSS, Testable } from '../minifyCSS.js'


test('Acceptance', () => {
equal(minifyCSS(`
describe('minifyCSS', () => {
test('Acceptance', () => {
equal(minifyCSS(`
:root {
--navBG: #000;
--accentLightestMoreMore: #dbdcff;
Expand All @@ -28,28 +29,28 @@ test('Acceptance', () => {
.h { color: var(--late) }
.i { color: var(--doubly_N) }
`),
`.a{color:green}.b{color:orange}.c{color:red}.d{color:rgb(255,255,0)}.e{color:#111}.f{color:blue}.g{color:teal}.h{color:yellow}.i{color:yellow}`)
})
`.a{color:green}.b{color:orange}.c{color:red}.d{color:rgb(255,255,0)}.e{color:#111}.f{color:blue}.g{color:teal}.h{color:yellow}.i{color:yellow}`)
})

test('Comments', () => {
testRegexMatchesGetDeleted(Testable.BlockComments, `
test('Comments', () => {
testRegexMatchesGetDeleted(Testable.BlockComments, `
/* Foo */
/* Multiline line 1
Line 2 */
.a { color: red; } /* Bar */
/*/*/
`,
`
`
.a { color: red; }
`)
})
})


test('Trimming', () => {
testRegexMatchesGetDeleted(Testable.LeadingAndTrailingWhitespace, `
test('Trimming', () => {
testRegexMatchesGetDeleted(Testable.LeadingAndTrailingWhitespace, `
.b .c {
color: orange;
width: 20px;
Expand All @@ -58,80 +59,80 @@ test('Trimming', () => {
height: 30px;
}
`,
`.b .c {
`.b .c {
color: orange;
width: 20px;
}
.d {
height: 30px;
}`)
})
})


test('Inner Prop Value space', () => {
testRegexMatchesGetDeleted(Testable.PropValueWhitespaceSeparator, `
test('Inner Prop Value space', () => {
testRegexMatchesGetDeleted(Testable.PropValueWhitespaceSeparator, `
.e {
color: #f00;
height: 100px;
width: 100px;
content: 'a';
}
`,
`
`
.e {
color:#f00;
height:100px;
width:100px;
content:'a';
}
`)
})
})


test('Newlines', () => {
testRegexMatchesGetDeleted(Testable.Newlines, `
test('Newlines', () => {
testRegexMatchesGetDeleted(Testable.Newlines, `
.f {
color: blue;
height: 300px;
width: 300px;
}
`,
`.f { color: blue; height: 300px; width: 300px;}`)
})
`.f { color: blue; height: 300px; width: 300px;}`)
})


test('White spaces before braces', () => {
testRegexMatchesGetDeleted(Testable.WhitespaceBeforeBraces, `
test('White spaces before braces', () => {
testRegexMatchesGetDeleted(Testable.WhitespaceBeforeBraces, `
.g { color: pink; width: 400px; } `,
`
`
.g{ color: pink; width: 400px;} `)
})
})


test('White spaces after braces', () => {
testRegexMatchesGetDeleted(Testable.WhitespaceAfterBraces, `
test('White spaces after braces', () => {
testRegexMatchesGetDeleted(Testable.WhitespaceAfterBraces, `
.G { color: green; width: 410px; } `,
`
`
.G {color: green; width: 410px; }`)
})
})

test('Final semicolon', () => {
testRegexMatchesGetDeleted(Testable.LastSemicolonInSet,
'.h {color: cyan; width: 500px;}',
'.h {color: cyan; width: 500px}'
)
})
test('Final semicolon', () => {
testRegexMatchesGetDeleted(Testable.LastSemicolonInSet,
'.h {color: cyan; width: 500px;}',
'.h {color: cyan; width: 500px}'
)
})

test('Comma + Space', () => {
testRegexMatchesGetDeleted(Testable.SpacesAfterComma,
'.H { color: rgb(255, 255, 0); }',
'.H { color: rgb(255,255,0); }'
)
})
test('Comma + Space', () => {
testRegexMatchesGetDeleted(Testable.SpacesAfterComma,
'.H { color: rgb(255, 255, 0); }',
'.H { color: rgb(255,255,0); }'
)
})


test('Finds variables definitions', () => {
const inCSS = `
test('Finds variables definitions', () => {
const inCSS = `
:root {
--foo: 100px; /* @Comment */
--fooBar: red;--inline: coral;
Expand All @@ -141,20 +142,20 @@ section {
--bar: 300px;
--tom: 400px
}`
const expected = [
['--foo', '100px'],
['--fooBar', 'red'],
['--inline', 'coral'],
['--hashed', '#000'],
['--bar', '300px'],
['--tom', '400px']
]
deepEqual(Array.from(Testable.findVariablesDefinitions(inCSS)), expected)
})


test('Inline Vars', () => {
const inCSS = `
const expected = [
['--foo', '100px'],
['--fooBar', 'red'],
['--inline', 'coral'],
['--hashed', '#000'],
['--bar', '300px'],
['--tom', '400px']
]
deepEqual(Array.from(Testable.findVariablesDefinitions(inCSS)), expected)
})


test('Inline Vars', () => {
const inCSS = `
:root {
--foo: 100px;
--fooBar: 200px;
Expand All @@ -164,30 +165,31 @@ test('Inline Vars', () => {
height: var(--fooBar);
left: var( --fooBar );
}`
const outCSS = `
const outCSS = `
:root {}
.a {
width: 100px;
height: 200px;
left: 200px;
}`
equal(Testable.inlineVars(inCSS), outCSS)
})
equal(Testable.inlineVars(inCSS), outCSS)
})


test('Circular defintions throw', () => throws(() =>
Testable.inlineVars(`
test('Circular defintions throw', () => throws(() =>
Testable.inlineVars(`
:root {
--circleA: var(--circleB);
--circleB: var(--circleA);
}`)))


test('Undefined variable throws', () => throws(() =>
Testable.inlineVars(`
test('Undefined variable throws', () => throws(() =>
Testable.inlineVars(`
.a { color: var(--undefB) }`), /^Missing CSS variable --undefB$/))


function testRegexMatchesGetDeleted(regex, input, expected) {
equal(input.replace(regex, ''), expected)
}
function testRegexMatchesGetDeleted(regex, input, expected) {
equal(input.replace(regex, ''), expected)
}
})
12 changes: 7 additions & 5 deletions static-pages-bundler/tests/minifyHTML.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import test from 'node:test'
import { describe, test } from 'node:test'
import { equal } from 'node:assert/strict'
import { minifyHTML } from '../minifyHTML.js'


test('minifyHTML', () => {
equal(minifyHTML(`
describe('minifyHTML', () => {
test('Acceptance', () => {
equal(minifyHTML(`
<!DOCTYPE html>
<html
lang="en">
Expand Down Expand Up @@ -39,7 +40,7 @@ test('minifyHTML', () => {
</html>
`),

`<!DOCTYPE html>
`<!DOCTYPE html>
<html lang="en">
<head>
<title>Foo Bar</title>
Expand All @@ -66,5 +67,6 @@ Removes leading tabs.
</p>
</body>
</html>`
)
)
})
})
Loading

0 comments on commit 4ee7604

Please sign in to comment.