2009-07-10 01:14:10 +00:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), "jasmine_runner.rb"))
|
|
|
|
|
|
|
|
module Jasmine
|
|
|
|
|
2009-07-13 20:20:09 +00:00
|
|
|
class SpecBuilder
|
2009-07-10 01:14:10 +00:00
|
|
|
attr_accessor :suites
|
|
|
|
|
2009-07-10 17:14:27 +00:00
|
|
|
def initialize(spec_files, runner)
|
2009-07-10 01:14:10 +00:00
|
|
|
@spec_files = spec_files
|
2009-07-10 17:14:27 +00:00
|
|
|
@runner = runner
|
2009-07-10 21:35:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
2009-07-10 01:14:10 +00:00
|
|
|
guess_example_locations
|
2009-07-10 17:14:27 +00:00
|
|
|
|
|
|
|
@runner.start
|
2009-07-10 01:14:10 +00:00
|
|
|
load_suite_info
|
|
|
|
@spec_results = {}
|
2009-07-10 21:35:24 +00:00
|
|
|
end
|
2009-07-10 17:14:27 +00:00
|
|
|
|
2009-07-10 21:35:24 +00:00
|
|
|
def stop
|
|
|
|
@runner.stop
|
2009-07-10 01:14:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def script_path
|
|
|
|
File.expand_path(__FILE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def guess_example_locations
|
|
|
|
@example_locations = {}
|
|
|
|
|
|
|
|
example_name_parts = []
|
|
|
|
previous_indent_level = 0
|
|
|
|
@spec_files.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
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_suite_info
|
2009-07-10 21:35:24 +00:00
|
|
|
while !eval_js('jsApiReporter.started') do
|
2009-07-10 01:14:10 +00:00
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
|
2009-07-10 21:35:24 +00:00
|
|
|
@suites = eval_js('JSON.stringify(jsApiReporter.suites)')
|
2009-07-10 01:14:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def results_for(spec_id)
|
|
|
|
spec_id = spec_id.to_s
|
|
|
|
return @spec_results[spec_id] if @spec_results[spec_id]
|
|
|
|
|
2009-07-10 21:35:24 +00:00
|
|
|
@spec_results[spec_id] = eval_js("JSON.stringify(jsApiReporter.results[#{spec_id}])")
|
2009-07-10 01:14:10 +00:00
|
|
|
while @spec_results[spec_id].nil? do
|
|
|
|
sleep 0.1
|
2009-07-10 21:35:24 +00:00
|
|
|
@spec_results[spec_id] = eval_js("JSON.stringify(jsApiReporter.results[#{spec_id}])")
|
2009-07-10 01:14:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec_results[spec_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def declare_suites
|
|
|
|
me = self
|
|
|
|
suites.each do |suite|
|
|
|
|
declare_suite(self, suite)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def declare_suite(parent, suite)
|
|
|
|
me = self
|
|
|
|
parent.describe suite["name"] do
|
|
|
|
suite["children"].each do |suite_or_spec|
|
|
|
|
type = suite_or_spec["type"]
|
|
|
|
if type == "suite"
|
|
|
|
me.declare_suite(self, suite_or_spec)
|
|
|
|
elsif type == "spec"
|
|
|
|
me.declare_spec(self, suite_or_spec)
|
|
|
|
else
|
2009-07-10 21:35:24 +00:00
|
|
|
raise "unknown type #{type} for #{suite_or_spec.inspect}"
|
2009-07-10 01:14:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def declare_spec(parent, spec)
|
|
|
|
me = self
|
|
|
|
example_name = spec["name"]
|
|
|
|
|
|
|
|
backtrace = @example_locations[parent.description + " " + example_name]
|
|
|
|
parent.it example_name, {}, backtrace do
|
|
|
|
me.report_spec(spec["id"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def report_spec(spec_id)
|
|
|
|
spec_results = results_for(spec_id)
|
|
|
|
|
2009-07-10 21:35:24 +00:00
|
|
|
out = ""
|
|
|
|
messages = spec_results['messages'].each do |message|
|
|
|
|
out << message["message"]
|
|
|
|
out << "\n"
|
|
|
|
|
|
|
|
unless message["passed"]
|
|
|
|
stack_trace = message["trace"]["stack"]
|
|
|
|
out << stack_trace.gsub(/\(.*\)@http:\/\/localhost:[0-9]+\/specs\//, "/spec/")
|
|
|
|
out << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
fail out unless spec_results['result'] == 'passed'
|
|
|
|
puts out
|
2009-07-10 01:14:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2009-07-10 17:14:27 +00:00
|
|
|
def eval_js(js)
|
|
|
|
@runner.eval_js(js)
|
2009-07-10 01:14:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|