-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattendant.js
83 lines (70 loc) · 2.69 KB
/
attendant.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var test = require('tape').test
function Attendant (options) {
this.test = test
this.document = getDocument(options.file)
this.TurndownService = options.TurndownService
this.beforeEach = options.beforeEach || function id (input) { return input }
}
Attendant.prototype = {
run: function () {
var testCases = this.document.querySelectorAll('.case')
for (var i = 0; i < testCases.length; i++) {
runTestCase.call(this, testCases[i], { beforeEach: this.beforeEach })
}
}
}
function getDocument (file) {
if (typeof window === 'undefined') {
var JSDOM = require('jsdom').JSDOM
var fs = require('fs')
var dom = new JSDOM(fs.readFileSync(file).toString())
return dom.window.document
} else {
return document
}
}
function runTestCase (testCase, options) {
var testCaseName = testCase.getAttribute('data-name')
var jsonOptions = testCase.getAttribute('data-options')
var turndownServiceOptions = jsonOptions ? JSON.parse(jsonOptions) : {}
var turndownService = new this.TurndownService(turndownServiceOptions)
options.beforeEach(turndownService)
var inputElement = testCase.querySelector('.input')
var expectedElement = testCase.querySelector('.expected')
var expected = expectedElement.textContent
var output = turndownService.turndown(inputElement)
var outputElement = this.document.createElement('pre')
outputElement.className = 'output'
testCase.insertBefore(outputElement, inputElement.nextSibling)
outputElement.textContent = output
var outputHeading = this.document.createElement('h3')
outputHeading.className = 'output-heading'
outputHeading.textContent = 'output'
testCase.insertBefore(outputHeading, outputElement)
var heading = this.document.createElement('h2')
heading.className = 'test-case-heading'
heading.textContent = testCaseName
testCase.insertBefore(heading, inputElement)
var inputHeading = this.document.createElement('h3')
inputHeading.className = 'input-heading'
inputHeading.textContent = 'Input'
testCase.insertBefore(inputHeading, inputElement)
var expectedHeading = this.document.createElement('h3')
expectedHeading.className = 'expected-heading'
expectedHeading.textContent = 'Expected Output'
testCase.insertBefore(expectedHeading, expectedElement)
if (output !== expected) {
expectedElement.className += ' expected--err'
expectedHeading.className += ' expected-heading--err'
outputElement.className += ' output--err'
}
this.test(testCaseName + ' (DOM)', function (t) {
t.plan(1)
t.equal(output, expected)
})
this.test(testCaseName + ' (string)', function (t) {
t.plan(1)
t.equal(turndownService.turndown(inputElement.innerHTML), expected)
})
}
module.exports = Attendant