-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- use json-to-ast to find a location of test in json file (no more dirty hacks) - check for test name duplication (common problem that we can avoid now, found and fixed few duplicates) - tests with same name but variations can be grouped in array (for instance much easier to describe error tests)
- Loading branch information
Showing
7 changed files
with
179 additions
and
118 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,56 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var parseJSON = require('json-to-ast'); | ||
|
||
function JsonLocator(filename) { | ||
this.content = fs.readFileSync(filename, 'utf-8'); | ||
this.filename = path.relative(__dirname + '/../..', filename); | ||
this.map = Object.create(null); | ||
|
||
var ast = parseJSON(fs.readFileSync(filename, 'utf-8')); | ||
|
||
if (ast && ast.type === 'object') { | ||
for (var i = 0; i < ast.properties.length; i++) { | ||
var property = ast.properties[i]; | ||
|
||
if (hasOwnProperty.call(this.map, property.key.value)) { | ||
throw new Error('Duplicate key `' + property.key.value + '` in ' + this.getLocation(property.key.position.start)); | ||
} | ||
|
||
// use JSON.parse to unescape chars | ||
this.map[JSON.parse('"' + property.key.value + '"')] = { | ||
loc: this.getLocation(property.key.position.start), | ||
value: property.value | ||
}; | ||
} | ||
} | ||
} | ||
|
||
JsonLocator.prototype.get = function(name) { | ||
var lines = this.content.split('"' + name + '"')[0].split('\n'); | ||
JsonLocator.prototype.getLocation = function(loc) { | ||
return [ | ||
this.filename, | ||
lines.length, | ||
lines.pop().length + name.length + 5 | ||
].join(':') + ' (' + name + ')'; | ||
loc.line, | ||
loc.column | ||
].join(':'); | ||
}; | ||
|
||
JsonLocator.prototype.get = function(name, index) { | ||
var loc; | ||
|
||
if (hasOwnProperty.call(this.map, name) === false) { | ||
throw new Error('Key `' + name + '` not found in ' + this.filename); | ||
} | ||
|
||
if (typeof index === 'number' && this.map[name].value.type === 'array') { | ||
if (index in this.map[name].value.items === false) { | ||
throw new Error('Wrong index `' + index + '` for `' + name + '` in ' + this.filename); | ||
} | ||
loc = this.getLocation(this.map[name].value.items[index].position.start); | ||
name += ' #' + index; | ||
} else { | ||
loc = this.map[name].loc; | ||
} | ||
|
||
return loc + ' (' + name + ')'; | ||
}; | ||
|
||
module.exports = JsonLocator; |
This file contains 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