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