finish new-style reporting, time to use it

This commit is contained in:
John Bintz 2011-09-02 17:00:21 -04:00
parent f1318a5223
commit cae6f16623
11 changed files with 214 additions and 12 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,5 @@
module Jasmine::Headless::ReportMessage
class Fail < Spec
end
end

View File

@ -0,0 +1,5 @@
module Jasmine::Headless::ReportMessage
class Pass < Spec
end
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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