Permalink
Browse files

Refactoring all the test frameworks improving the #parse_results meth…

…ods via metaprogramming
  • Loading branch information...
1 parent 08b344d commit b3202281cacd0aa417b8785ba9d20beb7f99374f Tomas D'Stefano committed Oct 13, 2010
View
61 lib/infinity_test/test_framework.rb
@@ -2,30 +2,71 @@ module InfinityTest
class TestFramework
attr_accessor :message
+ # Method used in the subclasses of TestFramework
+ #
+ # Example:
+ #
+ # class Rspec < TestFramework
+ # parse_results :example => /(\d+) example/, :failure => /(\d+) failure/, :pending => /(\d+) pending/
+ # end
+ #
+ # Then will create @examples, @failure and @pending instance variables with the values in the test result
+ #
+ # Or with Test::Unit:
+ #
+ # class TestUnit < TestFramework
+ # parse_results :tests => /(\d+) tests/, :assertions => /(\d+) assertions/, :failures => /(\d+) failures/, :errors => /(\d+) errors/
+ # end
+ #
+ # Then will create @tests, @assertions, @failures and @errors instance variables with the values in the test result
+ #
def self.parse_results(patterns)
raise(ArgumentError, 'patterns should not be empty') if patterns.empty?
- patterns.each do |attribute, pattern|
- attr_accessor attribute
- end
+ create_accessors(patterns)
define_method(:parse_results) do |results|
- shell_result = test_message(results)
- if shell_result =~ /example/ or shell_result =~ /tests/
+ shell_result = test_message(results, patterns)
+ if shell_result
create_pattern_instance_variables(patterns, shell_result)
else
+ patterns.each { |instance, pattern| instance_variable_set("@#{instance}", 1) } # set all to 1 to show that an error occurred
@message = "An exception occurred"
end
end
end
+ # Create accessors for keys of the Hash passed in argument
+ #
+ # create_accessors({ :example => '...', :failure => '...'}) # => attr_accessor :example, :failure
+ #
+ def self.create_accessors(hash)
+ hash.keys.each do |attribute|
+ attr_accessor attribute
+ end
+ end
+
+ # Create the instance pass in the patterns options
+ #
+ # Useful for the parse results:
+ # parse_results :tests => /.../, :assertions => /.../
+ #
+ # Then will create @tests ans @assertions (the keys of the Hash)
+ #
def create_pattern_instance_variables(patterns, shell_result)
patterns.each do |key, pattern|
- instance_variable_set("@#{key}", shell_result[pattern, 1].to_i)
- end
- @message = shell_result
+ number = shell_result[pattern, 1].to_i
+ instance_variable_set("@#{key}", number)
+ end
+ @message = shell_result.gsub(/\e\[\d+?m/, '')
end
- def test_message(output)
- output.split("\n").last
+ # Return the message of the tests
+ #
+ # test_message('0 examples, 0 failures', { :example => /(\d) example/}) # => '0 examples, 0 failures'
+ # test_message('....\n4 examples, 0 failures', { :examples => /(\d) examples/}) # => '4 examples, 0 failures'
+ #
+ def test_message(output, patterns)
+ lines = output.split("\n")
+ lines.select { |line| line =~ patterns.values.first }.first
end
end
View
28 lib/infinity_test/test_library/bacon.rb
@@ -2,6 +2,7 @@ module InfinityTest
module TestLibrary
class Bacon < TestFramework
+ parse_results :specifications => /(\d+) specifications/, :requirements => /(\d+) requirements/, :failures => /(\d+) failure/, :errors => /(\d+) errors/
#
# test_pattern = 'spec/**/*_spec.rb'
@@ -20,7 +21,7 @@ class Bacon < TestFramework
# end
# end
#
- # parse_results :examples => /(\d+) example/, :failures => /(\d+) failure/, :pending => /(\d+) pending/
+ #
#
include BinaryPath
@@ -58,7 +59,7 @@ def construct_rubies_commands(file=nil)
unless have_binary?(bacon_binary)
print_message('bacon', ruby_version)
else
- results[ruby_version] = "rvm #{ruby_version} ruby #{bacon_binary} #{decide_files(file)}"
+ results[ruby_version] = "rvm #{ruby_version} ruby #{bacon_binary} -Ilib -d #{decide_files(file)}"
end
end
results
@@ -74,31 +75,14 @@ def decide_files(file)
def search_bacon(environment)
search_binary('bacon', :environment => environment)
end
-
- def parse_results(results)
- shell_result = results.split("\n").last
- if shell_result =~ /example/
- @example = shell_result[/(\d+) example/, 1].to_i
- @failure = shell_result[/(\d+) failure/, 1].to_i
- @pending = shell_result[/(\d+) pending/, 1].to_i
- @message = "#{@example} examples, #{@failure} failures, #{@pending} pending"
- else
- @example, @pending, @failure = 0, 0, 1
- @message = "An exception occurred"
- end
- end
-
+
def sucess?
- return false if failure? or pending?
+ return false if failure?
true
end
def failure?
- @failure > 0
- end
-
- def pending?
- @pending > 0 and not failure?
+ @failures > 0
end
end
View
20 lib/infinity_test/test_library/rspec.rb
@@ -1,10 +1,13 @@
module InfinityTest
module TestLibrary
- class Rspec
+ class Rspec < TestFramework
+
include BinaryPath
attr_accessor :rubies, :test_directory_pattern, :message, :test_pattern,
:failure, :sucess, :pending
+ parse_results :examples => /(\d+) example/, :failures => /(\d+) failure/, :pending => /(\d+) pending/
+
#
# rspec = InfinityTest::Rspec.new(:rubies => '1.9.1,1.9.2')
# rspec.rubies # => '1.9.1,1.9.2'
@@ -58,26 +61,13 @@ def search_rspec_one(environment)
search_binary('spec', :environment => environment)
end
- def parse_results(results)
- shell_result = results.split("\n").last
- if shell_result =~ /example/
- @example = shell_result[/(\d+) example/, 1].to_i
- @failure = shell_result[/(\d+) failure/, 1].to_i
- @pending = shell_result[/(\d+) pending/, 1].to_i
- @message = "#{@example} examples, #{@failure} failures, #{@pending} pending"
- else
- @example, @pending, @failure = 0, 0, 1
- @message = "An exception occurred"
- end
- end
-
def sucess?
return false if failure? or pending?
true
end
def failure?
- @failure > 0
+ @failures > 0
end
def pending?
View
23 lib/infinity_test/test_library/test_unit.rb
@@ -1,9 +1,11 @@
module InfinityTest
module TestLibrary
- class TestUnit
- attr_reader :rubies, :message, :test_directory_pattern, :tests,
- :assertions, :failures, :errors
+ class TestUnit < TestFramework
+ attr_reader :rubies, :message, :test_directory_pattern, :tests, :assertions, :failures, :errors
+
+ parse_results :tests => /(\d+) tests/, :assertions => /(\d+) assertions/, :failures => /(\d+) failures/, :errors => /(\d+) errors/
+
def initialize(options={})
@rubies = options[:rubies] || []
@test_directory_pattern = "^test/*/(.*)_test.rb"
@@ -48,21 +50,6 @@ def test_loader
end
end
- def parse_results(results)
- shell_result = results.split("\n")
- shell_result = shell_result.select { |line| line =~ /(\d+) tests/}.first
- if shell_result
- @tests = shell_result[/(\d+) tests/, 1].to_i
- @assertions = shell_result[/(\d+) assertions/, 1].to_i
- @failures = shell_result[/(\d+) failures/, 1].to_i
- @errors = shell_result[/(\d+) errors/, 1].to_i
- @message = shell_result
- else
- @tests, @assertions, @failures, @errors = 0, 0, 1, 1
- @message = "An exception ocurred"
- end
- end
-
def failure?
@failures > 0 or @errors > 0
end
View
20 spec/infinity_test/test_framework_spec.rb
@@ -54,15 +54,31 @@ class OtherFramework < TestFramework
end
it "should create a message based in the keys of framework" do
- some_framework.parse_results(".....\n200 examples, 0 failues, 1 pending")
- some_framework.message.should == "200 examples, 0 failues, 1 pending"
+ some_framework.parse_results(".....\n200 examples, 0 failures, 1 pending")
+ some_framework.message.should == "200 examples, 0 failures, 1 pending"
end
it "should create a error message to runtime errors and similars" do
some_framework.parse_results("RunTimeError: undefined method 'my_method' ...")
some_framework.message.should == "An exception occurred"
end
+ it "should parse the results correctly(when not have in last line)" do
+ other_framework.parse_results("....\n10 tests, 35 assertions, 0 failures, 0 errors\nTest run options: --seed 18841")
+ other_framework.tests.should == 10
+ other_framework.assertions.should == 35
+ end
+
+ it "should clear the term ansi colors strings" do
+ other_framework.parse_results("seconds\n\e[33m406 tests, 34 assertions, 0 failures, 2 pending\e[0m\n")
+ other_framework.message.should == "406 tests, 34 assertions, 0 failures, 2 pending"
+ end
+
+ it "should clear the term ansi colors strings" do
+ some_framework.parse_results("seconds\n\e[33m406 examples, 0 failures, 2 pending\e[0m\n")
+ some_framework.message.should == "406 examples, 0 failures, 2 pending"
+ end
+
it "should raise error when not have patterns" do
lambda {
class Abc < TestFramework
View
67 spec/infinity_test/test_library/bacon_spec.rb
@@ -78,35 +78,29 @@ module TestLibrary
end
it "should handle a example that succeed" do
- results = "........Finished in 0.299817 seconds\n\n105 examples, 0 failures, 0 pending\n"
+ results = "should be true\n\n2 specifications (2 requirements), 0 failures, 0 errors\n"
@bacon.parse_results(results)
- @bacon.message.should == "105 examples, 0 failures, 0 pending"
+ @bacon.message.should == "2 specifications (2 requirements), 0 failures, 0 errors"
end
it "should parse without the terminal ansi color" do
- results = "ork\e[0m\n\e[90m # No reason given\e[0m\n\e[90m # ./spec/infinity_test/configuration_spec.rb:31\e[0m\n\nFinished in 0.10487 seconds\n\e[33m406 examples, 5 failures, 2 pending\e[0m\n"
+ results = "should be true\n\n3 specifications (2 requirements), 0 failures, 0 errors\n"
@bacon.parse_results(results)
- @bacon.message.should == "406 examples, 5 failures, 2 pending"
+ @bacon.message.should == "3 specifications (2 requirements), 0 failures, 0 errors"
end
it "should handle a example that succeed and return false for failure?" do
- results = "........Finished in 0.299817 seconds\n\n105 examples, 0 failures, 0 pending\n"
+ results = "3 specifications (2 requirements), 0 failures, 0 errors"
@bacon.parse_results(results)
@bacon.failure?.should equal false
end
it "should parse without the terminal ansi color and grep the failure" do
- results = "ork\e[0m\n\e[90m # No reason given\e[0m\n\e[90m # ./spec/infinity_test/configuration_spec.rb:31\e[0m\n\nFinished in 0.10487 seconds\n\e[33m406 examples, 5 failures, 2 pending\e[0m\n"
+ results = "\n\e[33m3 specifications (2 requirements), 10 failures, 0 errors\e[0m\n"
@bacon.parse_results(results)
@bacon.failure?.should be_true
end
- it "should parse without the terminal ansi color and grep the pending" do
- results = "ork\e[0m\n\e[90m # No reason given\e[0m\n\e[90m # ./spec/infinity_test/configuration_spec.rb:31\e[0m\n\nFinished in 0.10487 seconds\n\e[33m406 examples, 0 failures, 2 pending\e[0m\n"
- @bacon.parse_results(results)
- @bacon.pending?.should be_true
- end
-
it "should parse bacon tests errors" do
results = "/Users/tomas/.rvm/gems/ruby-1.9.2@infinity_test/gems/my_class/bin/klass:2:in `require': no such file to load -- MyClass (LoadError)"
@bacon.parse_results(results)
@@ -123,69 +117,26 @@ module TestLibrary
results = "/Users/tomas/.rvm/gems/ruby-1.9.2@infinity_test/gems/my_class/bin/klass:2:in `require': no such file to load -- MyClass (LoadError)"
@bacon.parse_results(results)
@bacon.sucess?.should equal false
- end
-
- it "should parse bacon tests errors" do
- results = "/Users/tomas/.rvm/gems/ruby-1.9.2@infinity_test/gems/my_class/bin/klass:2:in `require': no such file to load -- MyClass (LoadError)"
- @bacon.parse_results(results)
- @bacon.pending?.should equal false
- end
+ end
end
describe '#sucess?' do
before { @bacon = Bacon.new }
it "should return false when have failures" do
- results = "ork\e[0m\n\e[90m # No reason given\e[0m\n\e[90m # ./spec/infinity_test/configuration_spec.rb:31\e[0m\n\nFinished in 0.10487 seconds\n\e[33m406 examples, 5 failures, 2 pending\e[0m\n"
+ results = "3 specifications (3 requirements), 3 failures, 0 errors"
@bacon.parse_results(results)
@bacon.sucess?.should equal false
end
-
- it "should return false when have pending" do
- results = "ork\e[0m\n\e[90m # No reason given\e[0m\n\e[90m # ./spec/infinity_test/configuration_spec.rb:31\e[0m\n\nFinished in 0.10487 seconds\n\e[33m806 examples, 0 failures, 2 pending\e[0m\n"
- @bacon.parse_results(results)
- @bacon.sucess?.should be_false
- end
it "should return true when have zero failures and zero pending" do
- results = "........Finished in 0.299817 seconds\n\n105 examples, 0 failures, 0 pending\n"
+ results = "13 specifications (20 requirements), 0 failures, 0 errors"
@bacon.parse_results(results)
@bacon.sucess?.should be_true
end
end
- describe '#pending?' do
- let(:bacon) { Bacon.new }
-
- it "should return true when have pending" do
- bacon.pending = 1
- bacon.failure = 0
- bacon.pending?.should be_true
- end
-
- it "should return false when have pending bu thave failures" do
- bacon.pending = 1
- bacon.failure = 1
- bacon.pending?.should equal false
- end
-
- end
-
- def redefine_const(name,value)
- if Object.const_defined?(name)
- old_value = Object.const_get(name)
- Object.send(:remove_const, name)
- else
- old_value = value
- end
- Object.const_set(name,value)
- yield
- ensure
- Object.send(:remove_const, name)
- Object.const_set(name, old_value)
- end
-
end
end
end
View
4 spec/infinity_test/test_library/rspec_spec.rb
@@ -160,13 +160,13 @@ module TestLibrary
it "should return true when have pending" do
rspec.pending = 1
- rspec.failure = 0
+ rspec.failures = 0
rspec.pending?.should be_true
end
it "should return false when have pending bu thave failures" do
rspec.pending = 1
- rspec.failure = 1
+ rspec.failures = 1
rspec.pending?.should equal false
end
View
6 spec/infinity_test/test_library/test_unit_spec.rb
@@ -77,7 +77,7 @@ module TestLibrary
it "should parse when have a exception" do
@test_unit.parse_results("")
- @test_unit.message.should == "An exception ocurred"
+ @test_unit.message.should == "An exception occurred"
end
it "should parse and set correctly the tests" do
@@ -131,8 +131,8 @@ module TestLibrary
it "should parse when have a exception and set failure to 1" do
@test_unit.parse_results("")
@test_unit.failures.should == 1
- @test_unit.tests.should == 0
- @test_unit.assertions.should == 0
+ @test_unit.tests.should == 1
+ @test_unit.assertions.should == 1
@test_unit.errors.should == 1
end

0 comments on commit b320228

Please sign in to comment.