Created
January 14, 2014 13:48
-
-
Save jesslilly/8418545 to your computer and use it in GitHub Desktop.
Parameterized jasmine test for your logax parser. https://github.com/jesslilly/logax. This is a really good idea to make sure that minor changes to your parser don't break your JSON output. Thanks to https://gist.github.com/basti1302/5051200 for the parameterized test gist. This test is in javascript, not coffeescript. This is a template. You wi…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var exec = require('child_process').exec, child; | |
// Inputs | |
var inputDir = './developer/test/unit/data/input/'; | |
var expectedDir = './developer/test/unit/data/expected/'; | |
var outputDir = './developer/test/unit/data/output/'; | |
var parserA = './server/load-it/logax-a-parser.js'; | |
var parserB = './server/load-it/logax-b-parser.js'; | |
var inputFiles = new Array(3); | |
// These are the files to test. | |
inputFiles.push({ | |
file : 'file1', | |
parser : parserA | |
}); | |
inputFiles.push({ | |
file : 'file2', | |
parser : parserA | |
}); | |
inputFiles.push({ | |
file : 'file3', | |
parser : parserB | |
}); | |
// Declare a parameterized test (keepin it DRY baby) | |
jsonDiff = function(inputFile, parser) { | |
describe('logax parse of ' + inputFile, function() { | |
var asyncFinished = false; | |
var fileData = ""; | |
var cmd = ""; | |
cmd += "gunzip " + inputDir + inputFile + ".gz"; | |
cmd += "; "; | |
cmd += "$(npm bin)/logax --parserFile " + parser + " "; | |
cmd += "--input " + inputDir + inputFile + " "; | |
cmd += "--outputDir " + outputDir; | |
cmd += " > " + outputDir + inputFile + '.log 2>&1'; | |
cmd += "; "; | |
cmd += "gzip " + inputDir + inputFile + "; " | |
child = exec(cmd, function(error, stdout, stderr) { | |
if (error) { | |
console.info(error); | |
} | |
asyncFinished = true; | |
}); | |
it('should create expected json.', function() { | |
waitsFor(function() { | |
return asyncFinished; | |
}, "logax.js never completed. Check for missing callback.", 10000); | |
runs(function() { | |
var expectedArray = JSON.parse(fs.readFileSync(expectedDir + inputFile + ".json")); | |
expectedArray.forEach(function(row) { | |
delete row.createdAt; | |
delete row.updatedAt; | |
}); | |
var outputArray = JSON.parse(fs.readFileSync(outputDir + inputFile + ".json")); | |
outputArray.forEach(function(row) { | |
delete row.createdAt; | |
delete row.updatedAt; | |
}); | |
// expect(expectedArray).toEqual(outputArray); | |
expect(expectedArray.length).toEqual(outputArray.length); | |
expectedArray.forEach(function(expectedObj, idx) { | |
var outputObj = outputArray[idx]; | |
var expectedKeys = Object.keys(expectedObj); | |
var outputKeys = Object.keys(outputObj); | |
expect(expectedKeys.sort()).toEqual(outputKeys.sort()); | |
expectedKeys.forEach(function(row) { | |
var label = "arr[" + idx + "][" + row + "]="; | |
expect(label + expectedObj[row]).toEqual(label + outputObj[row]); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}; | |
// Run all inputs against the parameterized tests. | |
inputFiles.forEach(function(row, idx) { | |
jsonDiff(row.file, row.parser); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment