test-unit-must 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +187 -0
  2. data/lib/test/unit/must.rb +250 -0
  3. metadata +57 -0
data/README.md ADDED
@@ -0,0 +1,187 @@
1
+ # test-unit-must - Selfish "must" assertions for Test::Unit
2
+
3
+ test-unit-must is a set of assertions for [Test::Unit] that treat the receiver
4
+ of an assertion method call as the subject of the assertion. In other words,
5
+ these are [MiniTest::Spec]-style assertions, but their names only begin with
6
+ `must_` or `must_not_`; there is no `wont_` or `refute_` prefix.
7
+
8
+ ------------------------------------------------------------------------------
9
+ Compatibility
10
+ ------------------------------------------------------------------------------
11
+
12
+ test-unit-must was designed for [Test::Unit] 2.2.0. However, I found that it
13
+ works just as well with the MiniTest library bundled in Ruby 1.9.2! :-) In
14
+ addition, it politely accomodates [Rails 3's ActiveSupport extensions to
15
+ Test::Unit](
16
+ http://rails.rubyonrails.org/classes/ActiveSupport/Testing/SetupAndTeardown/ForClassicTestUnit.html).
17
+
18
+ ------------------------------------------------------------------------------
19
+ Installation
20
+ ------------------------------------------------------------------------------
21
+
22
+ As a Ruby gem:
23
+
24
+ gem install test-unit-must
25
+
26
+ As a Git clone:
27
+
28
+ git clone git://github.com/sunaku/test-unit-must
29
+
30
+ ------------------------------------------------------------------------------
31
+ Usage
32
+ ------------------------------------------------------------------------------
33
+
34
+ Load the library somewhere in your test suite, such as in your test helper:
35
+
36
+ require 'test/unit/must'
37
+
38
+ In your test methods, call `must_*` methods on any Ruby object you like:
39
+
40
+ class ExampleTest < Test::Unit::TestCase
41
+ def test_example
42
+ ary = (1..10).to_a
43
+
44
+ ary.must_not_be_empty
45
+ assert_not_empty ary
46
+
47
+ ary.length.must_equal 10
48
+ assert_equal 10, ary.length
49
+
50
+ ary.must_not_equal []
51
+ assert_not_equal [], ary
52
+
53
+ ary.first.must_equal 1
54
+ assert_equal 1, ary.first
55
+
56
+ (ary[1] * 5).must_equal ary.length
57
+ assert_equal ary.length, (ary[1] * 5)
58
+
59
+ # and so on ...
60
+ end
61
+ end
62
+
63
+ Note that you can still use traditional [Test::Unit] assertions freely.
64
+
65
+ ------------------------------------------------------------------------------
66
+ Reference
67
+ ------------------------------------------------------------------------------
68
+
69
+ Here is the mapping from test-unit-must assertions to [Test::Unit] assertions.
70
+
71
+ def must_alias_method alias_name, original_name, message=nil
72
+ assert_alias_method self, alias_name, original_name, message
73
+
74
+ def must_be_boolean message=nil
75
+ assert_boolean self, message
76
+
77
+ def must_compare operator, expected, message=nil
78
+ assert_compare expected, operator, self, message
79
+
80
+ def must_define_constant constant_name, message=nil
81
+ assert_const_defined self, constant_name, message
82
+
83
+ def must_not_define_constant constant_name, message=nil
84
+ assert_not_const_defined self, constant_name, message
85
+
86
+ def must_be_empty message=nil
87
+ assert_empty self, message
88
+
89
+ def must_not_be_empty message=nil
90
+ assert_not_empty self, message
91
+
92
+ def must_equal expected, message=nil
93
+ assert_equal expected, self, message
94
+
95
+ def must_not_equal expected, message=nil
96
+ assert_not_equal expected, self, message
97
+
98
+ def must_be_false message=nil
99
+ assert_false self, message
100
+
101
+ def must_be_in_delta expected_float, delta=0.001, message=nil
102
+ assert_in_delta expected_float, self, delta, message
103
+
104
+ def must_not_be_in_delta expected_float, delta=0.001, message=nil
105
+ assert_not_in_delta expected_float, self, delta, message
106
+
107
+ def must_be_in_epsilon expected_float, epsilon=0.001, message=nil
108
+ assert_in_epsilon expected_float, self, epsilon, message
109
+
110
+ def must_not_be_in_epsilon expected_float, epsilon=0.001, message=nil
111
+ assert_not_in_epsilon expected_float, self, epsilon, message
112
+
113
+ def must_include member, message=nil
114
+ assert_include self, member, message
115
+
116
+ def must_not_include member, message=nil
117
+ assert_not_include self, member, message
118
+
119
+ def must_be_instance_of klass, message=nil
120
+ assert_instance_of klass, self, message
121
+
122
+ def must_be_kind_of klass, message=nil
123
+ assert_kind_of klass, self, message
124
+
125
+ def must_match pattern, message=nil
126
+ assert_match pattern, self, message
127
+
128
+ def must_not_match pattern, message=nil
129
+ assert_not_match pattern, self, message
130
+
131
+ def must_be_nil message=nil
132
+ assert_nil self, message
133
+
134
+ def must_not_be_nil message=nil
135
+ assert_not_nil self, message
136
+
137
+ def must_operate operator, argument, message=nil
138
+ assert_operator self, operator, argument, message
139
+
140
+ def must_raise *arguments
141
+ assert_raise *arguments, &self
142
+
143
+ def must_raise_kind_of *arguments
144
+ assert_raise_kind_of *arguments, &self
145
+
146
+ def must_raise_message expected, message=nil
147
+ assert_raise_message expected, message, &self
148
+
149
+ def must_raise_nothing *arguments
150
+ assert_nothing_raised *arguments, &self
151
+
152
+ def must_be expected, message=nil
153
+ assert_same expected, self, message
154
+
155
+ def must_not_be expected, message=nil
156
+ assert_not_same expected, self, message
157
+
158
+ def must_receive send_array, message=nil
159
+ assert_send [self] + send_array, message
160
+
161
+ def must_not_receive send_array, message=nil
162
+ assert_not_send [self] + send_array, message
163
+
164
+ def must_respond_to method, message=nil
165
+ assert_respond_to self, method, message
166
+
167
+ def must_not_respond_to method, message=nil
168
+ assert_not_respond_to self, method, message
169
+
170
+ def must_throw expected_object, message=nil
171
+ assert_throw expected_object, message, &self
172
+
173
+ def must_throw_nothing *arguments
174
+ assert_nothing_thrown *arguments, &self
175
+
176
+ def must_be_true message=nil
177
+ assert_true self, message
178
+
179
+ ------------------------------------------------------------------------------
180
+ License
181
+ ------------------------------------------------------------------------------
182
+
183
+ Released under the ISC license. See the `lib/test/unit/must.rb` file.
184
+
185
+ [Test::Unit]: http://test-unit.rubyforge.org/
186
+ [MiniTest]: http://rubydoc.info/stdlib/minitest/
187
+ [MiniTest::Spec]: http://rubydoc.info/stdlib/minitest/
@@ -0,0 +1,250 @@
1
+ ##############################################################################
2
+ #
3
+ # test-unit-must - Selfish "must" assertions for Test::Unit
4
+ # https://github.com/sunaku/test-unit-must
5
+ #
6
+ ##############################################################################
7
+ #
8
+ # (the ISC license)
9
+ #
10
+ # Copyright 2011 Suraj N. Kurapati <sunaku@gmail.com>
11
+ #
12
+ # Permission to use, copy, modify, and/or distribute this software for any
13
+ # purpose with or without fee is hereby granted, provided that the above
14
+ # copyright notice and this permission notice appear in all copies.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19
+ # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21
+ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22
+ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
+ #
24
+ ##############################################################################
25
+
26
+ require 'test/unit'
27
+
28
+ module Test::Unit::Must
29
+ module CaptureCurrentTestCase
30
+ def self.included target
31
+ target.module_eval do
32
+ alias ebb6cc8f_e966_4d1f_969f_530ea365eb36 run
33
+ def run *arguments, &block
34
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36 = self
35
+ ebb6cc8f_e966_4d1f_969f_530ea365eb36 *arguments, &block
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ module Assertions
42
+ def must_alias_method alias_name, original_name, message=nil
43
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
44
+ :assert_alias_method, self, alias_name, original_name, message
45
+ end
46
+
47
+ def must_be_boolean message=nil
48
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
49
+ :assert_boolean, self, message
50
+ end
51
+
52
+ def must_compare operator, expected, message=nil
53
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
54
+ :assert_compare, expected, operator, self, message
55
+ end
56
+
57
+ def must_define_constant constant_name, message=nil
58
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
59
+ :assert_const_defined, self, constant_name, message
60
+ end
61
+
62
+ def must_not_define_constant constant_name, message=nil
63
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
64
+ :assert_not_const_defined, self, constant_name, message
65
+ end
66
+
67
+ def must_be_empty message=nil
68
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
69
+ :assert_empty, self, message
70
+ end
71
+
72
+ def must_not_be_empty message=nil
73
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
74
+ :assert_not_empty, self, message
75
+ end
76
+
77
+ def must_equal expected, message=nil
78
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
79
+ :assert_equal, expected, self, message
80
+ end
81
+
82
+ def must_not_equal expected, message=nil
83
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
84
+ :assert_not_equal, expected, self, message
85
+ end
86
+
87
+ def must_be_false message=nil
88
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
89
+ :assert_false, self, message
90
+ end
91
+
92
+ def must_be_in_delta expected_float, delta=0.001, message=nil
93
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
94
+ :assert_in_delta, expected_float, self, delta, message
95
+ end
96
+
97
+ def must_not_be_in_delta expected_float, delta=0.001, message=nil
98
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
99
+ :assert_not_in_delta, expected_float, self, delta, message
100
+ end
101
+
102
+ def must_be_in_epsilon expected_float, epsilon=0.001, message=nil
103
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
104
+ :assert_in_epsilon, expected_float, self, epsilon, message
105
+ end
106
+
107
+ def must_not_be_in_epsilon expected_float, epsilon=0.001, message=nil
108
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
109
+ :assert_not_in_epsilon, expected_float, self, epsilon, message
110
+ end
111
+
112
+ def must_include member, message=nil
113
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
114
+ :assert_include, self, member, message
115
+ end
116
+
117
+ def must_not_include member, message=nil
118
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
119
+ :assert_not_include, self, member, message
120
+ end
121
+
122
+ def must_be_instance_of klass, message=nil
123
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
124
+ :assert_instance_of, klass, self, message
125
+ end
126
+
127
+ def must_be_kind_of klass, message=nil
128
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
129
+ :assert_kind_of, klass, self, message
130
+ end
131
+
132
+ def must_match pattern, message=nil
133
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
134
+ :assert_match, pattern, self, message
135
+ end
136
+
137
+ def must_not_match pattern, message=nil
138
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
139
+ :assert_not_match, pattern, self, message
140
+ end
141
+
142
+ def must_be_nil message=nil
143
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
144
+ :assert_nil, self, message
145
+ end
146
+
147
+ def must_not_be_nil message=nil
148
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
149
+ :assert_not_nil, self, message
150
+ end
151
+
152
+ def must_operate operator, argument, message=nil
153
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
154
+ :assert_operator, self, operator, argument, message
155
+ end
156
+
157
+ def must_raise *arguments
158
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
159
+ :assert_raise, *arguments, &self
160
+ end
161
+
162
+ def must_raise_kind_of *arguments
163
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
164
+ :assert_raise_kind_of, *arguments, &self
165
+ end
166
+
167
+ def must_raise_message expected, message=nil
168
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
169
+ :assert_raise_message, expected, message, &self
170
+ end
171
+
172
+ def must_raise_nothing *arguments
173
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
174
+ :assert_nothing_raised, *arguments, &self
175
+ end
176
+
177
+ def must_be expected, message=nil
178
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
179
+ :assert_same, expected, self, message
180
+ end
181
+
182
+ def must_not_be expected, message=nil
183
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
184
+ :assert_not_same, expected, self, message
185
+ end
186
+
187
+ def must_receive send_array, message=nil
188
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
189
+ :assert_send, [self] + send_array, message
190
+ end
191
+
192
+ def must_not_receive send_array, message=nil
193
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
194
+ :assert_not_send, [self] + send_array, message
195
+ end
196
+
197
+ def must_respond_to method, message=nil
198
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
199
+ :assert_respond_to, self, method, message
200
+ end
201
+
202
+ def must_not_respond_to method, message=nil
203
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
204
+ :assert_not_respond_to, self, method, message
205
+ end
206
+
207
+ def must_throw expected_object, message=nil
208
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
209
+ :assert_throw, expected_object, message, &self
210
+ end
211
+
212
+ def must_throw_nothing *arguments
213
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
214
+ :assert_nothing_thrown, *arguments, &self
215
+ end
216
+
217
+ def must_be_true message=nil
218
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send \
219
+ :assert_true, self, message
220
+ end
221
+
222
+ # Implicitly handle negations for positive assertion methods
223
+ # that lack an explicitly defined negative assertion method.
224
+ def method_missing method, *arguments, &block
225
+ if method =~ /^(must_)(not_)/ and respond_to? positive = $1 + $'
226
+ $ebb6cc8f_e966_4d1f_969f_530ea365eb36.send :assert_fail_assertion do
227
+ __send__ positive, *arguments, &block
228
+ end
229
+ else
230
+ super
231
+ end
232
+ end
233
+ end
234
+ end
235
+
236
+ # Rails 3 replaces Test::Unit::TestSuite with its own class, so
237
+ # the standard Test::Unit::TestCase#run() method is never called.
238
+ if defined? ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit
239
+ module ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit
240
+ include Test::Unit::Must::CaptureCurrentTestCase
241
+ end
242
+ end
243
+
244
+ class Test::Unit::TestCase
245
+ include Test::Unit::Must::CaptureCurrentTestCase
246
+ end
247
+
248
+ class Object
249
+ include Test::Unit::Must::Assertions
250
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: test-unit-must
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Suraj N. Kurapati
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-29 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - README.md
27
+ - lib/test/unit/must.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/sunaku/test-unit-must
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.6.2
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Selfish "must" assertions for Test::Unit
56
+ test_files: []
57
+