much better accuracy

This commit is contained in:
John Bintz 2011-09-12 15:38:40 -04:00
parent 84a87d423c
commit 262b7a8223
3 changed files with 70 additions and 47 deletions

View File

@ -42,6 +42,26 @@ jasmine.Spec.prototype.fail = (e) ->
})
@results_.addResult(expectationResult)
jasmine.NestedResults.isValidSpecLine = (line) ->
line.match(/^\s*expect/) != null || line.match(/^\s*return\s*expect/) != null
jasmine.NestedResults.parseFunction = (func) ->
lines = []
lineCount = 0
for line in func.split("\n")
if jasmine.NestedResults.isValidSpecLine(line)
line = line.replace(/^\s*/, '').replace(/\s*$/, '').replace(/^return\s*/, '')
lines.push([line, lineCount])
lineCount += 1
lines
jasmine.NestedResults.parseAndStore = (func) ->
if !jasmine.NestedResults.ParsedFunctions[func]
jasmine.NestedResults.ParsedFunctions[func] = jasmine.NestedResults.parseFunction(func)
jasmine.NestedResults.ParsedFunctions[func]
jasmine.NestedResults.ParsedFunctions = []
if !jasmine.WaitsBlock.prototype._execute
jasmine.WaitsBlock.prototype._execute = jasmine.WaitsBlock.prototype.execute
jasmine.WaitsForBlock.prototype._execute = jasmine.WaitsForBlock.prototype.execute
@ -56,30 +76,13 @@ if !jasmine.WaitsBlock.prototype._execute
jasmine.WaitsForBlock.prototype.execute = pauseAndRun
jasmine.NestedResults.prototype.addResult_ = jasmine.NestedResults.prototype.addResult
jasmine.NestedResults.ParsedFunctions = []
jasmine.NestedResults.prototype.addResult = (result) ->
result.expectations = []
# always three up?
lineCount = 0
functionSignature = arguments.callee.caller.caller.caller.toString()
if !jasmine.NestedResults.ParsedFunctions[functionSignature]
lines = []
for line in functionSignature.split("\n")
if line.match(/^\s*expect/)
line = line.replace(/^\s*/, '').replace(/\s*$/, '')
lines.push(line)
lineCount += 1
jasmine.NestedResults.ParsedFunctions[functionSignature] = lines
result.expectations = jasmine.NestedResults.ParsedFunctions[functionSignature]
result.expectations = jasmine.NestedResults.parseAndStore(arguments.callee.caller.caller.caller.toString())
this.addResult_(result)
jasmine.ExpectationResult.prototype.line = ->
if @expectations && @lineNumber then @expectations[@lineNumber] else ''
# Try to get the line number of a failed spec
class window.HeadlessReporterResult
@ -96,7 +99,7 @@ class window.HeadlessReporterResult
for result in @results
output = result.message
if result.lineNumber
output += " (line ~#{bestChoice.lineNumber + result.lineNumber})\n #{result.line()}"
output += " (line ~#{bestChoice.lineNumber + result.lineNumber})\n #{result.line}"
JHW.printResult(output)
@findSpecLine: (splitName) ->
bestChoice = { accuracy: 0, file: null, lineNumber: null }
@ -155,7 +158,7 @@ class jasmine.HeadlessReporter
for result in results.getItems()
if result.type == 'expect' and !result.passed_
if foundLine = result.expectations[testCount - 1]
result.lineNumber = testCount - 1
[ result.line, result.lineNumber ] = foundLine
failureResult.addResult(result)
testCount += 1
@results.push(failureResult)

View File

@ -48,6 +48,31 @@
});
return this.results_.addResult(expectationResult);
};
jasmine.NestedResults.isValidSpecLine = function(line) {
return line.match(/^\s*expect/) !== null || line.match(/^\s*return\s*expect/) !== null;
};
jasmine.NestedResults.parseFunction = function(func) {
var line, lineCount, lines, _i, _len, _ref;
lines = [];
lineCount = 0;
_ref = func.split("\n");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
line = _ref[_i];
if (jasmine.NestedResults.isValidSpecLine(line)) {
line = line.replace(/^\s*/, '').replace(/\s*$/, '').replace(/^return\s*/, '');
lines.push([line, lineCount]);
}
lineCount += 1;
}
return lines;
};
jasmine.NestedResults.parseAndStore = function(func) {
if (!jasmine.NestedResults.ParsedFunctions[func]) {
jasmine.NestedResults.ParsedFunctions[func] = jasmine.NestedResults.parseFunction(func);
}
return jasmine.NestedResults.ParsedFunctions[func];
};
jasmine.NestedResults.ParsedFunctions = [];
if (!jasmine.WaitsBlock.prototype._execute) {
jasmine.WaitsBlock.prototype._execute = jasmine.WaitsBlock.prototype.execute;
jasmine.WaitsForBlock.prototype._execute = jasmine.WaitsForBlock.prototype.execute;
@ -61,35 +86,11 @@
jasmine.WaitsBlock.prototype.execute = pauseAndRun;
jasmine.WaitsForBlock.prototype.execute = pauseAndRun;
jasmine.NestedResults.prototype.addResult_ = jasmine.NestedResults.prototype.addResult;
jasmine.NestedResults.ParsedFunctions = [];
jasmine.NestedResults.prototype.addResult = function(result) {
var functionSignature, line, lineCount, lines, _i, _len, _ref;
result.expectations = [];
lineCount = 0;
functionSignature = arguments.callee.caller.caller.caller.toString();
if (!jasmine.NestedResults.ParsedFunctions[functionSignature]) {
lines = [];
_ref = functionSignature.split("\n");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
line = _ref[_i];
if (line.match(/^\s*expect/)) {
line = line.replace(/^\s*/, '').replace(/\s*$/, '');
lines.push(line);
}
lineCount += 1;
}
jasmine.NestedResults.ParsedFunctions[functionSignature] = lines;
}
result.expectations = jasmine.NestedResults.ParsedFunctions[functionSignature];
result.expectations = jasmine.NestedResults.parseAndStore(arguments.callee.caller.caller.caller.toString());
return this.addResult_(result);
};
jasmine.ExpectationResult.prototype.line = function() {
if (this.expectations && this.lineNumber) {
return this.expectations[this.lineNumber];
} else {
return '';
}
};
}
window.HeadlessReporterResult = (function() {
function HeadlessReporterResult(name, splitName) {
@ -114,7 +115,7 @@
result = _ref[_i];
output = result.message;
if (result.lineNumber) {
output += " (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")\n " + (result.line());
output += " (line ~" + (bestChoice.lineNumber + result.lineNumber) + ")\n " + result.line;
}
_results.push(JHW.printResult(output));
}
@ -204,7 +205,7 @@
result = _ref[_i];
if (result.type === 'expect' && !result.passed_) {
if (foundLine = result.expectations[testCount - 1]) {
result.lineNumber = testCount - 1;
result.line = foundLine[0], result.lineNumber = foundLine[1];
}
failureResult.addResult(result);
}

View File

@ -94,3 +94,22 @@ describe 'jasmine.WaitsBlock and jasmine.WaitsForBlock', ->
runs ->
expect(true).toEqual(true)
describe 'jasmine.NestedResults.isValidSpecLine', ->
it 'should check the lines', ->
expect(jasmine.NestedResults.isValidSpecLine('yes')).toEqual(false)
expect(jasmine.NestedResults.isValidSpecLine('expect')).toEqual(true)
expect(jasmine.NestedResults.isValidSpecLine(' expect')).toEqual(true)
expect(jasmine.NestedResults.isValidSpecLine('return expect')).toEqual(true)
expect(jasmine.NestedResults.isValidSpecLine(' return expect')).toEqual(true)
describe 'jasmine.nestedResults.parseFunction', ->
it 'should parse the function', ->
expect(jasmine.NestedResults.parseFunction("""
test
expect("cat")
return expect("dog")
""")).toEqual([
[ 'expect("cat")', 1 ],
[ 'expect("dog")', 2 ]
])