From cae6f166239867feab3a921879934a53ae3c8dd7 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 2 Sep 2011 17:00:21 -0400 Subject: [PATCH] finish new-style reporting, time to use it --- lib/jasmine-headless-webkit.rb | 3 + lib/jasmine/headless/report.rb | 59 +++++++++++++++++++ lib/jasmine/headless/report_message.rb | 11 ++++ .../headless/report_message/console.rb | 20 +++++++ lib/jasmine/headless/report_message/error.rb | 20 +++++++ lib/jasmine/headless/report_message/fail.rb | 5 ++ lib/jasmine/headless/report_message/pass.rb | 5 ++ lib/jasmine/headless/report_message/spec.rb | 22 +++++++ lib/jasmine/headless/report_message/total.rb | 30 ++++++++++ spec/lib/jasmine/headless/report_spec.rb | 30 +++++++++- spec/spec_helper.rb | 21 ++++--- 11 files changed, 214 insertions(+), 12 deletions(-) create mode 100644 lib/jasmine/headless/report.rb create mode 100644 lib/jasmine/headless/report_message.rb create mode 100644 lib/jasmine/headless/report_message/console.rb create mode 100644 lib/jasmine/headless/report_message/error.rb create mode 100644 lib/jasmine/headless/report_message/fail.rb create mode 100644 lib/jasmine/headless/report_message/pass.rb create mode 100644 lib/jasmine/headless/report_message/spec.rb create mode 100644 lib/jasmine/headless/report_message/total.rb diff --git a/lib/jasmine-headless-webkit.rb b/lib/jasmine-headless-webkit.rb index 561a8a1..5cd2dba 100644 --- a/lib/jasmine-headless-webkit.rb +++ b/lib/jasmine-headless-webkit.rb @@ -10,6 +10,9 @@ module Jasmine autoload :Runner, 'jasmine/headless/runner' autoload :Options, 'jasmine/headless/options' autoload :Task, 'jasmine/headless/task' + + autoload :Report, 'jasmine/headless/report' + autoload :ReportMessage, 'jasmine/headless/report_message' end end diff --git a/lib/jasmine/headless/report.rb b/lib/jasmine/headless/report.rb new file mode 100644 index 0000000..b4abfdf --- /dev/null +++ b/lib/jasmine/headless/report.rb @@ -0,0 +1,59 @@ +require 'forwardable' + +module Jasmine::Headless + class Report + extend Forwardable + + def_delegators :report, :length, :[] + + class << self + def load(file) + new(file).process + end + end + + attr_reader :file, :report + + def initialize(file) + @file = file + end + + def process + @report = File.readlines(file).collect do |line| + type, *parts = line.split('||') + + Jasmine::Headless::ReportMessage.const_get( + Jasmine::Headless::ReportMessage.constants.find { |k| k.to_s.downcase == type.downcase } + ).new_from_parts(parts) + end + self + end + + def total + last_total.total + end + + def failed + last_total.failed + end + + def has_used_console? + @report.any? { |entry| entry.class == Jasmine::Headless::ReportMessage::Console } + end + + def has_failed_on?(statement) + @report.any? { |entry| + if entry.class == Jasmine::Headless::ReportMessage::Fail + entry.statement == statement + end + } + end + + private + + def last_total + @report.reverse.find { |entry| entry.respond_to?(:total) } + end + end +end + diff --git a/lib/jasmine/headless/report_message.rb b/lib/jasmine/headless/report_message.rb new file mode 100644 index 0000000..218ba17 --- /dev/null +++ b/lib/jasmine/headless/report_message.rb @@ -0,0 +1,11 @@ +module Jasmine::Headless + module ReportMessage + autoload :Spec, 'jasmine/headless/report_message/spec' + autoload :Pass, 'jasmine/headless/report_message/pass' + autoload :Fail, 'jasmine/headless/report_message/fail' + autoload :Console, 'jasmine/headless/report_message/console' + autoload :Error, 'jasmine/headless/report_message/error' + autoload :Total, 'jasmine/headless/report_message/total' + end +end + diff --git a/lib/jasmine/headless/report_message/console.rb b/lib/jasmine/headless/report_message/console.rb new file mode 100644 index 0000000..25c64ef --- /dev/null +++ b/lib/jasmine/headless/report_message/console.rb @@ -0,0 +1,20 @@ +module Jasmine::Headless::ReportMessage + class Console + class << self + def new_from_parts(parts) + new(parts.first) + end + end + + attr_reader :message + + def initialize(message) + @message = message + end + + def ==(other) + self.message == other.message + end + end +end + diff --git a/lib/jasmine/headless/report_message/error.rb b/lib/jasmine/headless/report_message/error.rb new file mode 100644 index 0000000..31bca2e --- /dev/null +++ b/lib/jasmine/headless/report_message/error.rb @@ -0,0 +1,20 @@ +module Jasmine::Headless::ReportMessage + class Error + class << self + def new_from_parts(parts) + new(*parts) + end + end + + attr_reader :message, :file_info + + def initialize(message, file_info) + @message, @file_info = message, file_info + end + + def ==(other) + self.message == other.message && self.file_info == other.file_info + end + end +end + diff --git a/lib/jasmine/headless/report_message/fail.rb b/lib/jasmine/headless/report_message/fail.rb new file mode 100644 index 0000000..e8f905c --- /dev/null +++ b/lib/jasmine/headless/report_message/fail.rb @@ -0,0 +1,5 @@ +module Jasmine::Headless::ReportMessage + class Fail < Spec + end +end + diff --git a/lib/jasmine/headless/report_message/pass.rb b/lib/jasmine/headless/report_message/pass.rb new file mode 100644 index 0000000..a76f6a3 --- /dev/null +++ b/lib/jasmine/headless/report_message/pass.rb @@ -0,0 +1,5 @@ +module Jasmine::Headless::ReportMessage + class Pass < Spec + end +end + diff --git a/lib/jasmine/headless/report_message/spec.rb b/lib/jasmine/headless/report_message/spec.rb new file mode 100644 index 0000000..1af14fd --- /dev/null +++ b/lib/jasmine/headless/report_message/spec.rb @@ -0,0 +1,22 @@ +module Jasmine::Headless::ReportMessage + class Spec + class << self + def new_from_parts(parts) + file_info = parts.pop + + new(parts.join(' '), file_info) + end + end + + attr_reader :statement, :file_info + + def initialize(statement, file_info) + @statement, @file_info = statement, file_info + end + + def ==(other) + self.statement == other.statement && self.file_info == other.file_info + end + end +end + diff --git a/lib/jasmine/headless/report_message/total.rb b/lib/jasmine/headless/report_message/total.rb new file mode 100644 index 0000000..dc1cc12 --- /dev/null +++ b/lib/jasmine/headless/report_message/total.rb @@ -0,0 +1,30 @@ +module Jasmine::Headless::ReportMessage + class Total + class << self + def new_from_parts(parts) + new(*parts) + end + end + + attr_reader :total, :failed, :time, :has_js_error + + def initialize(total, failed, time, has_js_error) + @total, @failed, @time = total.to_i, failed.to_i, time.to_f + + @has_js_error = case has_js_error + when String + has_js_error == "T" + else + has_js_error + end + end + + def ==(other) + self.total == other.total && + self.failed == other.failed && + self.time == other.time && + self.has_js_error == other.has_js_error + end + end +end + diff --git a/spec/lib/jasmine/headless/report_spec.rb b/spec/lib/jasmine/headless/report_spec.rb index f6424f5..e5f6d62 100644 --- a/spec/lib/jasmine/headless/report_spec.rb +++ b/spec/lib/jasmine/headless/report_spec.rb @@ -3,15 +3,43 @@ require 'spec_helper' describe Jasmine::Headless::Report do include FakeFS::SpecHelpers + let(:file) { 'report.txt' } + describe '.load' do + let(:report) { described_class.load(file) } + context 'no file' do it 'should raise an exception' do - expect { described_class.load(file) }.to raise_error(Errno::ENOENT) + expect { report }.to raise_error(Errno::ENOENT) end end context 'file' do + before do + File.open(file, 'wb') { |fh| fh.puts <<-REPORT } +PASS||Statement||One||file.js:23 +FAIL||Statement||Two||file.js:23 +CONSOLE||Yes +ERROR||Uh oh||file.js:23 +TOTAL||1||2||3||T +REPORT + end + it 'should read the report file' do + report.length.should == 5 + + report[0].should == Jasmine::Headless::ReportMessage::Pass.new("Statement One", "file.js:23") + report[1].should == Jasmine::Headless::ReportMessage::Fail.new("Statement Two", "file.js:23") + report[2].should == Jasmine::Headless::ReportMessage::Console.new("Yes") + report[3].should == Jasmine::Headless::ReportMessage::Error.new("Uh oh", "file.js:23") + report[4].should == Jasmine::Headless::ReportMessage::Total.new(1, 2, 3, true) + + report.total.should == 1 + report.failed.should == 2 + report.should have_used_console + + report.should have_failed_on("Statement Two") + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 472d170..2348573 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,32 +18,31 @@ if !File.file?(specrunner) end module RSpec::Matchers - define :be_a_report_containing do |total, fails, used_console| + define :be_a_report_containing do |total, failed, used_console| match do |filename| - parts(filename).length.should == 4 - parts[0].should == total.to_s - parts[1].should == fails.to_s - parts[2].should == (used_console ? "T" : "F") + report(filename) + report.total.should == total + report.failed.should == failed + report.has_used_console?.should == used_console true end failure_message_for_should do |filename| - parts(filename) - "expected #{filename} to be a report containing (#{total}, #{fails}, #{used_console.inspect}), instead it contained (#{parts[0]}, #{parts[1]}, #{(parts[2] == "T").inspect})" + "expected #{filename} to be a report containing (#{total}, #{failed}, #{used_console.inspect})" end - def parts(filename = nil) - @parts ||= File.readlines(filename).first.strip.split('/') + def report(filename = nil) + @report ||= Jasmine::Headless::Report.load(filename) end end define :contain_a_failing_spec do |*parts| match do |filename| - report(filename).include?(parts.join("||")).should be_true + report(filename).should have_failed_on(parts.join(" ")) end def report(filename) - @report ||= File.readlines(filename)[1..-1].collect(&:strip) + @report ||= Jasmine::Headless::Report.load(filename) end end