From e72eaef7a123c241265209d0023d04eb538efa47 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 14 Jul 2011 10:54:44 -0400 Subject: [PATCH] better finding of spec lines --- Guardfile | 5 ++++ jasmine/jasmine.headless-reporter.coffee | 18 ++++++++++--- jasmine/jasmine.headless-reporter.js | 27 ++++++++++++++----- lib/jasmine/files_list.rb | 10 ++++--- lib/jasmine/template_writer.rb | 2 +- .../jasmine.headless-reporter_spec.coffee | 16 +++++++++++ spec/javascripts/support/jasmine.yml | 5 ++++ spec/lib/jasmine/files_list_spec.rb | 16 +++++------ 8 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 spec/javascripts/jasmine.headless-reporter_spec.coffee create mode 100644 spec/javascripts/support/jasmine.yml diff --git a/Guardfile b/Guardfile index f3a837a..0db8fe8 100644 --- a/Guardfile +++ b/Guardfile @@ -16,6 +16,11 @@ guard 'rspec', :version => 2, :all_on_start => false do watch('spec/spec_helper.rb') { "spec" } 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 system %{cd ext/jasmine-webkit-specrunner && ruby extconf.rb} end diff --git a/jasmine/jasmine.headless-reporter.coffee b/jasmine/jasmine.headless-reporter.coffee index d743a84..447c102 100644 --- a/jasmine/jasmine.headless-reporter.coffee +++ b/jasmine/jasmine.headless-reporter.coffee @@ -1,7 +1,7 @@ if !jasmine? throw new Error("jasmine not laoded!") -class HeadlessReporterResult +class window.HeadlessReporterResult constructor: (@name, @splitName) -> @results = [] addResult: (message) -> @@ -18,11 +18,21 @@ class HeadlessReporterResult _findSpecLine: -> bestChoice = { accuracy: 0, file: null, lineNumber: null } - for file, lines of SPEC_LINE_NUMBERS + for file, lines of HeadlessReporterResult.specLineNumbers 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++ - lineNumber = newLineNumber if index > bestChoice.accuracy bestChoice = { accuracy: index, file: file, lineNumber: lineNumber } diff --git a/jasmine/jasmine.headless-reporter.js b/jasmine/jasmine.headless-reporter.js index ea6f51a..d9b4eb5 100644 --- a/jasmine/jasmine.headless-reporter.js +++ b/jasmine/jasmine.headless-reporter.js @@ -1,10 +1,9 @@ (function() { - var HeadlessReporterResult; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; if (!(typeof jasmine !== "undefined" && jasmine !== null)) { throw new Error("jasmine not laoded!"); } - HeadlessReporterResult = (function() { + window.HeadlessReporterResult = (function() { function HeadlessReporterResult(name, splitName) { this.name = name; this.splitName = splitName; @@ -32,18 +31,32 @@ return _results; }; HeadlessReporterResult.prototype._findSpecLine = function() { - var bestChoice, file, index, lineNumber, lines, newLineNumber; + var bestChoice, file, index, lastLine, line, lineNumber, lines, newLineNumberInfo, _i, _len, _ref; bestChoice = { accuracy: 0, file: null, lineNumber: null }; - for (file in SPEC_LINE_NUMBERS) { - lines = SPEC_LINE_NUMBERS[file]; + _ref = HeadlessReporterResult.specLineNumbers; + for (file in _ref) { + lines = _ref[file]; 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++; - lineNumber = newLineNumber; } if (index > bestChoice.accuracy) { bestChoice = { diff --git a/lib/jasmine/files_list.rb b/lib/jasmine/files_list.rb index aed55de..9f0c8d3 100644 --- a/lib/jasmine/files_list.rb +++ b/lib/jasmine/files_list.rb @@ -21,11 +21,15 @@ module Jasmine class << self 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] - [ description, index + 1 ] + (line_numbers[description] ||= []) << (index + 1) end - }.compact] + } + + line_numbers end end diff --git a/lib/jasmine/template_writer.rb b/lib/jasmine/template_writer.rb index da51297..6a99ca4 100644 --- a/lib/jasmine/template_writer.rb +++ b/lib/jasmine/template_writer.rb @@ -34,7 +34,7 @@ module Jasmine #{files.join("\n")} diff --git a/spec/javascripts/jasmine.headless-reporter_spec.coffee b/spec/javascripts/jasmine.headless-reporter_spec.coffee new file mode 100644 index 0000000..4a54c12 --- /dev/null +++ b/spec/javascripts/jasmine.headless-reporter_spec.coffee @@ -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) diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml new file mode 100644 index 0000000..de1a551 --- /dev/null +++ b/spec/javascripts/support/jasmine.yml @@ -0,0 +1,5 @@ +src_files: [ 'jasmine/*.coffee' ] +spec_files: [ 'spec/javascripts/*_spec.coffee' ] +src_dir: . +spec_dir: . + diff --git a/spec/lib/jasmine/files_list_spec.rb b/spec/lib/jasmine/files_list_spec.rb index 9a8c4be..3cd8195 100644 --- a/spec/lib/jasmine/files_list_spec.rb +++ b/spec/lib/jasmine/files_list_spec.rb @@ -223,9 +223,9 @@ describe 'test', -> end it 'should get the line numbers' do - line_numbers['test'].should == 1 - line_numbers['yes'].should == 2 - line_numbers['should do something'].should == 3 + line_numbers['test'].should == [ 1 ] + line_numbers['yes'].should == [ 2 ] + line_numbers['should do something'].should == [ 3 ] end end @@ -243,9 +243,9 @@ describe('test', function() { end it 'should get the line numbers' do - line_numbers['test'].should == 1 - line_numbers['yes'].should == 2 - line_numbers['should do something'].should == 3 + line_numbers['test'].should == [ 1 ] + line_numbers['yes'].should == [ 2 ] + line_numbers['should do something'].should == [ 3 ] end end end @@ -259,13 +259,13 @@ describe('test', function() { '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" } end it 'should generate filenames and line number info' do files_list.spec_file_line_numbers.should == { - 'test.coffee' => { 'cat' => 1 } + 'test.coffee' => { 'cat' => [ 1, 2 ] } } end end