Created
February 10, 2009 11:55
-
-
Save manveru/61361 to your computer and use it in GitHub Desktop.
How to use Bacon & Heckle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec/helper' | |
require 'innate/helper/cgi' | |
require 'heckle' | |
module Bacon | |
class HeckleRunner | |
def initialize(filter, heckle_class = Heckler) | |
@filter, @heckle_class = filter, heckle_class | |
end | |
def heckle_with | |
if @filter =~ /(.*)[#\.](.*)/ | |
heckle_method($1, $2) | |
else | |
heckle_class_or_module(@filter) | |
end | |
end | |
def heckle_method(class_name, method_name) | |
verify_constant(class_name) | |
heckle = @heckle_class.new(class_name, method_name) | |
heckle.validate | |
end | |
def heckle_class_or_module(class_or_module_name) | |
verify_constant(class_or_module_name) | |
pattern = /^#{class_or_module_name}/ | |
classes = [] | |
ObjectSpace.each_object(Class) do |klass| | |
classes << klass if klass.name =~ pattern | |
end | |
ObjectSpace.each_object(Module) do |klass| | |
classes << klass if klass.name =~ pattern | |
end | |
classes.each do |klass| | |
klass.instance_methods(false).each do |method_name| | |
heckle = @heckle_class.new(klass.name, method_name) | |
heckle.validate | |
end | |
end | |
end | |
def verify_constant(name) | |
name.to_class # This is defined in Heckle | |
rescue | |
raise "Heckling failed - \"#{name}\" is not a known class or module" | |
end | |
class Heckler < Heckle | |
def initialize(klass_name, method_name) | |
super(klass_name, method_name) | |
Bacon::Counter[:installed_summary] += 1 | |
end | |
def tests_pass? | |
Bacon.handle_summary | |
return false if $! | |
counter = Bacon::Counter | |
return false if counter[:errors] + counter[:failed] > 0 | |
true | |
end | |
end | |
end | |
module HeckleOutput | |
include TestUnitOutput | |
SPECS = [] | |
def handle_specification(name, &block) | |
SPECS << block | |
end | |
def handle_summary | |
SPECS.each{|block| block.call } | |
# super | |
end | |
end | |
extend HeckleOutput | |
end | |
describe Innate::Helper::CGI do | |
extend Innate::Helper::CGI | |
it 'encode strings' do | |
url_encode('title with spaces').should == 'title+with+spaces' | |
url_encode('[foo]').should == '%5Bfoo%5D' | |
u('//').should == '%2F%2F' | |
end | |
it 'decodes urls' do | |
url_decode('title%20with%20spaces').should == 'title with spaces' | |
url_decode('title+with+spaces').should == 'title with spaces' | |
end | |
it 'ecode and decode an url' do | |
url_decode(u('../ etc/passwd')).should == '../ etc/passwd' | |
end | |
it 'escapes html' do | |
html_escape('& < >').should == '& < >' | |
h('<&>').should == '<&>' | |
h('#{foo}').should == '#{foo}' | |
end | |
it 'unescapes html' do | |
html_unescape('< & >').should == '< & >' | |
end | |
it 'escapes and unescapes again' do | |
html_unescape(html_escape('2 > b && b <= 0')).should == '2 > b && b <= 0' | |
end | |
end | |
Bacon::HeckleRunner.new('Innate::Helper::CGI').heckle_with |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment