|
| 1 | +const {range, zip, escape} = require('lodash'); |
| 2 | + |
| 3 | +// this test file is designed to be run with Mocha |
| 4 | +const assert = require('assert') |
| 5 | +const fs = require('fs') |
| 6 | +const ok = specify |
| 7 | +const { spawnSync } = require('node:child_process') |
| 8 | + |
| 9 | +describe('check spans for Yaml', function () { |
| 10 | + testSpans('test/fixtures/test.yml') |
| 11 | +}) |
| 12 | + |
| 13 | +describe('check spans for Asciidoc', function () { |
| 14 | + testSpans('test/fixtures/basic.adoc') |
| 15 | +}) |
| 16 | + |
| 17 | +function testSpans(file) { |
| 18 | + const vale = runVale(file) |
| 19 | + |
| 20 | + const text = fs.readFileSync(file).toString() |
| 21 | + const lines = text.match(/^.*?(\n|$)/gm) |
| 22 | + |
| 23 | + for (const item of vale[file]) { |
| 24 | + const line = lines[item.Line - 1] |
| 25 | + const [from,to] = item.Span |
| 26 | + const span = line.substr(from - 1, to-from+1) |
| 27 | + |
| 28 | + ok(`Match ${item.Match}`, function () { |
| 29 | + assert.equal(item.Match, span) |
| 30 | + console.log({line, span, match: item.Match}) |
| 31 | + }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function runVale(file) { |
| 36 | + let vale |
| 37 | + try { |
| 38 | + vale = spawnSync( |
| 39 | + 'vale', |
| 40 | + [ |
| 41 | + '--output', 'JSON', |
| 42 | + file |
| 43 | + ] |
| 44 | + ) |
| 45 | + } |
| 46 | + catch (err) { |
| 47 | + console.log("Failed to run vale", err) |
| 48 | + } |
| 49 | + |
| 50 | + return JSON.parse(vale.stdout) |
| 51 | +} |
0 commit comments