manveru (owner)

Revisions

gist: 61361 Download_button fork
public
Description:
How to use Bacon & Heckle
Public Clone URL: git://gist.github.com/61361.git
Embed All Files: show embed
cgi.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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