Skip to content

Instantly share code, notes, and snippets.

@tobynet
Created January 25, 2011 18:59
Show Gist options
  • Select an option

  • Save tobynet/795420 to your computer and use it in GitHub Desktop.

Select an option

Save tobynet/795420 to your computer and use it in GitHub Desktop.
SuperMarioSoundFormatter,it's one of supporting tools for EDD(Enjoyment Driven Development)
# -*- encoding: utf-8 -*-
# $ cat spec/spec.opts
# --require ~/lib/supermario_progress_bar_formatter.rb
# --format Spec::Runner::Formatter::SuperMarioProgressBarFormatter
require 'spec/runner/formatter/base_text_formatter'
require 'spec/runner/formatter/no_op_method_missing'
module Spec::Runner::Formatter
class SuperMarioProgressBarFormatter < BaseTextFormatter
include NOOPMethodMissing
def example_failed(example, counter, failure)
@output.print colorize_failure('☠', failure)
@output.flush
end
def example_passed(example)
@output.print green('$')
@output.flush
end
def example_pending(example, message, deprecated_pending_location=nil)
super
@output.print yellow('▓')
@output.flush
end
def start_dump
@output.puts
@output.flush
end
end
end
# -*- encoding: utf-8 -*-
# === What's this? ===
#
# A supporting tool for EDD(Enjoyment Driven Development)
# When you run rspec with this tool, you will get funny sound effects in your brain.
#
# === Install ===
# 0. download this file and put on ~/lib/
# 1. put sound files on ~/autotest/sound/*.mp3 (this is default directory)
# (for examples 1)
# $ mkdir -p ~/autotest/sound/
# $ cd ~/autotest/sound/
# $ wget http://dl.dropbox.com/u/465602/smb_sound_effects_for_edd.zip
# $ unzip smb_sound_effects_for_edd.zip
#
# (for examples 2)
# put on ~/autotest/sound/
# start*.mp3 : when starts rspec
# example_pending*.mp3 : pending a example
# example_passed*.mp3 : passed a example
# example_failed*.mp3 : failed a example
# summary_pending*.mp3 : got pending results when finished all examples
# summary_passed*.mp3 : got passed results when finished all examples
# summary_failed*.mp3 : got failed results when finished all examples
#
# 2. Setup the command line music player. (default player is "afplay" in PATH)
# (for examples)
# $ sudo aptitude install mplayer # or mplayer-nogui
# $ sudo vim /usr/local/bin/afplay
# $ cat /usr/local/bin/afplay # like that :p
# #!/bin/sh
# /usr/bin/mplayer "$1" > /dev/null 2>&1 &
# $ /usr/bin/env afplay ~/autotest/sound/example_passed*.mp3 # testing
#
# 3. run rspec with Spec::Runner::Formatter::SuperMarioSoundFormatter
# (for examples)
#
# $ cat spec/spec.opts
# --require ~/lib/supermario_sound_formatter.rb
# --format Spec::Runner::Formatter::SuperMarioSoundFormatter
# $ spec spec/*_spec.rb
# or
# $ rspec spec/*_spec.rb
# or
# $ autotest
# or
# # your spec
#
# 4. Enjoy your development!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# === TODO ===
# * make gems
# * support ruby 1.9
# * writing customize configs
require 'rubygems'
require 'spec/runner/formatter/base_formatter'
require 'spec/runner/formatter/no_op_method_missing'
require 'active_support'
module Spec
module Runner
module Formatter
module SoundPlayer
mattr_accessor :player_name
mattr_accessor :sound_path
mattr_accessor :sleep_wait_interval
mattr_accessor :uses_thread
@@player_name = "/usr/bin/env afplay"
@@sound_path = "~/autotest/sound/"
@@sleep_wait_interval = 0.1
@@uses_thread = true
class << self
def sound_files(name)
Dir[File.expand_path(File.join(sound_path, ''+ name.to_s + '*.mp3'))]
end
def random_file(files)
files.empty? ? nil : files[rand(files.size)]
end
def run_play_command(name)
sound_file = random_file(sound_files(name))
return unless sound_file
system [player_name, sound_file].join " "
sleep sleep_wait_interval if sleep_wait_interval
end
def play(name)
if uses_thread
Thread.new { run_play_command(name) }
else
run_play_command(name)
end
end
end
end
class SuperMarioSoundFormatter < BaseFormatter
include NOOPMethodMissing
def start(example_count)
SoundPlayer::play("start")
end
def example_failed(example, counter, failure)
SoundPlayer::play("example_failed")
end
def example_passed(example)
SoundPlayer::play("example_passed")
end
def example_pending(example, message, deprecated_pending_location=nil)
SoundPlayer::play("example_pending")
end
def start_dump
end
def dump_failure(counter, failure)
SoundPlayer::play("dump_failure")
end
def dump_summary(duration, example_count, failure_count, pending_count)
return if dry_run?
if failure_count == 0
if pending_count > 0
SoundPlayer::play("summary_pending")
else
SoundPlayer::play("summary_passed")
end
else
SoundPlayer::play("summary_failed")
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment