assertions 0.9.0
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/History.txt +2 -0
- data/LICENSE +20 -0
- data/Manifest.txt +7 -0
- data/README.txt +72 -0
- data/Rakefile +15 -0
- data/lib/assertions.rb +202 -0
- data/test/test_assertions.rb +114 -0
- metadata +71 -0
data/History.txt
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Designing Patterns
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
'Software'), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
= assertions
|
2
|
+
|
3
|
+
Project Page: http://rubyforge.org/projects/assertions/
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
This package adds some additional assertions to Test::Unit::Assertions,
|
8
|
+
including:
|
9
|
+
* Assertions for all of the comparison operators (assert_greater_than,
|
10
|
+
assert_less_than_or_equal_to, etc.). Shorter aliases also are provided
|
11
|
+
for these (assert_gt, assert_le, etc.).
|
12
|
+
* An assertion that tests whether a given block raises a specified exception
|
13
|
+
with a specified message (assert_raise_message). This allows full testing
|
14
|
+
of error messages.
|
15
|
+
* An assertion that tests whether a given block contains an assertion that
|
16
|
+
fails, which can be used to test new assertions.
|
17
|
+
|
18
|
+
== PROBLEMS:
|
19
|
+
|
20
|
+
None (known).
|
21
|
+
|
22
|
+
== SYNOPSIS:
|
23
|
+
require 'rubygems'
|
24
|
+
require 'assertions'
|
25
|
+
|
26
|
+
require 'test/unit'
|
27
|
+
|
28
|
+
class Tests < Test::Unit::TestCase
|
29
|
+
def test_assertions
|
30
|
+
#
|
31
|
+
# Verify that 4 < 5
|
32
|
+
#
|
33
|
+
assert_less_than(4, 5)
|
34
|
+
|
35
|
+
#
|
36
|
+
# Verify that 4 < 5 again, but this time with the
|
37
|
+
# shorter alias.
|
38
|
+
#
|
39
|
+
assert_lt(4, 5)
|
40
|
+
|
41
|
+
#
|
42
|
+
# Verify that 5 >= 5
|
43
|
+
#
|
44
|
+
assert_ge(5, 5)
|
45
|
+
|
46
|
+
#
|
47
|
+
# Verify that the specified exception is raised.
|
48
|
+
#
|
49
|
+
assert_raise_message("Hello, exception!", RuntimeError) do
|
50
|
+
raise "Hello, exception!"
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
# Verify that an assertion failed.
|
55
|
+
#
|
56
|
+
assert_fail do
|
57
|
+
assert_equal(5, 4)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
== REQUIREMENTS:
|
63
|
+
|
64
|
+
Hoe is required but only for running the tests.
|
65
|
+
|
66
|
+
== INSTALL:
|
67
|
+
|
68
|
+
sudo gem install assertions
|
69
|
+
|
70
|
+
== LICENSE:
|
71
|
+
|
72
|
+
See LICENSE, at the root of the distribution.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift("lib")
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'hoe'
|
7
|
+
|
8
|
+
$stderr = STDERR
|
9
|
+
|
10
|
+
Hoe.new('assertions', "0.9.0") do |p|
|
11
|
+
p.remote_rdoc_dir = ''
|
12
|
+
p.developer('DesigningPatterns', 'technical.inquiries@designingpatterns.com')
|
13
|
+
end
|
14
|
+
|
15
|
+
# vim: syntax=Ruby
|
data/lib/assertions.rb
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
module Test
|
4
|
+
module Unit
|
5
|
+
|
6
|
+
#
|
7
|
+
# Some useful extra assertions.
|
8
|
+
# Require 'assertions', and Test::Unit::Assertions will have
|
9
|
+
# the additional assertions.
|
10
|
+
#
|
11
|
+
module Assertions
|
12
|
+
public
|
13
|
+
|
14
|
+
#
|
15
|
+
# ====Description:
|
16
|
+
# This assertion passes if and only if block contains an assertion
|
17
|
+
# that fails (but which is suppressed from propagating outside of
|
18
|
+
# block). If the assertion passes, the failed assertion is written to
|
19
|
+
# stdout. This method is (only?) useful when testing other assertions.
|
20
|
+
#
|
21
|
+
# ====Example:
|
22
|
+
# assert_fail do
|
23
|
+
# assert_equal(5, 4)
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# ====Parameters:
|
27
|
+
# [message = ""]
|
28
|
+
# An optional additional message that will be displayed if the
|
29
|
+
# assertion fails.
|
30
|
+
# [&block]
|
31
|
+
# This block should contain an assertion that fails.
|
32
|
+
#
|
33
|
+
def assert_fail(message = "", &block)
|
34
|
+
_wrap_assertion do
|
35
|
+
full_message = build_message(message,
|
36
|
+
"Failed assertion was expected, but it did not occur.")
|
37
|
+
|
38
|
+
assert_block(full_message) do
|
39
|
+
begin
|
40
|
+
yield
|
41
|
+
false
|
42
|
+
rescue AssertionFailedError => e
|
43
|
+
print("Assertion correctly failed:\n#{e.message}\n")
|
44
|
+
true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
#
|
51
|
+
# ====Description:
|
52
|
+
# This is a convenience wrapper around assert_operator. It asserts that
|
53
|
+
# lhs > rhs.
|
54
|
+
#
|
55
|
+
# ====Example:
|
56
|
+
# assert_greater_than(5, 4)
|
57
|
+
#
|
58
|
+
# ====Parameters:
|
59
|
+
# [lhs]
|
60
|
+
# The left-hand side of the comparison.
|
61
|
+
# [rhs]
|
62
|
+
# The right-hand side of the comparison.
|
63
|
+
# [message = ""]
|
64
|
+
# An optional additional message that will be displayed if the
|
65
|
+
# assertion fails.
|
66
|
+
#
|
67
|
+
def assert_greater_than(lhs, rhs, message = "")
|
68
|
+
assert_operator(lhs, :>, rhs, message)
|
69
|
+
end
|
70
|
+
alias assert_gt assert_greater_than
|
71
|
+
|
72
|
+
#
|
73
|
+
# ====Description:
|
74
|
+
# This is a convenience wrapper around assert_operator. It asserts that
|
75
|
+
# lhs >= rhs.
|
76
|
+
#
|
77
|
+
# ====Example:
|
78
|
+
# assert_greater_than_or_equal_to(5, 5)
|
79
|
+
#
|
80
|
+
# ====Parameters:
|
81
|
+
# [lhs]
|
82
|
+
# The left-hand side of the comparison.
|
83
|
+
# [rhs]
|
84
|
+
# The right-hand side of the comparison.
|
85
|
+
# [message = ""]
|
86
|
+
# An optional additional message that will be displayed if the
|
87
|
+
# assertion fails.
|
88
|
+
#
|
89
|
+
def assert_greater_than_or_equal_to(lhs, rhs, message = "")
|
90
|
+
assert_operator(lhs, :>=, rhs, message)
|
91
|
+
end
|
92
|
+
alias assert_ge assert_greater_than_or_equal_to
|
93
|
+
|
94
|
+
#
|
95
|
+
# ====Description:
|
96
|
+
# This is a convenience wrapper around assert_operator. It asserts that
|
97
|
+
# lhs < rhs.
|
98
|
+
#
|
99
|
+
# ====Example:
|
100
|
+
# assert_less_than(4, 5)
|
101
|
+
#
|
102
|
+
# ====Parameters:
|
103
|
+
# [lhs]
|
104
|
+
# The left-hand side of the comparison.
|
105
|
+
# [rhs]
|
106
|
+
# The right-hand side of the comparison.
|
107
|
+
# [message = ""]
|
108
|
+
# An optional additional message that will be displayed if the
|
109
|
+
# assertion fails.
|
110
|
+
#
|
111
|
+
def assert_less_than(lhs, rhs, message = "")\
|
112
|
+
assert_operator(lhs, :<, rhs, message)
|
113
|
+
end
|
114
|
+
alias assert_lt assert_less_than
|
115
|
+
|
116
|
+
#
|
117
|
+
# ====Description:
|
118
|
+
# This is a convenience wrapper around assert_operator. It asserts that
|
119
|
+
# lhs <= rhs.
|
120
|
+
#
|
121
|
+
# ====Example:
|
122
|
+
# assert_less_than_or_equal_to(4, 4)
|
123
|
+
#
|
124
|
+
# ====Parameters:
|
125
|
+
# [lhs]
|
126
|
+
# The left-hand side of the comparison.
|
127
|
+
# [rhs]
|
128
|
+
# The right-hand side of the comparison.
|
129
|
+
# [message = ""]
|
130
|
+
# An optional additional message that will be displayed if the
|
131
|
+
# assertion fails.
|
132
|
+
#
|
133
|
+
def assert_less_than_or_equal_to(lhs, rhs, message = "")
|
134
|
+
assert_operator(lhs, :<=, rhs, message)
|
135
|
+
end
|
136
|
+
alias assert_le assert_less_than_or_equal_to
|
137
|
+
|
138
|
+
#
|
139
|
+
# ====Description:
|
140
|
+
# This assertion passes if and only if block throws an exception of
|
141
|
+
# class (or descended from class) expected_exception_class with
|
142
|
+
# message expected_exception_message.
|
143
|
+
#
|
144
|
+
# ====Example:
|
145
|
+
# assert_raise_message("Hello, exception!", RuntimeError) do
|
146
|
+
# raise "Hello, exception!"
|
147
|
+
# end
|
148
|
+
#
|
149
|
+
# ====Parameters:
|
150
|
+
# [expected_exception_message]
|
151
|
+
# The message expected to be contained in the exception thrown
|
152
|
+
# by block.
|
153
|
+
# [expected_exception_class]
|
154
|
+
# The expected class (or parent class) of the exception thrown by
|
155
|
+
# block.
|
156
|
+
# [message = ""]
|
157
|
+
# An optional additional message that will be displayed if the
|
158
|
+
# assertion fails.
|
159
|
+
# [&block]
|
160
|
+
# The block that is supposed to throw the specified exception.
|
161
|
+
#
|
162
|
+
def assert_raise_message(expected_exception_message,
|
163
|
+
expected_exception_class,
|
164
|
+
message = "",
|
165
|
+
&block)
|
166
|
+
_wrap_assertion do
|
167
|
+
full_message = build_message(message,
|
168
|
+
"<?> exception expected but none was thrown.",
|
169
|
+
expected_exception_class)
|
170
|
+
actual_exception = nil
|
171
|
+
assert_block(full_message) do
|
172
|
+
begin
|
173
|
+
yield
|
174
|
+
false
|
175
|
+
rescue Exception => e
|
176
|
+
actual_exception = e
|
177
|
+
true
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
full_message = build_message(message,
|
182
|
+
"<?> exception expected but was\n?",
|
183
|
+
expected_exception_class,
|
184
|
+
actual_exception)
|
185
|
+
|
186
|
+
assert_block(full_message) do
|
187
|
+
actual_exception.is_a?(expected_exception_class)
|
188
|
+
end
|
189
|
+
|
190
|
+
full_message = build_message(message,
|
191
|
+
"<?> exception message expected but was\n?",
|
192
|
+
expected_exception_message,
|
193
|
+
actual_exception)
|
194
|
+
assert_block(full_message) do
|
195
|
+
expected_exception_message == actual_exception.message
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'assertions'
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
class AssertionsTest < Test::Unit::TestCase
|
8
|
+
def test_assert_fail
|
9
|
+
#
|
10
|
+
# Verify that assert_fail fails if the block has
|
11
|
+
# no failed assertions.
|
12
|
+
#
|
13
|
+
failed_assertion = false
|
14
|
+
begin
|
15
|
+
assert_fail do
|
16
|
+
end
|
17
|
+
rescue Test::Unit::AssertionFailedError => e
|
18
|
+
failed_assertion = true
|
19
|
+
end
|
20
|
+
|
21
|
+
if(!failed_assertion)
|
22
|
+
flunk("assert_fail incorrectly passed")
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Verify that assert_fail succeeds when the block
|
27
|
+
# has a failed assertion.
|
28
|
+
#
|
29
|
+
assert_fail do
|
30
|
+
assert_equal(4, 5)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_assert_greater_than
|
35
|
+
assert_greater_than(5, 4)
|
36
|
+
|
37
|
+
assert_fail do
|
38
|
+
assert_greater_than(4, 4)
|
39
|
+
end
|
40
|
+
|
41
|
+
assert_fail do
|
42
|
+
assert_gt(4, 5, "test greater than")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_assert_greater_than_or_equal_to_or_equal_to
|
47
|
+
assert_greater_than_or_equal_to(5, 4)
|
48
|
+
assert_greater_than_or_equal_to(4, 4)
|
49
|
+
|
50
|
+
assert_fail do
|
51
|
+
assert_ge(4, 5, "test greater than or equal to")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_assert_less_than
|
56
|
+
assert_less_than(4, 5)
|
57
|
+
|
58
|
+
assert_fail do
|
59
|
+
assert_less_than(4, 4)
|
60
|
+
end
|
61
|
+
|
62
|
+
assert_fail do
|
63
|
+
assert_lt(5, 4, "test less than")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_assert_less_than_or_equal_to_or_equal_to
|
68
|
+
assert_less_than_or_equal_to(4, 5)
|
69
|
+
assert_less_than_or_equal_to(4, 4)
|
70
|
+
|
71
|
+
assert_fail do
|
72
|
+
assert_le(5, 4, "test less than or equal to")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_assert_raise_message
|
77
|
+
#
|
78
|
+
# Verify that the assertion passes correctly.
|
79
|
+
#
|
80
|
+
assert_nothing_raised do
|
81
|
+
assert_raise_message("hello, error!", RuntimeError) do
|
82
|
+
raise RuntimeError, "hello, error!"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Verify that the assertion fails if no exception is thrown.
|
88
|
+
#
|
89
|
+
assert_fail do
|
90
|
+
assert_raise_message("hello, error!", RuntimeError) do
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Verify that the assertion fails if the wrong kind of
|
96
|
+
# exception is thrown.
|
97
|
+
#
|
98
|
+
assert_fail do
|
99
|
+
assert_raise_message("hello, error!", ArgumentError) do
|
100
|
+
raise RuntimeError, "hello, error!"
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
# Verify that the assertion fails if a different
|
106
|
+
# error message is specified.
|
107
|
+
#
|
108
|
+
assert_fail do
|
109
|
+
assert_raise_message("hello, error!", RuntimeError, "test message comparison") do
|
110
|
+
raise RuntimeError, "hello, errrr!"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assertions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- DesigningPatterns
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-07 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.5.3
|
23
|
+
version:
|
24
|
+
description: "This package adds some additional assertions to Test::Unit::Assertions, including: * Assertions for all of the comparison operators (assert_greater_than, assert_less_than_or_equal_to, etc.). Shorter aliases also are provided for these (assert_gt, assert_le, etc.). * An assertion that tests whether a given block raises a specified exception with a specified message (assert_raise_message). This allows full testing of error messages. * An assertion that tests whether a given block contains an assertion that fails, which can be used to test new assertions."
|
25
|
+
email:
|
26
|
+
- technical.inquiries@designingpatterns.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- Manifest.txt
|
33
|
+
- README.txt
|
34
|
+
- History.txt
|
35
|
+
files:
|
36
|
+
- Manifest.txt
|
37
|
+
- LICENSE
|
38
|
+
- README.txt
|
39
|
+
- History.txt
|
40
|
+
- Rakefile
|
41
|
+
- lib/assertions.rb
|
42
|
+
- test/test_assertions.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: "Project Page: http://rubyforge.org/projects/assertions/"
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: assertions
|
66
|
+
rubygems_version: 1.0.1
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: "This package adds some additional assertions to Test::Unit::Assertions, including: * Assertions for all of the comparison operators (assert_greater_than, assert_less_than_or_equal_to, etc.)"
|
70
|
+
test_files:
|
71
|
+
- test/test_assertions.rb
|