Skip to content

Commit

Permalink
Added re-formatting test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisIsManta committed Oct 21, 2017
1 parent 453311b commit e9548f7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
spec/**/debugging.json
spec/**/actual.styl
spec/**/*-debugging.json
spec/**/*-formatted.styl
*.log
121 changes: 76 additions & 45 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,51 @@ const filesAndDirectories = _.chain(ps.argv.length > 2 ? ps.argv.slice(2) : ['*'
const filesOnly = path => pt.extname(path) === '.js'
const directoriesOnly = path => pt.extname(path) === ''
const createComparableLines = text => text.replace(/\r/g, '¶').replace(/\t/g, '→').replace(/^\s+/gm, spaces => _.repeat('·', spaces.length)).split('\n')
const createComparisonTest = (actualContent, expectContent, stack) => {
const resultLines = createComparableLines(actualContent)
const expectLines = createComparableLines(expectContent)

let lineIndex = -1
const lineLimit = Math.min(resultLines.length, expectLines.length)
while (++lineIndex < Math.min(resultLines.length, expectLines.length)) {
if (resultLines[lineIndex] !== expectLines[lineIndex]) {
let diffs = ''
let charIndex = -1
const charLimit = Math.max(resultLines[lineIndex].length, expectLines[lineIndex].length)
while (++charIndex < charLimit) {
if (resultLines[lineIndex][charIndex] !== expectLines[lineIndex][charIndex]) {
diffs += '^'
} else {
diffs += ' '
}
}

return fail({
message: [
'The first mismatched was at line ' + (lineIndex + 1) + '.',
' Actual: ' + resultLines[lineIndex],
' Expect: ' + expectLines[lineIndex],
' ' + diffs
].join('\n'),
stack
})
}
}

return fail({
message: 'It was not clear to show the difference. Please check out the files below.',
stack
})
}

filesAndDirectories.filter(directoriesOnly).forEach(directory => {
const inputFilePath = pt.join(directory, 'input.styl')
const optionFilePath = pt.join(directory, 'formattingOptions.json')
const inputFilePath = pt.join(directory, 'input.styl')
const inputFormattedFilePath = pt.join(directory, 'input-formatted.styl')
const inputDebuggingFilePath = pt.join(directory, 'input-debugging.json')
const outputFilePath = pt.join(directory, 'output.styl')
const actualFilePath = pt.join(directory, 'actual.styl')
const debuggingFilePath = pt.join(directory, 'debugging.json')
const outputFormattedFilePath = pt.join(directory, 'output-formatted.styl')
const outputDebuggingFilePath = pt.join(directory, 'output-debugging.json')

const inputContent = fs.readFileSync(inputFilePath, 'utf8')
const outputContent = fs.readFileSync(outputFilePath, 'utf8')
Expand All @@ -42,64 +80,31 @@ filesAndDirectories.filter(directoriesOnly).forEach(directory => {
const testSpecName = pt.basename(directory)
describe(testSpecName, () => {
it('can be formatted', () => {
if (fs.existsSync(actualFilePath)) fs.unlinkSync(actualFilePath)
if (fs.existsSync(inputFormattedFilePath)) fs.unlinkSync(inputFormattedFilePath)

try {
const tree = new Stylus.Parser(inputContent).parse()
fs.writeFileSync(debuggingFilePath, JSON.stringify(tree, null, '\t'))
fs.writeFileSync(inputDebuggingFilePath, JSON.stringify(tree, null, '\t'))
} catch (ex) {
// Do nothing
}

const actualContent = format(inputContent, formattingOptions)

if (actualContent === outputContent) { // In case of success
if (actualContent === outputContent) {
expect(true).toBeTruthy()

} else { // In case of failure
fs.writeFileSync(actualFilePath, actualContent)
} else {
fs.writeFileSync(inputFormattedFilePath, actualContent)

const stack = [
inputFilePath,
actualFilePath,
inputFormattedFilePath,
inputDebuggingFilePath,
outputFilePath,
debuggingFilePath
].map(path => pt.resolve(path)).join('\n')

const resultLines = createComparableLines(actualContent)
const expectLines = createComparableLines(outputContent)

let lineIndex = -1
const lineLimit = Math.min(resultLines.length, expectLines.length)
while (++lineIndex < Math.min(resultLines.length, expectLines.length)) {
if (resultLines[lineIndex] !== expectLines[lineIndex]) {
let diffs = ''
let charIndex = -1
const charLimit = Math.max(resultLines[lineIndex].length, expectLines[lineIndex].length)
while (++charIndex < charLimit) {
if (resultLines[lineIndex][charIndex] !== expectLines[lineIndex][charIndex]) {
diffs += '^'
} else {
diffs += ' '
}
}

return fail({
message: [
'The first mismatched was at line ' + (lineIndex + 1) + '.',
' Actual: ' + resultLines[lineIndex],
' Expect: ' + expectLines[lineIndex],
' ' + diffs
].join('\n'),
stack
})
}
}

return fail({
message: 'It was not clear to show the difference. Please check out the files below.',
stack
})
createComparisonTest(actualContent, outputContent, stack)
}
})

Expand All @@ -108,12 +113,38 @@ filesAndDirectories.filter(directoriesOnly).forEach(directory => {
return null
}

it('can be re-parsed', () => {
/* xit('can be re-parsed', () => {
try {
new Stylus.Parser(outputContent).parse()
} catch (ex) {
fail(ex)
}
}) */

it('can be re-formatted', () => {
const actualContent = format(outputContent, formattingOptions)

try {
const tree = new Stylus.Parser(outputContent).parse()
fs.writeFileSync(outputDebuggingFilePath, JSON.stringify(tree, null, '\t'))
} catch (ex) {
// Do nothing
}

if (actualContent === outputContent) {
expect(true).toBeTruthy()

} else {
fs.writeFileSync(outputFormattedFilePath, actualContent)

stack = [
outputFilePath,
outputFormattedFilePath,
outputDebuggingFilePath,
].map(path => pt.resolve(path)).join('\n')

createComparisonTest(actualContent, outputContent, stack)
}
})
})
})
Expand Down

0 comments on commit e9548f7

Please sign in to comment.