assert 2.10.1 → 2.11.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/Gemfile +1 -1
- data/README.md +65 -1
- data/lib/assert.rb +25 -0
- data/lib/assert/stub.rb +95 -0
- data/lib/assert/version.rb +1 -1
- data/test/unit/assert_tests.rb +69 -1
- data/test/unit/assertions/assert_includes_tests.rb +1 -1
- data/test/unit/assertions/assert_respond_to_tests.rb +1 -1
- data/test/unit/assertions/assert_same_tests.rb +1 -1
- data/test/unit/assertions_tests.rb +1 -1
- data/test/unit/context_tests.rb +1 -1
- data/test/unit/macro_tests.rb +6 -6
- data/test/unit/result_tests.rb +13 -17
- data/test/unit/runner_tests.rb +2 -2
- data/test/unit/stub_tests.rb +195 -0
- data/test/unit/test_tests.rb +8 -8
- data/test/unit/view_tests.rb +2 -1
- metadata +13 -10
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -40,7 +40,7 @@ Running tests in random order, seeded with "56382"
|
|
40
40
|
|
41
41
|
* **Rspec**
|
42
42
|
* **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.
|
43
|
-
* **Mock/Spec/BDD/
|
43
|
+
* **Mock/Spec/BDD/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.
|
44
44
|
|
45
45
|
## Description
|
46
46
|
|
@@ -50,6 +50,70 @@ In addition, Assert adds some helpers and syntax sugar to enhance the way tests
|
|
50
50
|
|
51
51
|
**Note**: Assert is tested using itself. The tests are a pretty good place to look for examples and usage patterns.
|
52
52
|
|
53
|
+
## Defining tests
|
54
|
+
|
55
|
+
TODO
|
56
|
+
|
57
|
+
## Factory
|
58
|
+
|
59
|
+
TODO
|
60
|
+
|
61
|
+
## Stub
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
myclass = Class.new do
|
65
|
+
def mymeth; 'meth'; end
|
66
|
+
def myval(val); val; end
|
67
|
+
end
|
68
|
+
myobj = myclass.new
|
69
|
+
|
70
|
+
myobj.mymeth
|
71
|
+
# => 'meth'
|
72
|
+
myobj.myval(123)
|
73
|
+
# => 123
|
74
|
+
myobj.myval(456)
|
75
|
+
# => 456
|
76
|
+
|
77
|
+
Assert.stub(myobj, :mymeth)
|
78
|
+
myobj.mymeth
|
79
|
+
# => StubError: `mymeth` not stubbed.
|
80
|
+
Assert.stub(myobj, :mymeth){ 'stub-meth' }
|
81
|
+
myobj.mymeth
|
82
|
+
# => 'stub-meth'
|
83
|
+
myobj.mymeth(123)
|
84
|
+
# => StubError: arity mismatch
|
85
|
+
Assert.stub(myobj, :mymeth).with(123){ 'stub-meth' }
|
86
|
+
# => StubError: arity mismatch
|
87
|
+
|
88
|
+
Assert.stub(myobj, :myval){ 'stub-meth' }
|
89
|
+
# => StubError: arity mismatch
|
90
|
+
Assert.stub(myobj, :myval).with(123){ |val| val.to_s }
|
91
|
+
myobj.myval
|
92
|
+
# => StubError: arity mismatch
|
93
|
+
myobj.mymeth(123)
|
94
|
+
# => '123'
|
95
|
+
myobj.mymeth(456)
|
96
|
+
# => StubError: `mymeth(456)` not stubbed.
|
97
|
+
|
98
|
+
Assert.unstub(myobj, :mymeth)
|
99
|
+
|
100
|
+
myobj.mymeth
|
101
|
+
# => 'meth'
|
102
|
+
myobj.myval(123)
|
103
|
+
# => 123
|
104
|
+
myobj.myval(456)
|
105
|
+
# => 456
|
106
|
+
```
|
107
|
+
|
108
|
+
Assert comes with a simple stubbing API - all it does is replace method calls. In general it tries
|
109
|
+
to be friendly and complain if stubbing doesn't match up with the object/method being stubbed:
|
110
|
+
|
111
|
+
* each stub takes a block that is called in place of the method
|
112
|
+
* complains if you stub a method that the object doesn't respond to
|
113
|
+
* complains if you stub with an arity mismatch
|
114
|
+
* no methods added to `Object`
|
115
|
+
* stubs are auto-unstubbed on test teardown
|
116
|
+
|
53
117
|
## CLI
|
54
118
|
|
55
119
|
```sh
|
data/lib/assert.rb
CHANGED
@@ -3,6 +3,7 @@ require 'assert/version'
|
|
3
3
|
require 'assert/config'
|
4
4
|
require 'assert/context'
|
5
5
|
require 'assert/runner'
|
6
|
+
require 'assert/stub'
|
6
7
|
require 'assert/suite'
|
7
8
|
require 'assert/utils'
|
8
9
|
require 'assert/view'
|
@@ -16,4 +17,28 @@ module Assert
|
|
16
17
|
def self.suite; self.config.suite; end
|
17
18
|
def self.runner; self.config.runner; end
|
18
19
|
|
20
|
+
def self.stubs
|
21
|
+
@stubs ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.stub(*args, &block)
|
25
|
+
(self.stubs[Assert::Stub.key(*args)] ||= Assert::Stub.new(*args)).tap do |s|
|
26
|
+
s.do = block
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.unstub(*args)
|
31
|
+
(self.stubs.delete(Assert::Stub.key(*args)) || Assert::Stub::NullStub.new).teardown
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.unstub!
|
35
|
+
self.stubs.keys.each{ |key| self.stubs.delete(key).teardown }
|
36
|
+
end
|
37
|
+
|
38
|
+
class Context
|
39
|
+
|
40
|
+
teardown{ Assert.unstub! } # unstub all stubs automatically
|
41
|
+
|
42
|
+
end
|
43
|
+
|
19
44
|
end
|
data/lib/assert/stub.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
module Assert
|
2
|
+
|
3
|
+
StubError = Class.new(ArgumentError)
|
4
|
+
|
5
|
+
class Stub
|
6
|
+
|
7
|
+
NullStub = Class.new do
|
8
|
+
def teardown; end # no-op
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.key(object, method_name)
|
12
|
+
"--#{object.object_id}--#{method_name}--"
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :method_name, :name, :do
|
16
|
+
|
17
|
+
def initialize(object, method_name, &block)
|
18
|
+
@metaclass = class << object; self; end
|
19
|
+
@method_name = method_name.to_s
|
20
|
+
@name = "__assert_stub__#{@method_name}"
|
21
|
+
|
22
|
+
setup(object)
|
23
|
+
|
24
|
+
@do = block || Proc.new do |*args, &block|
|
25
|
+
err_msg = "#{inspect_call(args)} not stubbed."
|
26
|
+
inspect_lookup_stubs.tap do |stubs|
|
27
|
+
err_msg += "\nStubs:\n#{stubs}" if !stubs.empty?
|
28
|
+
end
|
29
|
+
raise StubError, err_msg
|
30
|
+
end
|
31
|
+
@lookup = Hash.new{ |hash, key| self.do }
|
32
|
+
end
|
33
|
+
|
34
|
+
def call(*args, &block)
|
35
|
+
raise StubError, "artiy mismatch" unless arity_matches?(args)
|
36
|
+
@lookup[args].call(*args, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
def with(*args, &block)
|
40
|
+
raise StubError, "artiy mismatch" unless arity_matches?(args)
|
41
|
+
@lookup[args] = block
|
42
|
+
end
|
43
|
+
|
44
|
+
def do=(block)
|
45
|
+
@do = block || @do
|
46
|
+
end
|
47
|
+
|
48
|
+
def teardown
|
49
|
+
@metaclass.send(:undef_method, @method_name)
|
50
|
+
@metaclass.send(:alias_method, @method_name, @name)
|
51
|
+
@metaclass.send(:undef_method, @name)
|
52
|
+
end
|
53
|
+
|
54
|
+
protected
|
55
|
+
|
56
|
+
def setup(object)
|
57
|
+
unless object.respond_to?(@method_name)
|
58
|
+
raise StubError, "#{object.inspect} does not respond to `#{@method_name}`"
|
59
|
+
end
|
60
|
+
if !object.methods.map(&:to_s).include?(@method_name)
|
61
|
+
@metaclass.send(:define_method, @method_name) do |*args, &block|
|
62
|
+
super(*args, &block)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
if !object.respond_to?(@name) # already stubbed
|
67
|
+
@metaclass.send(:alias_method, @name, @method_name)
|
68
|
+
end
|
69
|
+
@method = object.method(@name)
|
70
|
+
|
71
|
+
stub = self
|
72
|
+
@metaclass.send(:define_method, @method_name) do |*args, &block|
|
73
|
+
stub.call(*args, &block)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def arity_matches?(args)
|
80
|
+
return true if @method.arity == args.size # mandatory args
|
81
|
+
return true if @method.arity < 0 && args.size >= (@method.arity+1).abs # variable args
|
82
|
+
return false
|
83
|
+
end
|
84
|
+
|
85
|
+
def inspect_lookup_stubs
|
86
|
+
@lookup.keys.map{ |args| " - #{inspect_call(args)}" }.join("\n")
|
87
|
+
end
|
88
|
+
|
89
|
+
def inspect_call(args)
|
90
|
+
"`#{@method_name}(#{args.map(&:inspect).join(',')})`"
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
data/lib/assert/version.rb
CHANGED
data/test/unit/assert_tests.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require 'assert'
|
2
2
|
|
3
3
|
require 'assert/config'
|
4
|
+
require 'assert/stub'
|
4
5
|
|
5
6
|
module Assert
|
6
7
|
|
7
8
|
class UnitTests < Assert::Context
|
8
|
-
desc "
|
9
|
+
desc "Assert"
|
9
10
|
subject { Assert }
|
10
11
|
|
11
12
|
should have_imeths :config, :configure, :view, :suite, :runner
|
13
|
+
should have_imeths :stubs, :stub, :unstub, :unstub!
|
12
14
|
|
13
15
|
should "know its config instance" do
|
14
16
|
assert_kind_of Assert::Config, subject.config
|
@@ -25,4 +27,70 @@ module Assert
|
|
25
27
|
|
26
28
|
end
|
27
29
|
|
30
|
+
class StubTests < UnitTests
|
31
|
+
setup do
|
32
|
+
@myclass = Class.new do
|
33
|
+
def mymeth; 'meth'; end
|
34
|
+
end
|
35
|
+
@myobj = @myclass.new
|
36
|
+
end
|
37
|
+
|
38
|
+
should "build a stub" do
|
39
|
+
stub1 = Assert.stub(@myobj, :mymeth)
|
40
|
+
assert_kind_of Assert::Stub, stub1
|
41
|
+
end
|
42
|
+
|
43
|
+
should "lookup stubs that have been called before" do
|
44
|
+
stub1 = Assert.stub(@myobj, :mymeth)
|
45
|
+
stub2 = Assert.stub(@myobj, :mymeth)
|
46
|
+
assert_same stub1, stub2
|
47
|
+
end
|
48
|
+
|
49
|
+
should "set the stub's do block if given a block" do
|
50
|
+
Assert.stub(@myobj, :mymeth)
|
51
|
+
assert_raises(StubError){ @myobj.mymeth }
|
52
|
+
Assert.stub(@myobj, :mymeth){ 'mymeth' }
|
53
|
+
assert_equal 'mymeth', @myobj.mymeth
|
54
|
+
end
|
55
|
+
|
56
|
+
should "teardown stubs" do
|
57
|
+
assert_equal 'meth', @myobj.mymeth
|
58
|
+
Assert.unstub(@myobj, :mymeth)
|
59
|
+
assert_equal 'meth', @myobj.mymeth
|
60
|
+
|
61
|
+
assert_equal 'meth', @myobj.mymeth
|
62
|
+
Assert.stub(@myobj, :mymeth){ 'mymeth' }
|
63
|
+
assert_equal 'mymeth', @myobj.mymeth
|
64
|
+
Assert.unstub(@myobj, :mymeth)
|
65
|
+
assert_equal 'meth', @myobj.mymeth
|
66
|
+
end
|
67
|
+
|
68
|
+
should "know and teardown all stubs" do
|
69
|
+
assert_equal 'meth', @myobj.mymeth
|
70
|
+
|
71
|
+
Assert.stub(@myobj, :mymeth){ 'mymeth' }
|
72
|
+
assert_equal 'mymeth', @myobj.mymeth
|
73
|
+
assert_equal 1, Assert.stubs.size
|
74
|
+
|
75
|
+
Assert.unstub!
|
76
|
+
assert_equal 'meth', @myobj.mymeth
|
77
|
+
assert_empty Assert.stubs
|
78
|
+
end
|
79
|
+
|
80
|
+
should "auto-unstub any stubs on teardown" do
|
81
|
+
context_class = ::Factory.modes_off_context_class do
|
82
|
+
setup do
|
83
|
+
Assert.stub('1', :to_s){ 'one' }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context_class.run_setups('scope')
|
88
|
+
assert_equal 1, Assert.stubs.size
|
89
|
+
|
90
|
+
context_class.run_teardowns('scope')
|
91
|
+
assert_empty Assert.stubs
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
28
96
|
end
|
data/test/unit/context_tests.rb
CHANGED
data/test/unit/macro_tests.rb
CHANGED
@@ -4,7 +4,7 @@ require 'assert/macro'
|
|
4
4
|
class Assert::Macro
|
5
5
|
|
6
6
|
class UnitTests < Assert::Context
|
7
|
-
desc "
|
7
|
+
desc "Assert::Macro"
|
8
8
|
setup do
|
9
9
|
@macro = Assert::Macro.new {}
|
10
10
|
end
|
@@ -34,7 +34,7 @@ class Assert::Macro
|
|
34
34
|
end
|
35
35
|
|
36
36
|
class InstanceMethodsTests < Assert::Context
|
37
|
-
desc "
|
37
|
+
desc "have_instance_methods macro: this class"
|
38
38
|
subject do
|
39
39
|
class ::InstExample
|
40
40
|
(1..6).each {|i| define_method("method_#{i}") {}}
|
@@ -55,7 +55,7 @@ class Assert::Macro
|
|
55
55
|
end
|
56
56
|
|
57
57
|
class ClassMethodsTests < Assert::Context
|
58
|
-
desc "
|
58
|
+
desc "have_class_methods macro: this class"
|
59
59
|
subject do
|
60
60
|
class ::ClassExample
|
61
61
|
class << self
|
@@ -78,7 +78,7 @@ class Assert::Macro
|
|
78
78
|
end
|
79
79
|
|
80
80
|
class ReadersTests < Assert::Context
|
81
|
-
desc "
|
81
|
+
desc "have_readers macro: this class"
|
82
82
|
subject do
|
83
83
|
class ::ReaderExample
|
84
84
|
(1..6).each {|i| attr_reader "method_#{i}"}
|
@@ -99,7 +99,7 @@ class Assert::Macro
|
|
99
99
|
end
|
100
100
|
|
101
101
|
class WritersTests < Assert::Context
|
102
|
-
desc "
|
102
|
+
desc "have_writers macro: this class"
|
103
103
|
subject do
|
104
104
|
class ::WriterExample
|
105
105
|
(1..6).each {|i| attr_writer "method_#{i}"}
|
@@ -120,7 +120,7 @@ class Assert::Macro
|
|
120
120
|
end
|
121
121
|
|
122
122
|
class AccessorsTests < Assert::Context
|
123
|
-
desc "
|
123
|
+
desc "have_accessors macro: this class"
|
124
124
|
subject do
|
125
125
|
class ::AccessorExample
|
126
126
|
(1..6).each {|i| attr_accessor "method_#{i}"}
|
data/test/unit/result_tests.rb
CHANGED
@@ -4,7 +4,7 @@ require 'assert/result'
|
|
4
4
|
module Assert::Result
|
5
5
|
|
6
6
|
class BacktraceTests < Assert::Context
|
7
|
-
desc "
|
7
|
+
desc "Assert::Result::Backtrace"
|
8
8
|
setup do
|
9
9
|
@backtrace = Backtrace.new(caller)
|
10
10
|
end
|
@@ -30,8 +30,8 @@ module Assert::Result
|
|
30
30
|
|
31
31
|
end
|
32
32
|
|
33
|
-
class
|
34
|
-
desc "
|
33
|
+
class BaseTests < Assert::Context
|
34
|
+
desc "Assert::Result::Base"
|
35
35
|
setup do
|
36
36
|
@test = Factory.test("a test name")
|
37
37
|
@result = Assert::Result::Base.new(@test, "a message", ["line 1", "line2"])
|
@@ -73,10 +73,6 @@ module Assert::Result
|
|
73
73
|
assert_equal ['bt'], subject.backtrace
|
74
74
|
end
|
75
75
|
|
76
|
-
end
|
77
|
-
|
78
|
-
class ToStringTests < UnitTests
|
79
|
-
|
80
76
|
should "include its test context name in the to_s" do
|
81
77
|
assert subject.to_s.include?(subject.test_name)
|
82
78
|
end
|
@@ -99,14 +95,14 @@ module Assert::Result
|
|
99
95
|
end
|
100
96
|
|
101
97
|
class PassTests < Assert::Context
|
102
|
-
desc "
|
98
|
+
desc "Assert::Result::Pass"
|
103
99
|
setup do
|
104
100
|
@test = Factory.test("a test name")
|
105
101
|
@result = Assert::Result::Pass.new(@test, "passed", [])
|
106
102
|
end
|
107
103
|
subject { @result }
|
108
104
|
|
109
|
-
should "
|
105
|
+
should "pass?" do
|
110
106
|
assert_equal true, subject.pass?
|
111
107
|
end
|
112
108
|
|
@@ -131,14 +127,14 @@ module Assert::Result
|
|
131
127
|
end
|
132
128
|
|
133
129
|
class IgnoreTests < Assert::Context
|
134
|
-
desc "
|
130
|
+
desc "Assert::Result::Ignore"
|
135
131
|
setup do
|
136
132
|
@test = Factory.test("a test name")
|
137
133
|
@result = Assert::Result::Ignore.new(@test, "ignored", [])
|
138
134
|
end
|
139
135
|
subject { @result }
|
140
136
|
|
141
|
-
should "
|
137
|
+
should "ignore?" do
|
142
138
|
assert_equal true, subject.ignore?
|
143
139
|
end
|
144
140
|
|
@@ -163,7 +159,7 @@ module Assert::Result
|
|
163
159
|
end
|
164
160
|
|
165
161
|
class FailTests < Assert::Context
|
166
|
-
desc "
|
162
|
+
desc "Assert::Result::Fail"
|
167
163
|
setup do
|
168
164
|
@test = Factory.test("a test name")
|
169
165
|
@result = Assert::Result::Fail.new(@test, "failed", [])
|
@@ -174,7 +170,7 @@ module Assert::Result
|
|
174
170
|
assert_kind_of RuntimeError, Assert::Result::TestFailure.new
|
175
171
|
end
|
176
172
|
|
177
|
-
should "
|
173
|
+
should "fail?" do
|
178
174
|
assert_equal true, subject.fail?
|
179
175
|
end
|
180
176
|
|
@@ -199,7 +195,7 @@ module Assert::Result
|
|
199
195
|
end
|
200
196
|
|
201
197
|
class SkipTests < Assert::Context
|
202
|
-
desc "
|
198
|
+
desc "Assert::Result::Skip"
|
203
199
|
setup do
|
204
200
|
@test = Factory.test("a test name")
|
205
201
|
@exception = nil
|
@@ -216,7 +212,7 @@ module Assert::Result
|
|
216
212
|
assert_kind_of RuntimeError, Assert::Result::TestSkipped.new
|
217
213
|
end
|
218
214
|
|
219
|
-
should "
|
215
|
+
should "skip?" do
|
220
216
|
assert_equal true, subject.skip?
|
221
217
|
end
|
222
218
|
|
@@ -241,7 +237,7 @@ module Assert::Result
|
|
241
237
|
end
|
242
238
|
|
243
239
|
class ErrorTests < Assert::Context
|
244
|
-
desc "
|
240
|
+
desc "Assert::Result::Error"
|
245
241
|
setup do
|
246
242
|
@test = Factory.test("a test name")
|
247
243
|
@exception = nil
|
@@ -254,7 +250,7 @@ module Assert::Result
|
|
254
250
|
end
|
255
251
|
subject { @result }
|
256
252
|
|
257
|
-
should "
|
253
|
+
should "error?" do
|
258
254
|
assert_equal true, subject.error?
|
259
255
|
end
|
260
256
|
|
data/test/unit/runner_tests.rb
CHANGED
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'assert/stub'
|
3
|
+
|
4
|
+
class Assert::Stub
|
5
|
+
|
6
|
+
class UnitTests < Assert::Context
|
7
|
+
desc "Assert::Stub"
|
8
|
+
setup do
|
9
|
+
@myclass = Class.new do
|
10
|
+
def mymeth; 'meth'; end
|
11
|
+
def myval(val); val; end
|
12
|
+
def myargs(*args); args; end
|
13
|
+
def myvalargs(val1, val2, *args); [val1, val2, args]; end
|
14
|
+
def myblk(&block); block.call; end
|
15
|
+
end
|
16
|
+
@myobj = @myclass.new
|
17
|
+
|
18
|
+
@stub = Assert::Stub.new(@myobj, :mymeth)
|
19
|
+
end
|
20
|
+
subject{ @stub }
|
21
|
+
|
22
|
+
should have_readers :method_name, :name, :do
|
23
|
+
should have_writers :do
|
24
|
+
should have_cmeths :key
|
25
|
+
|
26
|
+
should "generate a key given an object and method name" do
|
27
|
+
obj = @myobj
|
28
|
+
meth = :mymeth
|
29
|
+
assert_equal "--#{obj.object_id}--#{meth}--", Assert::Stub.key(obj, meth)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "know its names" do
|
33
|
+
assert_equal 'mymeth', subject.method_name
|
34
|
+
assert_equal "__assert_stub__#{subject.method_name}", subject.name
|
35
|
+
end
|
36
|
+
|
37
|
+
should "complain when called if no do block was given" do
|
38
|
+
assert_raises Assert::StubError do
|
39
|
+
@myobj.mymeth
|
40
|
+
end
|
41
|
+
|
42
|
+
subject.do = proc{ 'mymeth' }
|
43
|
+
assert_nothing_raised do
|
44
|
+
@myobj.mymeth
|
45
|
+
end
|
46
|
+
|
47
|
+
assert_nothing_raised do
|
48
|
+
Assert::Stub.new(@myobj, :mymeth){ 'mymeth' }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
should "complain if stubbing a method that the object doesn't respond to" do
|
53
|
+
assert_raises Assert::StubError do
|
54
|
+
Assert::Stub.new(@myobj, :some_other_meth)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
should "complain if stubbed and called with no `do` proc given" do
|
59
|
+
assert_raises(Assert::StubError){ @myobj.mymeth }
|
60
|
+
end
|
61
|
+
|
62
|
+
should "complain if stubbed and called with mismatched arity" do
|
63
|
+
Assert::Stub.new(@myobj, :myval){ 'myval' }
|
64
|
+
assert_raises(Assert::StubError){ @myobj.myval }
|
65
|
+
assert_nothing_raised { @myobj.myval(1) }
|
66
|
+
assert_raises(Assert::StubError){ @myobj.myval(1,2) }
|
67
|
+
|
68
|
+
Assert::Stub.new(@myobj, :myargs){ 'myargs' }
|
69
|
+
assert_nothing_raised { @myobj.myargs }
|
70
|
+
assert_nothing_raised { @myobj.myargs(1) }
|
71
|
+
assert_nothing_raised { @myobj.myargs(1,2) }
|
72
|
+
|
73
|
+
Assert::Stub.new(@myobj, :myvalargs){ 'myvalargs' }
|
74
|
+
assert_raises(Assert::StubError){ @myobj.myvalargs }
|
75
|
+
assert_raises(Assert::StubError){ @myobj.myvalargs(1) }
|
76
|
+
assert_nothing_raised { @myobj.myvalargs(1,2) }
|
77
|
+
assert_nothing_raised { @myobj.myvalargs(1,2,3) }
|
78
|
+
end
|
79
|
+
|
80
|
+
should "complain if stubbed with mismatched arity" do
|
81
|
+
assert_raises(Assert::StubError) do
|
82
|
+
Assert::Stub.new(@myobj, :myval).with(){ 'myval' }
|
83
|
+
end
|
84
|
+
assert_raises(Assert::StubError) do
|
85
|
+
Assert::Stub.new(@myobj, :myval).with(1,2){ 'myval' }
|
86
|
+
end
|
87
|
+
assert_nothing_raised do
|
88
|
+
Assert::Stub.new(@myobj, :myval).with(1){ 'myval' }
|
89
|
+
end
|
90
|
+
|
91
|
+
assert_nothing_raised do
|
92
|
+
Assert::Stub.new(@myobj, :myargs).with(){ 'myargs' }
|
93
|
+
end
|
94
|
+
assert_nothing_raised do
|
95
|
+
Assert::Stub.new(@myobj, :myargs).with(1,2){ 'myargs' }
|
96
|
+
end
|
97
|
+
assert_nothing_raised do
|
98
|
+
Assert::Stub.new(@myobj, :myargs).with(1){ 'myargs' }
|
99
|
+
end
|
100
|
+
|
101
|
+
assert_raises(Assert::StubError) do
|
102
|
+
Assert::Stub.new(@myobj, :myvalargs).with(){ 'myvalargs' }
|
103
|
+
end
|
104
|
+
assert_raises(Assert::StubError) do
|
105
|
+
Assert::Stub.new(@myobj, :myvalargs).with(1){ 'myvalargs' }
|
106
|
+
end
|
107
|
+
assert_nothing_raised do
|
108
|
+
Assert::Stub.new(@myobj, :myvalargs).with(1,2){ 'myvalargs' }
|
109
|
+
end
|
110
|
+
assert_nothing_raised do
|
111
|
+
Assert::Stub.new(@myobj, :myvalargs).with(1,2,3){ 'myvalargs' }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
should "stub methods with no args" do
|
116
|
+
subject.teardown
|
117
|
+
|
118
|
+
assert_equal 'meth', @myobj.mymeth
|
119
|
+
Assert::Stub.new(@myobj, :mymeth){ 'mymeth' }
|
120
|
+
assert_equal 'mymeth', @myobj.mymeth
|
121
|
+
end
|
122
|
+
|
123
|
+
should "stub methods with required arg" do
|
124
|
+
assert_equal 1, @myobj.myval(1)
|
125
|
+
stub = Assert::Stub.new(@myobj, :myval){ |val| val.to_s }
|
126
|
+
assert_equal '1', @myobj.myval(1)
|
127
|
+
assert_equal '2', @myobj.myval(2)
|
128
|
+
stub.with(2){ 'two' }
|
129
|
+
assert_equal 'two', @myobj.myval(2)
|
130
|
+
end
|
131
|
+
|
132
|
+
should "stub methods with variable args" do
|
133
|
+
assert_equal [1,2], @myobj.myargs(1,2)
|
134
|
+
stub = Assert::Stub.new(@myobj, :myargs){ |*args| args.join(',') }
|
135
|
+
assert_equal '1,2', @myobj.myargs(1,2)
|
136
|
+
assert_equal '3,4,5', @myobj.myargs(3,4,5)
|
137
|
+
stub.with(3,4,5){ 'three-four-five' }
|
138
|
+
assert_equal 'three-four-five', @myobj.myargs(3,4,5)
|
139
|
+
end
|
140
|
+
|
141
|
+
should "stub methods with required args and variable args" do
|
142
|
+
assert_equal [1,2, [3]], @myobj.myvalargs(1,2,3)
|
143
|
+
stub = Assert::Stub.new(@myobj, :myvalargs){ |*args| args.join(',') }
|
144
|
+
assert_equal '1,2,3', @myobj.myvalargs(1,2,3)
|
145
|
+
assert_equal '3,4,5', @myobj.myvalargs(3,4,5)
|
146
|
+
stub.with(3,4,5){ 'three-four-five' }
|
147
|
+
assert_equal 'three-four-five', @myobj.myvalargs(3,4,5)
|
148
|
+
end
|
149
|
+
|
150
|
+
should "stub methods that yield blocks" do
|
151
|
+
blkcalled = false
|
152
|
+
blk = proc{ blkcalled = true }
|
153
|
+
@myobj.myblk(&blk)
|
154
|
+
assert_equal true, blkcalled
|
155
|
+
|
156
|
+
blkcalled = false
|
157
|
+
Assert::Stub.new(@myobj, :myblk){ blkcalled = 'true' }
|
158
|
+
@myobj.myblk(&blk)
|
159
|
+
assert_equal 'true', blkcalled
|
160
|
+
end
|
161
|
+
|
162
|
+
should "stub methods even if they are from a super class" do
|
163
|
+
mysubclass = Class.new(@myclass)
|
164
|
+
mysubobj = mysubclass.new
|
165
|
+
|
166
|
+
assert_equal 1, mysubobj.myval(1)
|
167
|
+
stub = Assert::Stub.new(mysubobj, :myval){ |val| val.to_s }
|
168
|
+
assert_equal '1', mysubobj.myval(1)
|
169
|
+
assert_equal '2', mysubobj.myval(2)
|
170
|
+
stub.with(2){ 'two' }
|
171
|
+
assert_equal 'two', mysubobj.myval(2)
|
172
|
+
end
|
173
|
+
|
174
|
+
should "be removable" do
|
175
|
+
assert_equal 1, @myobj.myval(1)
|
176
|
+
stub = Assert::Stub.new(@myobj, :myval){ |val| val.to_s }
|
177
|
+
assert_equal '1', @myobj.myval(1)
|
178
|
+
stub.teardown
|
179
|
+
assert_equal 1, @myobj.myval(1)
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
class NullStubTests < UnitTests
|
185
|
+
desc "NullStub"
|
186
|
+
setup do
|
187
|
+
@ns = NullStub.new
|
188
|
+
end
|
189
|
+
subject{ @ns }
|
190
|
+
|
191
|
+
should have_imeths :teardown
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
data/test/unit/test_tests.rb
CHANGED
@@ -5,8 +5,8 @@ require 'assert/config'
|
|
5
5
|
|
6
6
|
class Assert::Test
|
7
7
|
|
8
|
-
class
|
9
|
-
desc "
|
8
|
+
class UnitTests < Assert::Context
|
9
|
+
desc "Assert::Test"
|
10
10
|
setup do
|
11
11
|
@test_code = lambda{ assert(true) }
|
12
12
|
@context_class = Factory.modes_off_context_class{ desc "context class" }
|
@@ -68,7 +68,7 @@ class Assert::Test
|
|
68
68
|
|
69
69
|
end
|
70
70
|
|
71
|
-
class PassFailIgnoreTotalTests <
|
71
|
+
class PassFailIgnoreTotalTests < UnitTests
|
72
72
|
setup do
|
73
73
|
@test = Factory.test("pass fail ignore test", @context_info) do
|
74
74
|
ignore("something")
|
@@ -122,7 +122,7 @@ class Assert::Test
|
|
122
122
|
|
123
123
|
end
|
124
124
|
|
125
|
-
class SkipHandlingTests <
|
125
|
+
class SkipHandlingTests < UnitTests
|
126
126
|
setup do
|
127
127
|
@test = Factory.test("skip test", @context_info){ skip }
|
128
128
|
@test.run
|
@@ -163,7 +163,7 @@ class Assert::Test
|
|
163
163
|
|
164
164
|
end
|
165
165
|
|
166
|
-
class ErrorHandlingTests <
|
166
|
+
class ErrorHandlingTests < UnitTests
|
167
167
|
setup do
|
168
168
|
@test = Factory.test("error test", @context_info) do
|
169
169
|
raise StandardError, "WHAT"
|
@@ -206,7 +206,7 @@ class Assert::Test
|
|
206
206
|
|
207
207
|
end
|
208
208
|
|
209
|
-
class SignalExceptionHandlingTests <
|
209
|
+
class SignalExceptionHandlingTests < UnitTests
|
210
210
|
setup do
|
211
211
|
@test = Factory.test("signal test", @context_info) do
|
212
212
|
raise SignalException, "USR1"
|
@@ -234,7 +234,7 @@ class Assert::Test
|
|
234
234
|
|
235
235
|
end
|
236
236
|
|
237
|
-
class ComparingTests <
|
237
|
+
class ComparingTests < UnitTests
|
238
238
|
desc "<=> another test"
|
239
239
|
setup do
|
240
240
|
@test = Factory.test("mmm")
|
@@ -258,7 +258,7 @@ class Assert::Test
|
|
258
258
|
|
259
259
|
end
|
260
260
|
|
261
|
-
class CaptureOutTests <
|
261
|
+
class CaptureOutTests < UnitTests
|
262
262
|
desc "when capturing std out"
|
263
263
|
setup do
|
264
264
|
@capture_config = Assert::Config.new(:capture_output => true)
|
data/test/unit/view_tests.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 35
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 2.
|
8
|
+
- 11
|
9
|
+
- 0
|
10
|
+
version: 2.11.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,12 +16,10 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2014-
|
19
|
+
date: 2014-06-17 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
23
|
-
name: ansi
|
24
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
23
|
none: false
|
26
24
|
requirements:
|
27
25
|
- - ~>
|
@@ -31,8 +29,10 @@ dependencies:
|
|
31
29
|
- 1
|
32
30
|
- 3
|
33
31
|
version: "1.3"
|
32
|
+
type: :runtime
|
33
|
+
name: ansi
|
34
|
+
version_requirements: *id001
|
34
35
|
prerelease: false
|
35
|
-
requirement: *id001
|
36
36
|
description: Test::Unit style testing framework, just better than Test::Unit.
|
37
37
|
email:
|
38
38
|
- kelly@kellyredding.com
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/assert/macros/methods.rb
|
68
68
|
- lib/assert/result.rb
|
69
69
|
- lib/assert/runner.rb
|
70
|
+
- lib/assert/stub.rb
|
70
71
|
- lib/assert/suite.rb
|
71
72
|
- lib/assert/test.rb
|
72
73
|
- lib/assert/utils.rb
|
@@ -108,6 +109,7 @@ files:
|
|
108
109
|
- test/unit/macro_tests.rb
|
109
110
|
- test/unit/result_tests.rb
|
110
111
|
- test/unit/runner_tests.rb
|
112
|
+
- test/unit/stub_tests.rb
|
111
113
|
- test/unit/suite_tests.rb
|
112
114
|
- test/unit/test_tests.rb
|
113
115
|
- test/unit/utils_tests.rb
|
@@ -142,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
144
|
requirements: []
|
143
145
|
|
144
146
|
rubyforge_project:
|
145
|
-
rubygems_version: 1.8.
|
147
|
+
rubygems_version: 1.8.25
|
146
148
|
signing_key:
|
147
149
|
specification_version: 3
|
148
150
|
summary: Test::Unit style testing framework, just better than Test::Unit.
|
@@ -178,6 +180,7 @@ test_files:
|
|
178
180
|
- test/unit/macro_tests.rb
|
179
181
|
- test/unit/result_tests.rb
|
180
182
|
- test/unit/runner_tests.rb
|
183
|
+
- test/unit/stub_tests.rb
|
181
184
|
- test/unit/suite_tests.rb
|
182
185
|
- test/unit/test_tests.rb
|
183
186
|
- test/unit/utils_tests.rb
|