diff --git a/lib/rspec-extra-formatters/tap_formatter.rb b/lib/rspec-extra-formatters/tap_formatter.rb index cc3f5b5..80bf478 100644 --- a/lib/rspec-extra-formatters/tap_formatter.rb +++ b/lib/rspec-extra-formatters/tap_formatter.rb @@ -29,6 +29,7 @@ require "rspec/core/formatters/base_formatter" class TapFormatter < RSpec::Core::Formatters::BaseFormatter + RSpec::Core::Formatters.register self, :example_passed, :example_failed, :example_pending attr_reader :total $VERBOSE = nil @@ -42,37 +43,28 @@ def initialize(output) @total = 0 end - def start(example_count) - super(example_count) + def start(example_count_notification) output.puts("TAP version 13") - output.puts("1.." + example_count.to_s) - + output.puts("1.." + example_count_notification.count.to_s) end - def example_passed(example) - super(example) - tap_example_output(OK, example) + def example_passed(notification) + tap_example_output(OK, notification.example) end - def example_pending(example) - super(example) - tap_example_output(NOT_OK, example, SKIP) + def example_pending(notification) + tap_example_output(NOT_OK, notification.example, SKIP) end - def example_failed(example) - super(example) - tap_example_output(NOT_OK, example) + def example_failed(notification) + tap_example_output(NOT_OK, notification.example) output.puts(" ---") - my_exception = example.exception.to_s + my_exception = notification.example.exception.to_s my_exception.gsub! /"/, '' output.puts(" #{my_exception} ") output.puts(" ...") end - def dump_summary(duration, example_count, failure_count, pending_count) - super(duration, example_count, failure_count, pending_count) - end - private def tap_example_output(ok, example, modifier='') @total += 1 diff --git a/spec/rspec-extra-formatters/tap_formatter_spec.rb b/spec/rspec-extra-formatters/tap_formatter_spec.rb index f1d3712..87a7110 100644 --- a/spec/rspec-extra-formatters/tap_formatter_spec.rb +++ b/spec/rspec-extra-formatters/tap_formatter_spec.rb @@ -31,6 +31,17 @@ describe TapFormatter do + def example_notification(description, opts={}) + metadata = { + :execution_result => {:run_time => 0.01}, + :full_description => description, + :file_path => "./spec/rspec-extra-formatters/tap_formatter_spec.rb" + }.merge(opts) + dummy_example = double(:dummy_example) + allow(dummy_example).to receive(:metadata).and_return(metadata) + RSpec::Core::Notifications::ExampleNotification.new(dummy_example) + end + it "should initialize the counter to 0" do expect(TapFormatter.new(StringIO.new).total).to eql(0) end @@ -38,12 +49,12 @@ describe "example_passed" do it "should increment the counter and use the full_description attribute" do - example = double("example") - expect(example).to receive(:metadata).and_return({:full_description => "foobar"}) + notification = example_notification("example") + expect(notification.example).to receive(:metadata).and_return({:full_description => "foobar"}) output = StringIO.new f = TapFormatter.new(output) - f.example_passed(example) + f.example_passed(notification) expect(f.total).to eql(1) expect(output.string).to eq("ok 1 - foobar\n") @@ -54,13 +65,12 @@ describe "example_failed" do it "should increment the counter and use the full_description attribute" do - example = double("example") - allow(example).to receive(:exception) { "exception message" } - expect(example).to receive(:metadata).and_return({:full_description => "foobar"}) + notification = example_notification("example", {:full_description => "foobar"}) + allow(notification.example).to receive(:exception) { "exception message" } output = StringIO.new f = TapFormatter.new(output) - f.example_failed(example) + f.example_failed(notification) expect(f.total).to eql(1) expect(output.string).to eq(<<-EOF) @@ -75,13 +85,12 @@ describe "example_pending" do it "should do the same as example_failed with SKIP comment" do - example = double("example") - allow(example).to receive(:exception) { "exception message" } - expect(example).to receive(:metadata).and_return({:full_description => "foobar"}) + notification = example_notification("example", {:full_description => "foobar"}) + allow(notification.example).to receive(:exception) { "exception message" } output = StringIO.new f = TapFormatter.new(output) - f.example_pending(example) + f.example_pending(notification) expect(f.total).to eql(1) expect(output.string).to eq("not ok 1 - # SKIP foobar\n") @@ -89,23 +98,22 @@ end - describe "dump_summary" do + describe "start" do - it "should print the number of tests if there were tests" do - example = double("example") - allow(example).to receive(:exception) { "exception message" } - expect(example).to receive(:metadata).and_return({:full_description => "foobar"}) - expect(example).to receive(:metadata).and_return({:full_description => "foobar"}) - expect(example).to receive(:metadata).and_return({:full_description => "foobar"}) + it "should print the number of tests" do + notification = example_notification("example", {:full_description => "foobar"}) + allow(notification.example).to receive(:exception) { "exception message" } output = StringIO.new f = TapFormatter.new(output) - f.example_passed(example) - f.example_failed(example) - f.example_pending(example) - f.dump_summary(0.1, 3, 1, 1) + f.start(double(RSpec::Core::Notifications::CountNotification, :count => 3)) + f.example_passed(notification) + f.example_failed(notification) + f.example_pending(notification) expect(output.string).to eq(<<-EOF) +TAP version 13 +1..3 ok 1 - foobar not ok 2 - foobar --- @@ -115,11 +123,6 @@ EOF end - it "should print nothing if there were not tests" do - f = TapFormatter.new(@output) - f.dump_summary(0, 0, 0, 0) - end - end end