better finding of spec lines

This commit is contained in:
John Bintz 2011-07-14 10:54:44 -04:00
parent 3c4e557517
commit e72eaef7a1
8 changed files with 76 additions and 23 deletions

View File

@ -16,6 +16,11 @@ guard 'rspec', :version => 2, :all_on_start => false do
watch('spec/spec_helper.rb') { "spec" } watch('spec/spec_helper.rb') { "spec" }
end end
guard 'jasmine-headless-webkit', :all_on_start => false do
watch(%r{^spec/javascripts/.+_spec\.coffee})
watch(%r{^jasmine/(.+)\.coffee$}) { |m| "spec/javascripts/#{m[1]}_spec.coffee" }
end
def compile def compile
system %{cd ext/jasmine-webkit-specrunner && ruby extconf.rb} system %{cd ext/jasmine-webkit-specrunner && ruby extconf.rb}
end end

View File

@ -1,7 +1,7 @@
if !jasmine? if !jasmine?
throw new Error("jasmine not laoded!") throw new Error("jasmine not laoded!")
class HeadlessReporterResult class window.HeadlessReporterResult
constructor: (@name, @splitName) -> constructor: (@name, @splitName) ->
@results = [] @results = []
addResult: (message) -> addResult: (message) ->
@ -18,11 +18,21 @@ class HeadlessReporterResult
_findSpecLine: -> _findSpecLine: ->
bestChoice = { accuracy: 0, file: null, lineNumber: null } bestChoice = { accuracy: 0, file: null, lineNumber: null }
for file, lines of SPEC_LINE_NUMBERS for file, lines of HeadlessReporterResult.specLineNumbers
index = 0 index = 0
while newLineNumber = lines[@splitName[index]] lineNumber = 0
while newLineNumberInfo = lines[@splitName[index]]
if newLineNumberInfo.length == 0
lineNumber = newLineNumberInfo[0]
else
lastLine = null
for line in newLineNumberInfo
lastLine = line
break if line > lineNumber
lineNumber = lastLine
index++ index++
lineNumber = newLineNumber
if index > bestChoice.accuracy if index > bestChoice.accuracy
bestChoice = { accuracy: index, file: file, lineNumber: lineNumber } bestChoice = { accuracy: index, file: file, lineNumber: lineNumber }

View File

@ -1,10 +1,9 @@
(function() { (function() {
var HeadlessReporterResult;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
if (!(typeof jasmine !== "undefined" && jasmine !== null)) { if (!(typeof jasmine !== "undefined" && jasmine !== null)) {
throw new Error("jasmine not laoded!"); throw new Error("jasmine not laoded!");
} }
HeadlessReporterResult = (function() { window.HeadlessReporterResult = (function() {
function HeadlessReporterResult(name, splitName) { function HeadlessReporterResult(name, splitName) {
this.name = name; this.name = name;
this.splitName = splitName; this.splitName = splitName;
@ -32,18 +31,32 @@
return _results; return _results;
}; };
HeadlessReporterResult.prototype._findSpecLine = function() { HeadlessReporterResult.prototype._findSpecLine = function() {
var bestChoice, file, index, lineNumber, lines, newLineNumber; var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref;
bestChoice = { bestChoice = {
accuracy: 0, accuracy: 0,
file: null, file: null,
lineNumber: null lineNumber: null
}; };
for (file in SPEC_LINE_NUMBERS) { _ref = HeadlessReporterResult.specLineNumbers;
lines = SPEC_LINE_NUMBERS[file]; for (file in _ref) {
lines = _ref[file];
index = 0; index = 0;
while (newLineNumber = lines[this.splitName[index]]) { lineNumber = 0;
while (newLineNumberInfo = lines[this.splitName[index]]) {
if (newLineNumberInfo.length === 0) {
lineNumber = newLineNumberInfo[0];
} else {
lastLine = null;
for (_i = 0, _len = newLineNumberInfo.length; _i < _len; _i++) {
line = newLineNumberInfo[_i];
lastLine = line;
if (line > lineNumber) {
break;
}
}
lineNumber = lastLine;
}
index++; index++;
lineNumber = newLineNumber;
} }
if (index > bestChoice.accuracy) { if (index > bestChoice.accuracy) {
bestChoice = { bestChoice = {

View File

@ -21,11 +21,15 @@ module Jasmine
class << self class << self
def get_spec_line_numbers(file) def get_spec_line_numbers(file)
Hash[file.lines.each_with_index.collect { |line, index| line_numbers = {}
file.lines.each_with_index.each { |line, index|
if description = line[%r{(describe|context|it)[( ]*(["'])(.*)\2}, 3] if description = line[%r{(describe|context|it)[( ]*(["'])(.*)\2}, 3]
[ description, index + 1 ] (line_numbers[description] ||= []) << (index + 1)
end end
}.compact] }
line_numbers
end end
end end

View File

@ -34,7 +34,7 @@ module Jasmine
</script> </script>
#{files.join("\n")} #{files.join("\n")}
<script type="text/javascript"> <script type="text/javascript">
SPEC_LINE_NUMBERS = #{MultiJson.encode(spec_lines)}; HeadlessReporterResult.specLineNumbers = #{MultiJson.encode(spec_lines)};
</script> </script>
</head> </head>
<body> <body>

View File

@ -0,0 +1,16 @@
describe 'HeadlessReporterResult', ->
beforeEach ->
HeadlessReporterResult.specLineNumbers = {
'one': {
'name': [ 1 ],
'of': [ 2, 9 ],
'test': [ 3, 10 ],
'other': [ 7 ]
}
}
it 'should find the best spec lines', ->
result = new HeadlessReporterResult('test', [ 'name', 'of', 'test' ])
expect(result._findSpecLine().lineNumber).toEqual(3)
result = new HeadlessReporterResult('test', [ 'other', 'of', 'test' ])
expect(result._findSpecLine().lineNumber).toEqual(10)

View File

@ -0,0 +1,5 @@
src_files: [ 'jasmine/*.coffee' ]
spec_files: [ 'spec/javascripts/*_spec.coffee' ]
src_dir: .
spec_dir: .

View File

@ -223,9 +223,9 @@ describe 'test', ->
end end
it 'should get the line numbers' do it 'should get the line numbers' do
line_numbers['test'].should == 1 line_numbers['test'].should == [ 1 ]
line_numbers['yes'].should == 2 line_numbers['yes'].should == [ 2 ]
line_numbers['should do something'].should == 3 line_numbers['should do something'].should == [ 3 ]
end end
end end
@ -243,9 +243,9 @@ describe('test', function() {
end end
it 'should get the line numbers' do it 'should get the line numbers' do
line_numbers['test'].should == 1 line_numbers['test'].should == [ 1 ]
line_numbers['yes'].should == 2 line_numbers['yes'].should == [ 2 ]
line_numbers['should do something'].should == 3 line_numbers['should do something'].should == [ 3 ]
end end
end end
end end
@ -259,13 +259,13 @@ describe('test', function() {
'test2.coffee' 'test2.coffee'
]) ])
File.open('test.coffee', 'w') { |fh| fh.print "describe('cat')" } File.open('test.coffee', 'w') { |fh| fh.print "describe('cat')\ndescribe('cat')" }
File.open('test2.coffee', 'w') { |fh| fh.print "no matches" } File.open('test2.coffee', 'w') { |fh| fh.print "no matches" }
end end
it 'should generate filenames and line number info' do it 'should generate filenames and line number info' do
files_list.spec_file_line_numbers.should == { files_list.spec_file_line_numbers.should == {
'test.coffee' => { 'cat' => 1 } 'test.coffee' => { 'cat' => [ 1, 2 ] }
} }
end end
end end