Skip to content

Instantly share code, notes, and snippets.

@applicake
Created October 31, 2011 10:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save applicake/1d742e7a2366f195f71c to your computer and use it in GitHub Desktop.
Save applicake/1d742e7a2366f195f71c to your computer and use it in GitHub Desktop.
Chuck testar rspec formatter

Chuck testr RSpec formatter

This formatter is a game changer... Nope! It's just Chuck TestaR

Requirements

It requires rspec and growl_notify gem. It works best on OS X.

Installation

Since we're using growl notifications you may want to download two additional images (not included in the gist):

curl http://img.skitch.com/20111031-8x26je3q4x41gy17qhh63j539f.png > chuck-normal.png
curl http://img.skitch.com/20111031-gmsuuk85ti7tircttd1237u7u7.png > chuck-nope.png

After that, you're good to go!

Usage

$ rspec -r ./chuck_testar.rb -f ChuckTestar -c chuck_testar_spec.rb
require 'rspec/core/formatters/base_text_formatter'
require './notifications'
require './portable'
class ChuckTestar < RSpec::Core::Formatters::BaseTextFormatter
def icon(name)
File.join(File.dirname(__FILE__), name)
end
def notify(text, icon_filename)
if Object.const_defined? "GrowlNotify"
GrowlNotify.normal({
:title => 'RSpec',
:description => text,
:icon => icon(icon_filename)
})
end
end
def say(text)
if Portable.platform == 'linux'
`echo "#{text}" | espeak`
elsif Portable.platform == 'osx'
`say "#{text}"`
end
end
def notify_with_voice!(text, icon_filename)
notify(text, icon_filename)
say(text)
end
def delay(seconds)
sleep(seconds)
end
def start(example_count)
super(example_count)
output.print green('Y')
end
def stop
output.print green('p')
output.print green("\n\nYour tests pass!\n")
notify_with_voice!('Your tests pass', 'chuck-normal.png')
delay(2)
if @failed_examples.length > 0
output.print magenta("\n\nNope! It's just Chuck Testa!")
notify_with_voice!("Nope! It\'s just Chuck Testa!", 'chuck-nope.png')
delay(1)
end
end
def example_passed(example)
super(example)
output.print green('e')
end
def example_pending(example)
super(example)
output.print green('e')
end
def example_failed(example)
super(example)
output.print green('e')
end
def start_dump
super()
output.puts
end
end
require './chuck_testar'
describe ChuckTestar do
let(:output) { StringIO.new }
let(:formatter) { ChuckTestar.new(output) }
before do
formatter.stub(:delay => true)
end
describe "output" do
let(:example) {
double("example 1",
:execution_result => {:status => 'failed', :exception => Exception.new }
)
}
it "displays 'Y' at the start of the suite" do
formatter.start(1)
output.string.should =~ /Y/
end
it "displays 'e' when examples pass" do
formatter.example_passed(example)
output.string.should =~ /e/
end
it "displays 'e' when examples fails" do
formatter.example_failed(example)
output.string.should =~ /e/
end
it "displays 'p' at the end of tests suite" do
formatter.stub(:notify_with_voice! => true)
formatter.stop
output.string.should =~ /p/
end
end
describe "an failing example" do
it "fails" do
false.should be_true
end
end
describe "notifications" do
before do
formatter.stub(:say => true)
end
it "display success notification when spec pass" do
formatter.example_passed(example)
GrowlNotify.should_receive(:normal).with(:title => 'RSpec', :description => 'Your tests pass', :icon => formatter.icon('chuck-normal.png'))
formatter.stop
end
it "display success and fail notifications when spec fail" do
formatter.example_failed(example)
GrowlNotify.should_receive(:normal).with(:title => 'RSpec', :description => 'Your tests pass', :icon => formatter.icon('chuck-normal.png'))
GrowlNotify.should_receive(:normal).with(:title => 'RSpec', :description => "Nope! It's just Chuck Testa!", :icon => formatter.icon('chuck-nope.png'))
formatter.stop
end
end
end
begin
require "growl_notify"
GrowlNotify.config do |config|
config.notifications = ["Chuck Testar"]
config.default_notifications = ["Chuck Testar"]
config.application_name = "Chuck Testar RSpec formatter" # this shoes up in the growl applications list in systems settings
end
rescue LoadError
puts "Please install growl_notify gem to enable growl notifications"
end
module Portable
def self.platform
if RbConfig::CONFIG['host_os'] =~ /mswin|windows|cygwin/i
'windows'
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
'osx'
else
'linux'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment