assert 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +17 -0
- data/README.rdoc +77 -0
- data/Rakefile +7 -0
- data/assert.gemspec +21 -0
- data/examples/empty_test.rb +5 -0
- data/examples/results_test.rb +25 -0
- data/examples/single_test.rb +9 -0
- data/lib/assert.rb +8 -0
- data/lib/assert/assertions.rb +253 -0
- data/lib/assert/context.rb +196 -0
- data/lib/assert/options.rb +43 -0
- data/lib/assert/rake_tasks.rb +95 -0
- data/lib/assert/result.rb +164 -0
- data/lib/assert/result_set.rb +14 -0
- data/lib/assert/runner.rb +60 -0
- data/lib/assert/setup/autorun.rb +34 -0
- data/lib/assert/setup/helpers.rb +62 -0
- data/lib/assert/setup/suite.rb +12 -0
- data/lib/assert/setup/view.rb +11 -0
- data/lib/assert/suite.rb +128 -0
- data/lib/assert/test.rb +90 -0
- data/lib/assert/version.rb +3 -0
- data/lib/assert/view/base.rb +54 -0
- data/lib/assert/view/terminal.rb +138 -0
- data/test/assertions/assert_block_test.rb +39 -0
- data/test/assertions/assert_instance_of_test.rb +43 -0
- data/test/assertions/assert_kind_of_test.rb +43 -0
- data/test/assertions/assert_not_block_test.rb +39 -0
- data/test/assertions/assert_not_instance_of_test.rb +43 -0
- data/test/assertions/assert_not_kind_of_test.rb +43 -0
- data/test/assertions/assert_not_respond_to_test.rb +43 -0
- data/test/assertions/assert_nothing_raised_test.rb +46 -0
- data/test/assertions/assert_raises_test.rb +49 -0
- data/test/assertions/assert_respond_to_test.rb +43 -0
- data/test/assertions_test.rb +334 -0
- data/test/context/class_methods_test.rb +314 -0
- data/test/context_test.rb +288 -0
- data/test/fixtures/inherited_stuff.rb +36 -0
- data/test/fixtures/sample_context.rb +13 -0
- data/test/helper.rb +52 -0
- data/test/irb.rb +10 -0
- data/test/options_test.rb +78 -0
- data/test/result_set_test.rb +89 -0
- data/test/result_test.rb +255 -0
- data/test/runner_test.rb +33 -0
- data/test/suite_test.rb +200 -0
- data/test/test/running_test.rb +327 -0
- data/test/test_test.rb +184 -0
- data/test/view_test.rb +35 -0
- metadata +155 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
= Assert
|
2
|
+
|
3
|
+
Test::Unit style testing framework, just better than Test::Unit.
|
4
|
+
|
5
|
+
== What Assert is
|
6
|
+
|
7
|
+
* *A framework:* you define tests and the context they run in - Assert runs them. Everything is pure ruby so use any 3rd party testing tools you like. Create 3rd party tools that extend Assert behavior.
|
8
|
+
* *First Class:* everything is a first class object and can be extended to your liking (and should be)
|
9
|
+
* *MVC:* tests and how they are defined (M) and executed (C) are distinct from how you view the test results (V).
|
10
|
+
* *Backwards compatible:* (assuming a few minor tweaks) with Test::Unit test suites
|
11
|
+
|
12
|
+
== What Assert is not
|
13
|
+
|
14
|
+
* *Rspec*
|
15
|
+
* *Unit/Functional/Integration/etc:* Assert is agnostic - you define whatever kinds of tests you like (one or more of the above) and assert runs them in context.
|
16
|
+
* *Mock/Spec/BDD/Factories/etc:* Assert is the framework and there are a variety of 3rd party tools to do such things - feel free to use whatever you like.
|
17
|
+
|
18
|
+
== Description
|
19
|
+
|
20
|
+
Assert is a Test::Unit style testing framework. This means you can write tests in Assert the same way you would with Test::Unit. In addition, Assert adds some helpers and syntax sugar to enhance the way tests are written - most taken from ideas in Shoulda and Leftright. Assert uses class-based contexts so if you want to nest your contexts, use good old inheritance.
|
21
|
+
|
22
|
+
Assert is tested using itself. The tests are a pretty good place to look for examples and usage patterns. In addition (TODO) check out the wiki and ./examples in the source.
|
23
|
+
|
24
|
+
== Installation
|
25
|
+
|
26
|
+
gem install assert
|
27
|
+
|
28
|
+
== Usage
|
29
|
+
|
30
|
+
require 'assert'
|
31
|
+
|
32
|
+
class MyTests < Assert::Context
|
33
|
+
|
34
|
+
def test_something
|
35
|
+
assert_equal 1, 1
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
== The Assert family of testing tools
|
41
|
+
|
42
|
+
These are all tools that use and extend Assert. If you write your own, share it with us and we will post it here.
|
43
|
+
|
44
|
+
(TODO)
|
45
|
+
|
46
|
+
== Contributing
|
47
|
+
|
48
|
+
The source code is hosted on Github. It's clean, modular, and easy to understand. Feel free to submit pull requests and file bugs on the issues tracker.
|
49
|
+
|
50
|
+
One note, however: please respect that Assert itself is intended to be the flexible, base-level type logic that should change little if at all. Pull requests for niche functionality or personal testing philosphy stuff will likely not be accepted.
|
51
|
+
|
52
|
+
If you wish to extend Assert for your niche purpose/desire/philosophy, please do so in it's own gem (named 'assert-<whatever>') that uses Assert as a dependency. When you do, tell us about it and we'll add to the Family of tools.
|
53
|
+
|
54
|
+
== License
|
55
|
+
|
56
|
+
Copyright (c) 2011 Kelly Redding, Collin Redding, and Team Insight
|
57
|
+
|
58
|
+
Permission is hereby granted, free of charge, to any person
|
59
|
+
obtaining a copy of this software and associated documentation
|
60
|
+
files (the "Software"), to deal in the Software without
|
61
|
+
restriction, including without limitation the rights to use,
|
62
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
63
|
+
copies of the Software, and to permit persons to whom the
|
64
|
+
Software is furnished to do so, subject to the following
|
65
|
+
conditions:
|
66
|
+
|
67
|
+
The above copyright notice and this permission notice shall be
|
68
|
+
included in all copies or substantial portions of the Software.
|
69
|
+
|
70
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
71
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
72
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
73
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
74
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
75
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
76
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
77
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/assert.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "assert/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "assert"
|
7
|
+
s.version = Assert::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Kelly Redding", "Collin Redding"]
|
10
|
+
s.email = ["kelly@kelredd.com"]
|
11
|
+
s.homepage = "http://github.com/teaminsight/assert"
|
12
|
+
s.summary = %q{Test::Unit style testing framework, just better than Test::Unit.}
|
13
|
+
s.description = %q{Test::Unit style testing framework, just better than Test::Unit.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency("bundler", ["~> 1.0"])
|
21
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'assert'
|
2
|
+
|
3
|
+
class ResultsTest < Assert::Context
|
4
|
+
|
5
|
+
def test_that_passes
|
6
|
+
assert 1==1
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_that_fails
|
10
|
+
assert 1==0
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_that_ignores
|
14
|
+
ignore
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_that_skips
|
18
|
+
skip
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_that_errors
|
22
|
+
raise Exception
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/lib/assert.rb
ADDED
@@ -0,0 +1,253 @@
|
|
1
|
+
module Assert
|
2
|
+
module Assertions
|
3
|
+
|
4
|
+
def assert_block(fail_desc=nil)
|
5
|
+
what_failed_msg ||= "Expected block to return true value."
|
6
|
+
assert(yield, fail_desc, what_failed_msg)
|
7
|
+
end
|
8
|
+
|
9
|
+
def assert_not_block(fail_desc=nil)
|
10
|
+
what_failed_msg ||= "Expected block to return false value."
|
11
|
+
assert(!yield, fail_desc, what_failed_msg)
|
12
|
+
end
|
13
|
+
alias_method :refute_block, :assert_not_block
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
def assert_raises(*args, &block)
|
18
|
+
assertion, fail_desc = check_exception(args, :raises, &block)
|
19
|
+
assert(assertion, fail_desc, "")
|
20
|
+
end
|
21
|
+
alias_method :assert_raise, :assert_raises
|
22
|
+
|
23
|
+
def assert_nothing_raised(*args, &block)
|
24
|
+
assertion, fail_desc = check_exception(args, :not_raises, &block)
|
25
|
+
assert(!assertion, fail_desc, "")
|
26
|
+
end
|
27
|
+
alias_method :assert_not_raises, :assert_nothing_raised
|
28
|
+
alias_method :assert_not_raise, :assert_nothing_raised
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
def assert_kind_of(klass, instance, fail_desc=nil)
|
33
|
+
what_failed_msg = [
|
34
|
+
"Expected #{instance.inspect} to be a kind ",
|
35
|
+
"of #{klass}, not #{instance.class}."
|
36
|
+
].join
|
37
|
+
assert(instance.kind_of?(klass), fail_desc, what_failed_msg)
|
38
|
+
end
|
39
|
+
|
40
|
+
def assert_not_kind_of(klass, instance, fail_desc=nil)
|
41
|
+
what_failed_msg = [
|
42
|
+
"#{instance.inspect} was not expected to be a ",
|
43
|
+
"kind of #{klass}."
|
44
|
+
].join
|
45
|
+
assert(!instance.kind_of?(klass), fail_desc, what_failed_msg)
|
46
|
+
end
|
47
|
+
alias_method :refute_kind_of, :assert_not_kind_of
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
def assert_instance_of(klass, instance, fail_desc=nil)
|
52
|
+
what_failed_msg = [
|
53
|
+
"Expected #{instance.inspect} to be an instance ",
|
54
|
+
"of #{klass}, not #{instance.class}."
|
55
|
+
].join
|
56
|
+
assert(instance.instance_of?(klass), fail_desc, what_failed_msg)
|
57
|
+
end
|
58
|
+
|
59
|
+
def assert_not_instance_of(klass, instance, fail_desc=nil)
|
60
|
+
what_failed_msg = [
|
61
|
+
"#{instance.inspect} was not expected to be an ",
|
62
|
+
"instance of #{klass}."
|
63
|
+
].join
|
64
|
+
assert(!instance.instance_of?(klass), fail_desc, what_failed_msg)
|
65
|
+
end
|
66
|
+
alias_method :refute_instance_of, :assert_not_instance_of
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
def assert_respond_to(object, method, fail_desc=nil)
|
71
|
+
what_failed_msg = [
|
72
|
+
"Expected #{object.inspect} (#{object.class}) to ",
|
73
|
+
"respond to ##{method}."
|
74
|
+
].join
|
75
|
+
assert(object.respond_to?(method), fail_desc, what_failed_msg)
|
76
|
+
end
|
77
|
+
|
78
|
+
def assert_not_respond_to(object, method, fail_desc=nil)
|
79
|
+
what_failed_msg = [
|
80
|
+
"#{object.inspect} (#{object.class}) not expected to ",
|
81
|
+
"respond to ##{method}."
|
82
|
+
].join
|
83
|
+
assert(!object.respond_to?(method), fail_desc, what_failed_msg)
|
84
|
+
end
|
85
|
+
alias_method :refute_respond_to, :assert_not_respond_to
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
def assert_same(left, right, fail_desc=nil)
|
90
|
+
what_failed_msg = [
|
91
|
+
"Expected #{left} (#{left.object_id}) to be the same ",
|
92
|
+
"as #{right} (#{right.object_id})."
|
93
|
+
].join
|
94
|
+
assert(right.equal?(left), fail_desc, what_failed_msg)
|
95
|
+
end
|
96
|
+
|
97
|
+
def assert_not_same(left, right, fail_desc=nil)
|
98
|
+
what_failed_msg = [
|
99
|
+
"#{left} (#{left.object_id}) not expected to be the same ",
|
100
|
+
"as #{right} (#{right.object_id})."
|
101
|
+
].join
|
102
|
+
assert(!right.equal?(left), fail_desc, what_failed_msg)
|
103
|
+
end
|
104
|
+
alias_method :refute_same, :assert_not_same
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
def assert_equal(left, right, fail_desc=nil)
|
109
|
+
what_failed_msg = "Expected #{left.inspect}, not #{right.inspect}."
|
110
|
+
assert(right == left, fail_desc, what_failed_msg)
|
111
|
+
end
|
112
|
+
|
113
|
+
def assert_not_equal(left, right, fail_desc=nil)
|
114
|
+
what_failed_msg = [
|
115
|
+
"#{left.inspect} not expected to be equal ", "to #{right.inspect}."
|
116
|
+
].join
|
117
|
+
assert(right != left, fail_desc, what_failed_msg)
|
118
|
+
end
|
119
|
+
alias_method :refute_equal, :assert_not_equal
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
def assert_match(left, right, fail_desc=nil)
|
124
|
+
what_failed_msg = "Expected #{left.inspect} to match #{right.inspect}."
|
125
|
+
left = /#{Regexp.escape(left)}/ if String === left && String === right
|
126
|
+
assert(left =~ right, fail_desc, what_failed_msg)
|
127
|
+
end
|
128
|
+
|
129
|
+
def assert_not_match(left, right, fail_desc=nil)
|
130
|
+
what_failed_msg = [
|
131
|
+
"#{left.inspect} not expected to ", "match #{right.inspect}."
|
132
|
+
].join
|
133
|
+
left = /#{Regexp.escape(left)}/ if String === left && String === right
|
134
|
+
assert(left !~ right, fail_desc, what_failed_msg)
|
135
|
+
end
|
136
|
+
alias_method :refute_match, :assert_not_match
|
137
|
+
alias_method :assert_no_match, :assert_not_match
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
# TODO: tests!
|
142
|
+
def assert_empty(collection, fail_desc=nil)
|
143
|
+
what_failed_msg = "Expected #{collection.inspect} to be empty."
|
144
|
+
assert(collection.empty?, fail_desc, what_failed_msg)
|
145
|
+
end
|
146
|
+
|
147
|
+
def assert_not_empty(collection, fail_desc=nil)
|
148
|
+
what_failed_msg = "Expected #{collection.inspect} to not be empty."
|
149
|
+
assert(!collection.empty?, fail_desc, what_failed_msg)
|
150
|
+
end
|
151
|
+
alias_method :refute_empty, :assert_not_empty
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
# TODO: tests!
|
156
|
+
def assert_includes(collection, object, fail_desc=nil)
|
157
|
+
what_failed_msg = "Expected #{collection.inspect} to include #{object.inspect}."
|
158
|
+
assert(collection.include?(object), fail_desc, what_failed_msg)
|
159
|
+
end
|
160
|
+
|
161
|
+
def assert_not_included(collection, object, fail_desc=nil)
|
162
|
+
what_failed_msg = "Expected #{collection.inspect} to not include #{object.inspect}."
|
163
|
+
assert(!collection.include?(object), fail_desc, what_failed_msg)
|
164
|
+
end
|
165
|
+
alias_method :refute_includes, :assert_not_included
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
# TODO: tests!
|
170
|
+
def assert_nil(object, fail_desc=nil)
|
171
|
+
what_failed_msg = "Expected nil, not #{object.inspect}."
|
172
|
+
assert(object.nil?, fail_desc, what_failed_msg)
|
173
|
+
end
|
174
|
+
|
175
|
+
def assert_not_nil(object, fail_desc=nil)
|
176
|
+
what_failed_msg = "Expected #{object.inspect} to not be nil."
|
177
|
+
assert(!object.nil?, fail_desc, what_failed_msg)
|
178
|
+
end
|
179
|
+
alias_method :refute_nil, :assert_not_nil
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
IGNORED_ASSERTION_HELPERS = [ :assert_throws, :assert_nothing_thrown, :assert_send,
|
184
|
+
:assert_operator, :refute_operator, :assert_in_epsilon, :refute_in_epsilon,
|
185
|
+
:assert_in_delta, :refute_in_delta
|
186
|
+
]
|
187
|
+
def method_missing(method, *args, &block)
|
188
|
+
if IGNORED_ASSERTION_HELPERS.include?(method.to_sym)
|
189
|
+
ignore([
|
190
|
+
"The assertion helper '#{method}' is not supported. Please use ",
|
191
|
+
"another helper or the basic assert."
|
192
|
+
].join)
|
193
|
+
else
|
194
|
+
super
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
private
|
199
|
+
|
200
|
+
def check_exception(args, which, &block)
|
201
|
+
fail_desc = String === args.last ? args.pop : nil
|
202
|
+
exceptions = args
|
203
|
+
begin
|
204
|
+
yield
|
205
|
+
rescue Exception => exception
|
206
|
+
end
|
207
|
+
assertion, what_failed_msg = if exception
|
208
|
+
test = exceptions.empty? || exceptions.any? do |exp|
|
209
|
+
exp.instance_of?(Module) ? exception.kind_of?(exp) : exp == exception.class
|
210
|
+
end
|
211
|
+
[ test, exception_details(exception, which) ]
|
212
|
+
else
|
213
|
+
[ false, exception_details(exception, which) ]
|
214
|
+
end
|
215
|
+
what_failed_msg = "#{exceptions_sentence(exceptions)} #{what_failed_msg}"
|
216
|
+
fail_desc = [ fail_desc, what_failed_msg ].compact.join("\n")
|
217
|
+
[ assertion, fail_desc ]
|
218
|
+
end
|
219
|
+
|
220
|
+
def exception_details(exception, which)
|
221
|
+
if exception
|
222
|
+
what_failed_msg = case(which)
|
223
|
+
when :raises
|
224
|
+
"exception expected, not:"
|
225
|
+
when :not_raises
|
226
|
+
"exception was not expected, but was raised:"
|
227
|
+
end
|
228
|
+
backtrace = Assert::Result::Backtrace.new(exception.backtrace)
|
229
|
+
[ what_failed_msg,
|
230
|
+
"Class: <#{exception.class}>",
|
231
|
+
"Message: <#{exception.message.inspect}>",
|
232
|
+
"---Backtrace---",
|
233
|
+
backtrace.filtered.to_s,
|
234
|
+
"---------------"
|
235
|
+
].compact.join("\n")
|
236
|
+
else
|
237
|
+
case(which)
|
238
|
+
when :raises
|
239
|
+
"exception expected but nothing was raised."
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
def exceptions_sentence(exceptions)
|
245
|
+
if exceptions.size <= 1
|
246
|
+
(exceptions.first || "An").to_s
|
247
|
+
else
|
248
|
+
"#{exceptions[0..-2].join(", ")} or #{exceptions[-1]}"
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
end
|
253
|
+
end
|