test/unit/result_tests.rb in assert-2.14.0 vs test/unit/result_tests.rb in assert-2.15.0

- old
+ new

@@ -1,280 +1,339 @@ require 'assert' require 'assert/result' module Assert::Result - class BacktraceTests < Assert::Context - desc "Assert::Result::Backtrace" + class UnitTests < Assert::Context + desc "Assert::Result" setup do - @backtrace = Backtrace.new(caller) + @test = Factory.test("a test name") end - subject { @backtrace } + subject{ Assert::Result } - should have_instance_methods :to_s, :filtered + should have_imeths :types, :new - should "be an Array" do - assert_kind_of ::Array, subject - end + should "know its types" do + exp = { + :pass => Pass, + :fail => Fail, + :ignore => Ignore, + :skip => Skip, + :error => Error + } + assert_equal exp, subject.types - should "render as a string by joining on the newline" do - assert_equal subject.join("\n"), subject.to_s + assert_equal Base, subject.types[Factory.string] end - should "another backtrace when filtered" do - assert_kind_of Backtrace, subject - end + should "create results from data hashes" do + type = Assert::Result.types.keys.choice + exp = Assert::Result.types[type].new(:type => type) - should "default itself when created from nil" do - assert_equal ["No backtrace"], Backtrace.new + assert_equal exp, Assert::Result.new(:type => type) end end - class BaseTests < Assert::Context - desc "Assert::Result::Base" + class BaseTests < UnitTests + desc "Base" setup do - @test = Factory.test("a test name") - @result = Assert::Result::Base.new(@test, "a message", ["line 1", "line2"]) + @given_data = { + :type => Factory.string, + :name => Factory.string, + :test_name => Factory.string, + :message => Factory.string, + :backtrace => Backtrace.new(caller), + :trace => Factory.string + } + @result = Base.new(@given_data) end subject{ @result } - should have_readers :test, :message, :backtrace - should have_imeths :test_name, :name, :to_sym, :to_s, :trace - should have_imeth :set_backtrace + should have_cmeths :type, :name, :for_test + should have_imeths :type, :name, :test_name, :message, :backtrace, :trace + should have_imeths *Assert::Result.types.keys.map{ |k| "#{k}?" } + should have_imeths :set_backtrace, :data, :to_sym, :to_s - Assert::Result.types.keys.each do |type| - should "respond to the instance method ##{type}?" do - assert_respond_to "#{type}?", subject - end + should "know its class-level type/name" do + assert_equal :unknown, subject.class.type + assert_equal '', subject.class.name + end - should "not be #{type}" do - assert_equal false, subject.send("#{type}?") - end + should "know how to build a result for a given test" do + message = Factory.text + bt = Factory.integer(3).times.map{ Factory.string } + result = Base.for_test(@test, message, bt) + + exp_backtrace = Backtrace.new(bt) + exp_trace = exp_backtrace.filtered.first.to_s + + assert_equal @test.name, result.test_name + assert_equal message, result.message + assert_equal exp_backtrace, result.backtrace + assert_equal exp_trace, result.trace end - should "know its test" do - assert_equal @test, subject.test + should "use any given attrs" do + assert_equal @given_data[:type].to_sym, subject.type + assert_equal @given_data[:name], subject.name + assert_equal @given_data[:test_name], subject.test_name + assert_equal @given_data[:message], subject.message + assert_equal @given_data[:backtrace], subject.backtrace + assert_equal @given_data[:trace], subject.trace end - should "nil out empty messages" do - assert_equal nil, Assert::Result::Base.new(@test, "").message + should "default its attrs" do + result = Base.new({}) + + assert_equal :unknown, result.type + assert_equal '', result.name + assert_equal '', result.test_name + assert_equal '', result.message + assert_equal Backtrace.new([]), result.backtrace + assert_equal '', result.trace end - should "show only its class and message when inspected" do - exp = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)}"\ - " @message=#{subject.message.inspect}>" - assert_equal exp, subject.inspect + should "know if it is a certain type of result" do + Assert::Result.types.keys.each do |type| + assert_false subject.send("#{type}?") + Assert.stub(subject, :type){ type } + assert_true subject.send("#{type}?") + end end - should "allow overriding the result backtrace with `set_backtrace`" do - subject.set_backtrace(['bt']) + should "allow setting a new backtrace" do + new_bt = Factory.integer(3).times.map{ Factory.string } + exp_backtrace = Backtrace.new(new_bt) + exp_trace = exp_backtrace.filtered.first.to_s - assert_kind_of Assert::Result::Backtrace, subject.backtrace - assert_equal ['bt'], subject.backtrace + subject.set_backtrace(new_bt) + + assert_equal exp_backtrace, subject.backtrace + assert_equal exp_trace, subject.trace end - should "include its test context name in the to_s" do - assert subject.to_s.include?(subject.test_name) + should "know its data" do + exp = { + :type => subject.type, + :name => subject.name, + :test_name => subject.test_name, + :message => subject.message, + :backtrace => subject.backtrace, + :trace => subject.trace, + } + assert_equal exp, subject.data end - should "include its test name in the to_s" do - assert subject.to_s.include?(subject.test_name) + should "know its symbol representation" do + assert_equal subject.type, subject.to_sym end - should "include its message in the to_s" do - assert subject.to_s.include?(subject.message) + should "know its string representation" do + str = subject.to_s + + assert_includes subject.name.upcase, str + assert_includes subject.test_name, str + assert_includes subject.message, str + assert_includes subject.trace, str + + assert_equal 3, str.split("\n").count + + Assert.stub(subject, :message){ '' } + Assert.stub(subject, :trace){ '' } + + assert_equal 1, subject.to_s.split("\n").count end - should "include its trace in the to_s" do - assert subject.to_s.include?(subject.trace) + should "know if it is equal to another result" do + other = Assert::Result::Base.new(@given_data) + assert_equal other, subject + + Assert.stub(other, [:type, :message].choice){ Factory.string } + assert_not_equal other, subject end - should "have a trace with the first filtered line of the backtrace" do - assert_equal subject.backtrace.filtered.first, subject.trace + should "show only its class and message when inspected" do + exp = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)}"\ + " @message=#{subject.message.inspect}>" + assert_equal exp, subject.inspect end + end - class PassTests < Assert::Context - desc "Assert::Result::Pass" + class PassTests < UnitTests + desc "Pass" setup do - @test = Factory.test("a test name") - @result = Assert::Result::Pass.new(@test, "passed", []) + @result = Pass.new({}) end subject { @result } - should "pass?" do - assert_equal true, subject.pass? + should "know its type/name" do + assert_equal :pass, subject.type + assert_equal :pass, subject.class.type + assert_equal 'Pass', subject.class.name end - Assert::Result.types.keys.reject{|k| k == :pass}.each do |type| - should "not be #{type}?" do - assert_equal false, subject.send("#{type}?") - end - end + end - should "know its to_sym" do - assert_equal :pass, subject.to_sym + class IgnoreTests < UnitTests + desc "Ignore" + setup do + @result = Ignore.new({}) end + subject { @result } - should "know its name" do - assert_equal "Pass", subject.name + should "know its type/name" do + assert_equal :ignore, subject.type + assert_equal :ignore, subject.class.type + assert_equal 'Ignore', subject.class.name end - should "include PASS in its to_s" do - assert subject.to_s.include?("PASS") + end + + class TestFailureTests < UnitTests + desc "TestFailure" + subject{ TestFailure } + + should "be a runtime error" do + assert_kind_of RuntimeError, subject.new end end - class IgnoreTests < Assert::Context - desc "Assert::Result::Ignore" + class FailTests < UnitTests + desc "Fail" setup do - @test = Factory.test("a test name") - @result = Assert::Result::Ignore.new(@test, "ignored", []) + @result = Fail.new({}) end subject { @result } - should "ignore?" do - assert_equal true, subject.ignore? + should "know its type/name" do + assert_equal :fail, subject.type + assert_equal :fail, subject.class.type + assert_equal 'Fail', subject.class.name end - Assert::Result.types.keys.reject{|k| k == :ignore}.each do |type| - should "not be #{type}?" do - assert_equal false, subject.send("#{type}?") - end - end + should "allow creating for a test with TestFailure exceptions" do + err = TestFailure.new + err.set_backtrace(caller) + result = Fail.for_test(@test, err) - should "know its to_sym" do - assert_equal :ignore, subject.to_sym + assert_equal err.message, result.message + + exp_bt = Backtrace.new(err.backtrace) + assert_equal exp_bt, result.backtrace end - should "know its name" do - assert_equal "Ignore", subject.name + should "not allow creating for a test with non-TestFailure exceptions" do + assert_raises(ArgumentError){ Fail.for_test(@test, RuntimeError.new) } end - should "include IGNORE in its to_s" do - assert subject.to_s.include?("IGNORE") + end + + class TestSkippedTests < UnitTests + desc "TestSkipped" + subject{ TestSkipped } + + should "be a runtime error" do + assert_kind_of RuntimeError, subject.new end end - class FailTests < Assert::Context - desc "Assert::Result::Fail" + class SkipTests < UnitTests + desc "Skip" setup do - @test = Factory.test("a test name") - @result = Assert::Result::Fail.new(@test, "failed", []) + @result = Skip.new({}) end subject { @result } - should "use a runtime error (TestFailure) for controlling flow" do - assert_kind_of RuntimeError, Assert::Result::TestFailure.new + should "know its type/name" do + assert_equal :skip, subject.type + assert_equal :skip, subject.class.type + assert_equal 'Skip', subject.class.name end - should "fail?" do - assert_equal true, subject.fail? - end + should "allow creating for a test with TestSkipped exceptions" do + err = TestSkipped.new + err.set_backtrace(caller) + result = Skip.for_test(@test, err) - Assert::Result.types.keys.reject{|k| k == :fail}.each do |type| - should "not be #{type}?" do - assert_equal false, subject.send("#{type}?") - end - end + assert_equal err.message, result.message - should "know its to_sym" do - assert_equal :fail, subject.to_sym + exp_bt = Backtrace.new(err.backtrace) + assert_equal exp_bt, result.backtrace end - should "know its name" do - assert_equal "Fail", subject.name + should "not allow creating for a test with non-TestSkipped exceptions" do + assert_raises(ArgumentError){ Skip.for_test(@test, RuntimeError.new) } end - should "include FAIL in its to_s" do - assert subject.to_s.include?("FAIL") - end - end - class SkipTests < Assert::Context - desc "Assert::Result::Skip" + class ErrorTests < UnitTests + desc "Error" setup do - @test = Factory.test("a test name") - @exception = nil - begin - raise TestSkipped, "test ski[" - rescue Exception => err - @exception = err - end - @result = Assert::Result::Skip.new(@test, @exception) + @result = Error.new({}) end subject { @result } - should "use a runtime error (TestSkipped) for controlling flow" do - assert_kind_of RuntimeError, Assert::Result::TestSkipped.new + should "know its class-level type/name" do + assert_equal :error, subject.class.type + assert_equal 'Error', subject.class.name end - should "skip?" do - assert_equal true, subject.skip? - end + should "allow creating for a test with exceptions" do + err = Exception.new + err.set_backtrace(caller) + result = Error.for_test(@test, err) - Assert::Result.types.keys.reject{|k| k == :skip}.each do |type| - should "not be #{type}?" do - assert_equal false, subject.send("#{type}?") - end - end + exp_msg = "#{err.message} (#{err.class.name})" + assert_equal exp_msg, result.message - should "know its to_sym" do - assert_equal :skip, subject.to_sym + exp_bt = Backtrace.new(err.backtrace) + assert_equal exp_bt, result.backtrace + assert_equal exp_bt.to_s, result.trace end - should "know its name" do - assert_equal "Skip", subject.name + should "not allow creating for a test without an exception" do + assert_raises(ArgumentError){ Error.for_test(@test, Factory.string) } end - should "include SKIP in its to_s" do - assert subject.to_s.include?("SKIP") - end - end - class ErrorTests < Assert::Context - desc "Assert::Result::Error" + class BacktraceTests < UnitTests + desc "Backtrace" setup do - @test = Factory.test("a test name") - @exception = nil - begin - raise Exception, "test error" - rescue Exception => err - @exception = err - end - @result = Assert::Result::Error.new(@test, @exception) + @backtrace = Backtrace.new(caller) end - subject { @result } + subject { @backtrace } - should "error?" do - assert_equal true, subject.error? + should have_cmeths :parse + should have_imeths :to_s, :filtered + + should "be parseable from its string representation" do + assert_equal subject, Backtrace.parse(subject.to_s) end - Assert::Result.types.keys.reject{|k| k == :error}.each do |type| - should "not be #{type}?" do - assert_equal false, subject.send("#{type}?") - end + should "be an Array" do + assert_kind_of ::Array, subject end - should "know its to_sym" do - assert_equal :error, subject.to_sym + should "know its DELIM" do + assert_equal "\n", Backtrace::DELIM end - should "know its name" do - assert_equal "Error", subject.name + should "render as a string by joining on the newline" do + assert_equal subject.join(Backtrace::DELIM), subject.to_s end - should "include ERRORED in its to_s" do - assert subject.to_s.include?("ERROR") + should "another backtrace when filtered" do + assert_kind_of Backtrace, subject end - should "have a trace created from the original exception's unfiltered backtrace" do - assert_equal @exception.backtrace.join("\n"), subject.trace + should "default itself when created from nil" do + assert_equal ["No backtrace"], Backtrace.new end end end