Skip to content

Instantly share code, notes, and snippets.

@manveru
Created February 10, 2009 11:55
Show Gist options
  • Save manveru/61361 to your computer and use it in GitHub Desktop.
Save manveru/61361 to your computer and use it in GitHub Desktop.
How to use Bacon & Heckle
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 == '&amp; &lt; &gt;'
h('<&>').should == '&lt;&amp;&gt;'
h('#{foo}').should == '&#35;{foo}'
end
it 'unescapes html' do
html_unescape('&lt; &amp; &gt;').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