test_unit-given 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +6 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +201 -0
- data/README.rdoc +156 -0
- data/Rakefile +27 -0
- data/lib/test/unit/given/simple.rb +84 -0
- data/lib/test/unit/given/strict.rb +34 -0
- data/lib/test/unit/given/test_case.rb +26 -0
- data/lib/test/unit/given/test_that.rb +63 -0
- data/lib/test/unit/given/version.rb +7 -0
- data/lib/test/unit/given.rb +1 -0
- data/test/bootstrap.rb +5 -0
- data/test/test_simple_given.rb +77 -0
- data/test/test_strict_given.rb +54 -0
- data/test/test_test_that.rb +35 -0
- data/test_unit-given.gemspec +24 -0
- metadata +100 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rvm use 1.9.2@test_unit-given --create
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.rdoc
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
= Test::Unit Given
|
|
2
|
+
|
|
3
|
+
Author:: Dave Copeland (mailto:davetron5000 at g mail dot com)
|
|
4
|
+
Copyright:: Copyright (c) 2011 by Dave Copeland
|
|
5
|
+
License:: Distributes under the Apache License, see LICENSE.txt in the source distro
|
|
6
|
+
|
|
7
|
+
Get your Test::Unit test cases fluent, without RSpec, magic, or crazy meta-programming.
|
|
8
|
+
|
|
9
|
+
This gives you two simple tools to make your test cases readable:
|
|
10
|
+
|
|
11
|
+
* Given/When/Then to delineate which parts of your tests do what
|
|
12
|
+
* A method "test_that" that defines test cases with strings instead of method names (much like +test+ in Rails tests)
|
|
13
|
+
|
|
14
|
+
== Install
|
|
15
|
+
|
|
16
|
+
gem install test_unit-given
|
|
17
|
+
|
|
18
|
+
== Example
|
|
19
|
+
|
|
20
|
+
class Circle
|
|
21
|
+
def initialize(radius)
|
|
22
|
+
@radius = radius
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def area
|
|
26
|
+
@radius * @radius * 3.14
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
require 'test/unit/given'
|
|
32
|
+
|
|
33
|
+
class CircleTest < Test::Unit::Given::TestCase
|
|
34
|
+
test_that {
|
|
35
|
+
Given {
|
|
36
|
+
@circle = Circle.new(10)
|
|
37
|
+
}
|
|
38
|
+
When {
|
|
39
|
+
@area = @circle.area
|
|
40
|
+
}
|
|
41
|
+
Then {
|
|
42
|
+
assert_equal 314,@area
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
You can, of course, provide a description for your test if you need to:
|
|
48
|
+
|
|
49
|
+
class CircleTest < Test::Unit::Given::TestCase
|
|
50
|
+
test_that "the area is correctly calculated" do
|
|
51
|
+
Given {
|
|
52
|
+
@circle = Circle.new(10)
|
|
53
|
+
}
|
|
54
|
+
When {
|
|
55
|
+
@area = @circle.area
|
|
56
|
+
}
|
|
57
|
+
Then {
|
|
58
|
+
assert_equal 314,@area
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
If you don't want to extend our base class, you can mix in the features explicitly:
|
|
63
|
+
|
|
64
|
+
require 'test/unit/given/simple'
|
|
65
|
+
|
|
66
|
+
class CircleTest < Test::Unit::TestCase
|
|
67
|
+
|
|
68
|
+
include Test::Unit::Given::Simple
|
|
69
|
+
include Test::Unit::Given::TestThat
|
|
70
|
+
|
|
71
|
+
test_that {
|
|
72
|
+
Given {
|
|
73
|
+
@circle = Circle.new(10)
|
|
74
|
+
}
|
|
75
|
+
When {
|
|
76
|
+
@area = @circle.area
|
|
77
|
+
}
|
|
78
|
+
Then {
|
|
79
|
+
assert_equal 314,@area
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
You can also strictly enforce the use in your tests:
|
|
85
|
+
|
|
86
|
+
require 'test/unit/given/strict'
|
|
87
|
+
|
|
88
|
+
class CircleTest < Test::Unit::TestCase
|
|
89
|
+
include Test::Unit::Given::Strict
|
|
90
|
+
include Test::Unit::Given::TestThat
|
|
91
|
+
|
|
92
|
+
test_that {
|
|
93
|
+
@circle = Circle.new(10)
|
|
94
|
+
When {
|
|
95
|
+
# this causes an error, because there's no Given
|
|
96
|
+
@area = @circle.area
|
|
97
|
+
}
|
|
98
|
+
Then {
|
|
99
|
+
assert_equal 314,@area
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
Finally, you can re-use blocks:
|
|
105
|
+
|
|
106
|
+
class CircleTest < Test::Unit::Given::TestCase
|
|
107
|
+
|
|
108
|
+
def circle_with_radius(r)
|
|
109
|
+
@circle = Circle.new(r)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def get_area
|
|
113
|
+
@area = @circle.area
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def area_should_be(area)
|
|
117
|
+
assert_equal area,@area
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
test_that {
|
|
121
|
+
Given circle_with_radius(10)
|
|
122
|
+
When get_radius
|
|
123
|
+
Then area_should_be(314)
|
|
124
|
+
}
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
=== What about block-based assertions, like +assert_raises+
|
|
128
|
+
|
|
129
|
+
You can use the non-strict version like so:
|
|
130
|
+
|
|
131
|
+
class CircleTest < Test::Unit::Given::TestCase
|
|
132
|
+
|
|
133
|
+
test_that "there is no diameter method" do
|
|
134
|
+
Given {
|
|
135
|
+
@cicle = Circle.new(10)
|
|
136
|
+
}
|
|
137
|
+
Then {
|
|
138
|
+
assert_raises NoMethodError do
|
|
139
|
+
When {
|
|
140
|
+
@cirlce.diameter
|
|
141
|
+
}
|
|
142
|
+
end
|
|
143
|
+
}
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
This won't work in strict mode for now.
|
|
148
|
+
|
|
149
|
+
== WTF? Why?
|
|
150
|
+
|
|
151
|
+
Just because you're using Test::Unit doesn't mean you can't write fluent, easy to understand tests.
|
|
152
|
+
You really don't need RSpec, and RSpec has some baggage, such as nonstandard assignment, confusing class_eval
|
|
153
|
+
blocks, and generally replaces stuff you can do in plain Ruby. Here, everything is simple, plain Ruby. No
|
|
154
|
+
magic, nothing to understand.
|
|
155
|
+
|
|
156
|
+
If you like Test::Unit, and you want to make your tests a bit more readable, this is for you.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'bundler'
|
|
2
|
+
require 'rake/clean'
|
|
3
|
+
require 'rake/testtask'
|
|
4
|
+
gem 'rdoc' # I need to use the installed RDoc gem, not what comes with the system
|
|
5
|
+
require 'rdoc/task'
|
|
6
|
+
|
|
7
|
+
include Rake::DSL
|
|
8
|
+
|
|
9
|
+
Bundler::GemHelper.install_tasks
|
|
10
|
+
|
|
11
|
+
desc 'run tests'
|
|
12
|
+
Rake::TestTask.new do |t|
|
|
13
|
+
t.libs << "lib"
|
|
14
|
+
t.libs << "test"
|
|
15
|
+
t.test_files = FileList['test/bootstrap.rb','test/test_*.rb']
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
desc 'build rdoc'
|
|
19
|
+
RDoc::Task.new do |rd|
|
|
20
|
+
rd.main = "README.rdoc"
|
|
21
|
+
rd.generator = 'hanna'
|
|
22
|
+
rd.rdoc_files.include("README.rdoc","lib/**/*.rb","bin/**/*")
|
|
23
|
+
rd.title = 'Methadone - Power Up your Command Line Apps'
|
|
24
|
+
rd.markup = "tomdoc"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
task :default => :test
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
module Test
|
|
2
|
+
module Unit
|
|
3
|
+
module Given
|
|
4
|
+
# A simple form that doesn't enforce the use of
|
|
5
|
+
# Given/When/Then. This is merely to make it clear
|
|
6
|
+
# which parts of your test do what
|
|
7
|
+
module Simple
|
|
8
|
+
# Public: Set up the conditions for the test
|
|
9
|
+
#
|
|
10
|
+
# existing_block - a callable object (e.g. a Proc) that will be called immediately
|
|
11
|
+
# by this Given. If nil, &block is expected to be passed
|
|
12
|
+
# block - a block given to this call that will be executed immediately
|
|
13
|
+
# by this Given. If existing_block is non-nil, this is ignored
|
|
14
|
+
#
|
|
15
|
+
# Examples
|
|
16
|
+
#
|
|
17
|
+
# given_block = Given {
|
|
18
|
+
# @foo = "bar"
|
|
19
|
+
# }
|
|
20
|
+
# # => executes block and returns it
|
|
21
|
+
# Given given_block
|
|
22
|
+
# # => executes the block again
|
|
23
|
+
#
|
|
24
|
+
# Returns the block that was executed
|
|
25
|
+
def Given(existing_block=nil,&block)
|
|
26
|
+
call_block_param_or_block_given(existing_block,&block)
|
|
27
|
+
end
|
|
28
|
+
# Public: Execute the code under test
|
|
29
|
+
#
|
|
30
|
+
# existing_block - a callable object (e.g. a Proc) that will be called immediately
|
|
31
|
+
# by this When. If nil, &block is expected to be passed
|
|
32
|
+
# block - a block given to this call that will be executed immediately
|
|
33
|
+
# by this When. If existing_block is non-nil, this is ignored
|
|
34
|
+
#
|
|
35
|
+
# Examples
|
|
36
|
+
#
|
|
37
|
+
# when_block = When {
|
|
38
|
+
# @foo.go
|
|
39
|
+
# }
|
|
40
|
+
# # => executes block and returns it
|
|
41
|
+
# When when_block
|
|
42
|
+
# # => executes the block again
|
|
43
|
+
#
|
|
44
|
+
# Returns the block that was executed
|
|
45
|
+
def When(existing_block=nil,&block)
|
|
46
|
+
Given(existing_block,&block)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Public: Verify the results of the test
|
|
50
|
+
#
|
|
51
|
+
# existing_block - a callable object (e.g. a Proc) that will be called immediately
|
|
52
|
+
# by this Then. If nil, &block is expected to be passed
|
|
53
|
+
# block - a block given to this call that will be executed immediately
|
|
54
|
+
# by this Then. If existing_block is non-nil, this is ignored
|
|
55
|
+
#
|
|
56
|
+
# Examples
|
|
57
|
+
#
|
|
58
|
+
# then_block = Then {
|
|
59
|
+
# assert_equal "bar",@foo
|
|
60
|
+
# }
|
|
61
|
+
# # => executes block and returns it
|
|
62
|
+
# Then then_block
|
|
63
|
+
# # => executes the block again
|
|
64
|
+
#
|
|
65
|
+
# Returns the block that was executed
|
|
66
|
+
def Then(existing_block=nil,&block)
|
|
67
|
+
Given(existing_block,&block)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def call_block_param_or_block_given(existing_block,&block)
|
|
73
|
+
if existing_block.nil?
|
|
74
|
+
block.call
|
|
75
|
+
block
|
|
76
|
+
else
|
|
77
|
+
existing_block.call
|
|
78
|
+
existing_block
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module Test
|
|
2
|
+
module Unit
|
|
3
|
+
module Given
|
|
4
|
+
# A strict Given/When/Then that will generate errors if you
|
|
5
|
+
# omit any one of these in your test.
|
|
6
|
+
module Strict
|
|
7
|
+
include Simple
|
|
8
|
+
|
|
9
|
+
def setup
|
|
10
|
+
@__next = :given
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Set up the conditions for the test, see Test::Unit::Given::Simple::Given
|
|
14
|
+
def Given(existing_block=nil,&block)
|
|
15
|
+
@__next = :when
|
|
16
|
+
call_block_param_or_block_given(existing_block,&block)
|
|
17
|
+
end
|
|
18
|
+
# Execute the code under test, failing if no Given was called first, see Test::Unit::Given::Simple::When
|
|
19
|
+
def When(existing_block=nil,&block)
|
|
20
|
+
raise "No Given block?" unless @__next == :when
|
|
21
|
+
@__next = :then
|
|
22
|
+
call_block_param_or_block_given(existing_block,&block)
|
|
23
|
+
end
|
|
24
|
+
# Verify the results of the test, failing if no When was called first, see Test::Unit::Given::Simple::Then
|
|
25
|
+
def Then(existing_block=nil,&block)
|
|
26
|
+
raise "No When block?" unless @__next == :then
|
|
27
|
+
@__next = :given
|
|
28
|
+
call_block_param_or_block_given(existing_block,&block)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
require 'test/unit/given/simple'
|
|
2
|
+
require 'test/unit/given/test_that'
|
|
3
|
+
|
|
4
|
+
module Test
|
|
5
|
+
module Unit
|
|
6
|
+
module Given
|
|
7
|
+
# Public: A Base class brings in both the Simple Given/Then/When
|
|
8
|
+
# stuff as well as the +test_that+ method you can
|
|
9
|
+
# use to define tests a bit more fluently.
|
|
10
|
+
#
|
|
11
|
+
# Example
|
|
12
|
+
#
|
|
13
|
+
# class TestCircle < Test::Unit::Given::TestCase
|
|
14
|
+
# test_that {
|
|
15
|
+
# Given { @circle = Circle.new(10) }
|
|
16
|
+
# When { @area = @circle.area }
|
|
17
|
+
# Then { assert_equal 314,@area }
|
|
18
|
+
# }
|
|
19
|
+
# end
|
|
20
|
+
class TestCase < Test::Unit::TestCase
|
|
21
|
+
include Simple
|
|
22
|
+
include TestThat
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module Test
|
|
2
|
+
module Unit
|
|
3
|
+
module Given
|
|
4
|
+
# Public: Module that, when included, makes a class method, +test_that+ available
|
|
5
|
+
# to create test methods in a more fluent way. See ClassMethods.
|
|
6
|
+
module TestThat
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
|
|
13
|
+
# Public: Create a new test method with the given optional description and
|
|
14
|
+
# body.
|
|
15
|
+
#
|
|
16
|
+
# description - a string describing what is being tested; write this as a follow on
|
|
17
|
+
# from the phrase "test that". If nil, a name will be constructed
|
|
18
|
+
# based on the block
|
|
19
|
+
# block - the body of the test.
|
|
20
|
+
#
|
|
21
|
+
# Example
|
|
22
|
+
#
|
|
23
|
+
# # Create a rails-style test method
|
|
24
|
+
# test_that "area is computed based on positive radius" do
|
|
25
|
+
# Given {
|
|
26
|
+
# @circle = Circle.new(10)
|
|
27
|
+
# }
|
|
28
|
+
# When {
|
|
29
|
+
# @area = @circle.area
|
|
30
|
+
# }
|
|
31
|
+
# Then {
|
|
32
|
+
# assert_equal 314,@area
|
|
33
|
+
# }
|
|
34
|
+
# end
|
|
35
|
+
#
|
|
36
|
+
# # Create an "anonymous" test, where the test body
|
|
37
|
+
# # is clear enough as to what's being tested
|
|
38
|
+
# test_that {
|
|
39
|
+
# Given a_circle(:radius => 10)
|
|
40
|
+
# When get_area
|
|
41
|
+
# Then area_should_be(314)
|
|
42
|
+
# }
|
|
43
|
+
def test_that(description=nil,&block)
|
|
44
|
+
raise "You must provide a block" if block.nil?
|
|
45
|
+
description = make_up_name(block) if description.nil?
|
|
46
|
+
test_name = "test_#{description.gsub(/\s+/,'_')}".to_sym
|
|
47
|
+
defined = instance_method(test_name) rescue false
|
|
48
|
+
raise "#{test_name} is already defined in #{self}" if defined
|
|
49
|
+
define_method(test_name, &block)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def make_up_name(some_proc)
|
|
55
|
+
name,location = some_proc.source_location
|
|
56
|
+
"anonymous test at #{name}, line #{location}"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'test/unit/given/test_case'
|
data/test/bootstrap.rb
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'test/unit/given'
|
|
3
|
+
|
|
4
|
+
class TestSimpleGiven < Test::Unit::Given::TestCase
|
|
5
|
+
|
|
6
|
+
def test_basics
|
|
7
|
+
Given {
|
|
8
|
+
@x = nil
|
|
9
|
+
}
|
|
10
|
+
When {
|
|
11
|
+
@x = 4
|
|
12
|
+
}
|
|
13
|
+
Then {
|
|
14
|
+
assert_equal 4,@x
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def test_cannot_use_locals
|
|
19
|
+
Given {
|
|
20
|
+
@x = nil
|
|
21
|
+
}
|
|
22
|
+
When {
|
|
23
|
+
x = 4
|
|
24
|
+
}
|
|
25
|
+
Then {
|
|
26
|
+
assert_nil @x
|
|
27
|
+
refute defined? x
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_can_reuse_blocks
|
|
32
|
+
invocations = 0
|
|
33
|
+
x_is_nil = Given {
|
|
34
|
+
@x = nil
|
|
35
|
+
invocations += 1
|
|
36
|
+
}
|
|
37
|
+
x_is_assigned_to_four = When {
|
|
38
|
+
@x = 4
|
|
39
|
+
invocations += 1
|
|
40
|
+
}
|
|
41
|
+
x_should_be_four = Then {
|
|
42
|
+
assert_equal 4,@x
|
|
43
|
+
invocations += 1
|
|
44
|
+
}
|
|
45
|
+
Given x_is_nil
|
|
46
|
+
When x_is_assigned_to_four
|
|
47
|
+
Then x_should_be_four
|
|
48
|
+
assert_equal 6,invocations
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_methods_that_return_blocks
|
|
52
|
+
Given a_nil_x
|
|
53
|
+
When {
|
|
54
|
+
@x = 4
|
|
55
|
+
}
|
|
56
|
+
Then {
|
|
57
|
+
assert_equal 4,@x
|
|
58
|
+
}
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_invert_for_block_based_asserts
|
|
62
|
+
Given a_nil_x
|
|
63
|
+
Then {
|
|
64
|
+
assert_raises NoMethodError do
|
|
65
|
+
When {
|
|
66
|
+
@x + 4
|
|
67
|
+
}
|
|
68
|
+
end
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def a_nil_x
|
|
75
|
+
Proc.new { @x = nil }
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'test/unit/given/strict'
|
|
3
|
+
|
|
4
|
+
class TestStrictGiven < Test::Unit::TestCase
|
|
5
|
+
include Test::Unit::Given::Strict
|
|
6
|
+
|
|
7
|
+
def test_wrong_order_then_first
|
|
8
|
+
assert_raises RuntimeError do
|
|
9
|
+
Then {
|
|
10
|
+
@x = 4
|
|
11
|
+
}
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_basics
|
|
16
|
+
Given {
|
|
17
|
+
@x = nil
|
|
18
|
+
}
|
|
19
|
+
When {
|
|
20
|
+
@x = 4
|
|
21
|
+
}
|
|
22
|
+
Then {
|
|
23
|
+
assert_equal 4,@x
|
|
24
|
+
}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_wrong_order
|
|
28
|
+
assert_raises RuntimeError do
|
|
29
|
+
When {
|
|
30
|
+
@x = 4
|
|
31
|
+
}
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_can_reuse_blocks
|
|
36
|
+
invocations = 0
|
|
37
|
+
x_is_nil = Given {
|
|
38
|
+
@x = nil
|
|
39
|
+
invocations += 1
|
|
40
|
+
}
|
|
41
|
+
x_is_assigned_to_four = When {
|
|
42
|
+
@x = 4
|
|
43
|
+
invocations += 1
|
|
44
|
+
}
|
|
45
|
+
x_should_be_four = Then {
|
|
46
|
+
assert_equal 4,@x
|
|
47
|
+
invocations += 1
|
|
48
|
+
}
|
|
49
|
+
Given x_is_nil
|
|
50
|
+
When x_is_assigned_to_four
|
|
51
|
+
Then x_should_be_four
|
|
52
|
+
assert_equal 6,invocations
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'test/unit/given'
|
|
3
|
+
|
|
4
|
+
class TestSimpleGiven < Test::Unit::Given::TestCase
|
|
5
|
+
|
|
6
|
+
test_that "when assigning @x to 4, it is 4" do
|
|
7
|
+
Given {
|
|
8
|
+
@x = nil
|
|
9
|
+
}
|
|
10
|
+
When {
|
|
11
|
+
@x = 4
|
|
12
|
+
}
|
|
13
|
+
Then {
|
|
14
|
+
assert_equal 4,@x
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
test_that {
|
|
19
|
+
Given {
|
|
20
|
+
@x = nil
|
|
21
|
+
}
|
|
22
|
+
When {
|
|
23
|
+
@x = 4
|
|
24
|
+
}
|
|
25
|
+
Then {
|
|
26
|
+
assert_equal 4,@x
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
def test_that_test_that_barfs_with_no_block
|
|
31
|
+
assert_raises RuntimeError do
|
|
32
|
+
self.class.test_that "foo"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
require "test/unit/given/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = "test_unit-given"
|
|
7
|
+
s.version = Test::Unit::Given::VERSION
|
|
8
|
+
s.platform = Gem::Platform::RUBY
|
|
9
|
+
s.authors = ["David Copeland"]
|
|
10
|
+
s.email = ["davetron5000@gmail.com"]
|
|
11
|
+
s.homepage = ""
|
|
12
|
+
s.summary = %q{Use Given/When/Then in your Test::Unit tests}
|
|
13
|
+
s.description = %q{We don't need no stinkin' RSpec! Get all the fluency you want in your Test::Unit tests, with no magic required, using straight Ruby syntax}
|
|
14
|
+
|
|
15
|
+
s.rubyforge_project = "test_unit-given"
|
|
16
|
+
|
|
17
|
+
s.files = `git ls-files`.split("\n")
|
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
|
+
s.require_paths = ["lib"]
|
|
21
|
+
s.add_development_dependency("rdoc")
|
|
22
|
+
s.add_development_dependency("rake")
|
|
23
|
+
s.add_development_dependency("simplecov")
|
|
24
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: test_unit-given
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- David Copeland
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2011-10-21 00:00:00.000000000Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rdoc
|
|
16
|
+
requirement: &70106635648040 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *70106635648040
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: rake
|
|
27
|
+
requirement: &70106635647620 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: *70106635647620
|
|
36
|
+
- !ruby/object:Gem::Dependency
|
|
37
|
+
name: simplecov
|
|
38
|
+
requirement: &70106635647200 !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ! '>='
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
type: :development
|
|
45
|
+
prerelease: false
|
|
46
|
+
version_requirements: *70106635647200
|
|
47
|
+
description: We don't need no stinkin' RSpec! Get all the fluency you want in your
|
|
48
|
+
Test::Unit tests, with no magic required, using straight Ruby syntax
|
|
49
|
+
email:
|
|
50
|
+
- davetron5000@gmail.com
|
|
51
|
+
executables: []
|
|
52
|
+
extensions: []
|
|
53
|
+
extra_rdoc_files: []
|
|
54
|
+
files:
|
|
55
|
+
- .gitignore
|
|
56
|
+
- .rvmrc
|
|
57
|
+
- Gemfile
|
|
58
|
+
- LICENSE.txt
|
|
59
|
+
- README.rdoc
|
|
60
|
+
- Rakefile
|
|
61
|
+
- lib/test/unit/given.rb
|
|
62
|
+
- lib/test/unit/given/simple.rb
|
|
63
|
+
- lib/test/unit/given/strict.rb
|
|
64
|
+
- lib/test/unit/given/test_case.rb
|
|
65
|
+
- lib/test/unit/given/test_that.rb
|
|
66
|
+
- lib/test/unit/given/version.rb
|
|
67
|
+
- test/bootstrap.rb
|
|
68
|
+
- test/test_simple_given.rb
|
|
69
|
+
- test/test_strict_given.rb
|
|
70
|
+
- test/test_test_that.rb
|
|
71
|
+
- test_unit-given.gemspec
|
|
72
|
+
homepage: ''
|
|
73
|
+
licenses: []
|
|
74
|
+
post_install_message:
|
|
75
|
+
rdoc_options: []
|
|
76
|
+
require_paths:
|
|
77
|
+
- lib
|
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
79
|
+
none: false
|
|
80
|
+
requirements:
|
|
81
|
+
- - ! '>='
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0'
|
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
|
+
none: false
|
|
86
|
+
requirements:
|
|
87
|
+
- - ! '>='
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
requirements: []
|
|
91
|
+
rubyforge_project: test_unit-given
|
|
92
|
+
rubygems_version: 1.8.10
|
|
93
|
+
signing_key:
|
|
94
|
+
specification_version: 3
|
|
95
|
+
summary: Use Given/When/Then in your Test::Unit tests
|
|
96
|
+
test_files:
|
|
97
|
+
- test/bootstrap.rb
|
|
98
|
+
- test/test_simple_given.rb
|
|
99
|
+
- test/test_strict_given.rb
|
|
100
|
+
- test/test_test_that.rb
|