diff --git a/lib/jasmine.rb b/lib/jasmine.rb index 470f9a35..a0961004 100644 --- a/lib/jasmine.rb +++ b/lib/jasmine.rb @@ -9,6 +9,7 @@ 'command_line_tool', 'page', 'asset_pipeline_mapper', + 'results_processor', 'results', File.join('runners', 'http')] diff --git a/lib/jasmine/results.rb b/lib/jasmine/results.rb index 86b76ec3..208df047 100644 --- a/lib/jasmine/results.rb +++ b/lib/jasmine/results.rb @@ -2,13 +2,18 @@ module Jasmine class Results attr_reader :suites - def initialize(result_hash, suite_hash) + def initialize(result_hash, suite_hash, example_locations) @suites = suite_hash @results = result_hash + @example_locations = example_locations end def for_spec_id(id) @results[id] end + + def example_location_for(spec_description) + @example_locations[spec_description] + end end end diff --git a/lib/jasmine/results_processor.rb b/lib/jasmine/results_processor.rb new file mode 100644 index 00000000..df16e204 --- /dev/null +++ b/lib/jasmine/results_processor.rb @@ -0,0 +1,37 @@ +module Jasmine + class ResultsProcessor + + def initialize(config) + @config = config + end + + def process(results_hash, suites_hash) + return Jasmine::Results.new(results_hash, suites_hash, example_locations) + end + + def example_locations + example_locations = {} + example_name_parts = [] + previous_indent_level = 0 + @config.spec_files_full_paths.each do |filename| + line_number = 1 + File.open(filename, "r") do |file| + file.readlines.each do |line| + match = /^(\s*)(describe|it)\s*\(\s*["'](.*)["']\s*,\s*function/.match(line) + if (match) + indent_level = match[1].length / 2 + example_name = match[3] + example_name_parts[indent_level] = example_name + + full_example_name = example_name_parts.slice(0, indent_level + 1).join(" ") + example_locations[full_example_name] = "#{filename}:#{line_number}: in `it'" + end + line_number += 1 + end + end + end + example_locations + end + + end +end diff --git a/lib/jasmine/rspec_formatter.rb b/lib/jasmine/rspec_formatter.rb index 048c0b84..6b92bf93 100644 --- a/lib/jasmine/rspec_formatter.rb +++ b/lib/jasmine/rspec_formatter.rb @@ -2,10 +2,6 @@ module Jasmine class RspecFormatter - def initialize(config) - @config = config - @spec_files = config.spec_files - end def format_results(results) @results = results @@ -38,7 +34,7 @@ def declare_suite(parent, suite) def declare_spec(parent, spec) me = self example_name = spec["name"] - backtrace = example_locations[parent.description + " " + example_name] + backtrace = @results.example_location_for(parent.description + " " + example_name) if Jasmine::Dependencies.rspec2? parent.it example_name, {} do me.report_spec(spec["id"]) @@ -85,31 +81,6 @@ def results_for(spec_id) @results.for_spec_id(spec_id.to_s) end - def example_locations - return @example_locations if @example_locations - @example_locations = {} - - example_name_parts = [] - previous_indent_level = 0 - @config.spec_files_full_paths.each do |filename| - line_number = 1 - File.open(filename, "r") do |file| - file.readlines.each do |line| - match = /^(\s*)(describe|it)\s*\(\s*["'](.*)["']\s*,\s*function/.match(line) - if (match) - indent_level = match[1].length / 2 - example_name = match[3] - example_name_parts[indent_level] = example_name - - full_example_name = example_name_parts.slice(0, indent_level + 1).join(" ") - @example_locations[full_example_name] = "#{filename}:#{line_number}: in `it'" - end - line_number += 1 - end - end - end - @example_locations - end end end diff --git a/lib/jasmine/run_specs.rb b/lib/jasmine/run_specs.rb index 1210b7bb..c8991643 100644 --- a/lib/jasmine/run_specs.rb +++ b/lib/jasmine/run_specs.rb @@ -26,7 +26,8 @@ Jasmine::wait_for_listener(jasmine_runner_config.port, "jasmine server") puts "jasmine server started." -results = Jasmine::Runners::HTTP.new(client).run -formatter = Jasmine::RspecFormatter.new(jasmine_runner_config) +results_processor = Jasmine::ResultsProcessor.new(jasmine_runner_config) +results = Jasmine::Runners::HTTP.new(client, results_processor).run +formatter = Jasmine::RspecFormatter.new formatter.format_results(results) diff --git a/lib/jasmine/runners/http.rb b/lib/jasmine/runners/http.rb index fc8c36d2..64cd88d4 100644 --- a/lib/jasmine/runners/http.rb +++ b/lib/jasmine/runners/http.rb @@ -3,17 +3,18 @@ module Runners class HTTP attr_accessor :suites - def initialize(client) + def initialize(client, results_processor) @client = client + @results_processor = results_processor end def run @client.connect load_suite_info wait_for_suites_to_finish_running - jasmine_results = Jasmine::Results.new(results, suites) + results = @results_processor.process(results_hash, suites) @client.disconnect - jasmine_results + results end private @@ -28,7 +29,7 @@ def load_suite_info @suites = eval_js("var result = jsApiReporter.suites(); if (window.Prototype && Object.toJSON) { return Object.toJSON(result) } else { return JSON.stringify(result) }") end - def results + def results_hash spec_results = {} spec_ids.each_slice(50) do |slice| spec_results.merge!(eval_js("var result = jsApiReporter.resultsForSpecs(#{json_generate(slice)}); if (window.Prototype && Object.toJSON) { return Object.toJSON(result) } else { return JSON.stringify(result) }")) diff --git a/spec/jasmine_self_test_spec.rb b/spec/jasmine_self_test_spec.rb index 5ae6965b..cf4f2212 100644 --- a/spec/jasmine_self_test_spec.rb +++ b/spec/jasmine_self_test_spec.rb @@ -17,6 +17,7 @@ Jasmine::wait_for_listener(jasmine_runner_config.port, "jasmine server") puts "jasmine server started." -results = Jasmine::Runners::HTTP.new(client).run -formatter = Jasmine::RspecFormatter.new(jasmine_runner_config) +results_processor = Jasmine::ResultsProcessor.new(jasmine_runner_config) +results = Jasmine::Runners::HTTP.new(client, results_processor).run +formatter = Jasmine::RspecFormatter.new formatter.format_results(results) diff --git a/spec/results_processor_spec.rb b/spec/results_processor_spec.rb new file mode 100644 index 00000000..702ba4ef --- /dev/null +++ b/spec/results_processor_spec.rb @@ -0,0 +1,3 @@ +describe Jasmine::ResultsProcessor do + +end diff --git a/spec/results_spec.rb b/spec/results_spec.rb index e155a41e..f0270233 100644 --- a/spec/results_spec.rb +++ b/spec/results_spec.rb @@ -2,19 +2,26 @@ describe Jasmine::Results do it "should be able to return suites" do - suites = {} - results = {} - Jasmine::Results.new(results, suites).suites.should == suites + suites = {:some => 'suite'} + Jasmine::Results.new({}, suites, {}).suites.should == suites end it "should return a result for a particular spec id" do - suites = {} result1 = {:some => 'result'} result2 = {:some => 'other result'} raw_results = {'1' => result1, '2' => result2 } - results = Jasmine::Results.new(raw_results, suites) + results = Jasmine::Results.new(raw_results, {}, {}) results.for_spec_id('1').should == result1 results.for_spec_id('2').should == result2 end + + it "should return an example location for a particular string" do + example_location1 = {:some => 'spec location'} + example_location2 = {:some => 'other spec location'} + example_locations = {'foo bar' => example_location1, 'baz quux' => example_location2 } + results = Jasmine::Results.new({}, {}, example_locations) + results.example_location_for('foo bar').should == example_location1 + results.example_location_for('baz quux').should == example_location2 + end end