ae 1.7.4 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,7 +16,6 @@ else
16
16
  #
17
17
  # Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
18
18
  # All rights reserved.
19
-
20
19
  class BasicObject #:nodoc:
21
20
 
22
21
  # Hide the method named +name+ in the BlankSlate class. Don't
@@ -1,23 +1,151 @@
1
1
  module AE
2
2
 
3
- module CheckOK
4
- def check(&block)
5
- @_check = block
3
+ # The Ok mixin is a reusable assertion helper that
4
+ # makes it easy to construct parameterized assertions
5
+ # with an elegant syntax.
6
+ #
7
+ module Check
8
+
9
+ # The Check::Proc class encapsulates a labeled procedure
10
+ # for making assertions using the `ok`/`no` methods.
11
+ #
12
+ class Proc
13
+ # Setup new check procedure.
14
+ def initialize(options={}, &check)
15
+ @name = options[:name]
16
+ @message = options[:message] || @name
17
+ @check = check
18
+ end
19
+
20
+ #
21
+ def message(&block)
22
+ if block
23
+ @message = message
24
+ end
25
+ @message
26
+ end
27
+
28
+ #
29
+ def message=(msg)
30
+ @message = msg
31
+ end
32
+
33
+ # Call check procedure.
34
+ def call(*args)
35
+ @check.call(*args)
36
+ end
37
+
38
+ #
39
+ def to_s(*args)
40
+ case @message
41
+ when nil
42
+ @name.to_s
43
+ when ::Proc
44
+ @message.call(*args)
45
+ else
46
+ # TODO: count %\S and apply `% args.map{|a|a.inspect}[0,count]`
47
+ @message.to_s
48
+ end
49
+ end
50
+
51
+ #
52
+ def ok!(*args)
53
+ assert(call(*args), to_s(*args))
54
+ end
55
+
56
+ #
57
+ def no!(*args)
58
+ refute(call(*args), to_s(*args))
59
+ end
6
60
  end
7
61
 
62
+ # TODO: Better way to customize error message so it can have
63
+ # arguments in the messages ?
64
+
65
+ # Built-in check procedures.
66
+ TABLE = {
67
+ :equality => Check::Proc.new(:message=>"should be equal"){|h| h.any?{|a,b| b==a}},
68
+ :case_equality => Check::Proc.new(:message=>"should be equal"){|h| h.any?{|a,b| b===a}}
69
+ }
70
+
71
+ #
72
+ def self.table
73
+ @table ||= TABLE.dup
74
+ end
75
+
76
+ # Define a univerally available ok/no check.
77
+ #
78
+ # AE::Check.define(:palindrome) do |x|
79
+ # x.reverse == x
80
+ # end
81
+ #
82
+ def self.define(name, &block)
83
+ table[name] = Check::Proc.new(name, &block)
84
+ end
85
+
86
+ #
87
+ def check_table
88
+ Check.table
89
+ end
90
+
91
+ # Define an ok/no check procedure. A one-off procedure is defined
92
+ # with a block.
93
+ #
94
+ # check do |x, y|
95
+ # x == y
96
+ # end
97
+ #
98
+ # ok 1,1
99
+ # no 1,2
100
+ #
101
+ # The check method can also be used to define reusable checks.
102
+ #
103
+ # check(:palindrome) do |x|
104
+ # x.reverse == x
105
+ # end
106
+ #
107
+ # This will also cause the current check to be set.
108
+ # Later in the code, the check procedure can be restored
109
+ # by just passing the symbolic name.
110
+ #
111
+ # check :palindrome
112
+ #
113
+ # ok 'abracarba'
114
+ # no 'foolishness'
115
+ #
116
+ def check(name=nil, &block)
117
+ if name
118
+ if block
119
+ check_table[name] = Check::Proc.new(:name=>name, &block)
120
+ end
121
+ @__check__ = check_table[name]
122
+ else
123
+ #raise ArgumentError if block.arity == 0
124
+ @__check__ = Check::Proc.new(&block)
125
+ end
126
+ end
127
+
128
+ #
8
129
  def ok(*args)
9
- @_check.call(*args)
130
+ __check__.ok!(*args)
10
131
  end
11
132
 
12
- #def no(*args)
13
- # @_check.call(*args)
14
- #end
15
- end
133
+ #
134
+ def no(*args)
135
+ __check__.no!(*args)
136
+ end
137
+
138
+ # Returns the current check.
139
+ def __check__
140
+ @__check__ || check_table[:equality]
141
+ end
16
142
 
17
- module World
18
- include CheckOK
19
143
  end
20
144
 
21
145
  end
22
146
 
147
+ module AE::World
148
+ # It's upto the test framework to include where needed.
149
+ include AE::Check
150
+ end
23
151
 
@@ -1,6 +1,5 @@
1
- require 'ae/assertor'
2
-
3
1
  module AE
2
+ require 'ae/assertor'
4
3
 
5
4
  # = Expect
6
5
  #
@@ -57,6 +56,7 @@ module AE
57
56
  # Like #expect but uses the reciever as the object
58
57
  # of expectation.
59
58
  #
59
+ # @example
60
60
  # /x/.expected do
61
61
  # "oooxooo"
62
62
  # end
@@ -69,7 +69,4 @@ module AE
69
69
 
70
70
  end
71
71
 
72
- class ::Object #:nodoc:
73
- include AE::Expect
74
- end
75
-
72
+ # Copyright (c) 2008 Thomas Sawyer
@@ -2,7 +2,7 @@ module AE
2
2
 
3
3
  module Legacy #:nodoc:
4
4
 
5
- # = Test::Unit Legacy Assertions
5
+ # Test::Unit Legacy Assertions
6
6
  #
7
7
  # This module provides a compatibility layer for Test::Unit.
8
8
  # This is an optional module and is intended for providing
@@ -16,21 +16,26 @@ module AE
16
16
  # Private method upon which all of the legacy assertions are based
17
17
  # (except for #assert itself).
18
18
  #
19
+ # @raise [Assertion] If test fails.
20
+ #
21
+ # @return nothing
19
22
  def __assert__(test, msg=nil)
20
23
  msg = "failed assertion (no message given)" unless msg
21
- raise Assertion.new(msg, caller[1..-1]) unless test
24
+ raise Assertion.new(msg, :backtrace=>caller[1..-1]) unless test
22
25
  end
23
26
 
24
27
  private :__assert__
25
28
 
26
29
  # The assertion upon which all other assertions are based.
27
30
  #
31
+ # @example
28
32
  # assert [1, 2].include?(5)
29
33
  #
34
+ # @return [Assertor] if `test` not given
30
35
  def assert(test=nil, msg=nil)
31
36
  if test
32
37
  msg = "failed assertion (no message given)" unless msg
33
- raise Assertion.new(msg, caller) unless test
38
+ raise Assertion.new(msg, :backtrace=>caller) unless test
34
39
  else
35
40
  Assertor.new(self, :backtrace=>caller) # TODO: Probably remove this!
36
41
  end
@@ -38,10 +43,14 @@ module AE
38
43
 
39
44
  # Passes if the block yields true.
40
45
  #
41
- # assert_block "Couldn't do the thing" do
42
- # do_the_thing
43
- # end
46
+ # @example
47
+ # assert_block "Couldn't do the thing" do
48
+ # do_the_thing
49
+ # end
50
+ #
51
+ # @raise [Assertion] if test fails
44
52
  #
53
+ # @return nothing
45
54
  def assert_block(msg=nil) # :yields:
46
55
  test = ! yield
47
56
  msg = "assertion failed" unless msg
@@ -54,8 +63,12 @@ module AE
54
63
  # since a helpful error message is generated when this
55
64
  # one fails that tells you the values of expected and actual.
56
65
  #
66
+ # @example
57
67
  # assert_equal 'MY STRING', 'my string'.upcase
58
68
  #
69
+ # @raise [Assertion] if test fails
70
+ #
71
+ # @return nothing
59
72
  def assert_equal(exp, act, msg=nil)
60
73
  test = (exp == act)
61
74
  msg = "Expected #{act.inspect} to be equal to #{exp.inspect}" unless msg
@@ -64,8 +77,12 @@ module AE
64
77
 
65
78
  # Passes if expected_float and actual_float are equal within delta tolerance.
66
79
  #
80
+ # @example
67
81
  # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
68
82
  #
83
+ # @raise [Assertion] if test fails
84
+ #
85
+ # @return nothing
69
86
  def assert_in_delta(exp, act, delta, msg=nil)
70
87
  test = (exp.to_f - act.to_f).abs <= delta.to_f
71
88
  msg = "Expected #{exp} to be within #{delta} of #{act}" unless msg
@@ -74,8 +91,12 @@ module AE
74
91
 
75
92
  # Passes if object .instance_of? klass
76
93
  #
94
+ # @example
77
95
  # assert_instance_of String, 'foo'
78
96
  #
97
+ # @raise [Assertion] if test fails
98
+ #
99
+ # @return nothing
79
100
  def assert_instance_of(cls, obj, msg=nil)
80
101
  test = (cls === obj)
81
102
  msg = "Expected #{obj} to be a #{cls}" unless msg
@@ -84,8 +105,12 @@ module AE
84
105
 
85
106
  # Passes if object .kind_of? klass
86
107
  #
108
+ # @example
87
109
  # assert_kind_of Object, 'foo'
88
110
  #
111
+ # @raise [Assertion] if test fails
112
+ #
113
+ # @return nothing
89
114
  def assert_kind_of(cls, obj, msg=nil)
90
115
  test = obj.kind_of?(cls)
91
116
  msg = "Expected #{obj.inspect} to be a kind of #{cls}" unless msg
@@ -94,8 +119,12 @@ module AE
94
119
 
95
120
  # Passes if string =~ pattern.
96
121
  #
122
+ # @example
97
123
  # assert_match(/\d+/, 'five, 6, seven')
98
124
  #
125
+ # @raise [Assertion] if test fails
126
+ #
127
+ # @return nothing
99
128
  def assert_match(exp, act, msg=nil)
100
129
  test = (act =~ exp)
101
130
  msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg
@@ -104,8 +133,12 @@ module AE
104
133
 
105
134
  # Passes if object is nil.
106
135
  #
136
+ # @example
107
137
  # assert_nil [1, 2].uniq!
108
138
  #
139
+ # @raise [Assertion] if test fails
140
+ #
141
+ # @return nothing
109
142
  def assert_nil(obj, msg=nil)
110
143
  test = obj.nil?
111
144
  msg = "Expected #{obj.inspect} to be nil" unless msg
@@ -114,8 +147,12 @@ module AE
114
147
 
115
148
  # Passes if regexp !~ string
116
149
  #
150
+ # @example
117
151
  # assert_no_match(/two/, 'one 2 three')
118
152
  #
153
+ # @raise [Assertion] if test fails
154
+ #
155
+ # @return nothing
119
156
  def assert_no_match(exp, act, msg=nil)
120
157
  test = (act !~ exp)
121
158
  msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg
@@ -124,8 +161,12 @@ module AE
124
161
 
125
162
  # Passes if expected != actual
126
163
  #
164
+ # @example
127
165
  # assert_not_equal 'some string', 5
128
166
  #
167
+ # @raise [Assertion] if test fails
168
+ #
169
+ # @return nothing
129
170
  def assert_not_equal(exp, act, msg=nil)
130
171
  test = (exp != act)
131
172
  msg = "Expected #{act.inspect} to not be equal to #{exp.inspect}" unless msg
@@ -134,8 +175,12 @@ module AE
134
175
 
135
176
  # Passes if ! object .nil?
136
177
  #
178
+ # @example
137
179
  # assert_not_nil '1 two 3'.sub!(/two/, '2')
138
180
  #
181
+ # @raise [Assertion] if test fails
182
+ #
183
+ # @return nothing
139
184
  def assert_not_nil(obj, msg=nil)
140
185
  test = ! obj.nil?
141
186
  msg = "Expected #{obj.inspect} to not be nil" unless msg
@@ -144,8 +189,12 @@ module AE
144
189
 
145
190
  # Passes if ! actual .equal? expected
146
191
  #
192
+ # @example
147
193
  # assert_not_same Object.new, Object.new
148
194
  #
195
+ # @raise [Assertion] if test fails
196
+ #
197
+ # @return nothing
149
198
  def assert_not_same(exp, act, msg=nil)
150
199
  test = ! exp.equal?(act)
151
200
  msg = "Expected #{act.inspect} to not be the same as #{exp.inspect}" unless msg
@@ -156,8 +205,12 @@ module AE
156
205
  #
157
206
  # Passes if object1.send(operator, object2) is true.
158
207
  #
208
+ # @example
159
209
  # assert_operator 5, :>=, 4
160
210
  #
211
+ # @raise [Assertion] if test fails
212
+ #
213
+ # @return nothing
161
214
  def assert_operator(o1, op, o2, msg="")
162
215
  test = o1.__send__(op, o2)
163
216
  msg = "Expected #{o1}.#{op}(#{o2}) to be true" unless msg
@@ -166,10 +219,14 @@ module AE
166
219
 
167
220
  # Passes if the block raises one of the given exceptions.
168
221
  #
222
+ # @example
169
223
  # assert_raise RuntimeError, LoadError do
170
224
  # raise 'Boom!!!'
171
225
  # end
172
226
  #
227
+ # @raise [Assertion] if test fails
228
+ #
229
+ # @return nothing
173
230
  def assert_raises(*args)
174
231
  msg = (Module === args.last ? nil : args.pop)
175
232
  begin
@@ -189,6 +246,7 @@ module AE
189
246
  # Provides a way to assert that a procedure
190
247
  # <i>does not</i> raise an exception.
191
248
  #
249
+ # @example
192
250
  # refute_raises(StandardError){ raise }
193
251
  #
194
252
  #def assert_raises!(exception, &block)
@@ -202,8 +260,12 @@ module AE
202
260
 
203
261
  # Passes if +object+ respond_to? +method+.
204
262
  #
263
+ # @example
205
264
  # assert_respond_to 'bugbear', :slice
206
265
  #
266
+ # @raise [Assertion] if test fails
267
+ #
268
+ # @return nothing
207
269
  def assert_respond_to(obj, meth, msg=nil)
208
270
  msg = "Expected #{obj} (#{obj.class}) to respond to ##{meth}" unless msg
209
271
  #flip = (Symbol === obj) && ! (Symbol === meth) # HACK for specs
@@ -214,9 +276,13 @@ module AE
214
276
 
215
277
  # Passes if +actual+ .equal? +expected+ (i.e. they are the same instance).
216
278
  #
279
+ # @example
217
280
  # o = Object.new
218
281
  # assert_same(o, o)
219
282
  #
283
+ # @raise [Assertion] if test fails
284
+ #
285
+ # @return nothing
220
286
  def assert_same(exp, act, msg=nil)
221
287
  msg = "Expected #{act.inspect} to be the same as #{exp.inspect}" unless msg
222
288
  test = exp.equal?(act)
@@ -230,10 +296,12 @@ module AE
230
296
  # * A method
231
297
  # * Arguments to the method
232
298
  #
233
- # Example:
234
- #
299
+ # @example
235
300
  # assert_send [[1, 2], :include?, 4]
236
301
  #
302
+ # @raise [Assertion] if test fails
303
+ #
304
+ # @return nothing
237
305
  def assert_send(send_array, msg=nil)
238
306
  r, m, *args = *send_array
239
307
  test = r.__send__(m, *args)
@@ -243,10 +311,14 @@ module AE
243
311
 
244
312
  # Passes if the block throws expected_symbol
245
313
  #
314
+ # @example
246
315
  # assert_throws :done do
247
316
  # throw :done
248
317
  # end
249
318
  #
319
+ # @raise [Assertion] if test fails
320
+ #
321
+ # @return nothing
250
322
  def assert_throws(sym, msg=nil)
251
323
  msg = "Expected #{sym} to have been thrown" unless msg
252
324
  test = true
@@ -265,8 +337,12 @@ module AE
265
337
 
266
338
  # Flunk always fails.
267
339
  #
340
+ # @example
268
341
  # flunk 'Not done testing yet.'
269
342
  #
343
+ # @raise [Assertion] always
344
+ #
345
+ # @return nothing
270
346
  def flunk(msg=nil)
271
347
  __assert__(false, msg)
272
348
  end