Skip to content

Commit 07852ca

Browse files
Revert test to CommonJS format for compatibility
1 parent 4936f4c commit 07852ca

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require('tap').mochaGlobals()
2+
const assert = require('assert')
3+
const remark = require('remark');
4+
5+
const frontmatter = require('remark-frontmatter')
6+
const extract = require('remark-extract-frontmatter')
7+
const yaml = require('yaml').parse
8+
const strip = require('strip-markdown')
9+
const vfile = require('to-vfile')
10+
const syllable = require('syllable')
11+
const fs = require('fs')
12+
13+
const dir = './_haikus/'
14+
const blockList = ['haikus.md']
15+
16+
const files = fs.readdirSync(dir)
17+
18+
files.forEach(async (file) => {
19+
if (!blockList.includes(file)) {
20+
const [text, meta] = await processMarkdown(dir + file)
21+
const lines = text.split('\n').filter((line) => line !== '')
22+
23+
if (meta.test !== false) {
24+
validateHaiku(file, lines, meta)
25+
}
26+
}
27+
})
28+
29+
function processMarkdown(filename) {
30+
return new Promise((resolve, reject) => {
31+
remark()
32+
.use(frontmatter)
33+
.use(extract, { yaml: yaml })
34+
.use(strip)
35+
.process(vfile.readSync(filename), (err, file) => {
36+
if (err) {
37+
reject(err)
38+
} else {
39+
resolve([file.toString(), file.data])
40+
}
41+
})
42+
})
43+
}
44+
45+
function validateHaiku(filename, lines, meta) {
46+
describe(filename, () => {
47+
it("should have a '.md' file extension", () => {
48+
assert.ok(/\.md$/.test(filename), "extension does not match '.md'")
49+
})
50+
51+
describe('file metadata', () => {
52+
it("should have layout equal to 'haiku'", () => {
53+
assert.equal(
54+
meta.layout,
55+
'haiku',
56+
"layout metadata should equal 'haiku'"
57+
)
58+
})
59+
60+
it('should have non-blank title', () => {
61+
assert.equal(typeof meta.title, 'string', 'title metadata is missing')
62+
})
63+
64+
it('should have non-blank author', () => {
65+
assert.equal(typeof meta.author, 'string', 'author metadata is missing')
66+
})
67+
})
68+
69+
describe('haiku structure', () => {
70+
it('should have three lines', () => {
71+
assert.equal(lines.length, 3)
72+
})
73+
74+
it('should have five syllables on the first line', () => {
75+
assert.equal(syllable(lines[0]), 5)
76+
})
77+
78+
it('should have seven syllables on the second line', () => {
79+
assert.equal(syllable(lines[1]), 7)
80+
})
81+
82+
it('should have five syllables on the third line', () => {
83+
assert.equal(syllable(lines[2]), 5)
84+
})
85+
})
86+
})
87+
}

0 commit comments

Comments
 (0)