Skip to content

Commit

Permalink
Tap Formatter is RSpec3 compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Grider committed Mar 26, 2014
1 parent 6d2187a commit 693f6ec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 45 deletions.
28 changes: 10 additions & 18 deletions lib/rspec-extra-formatters/tap_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,37 +43,28 @@ def initialize(output)
@total = 0
end

def start(example_count)
super(example_count)

This comment has been minimized.

Copy link
@cbandy

cbandy Mar 26, 2014

Looking more closely, I see that there is some work happening in BaseFormatter.start. It is probably good to call super for start and close.

It's irritating that some of these methods are on the base class and some are not... e.g. does every formatter need to respond to pending_examples with a correct array of examples?

This comment has been minimized.

Copy link
@mrerrormessage

mrerrormessage Mar 26, 2014

I agree, it's annoying. I'm going to see what the other guy says, as the formatter appears to be working correctly without the call to super.

This comment has been minimized.

Copy link
@JonRowe

JonRowe Mar 28, 2014

Hi! I'm the member of the RSpec team behind these changes... Just thought I'd chip in, feel free to ask me any questions.

It's irritating that some of these methods are on the base class and some are not...

Previously the removed ones were no-op's. I'd rather people thought about what they're supering too, and wether they need that functionality, ideally I want people to not inherit from our formatters at all, so if theres anything we can extract to make it easier, let us know.

e.g. does every formatter need to respond to pending_examples with a correct array of examples?

No, in fact that's a core reason for the API change, I want you to be able to only implement what you need :)

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
Expand Down
57 changes: 30 additions & 27 deletions spec/rspec-extra-formatters/tap_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,30 @@

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

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")
Expand All @@ -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)
Expand All @@ -75,37 +85,35 @@
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")
end

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

0 comments on commit 693f6ec

Please sign in to comment.