spec/spec_helpers.rb in vic-buildr-1.3.1 vs spec/spec_helpers.rb in vic-buildr-1.3.3

- old
+ new

@@ -14,65 +14,85 @@ # the License. # This file gets loaded twice when running 'spec spec/*' and not with pleasent results, # so ignore the second attempt to load it. -unless self.class.const_defined?('SpecHelpers') +unless defined?(SpecHelpers) require 'rubygems' - $LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__)) + $LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__)), File.expand_path('../addon', File.dirname(__FILE__)) require 'buildr' require File.expand_path('sandbox', File.dirname(__FILE__)) module SpecHelpers include Checks::Matchers - module ::Kernel #:nodoc: - def warn(message) - $warning ||= [] - $warning << message + [:info, :warn, :error].each do |severity| + ::Object.class_eval do + define_method severity do |message| + $messages ||= {} + ($messages[severity] ||= []) << message + end end end - + class << Buildr.application alias :deprecated_without_capture :deprecated def deprecated(message) verbose(true) { deprecated_without_capture message } end end - class WarningMatcher - def initialize(message) + class MessageWithSeverityMatcher + def initialize(severity, message) + @severity = severity @expect = message end def matches?(target) - $warning = [] + $messages = {@severity => []} target.call - return Regexp === @expect ? $warning.join('\n') =~ @expect : $warning.include?(@expect.to_s) + return Regexp === @expect ? $messages[@severity].join('\n') =~ @expect : $messages[@severity].include?(@expect.to_s) end def failure_message - $warning ? "Expected warning #{@expect.source}, found #{$warning}" : "Expected warning #{@expect.source}, no warning issued" + "Expected #{@severity} #{@expect.inspect}, " + + ($messages[@severity].empty? ? "no #{@severity} issued" : "found #{$messages[@severity].inspect}") end def negative_failure_message - "Found unexpected #{$warning}" + "Found unexpected #{$messages[@severity].inspect}" end end + # Test if an info message was shown. You can use a string or regular expression. + # + # For example: + # lambda { info 'ze test' }.should show_info(/ze test/) + def show_info(message) + MessageWithSeverityMatcher.new :info, message + end + # Tests if a warning was issued. You can use a string or regular expression. # # For example: - # lambda { warn 'ze test' }.should warn_that(/ze test/) - def warn_that(message) - WarningMatcher.new message + # lambda { warn 'ze test' }.should show_warning(/ze test/) + def show_warning(message) + MessageWithSeverityMatcher.new :warn, message end + # Test if an error message was shown. You can use a string or regular expression. + # + # For example: + # lambda { error 'ze test' }.should show_error(/ze test/) + def show_error(message) + MessageWithSeverityMatcher.new :error, message + end + class ::Rake::Task alias :execute_without_a_record :execute def execute(args) $executed ||= [] $executed << name @@ -93,10 +113,11 @@ return true end def failure_message return @but_not.negative_failure_message if all_ran? && @but_not - "Expected the tasks #{expected} to run, but #{remaining} did not run, or not in the order we expected them to." + "Expected the tasks #{expected} to run, but #{remaining} did not run, or not in the order we expected them to." + + " Tasks that ran: #{$executed.inspect}" end def negative_failure_message if all_ran? "Expected the tasks #{expected} to not run, but they all ran."