Skip to content

Commit

Permalink
Process results w/ result_processor
Browse files Browse the repository at this point in the history
- Minimize passing of config.
  • Loading branch information
ragaskar committed Jul 29, 2012
1 parent 59f1151 commit d54ec96
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 44 deletions.
1 change: 1 addition & 0 deletions lib/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'command_line_tool',
'page',
'asset_pipeline_mapper',
'results_processor',
'results',
File.join('runners', 'http')]

Expand Down
7 changes: 6 additions & 1 deletion lib/jasmine/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions lib/jasmine/results_processor.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 1 addition & 30 deletions lib/jasmine/rspec_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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
5 changes: 3 additions & 2 deletions lib/jasmine/run_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

9 changes: 5 additions & 4 deletions lib/jasmine/runners/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }"))
Expand Down
5 changes: 3 additions & 2 deletions spec/jasmine_self_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 3 additions & 0 deletions spec/results_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe Jasmine::ResultsProcessor do

end
17 changes: 12 additions & 5 deletions spec/results_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d54ec96

Please sign in to comment.