summary refs log tree commit diff
path: root/lib/bacon.rb
blob: bda684dd16274c5fedf93970f87bb43dafee7829 (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Bacon -- small RSpec clone.

module Bacon
  Counter = Hash.new(0)
  ErrorLog = ""

  def self.result_string
    "%d specifications (%d requirements), %d failures, %d errors" % 
      [Counter[:specifications], Counter[:requirements],
       Counter[:failed],         Counter[:error]]
  end

  def self.handle_specification(name)
    puts name
    yield
    puts
  end

  def self.handle_requirement(description)
    print "- #{description}"
    error = yield
    puts " [#{error}]"
  end

  class Error < RuntimeError
    attr_accessor :count_as
    
    def initialize(count_as, message)
      @count_as = count_as
      super message
    end
  end
  
  class Context
    def initialize(name, &block)
      @before = []
      @after = []
      @name = name
      
      Bacon.handle_specification(name) do
        instance_eval(&block)
      end
    end
    
    def before(&block); @before << block; end
    def after(&block);  @after << block; end
    
    def it(description, &block)
      Bacon::Counter[:requirements] += 1
      run_requirement description, block
    end
    
    def run_requirement(description, spec)
      Bacon.handle_requirement description do
        begin
          @before.each { |block| instance_eval(&block) }
          instance_eval(&spec)
          @after.each { |block| instance_eval(&block) }
        rescue Object => e
          ErrorLog << "#{e.class}: #{e.message}\n"
          e.backtrace.find_all { |line| line !~ /\/bacon.rb:\d+/ }.
            each_with_index { |line, i|
            ErrorLog << "\t#{line}#{i==0?": "+@name + " - "+description:""}\n"
          }
          ErrorLog << "\n"
          
          if e.kind_of? Bacon::Error
            Bacon::Counter[e.count_as] += 1
            e.count_as.to_s.upcase
          else
            Bacon::Counter[:errors] += 1
            "ERROR: #{e.class}"
        end
        else
          ""
        end
      end
    end
  end
end


class Object
  def true?; false; end
  def false?; false; end
end

class TrueClass
  def true?; true; end
end

class FalseClass
  def false?; true; end
end

class Proc
  def raise?(*exceptions)
    call
  rescue *(exceptions.empty? ? RuntimeError : exceptions)
    true
  rescue
    false
  else
    false
  end
end

class Object
  def should(*args, &block)
    Should.new(self).be(*args, &block)
  end

  def describe(name, &block)
    Bacon::Counter[:specifications] += 1
    Bacon::Context.new(name, &block)
  end
end

class Should
  # Kills ==, ===, =~, eql?, equal?, frozen?, instance_of?, is_a?,
  # kind_of?, nil?, respond_to?, tainted?
  instance_methods.each { |method|
    undef_method method  if method =~ /\?|^\W+$/
  }
  
  def initialize(object)
    @object = object
    @negated = false
  end

  def not
    @negated = !@negated
    self
  end

  def be(*args, &block)
    case args.size
    when 0
      self
    else
      block = args.shift  unless block_given?
      satisfy(*args, &block)
    end
  end
    
  alias a  be
  alias an be
  
  def satisfy(*args, &block)
    unless @negated ^ yield(@object, *args)
      raise Bacon::Error.new(:failed, "")
    end
    Bacon::Counter[:passed] += 1
    @object
  end

  def method_missing(name, *args, &block)
    satisfy { |x|
      name = "#{name}?"  if name.to_s =~ /\w/
      x.__send__(name, *args, &block)
    }
  end
end