assert 2.16.4 → 2.16.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -8
- data/assert.gemspec +3 -0
- data/lib/assert/factory.rb +3 -155
- data/lib/assert/stub.rb +14 -186
- data/lib/assert/version.rb +1 -1
- data/test/unit/assert_tests.rb +4 -3
- data/test/unit/factory_tests.rb +11 -150
- metadata +24 -8
- data/test/unit/stub_tests.rb +0 -375
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
data.tar.gz:
|
4
|
-
metadata.gz:
|
3
|
+
data.tar.gz: 624cea8dff7a65ada7c133ccf0ad93d8f70fb050
|
4
|
+
metadata.gz: f60b216b9827bab390bb1be74d2161392a087489
|
5
5
|
SHA512:
|
6
|
-
data.tar.gz:
|
7
|
-
metadata.gz:
|
6
|
+
data.tar.gz: 7dda4ee76847adaeb2b3044e186baa73c36c1f1f52c8575fa5afb73366636a86ef84381f9db39049e728881981add634206deb1fb9f1ba97932cecb0aa141f75
|
7
|
+
metadata.gz: 13e4b4811041f08d2686edad2f41654f6d711fdb9ed7f6d2863f7a090397ec8b7e9f460cfb6d1aba55f04cfe149992b5a44d57744f98fda4c001bd6223e7dea5
|
data/README.md
CHANGED
@@ -65,6 +65,16 @@ Assert is an assertion style testing framework, meaning you use assertion statem
|
|
65
65
|
|
66
66
|
## Stub
|
67
67
|
|
68
|
+
Assert comes with a stubbing API - all it does is replace method calls. In general it tries to be friendly and complain if stubbing doesn't match up with the object/method being stubbed:
|
69
|
+
|
70
|
+
* each stub takes a block that is called in place of the method
|
71
|
+
* complains if you stub a method that the object doesn't respond to
|
72
|
+
* complains if you stub with an arity mismatch
|
73
|
+
* no methods are added to `Object` to support stubbing
|
74
|
+
* stubs are auto-unstubbed on test teardown
|
75
|
+
|
76
|
+
**Note**: Assert's stubbing logic has been extracted into a separate gem: [MuchStub](https://github.com/redding/much-stub/#muchstub). However, the main `Assert.{stub|unstub|stub_send|etc}` api is still available (it just proxies MuchStub now).
|
77
|
+
|
68
78
|
```ruby
|
69
79
|
myclass = Class.new do
|
70
80
|
def mymeth; 'meth'; end
|
@@ -117,14 +127,6 @@ myobj.myval(456)
|
|
117
127
|
# => 456
|
118
128
|
```
|
119
129
|
|
120
|
-
Assert comes with a stubbing API - all it does is replace method calls. In general it tries to be friendly and complain if stubbing doesn't match up with the object/method being stubbed:
|
121
|
-
|
122
|
-
* each stub takes a block that is called in place of the method
|
123
|
-
* complains if you stub a method that the object doesn't respond to
|
124
|
-
* complains if you stub with an arity mismatch
|
125
|
-
* no methods are added to `Object` to support stubbing
|
126
|
-
* stubs are auto-unstubbed on test teardown
|
127
|
-
|
128
130
|
## Factory
|
129
131
|
|
130
132
|
```ruby
|
@@ -173,6 +175,8 @@ module Factory
|
|
173
175
|
end
|
174
176
|
```
|
175
177
|
|
178
|
+
**Note**: Assert's factory logic has been extracted into a separate gem: [MuchFactory](https://github.com/redding/much-factory/#muchfactory). However, the main api above is still available (it just delegates to MuchFactory now).
|
179
|
+
|
176
180
|
## CLI
|
177
181
|
|
178
182
|
```sh
|
data/assert.gemspec
CHANGED
data/lib/assert/factory.rb
CHANGED
@@ -1,162 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
require 'time'
|
1
|
+
require 'much-factory'
|
3
2
|
|
4
3
|
module Assert
|
5
4
|
|
6
5
|
module Factory
|
7
|
-
extend
|
8
|
-
|
9
|
-
def integer(max = nil)
|
10
|
-
self.type_cast(Random.integer(max), :integer)
|
11
|
-
end
|
12
|
-
|
13
|
-
def float(max = nil)
|
14
|
-
self.type_cast(Random.float(max), :float)
|
15
|
-
end
|
16
|
-
|
17
|
-
DAYS_IN_A_YEAR = 365
|
18
|
-
SECONDS_IN_DAY = 24 * 60 * 60
|
19
|
-
|
20
|
-
def date
|
21
|
-
@date ||= self.type_cast(Random.date_string, :date)
|
22
|
-
@date + Random.integer(DAYS_IN_A_YEAR)
|
23
|
-
end
|
24
|
-
|
25
|
-
def time
|
26
|
-
@time ||= self.type_cast(Random.time_string, :time)
|
27
|
-
@time + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
|
28
|
-
end
|
29
|
-
|
30
|
-
def datetime
|
31
|
-
@datetime ||= self.type_cast(Random.datetime_string, :datetime)
|
32
|
-
@datetime + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
|
33
|
-
end
|
34
|
-
|
35
|
-
def string(length = nil)
|
36
|
-
self.type_cast(Random.string(length || 10), :string)
|
37
|
-
end
|
38
|
-
|
39
|
-
def text(length = nil)
|
40
|
-
self.type_cast(Random.string(length || 20), :string)
|
41
|
-
end
|
42
|
-
|
43
|
-
def slug(length = nil)
|
44
|
-
self.type_cast(Random.string(length || 5), :string)
|
45
|
-
end
|
46
|
-
|
47
|
-
def hex(length = nil)
|
48
|
-
self.type_cast(Random.hex_string(length), :string)
|
49
|
-
end
|
50
|
-
|
51
|
-
def file_name(length = nil)
|
52
|
-
self.type_cast(Random.file_name_string(length), :string)
|
53
|
-
end
|
54
|
-
|
55
|
-
def dir_path(length = nil)
|
56
|
-
self.type_cast(Random.dir_path_string(length), :string)
|
57
|
-
end
|
58
|
-
|
59
|
-
def file_path
|
60
|
-
self.type_cast(Random.file_path_string, :string)
|
61
|
-
end
|
62
|
-
|
63
|
-
alias_method :path, :dir_path
|
64
|
-
|
65
|
-
def url(host = nil, length = nil)
|
66
|
-
self.type_cast(Random.url_string(host, length), :string)
|
67
|
-
end
|
68
|
-
|
69
|
-
def email(domain = nil, length = nil)
|
70
|
-
self.type_cast(Random.email_string(domain, length), :string)
|
71
|
-
end
|
72
|
-
|
73
|
-
def binary
|
74
|
-
self.type_cast(Random.binary, :binary)
|
75
|
-
end
|
76
|
-
|
77
|
-
def boolean
|
78
|
-
self.type_cast(Random.integer.even?, :boolean)
|
79
|
-
end
|
80
|
-
|
81
|
-
def type_cast(value, type)
|
82
|
-
self.type_converter.send(type, value)
|
83
|
-
end
|
84
|
-
|
85
|
-
def type_converter; TypeConverter; end
|
86
|
-
|
87
|
-
module TypeConverter
|
88
|
-
def self.string(input); input.to_s; end
|
89
|
-
def self.integer(input); input.to_i; end
|
90
|
-
def self.float(input); input.to_f; end
|
91
|
-
def self.datetime(input); DateTime.parse(input.to_s); end
|
92
|
-
def self.time(input); Time.parse(input.to_s); end
|
93
|
-
def self.date(input); Date.parse(input.to_s); end
|
94
|
-
def self.boolean(input); !!input; end
|
95
|
-
def self.binary(input); input; end
|
96
|
-
end
|
97
|
-
|
98
|
-
module Random
|
99
|
-
|
100
|
-
# rand given a max int value returns integers between 0 and max-1
|
101
|
-
def self.integer(max = nil)
|
102
|
-
rand(max || 32_766) + 1
|
103
|
-
end
|
104
|
-
|
105
|
-
# `rand` with no args gives a float between 0 and 1
|
106
|
-
def self.float(max = nil)
|
107
|
-
(max || 100).to_f * rand
|
108
|
-
end
|
109
|
-
|
110
|
-
def self.date_string
|
111
|
-
Time.now.strftime("%Y-%m-%d")
|
112
|
-
end
|
113
|
-
|
114
|
-
def self.datetime_string
|
115
|
-
Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
116
|
-
end
|
117
|
-
|
118
|
-
def self.time_string
|
119
|
-
Time.now.strftime("%H:%M:%S")
|
120
|
-
end
|
121
|
-
|
122
|
-
DICTIONARY = [*'a'..'z'].freeze
|
123
|
-
def self.string(length = nil)
|
124
|
-
[*0..((length || 10) - 1)].map{ |n| DICTIONARY[rand(DICTIONARY.size)] }.join
|
125
|
-
end
|
126
|
-
|
127
|
-
def self.hex_string(length = nil)
|
128
|
-
length ||= 10
|
129
|
-
self.integer(("f" * length).hex - 1).to_s(16).rjust(length, '0')
|
130
|
-
end
|
131
|
-
|
132
|
-
def self.file_name_string(length = nil)
|
133
|
-
length ||= 6
|
134
|
-
"#{self.string(length)}.#{self.string(3)}"
|
135
|
-
end
|
136
|
-
|
137
|
-
def self.dir_path_string(length = nil)
|
138
|
-
length ||= 12
|
139
|
-
File.join(*self.string(length).scan(/.{1,4}/))
|
140
|
-
end
|
141
|
-
|
142
|
-
def self.file_path_string
|
143
|
-
File.join(self.dir_path_string, self.file_name_string)
|
144
|
-
end
|
145
|
-
|
146
|
-
def self.url_string(host = nil, length = nil)
|
147
|
-
File.join(host.to_s, self.dir_path_string(length))
|
148
|
-
end
|
149
|
-
|
150
|
-
def self.email_string(domain = nil, length = nil)
|
151
|
-
domain ||= "#{self.string(5)}.com"
|
152
|
-
"#{self.string(length)}@#{domain}"
|
153
|
-
end
|
154
|
-
|
155
|
-
def self.binary
|
156
|
-
[ self.integer(10000) ].pack('N*')
|
157
|
-
end
|
158
|
-
|
159
|
-
end
|
6
|
+
extend MuchFactory
|
7
|
+
include MuchFactory
|
160
8
|
|
161
9
|
end
|
162
10
|
|
data/lib/assert/stub.rb
CHANGED
@@ -1,203 +1,31 @@
|
|
1
|
+
require 'much-stub'
|
2
|
+
|
1
3
|
module Assert
|
2
4
|
|
3
5
|
def self.stubs
|
4
|
-
|
6
|
+
MuchStub.stubs
|
5
7
|
end
|
6
8
|
|
7
|
-
def self.stub(
|
8
|
-
|
9
|
-
Assert::Stub.new(obj, meth, caller_locations)
|
10
|
-
end).tap{ |s| s.do = block }
|
9
|
+
def self.stub(*args, &block)
|
10
|
+
MuchStub.stub(*args, &block)
|
11
11
|
end
|
12
12
|
|
13
|
-
def self.unstub(
|
14
|
-
|
13
|
+
def self.unstub(*args)
|
14
|
+
MuchStub.unstub(*args)
|
15
15
|
end
|
16
16
|
|
17
17
|
def self.unstub!
|
18
|
-
|
18
|
+
MuchStub.unstub!
|
19
19
|
end
|
20
20
|
|
21
|
-
def self.stub_send(
|
21
|
+
def self.stub_send(*args, &block)
|
22
22
|
orig_caller = caller_locations
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
StubError = Class.new(ArgumentError)
|
30
|
-
NotStubbedError = Class.new(StubError)
|
31
|
-
StubArityError = Class.new(StubError)
|
32
|
-
|
33
|
-
class Stub
|
34
|
-
|
35
|
-
NullStub = Class.new do
|
36
|
-
def teardown; end # no-op
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.key(object, method_name)
|
40
|
-
"--#{object.object_id}--#{method_name}--"
|
41
|
-
end
|
42
|
-
|
43
|
-
attr_reader :method_name, :name, :ivar_name, :do
|
44
|
-
|
45
|
-
def initialize(object, method_name, orig_caller = nil, &block)
|
46
|
-
orig_caller ||= caller_locations
|
47
|
-
@metaclass = class << object; self; end
|
48
|
-
@method_name = method_name.to_s
|
49
|
-
@name = "__assert_stub__#{object.object_id}_#{@method_name}"
|
50
|
-
@ivar_name = "@__assert_stub_#{object.object_id}_" \
|
51
|
-
"#{@method_name.to_sym.object_id}"
|
52
|
-
|
53
|
-
setup(object, orig_caller)
|
54
|
-
|
55
|
-
@do = block
|
56
|
-
@lookup = {}
|
57
|
-
end
|
58
|
-
|
59
|
-
def do=(block)
|
60
|
-
@do = block || @do
|
61
|
-
end
|
62
|
-
|
63
|
-
def call_method(args, &block)
|
64
|
-
@method.call(*args, &block)
|
65
|
-
end
|
66
|
-
|
67
|
-
def call(args, orig_caller = nil, &block)
|
68
|
-
orig_caller ||= caller_locations
|
69
|
-
unless arity_matches?(args)
|
70
|
-
msg = "arity mismatch on `#{@method_name}`: " \
|
71
|
-
"expected #{number_of_args(@method.arity)}, " \
|
72
|
-
"called with #{args.size}"
|
73
|
-
raise StubArityError, msg, orig_caller.map(&:to_s)
|
74
|
-
end
|
75
|
-
lookup(args, orig_caller).call(*args, &block)
|
76
|
-
rescue NotStubbedError => exception
|
77
|
-
@lookup.rehash
|
78
|
-
lookup(args, orig_caller).call(*args, &block)
|
79
|
-
end
|
80
|
-
|
81
|
-
def with(*args, &block)
|
82
|
-
orig_caller = caller_locations
|
83
|
-
unless arity_matches?(args)
|
84
|
-
msg = "arity mismatch on `#{@method_name}`: " \
|
85
|
-
"expected #{number_of_args(@method.arity)}, " \
|
86
|
-
"stubbed with #{args.size}"
|
87
|
-
raise StubArityError, msg, orig_caller.map(&:to_s)
|
88
|
-
end
|
89
|
-
@lookup[args] = block
|
90
|
-
end
|
91
|
-
|
92
|
-
def teardown
|
93
|
-
@metaclass.send(:undef_method, @method_name)
|
94
|
-
Assert.send(:remove_instance_variable, @ivar_name)
|
95
|
-
@metaclass.send(:alias_method, @method_name, @name)
|
96
|
-
@metaclass.send(:undef_method, @name)
|
97
|
-
end
|
98
|
-
|
99
|
-
def inspect
|
100
|
-
"#<#{self.class}:#{'0x0%x' % (object_id << 1)}" \
|
101
|
-
" @method_name=#{@method_name.inspect}" \
|
102
|
-
">"
|
103
|
-
end
|
104
|
-
|
105
|
-
private
|
106
|
-
|
107
|
-
def setup(object, orig_caller)
|
108
|
-
unless object.respond_to?(@method_name)
|
109
|
-
msg = "#{object.inspect} does not respond to `#{@method_name}`"
|
110
|
-
raise StubError, msg, orig_caller.map(&:to_s)
|
111
|
-
end
|
112
|
-
is_constant = object.kind_of?(Module)
|
113
|
-
local_object_methods = object.methods(false).map(&:to_s)
|
114
|
-
all_object_methods = object.methods.map(&:to_s)
|
115
|
-
if (is_constant && !local_object_methods.include?(@method_name)) ||
|
116
|
-
(!is_constant && !all_object_methods.include?(@method_name))
|
117
|
-
params_list = ParameterList.new(object, @method_name)
|
118
|
-
@metaclass.class_eval <<-method
|
119
|
-
def #{@method_name}(#{params_list}); super; end
|
120
|
-
method
|
121
|
-
end
|
122
|
-
|
123
|
-
if !local_object_methods.include?(@name) # already stubbed
|
124
|
-
@metaclass.send(:alias_method, @name, @method_name)
|
125
|
-
end
|
126
|
-
@method = object.method(@name)
|
127
|
-
|
128
|
-
Assert.instance_variable_set(@ivar_name, self)
|
129
|
-
@metaclass.class_eval <<-stub_method
|
130
|
-
def #{@method_name}(*args, &block)
|
131
|
-
Assert.instance_variable_get("#{@ivar_name}").call(args, caller_locations, &block)
|
132
|
-
end
|
133
|
-
stub_method
|
134
|
-
end
|
135
|
-
|
136
|
-
def lookup(args, orig_caller)
|
137
|
-
@lookup.fetch(args) do
|
138
|
-
self.do || begin
|
139
|
-
msg = "#{inspect_call(args)} not stubbed."
|
140
|
-
inspect_lookup_stubs.tap do |stubs|
|
141
|
-
msg += "\nStubs:\n#{stubs}" if !stubs.empty?
|
142
|
-
end
|
143
|
-
raise NotStubbedError, msg, orig_caller.map(&:to_s)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
def arity_matches?(args)
|
149
|
-
return true if @method.arity == args.size # mandatory args
|
150
|
-
return true if @method.arity < 0 && args.size >= (@method.arity+1).abs # variable args
|
151
|
-
return false
|
152
|
-
end
|
153
|
-
|
154
|
-
def inspect_lookup_stubs
|
155
|
-
@lookup.keys.map{ |args| " - #{inspect_call(args)}" }.join("\n")
|
23
|
+
begin
|
24
|
+
MuchStub.stub_send(*args, &block)
|
25
|
+
rescue MuchStub::NotStubbedError => err
|
26
|
+
err.set_backtrace(orig_caller.map(&:to_s))
|
27
|
+
raise err
|
156
28
|
end
|
157
|
-
|
158
|
-
def inspect_call(args)
|
159
|
-
"`#{@method_name}(#{args.map(&:inspect).join(',')})`"
|
160
|
-
end
|
161
|
-
|
162
|
-
def number_of_args(arity)
|
163
|
-
if arity < 0
|
164
|
-
"at least #{(arity + 1).abs}"
|
165
|
-
else
|
166
|
-
arity
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
module ParameterList
|
171
|
-
LETTERS = ('a'..'z').to_a.freeze
|
172
|
-
|
173
|
-
def self.new(object, method_name)
|
174
|
-
arity = get_arity(object, method_name)
|
175
|
-
params = build_params_from_arity(arity)
|
176
|
-
params << '*args' if arity < 0
|
177
|
-
params << '&block'
|
178
|
-
params.join(', ')
|
179
|
-
end
|
180
|
-
|
181
|
-
private
|
182
|
-
|
183
|
-
def self.get_arity(object, method_name)
|
184
|
-
object.method(method_name).arity
|
185
|
-
rescue NameError
|
186
|
-
-1
|
187
|
-
end
|
188
|
-
|
189
|
-
def self.build_params_from_arity(arity)
|
190
|
-
number = arity < 0 ? (arity + 1).abs : arity
|
191
|
-
(0..(number - 1)).map{ |param_index| get_param_name(param_index) }
|
192
|
-
end
|
193
|
-
|
194
|
-
def self.get_param_name(param_index)
|
195
|
-
param_index += LETTERS.size # avoid getting 0 for the number of letters
|
196
|
-
number_of_letters, letter_index = param_index.divmod(LETTERS.size)
|
197
|
-
LETTERS[letter_index] * number_of_letters
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
29
|
end
|
202
30
|
|
203
31
|
end
|
data/lib/assert/version.rb
CHANGED
data/test/unit/assert_tests.rb
CHANGED
@@ -2,6 +2,7 @@ require 'assert'
|
|
2
2
|
|
3
3
|
require 'assert/config'
|
4
4
|
require 'assert/stub'
|
5
|
+
require 'much-stub'
|
5
6
|
|
6
7
|
module Assert
|
7
8
|
|
@@ -41,7 +42,7 @@ module Assert
|
|
41
42
|
|
42
43
|
should "build a stub" do
|
43
44
|
stub1 = Assert.stub(@myobj, :mymeth)
|
44
|
-
assert_kind_of
|
45
|
+
assert_kind_of MuchStub::Stub, stub1
|
45
46
|
end
|
46
47
|
|
47
48
|
should "lookup stubs that have been called before" do
|
@@ -52,7 +53,7 @@ module Assert
|
|
52
53
|
|
53
54
|
should "set the stub's do block if given a block" do
|
54
55
|
Assert.stub(@myobj, :mymeth)
|
55
|
-
assert_raises(NotStubbedError){ @myobj.mymeth }
|
56
|
+
assert_raises(MuchStub::NotStubbedError){ @myobj.mymeth }
|
56
57
|
Assert.stub(@myobj, :mymeth){ @stub_value }
|
57
58
|
assert_equal @stub_value, @myobj.mymeth
|
58
59
|
end
|
@@ -96,7 +97,7 @@ module Assert
|
|
96
97
|
end
|
97
98
|
|
98
99
|
should "be able to call a stub's original method" do
|
99
|
-
err = assert_raises(NotStubbedError){ Assert.stub_send(@myobj, :mymeth) }
|
100
|
+
err = assert_raises(MuchStub::NotStubbedError){ Assert.stub_send(@myobj, :mymeth) }
|
100
101
|
assert_includes 'not stubbed.', err.message
|
101
102
|
assert_includes 'test/unit/assert_tests.rb', err.backtrace.first
|
102
103
|
|
data/test/unit/factory_tests.rb
CHANGED
@@ -1,166 +1,27 @@
|
|
1
1
|
require 'assert'
|
2
2
|
require 'assert/factory'
|
3
3
|
|
4
|
+
require 'much-factory'
|
5
|
+
|
4
6
|
module Assert::Factory
|
5
7
|
|
6
8
|
class UnitTests < Assert::Context
|
7
9
|
desc "Assert::Factory"
|
8
10
|
subject{ Assert::Factory }
|
9
11
|
|
10
|
-
should
|
11
|
-
|
12
|
-
should have_imeths :string, :text, :slug, :hex
|
13
|
-
should have_imeths :file_name, :dir_path, :file_path
|
14
|
-
should have_imeths :path, :url, :email
|
15
|
-
should have_imeths :binary, :boolean
|
16
|
-
should have_imeths :type_cast, :type_converter
|
17
|
-
|
18
|
-
should "return a random integer using `integer`" do
|
19
|
-
assert_kind_of Integer, subject.integer
|
20
|
-
end
|
21
|
-
|
22
|
-
should "allow passing a maximum value using `integer`" do
|
23
|
-
assert_includes subject.integer(2), [1, 2]
|
24
|
-
end
|
25
|
-
|
26
|
-
should "return a random float using `float`" do
|
27
|
-
assert_kind_of Float, subject.float
|
28
|
-
end
|
29
|
-
|
30
|
-
should "allow passing a maximum value using `float`" do
|
31
|
-
float = subject.float(2)
|
32
|
-
assert float <= 2
|
33
|
-
assert float >= 0
|
34
|
-
|
35
|
-
float = subject.float(0)
|
36
|
-
assert_equal 0.0, float
|
37
|
-
end
|
38
|
-
|
39
|
-
should "return a random date using `date`" do
|
40
|
-
assert_kind_of Date, subject.date
|
41
|
-
end
|
42
|
-
|
43
|
-
should "return a random time object using `time`" do
|
44
|
-
assert_kind_of Time, subject.time
|
45
|
-
end
|
46
|
-
|
47
|
-
should "return a random time object using `datetime`" do
|
48
|
-
assert_kind_of DateTime, subject.datetime
|
49
|
-
end
|
50
|
-
|
51
|
-
should "return a random string using `string`" do
|
52
|
-
assert_kind_of String, subject.string
|
53
|
-
assert_equal 10, subject.string.length
|
54
|
-
end
|
55
|
-
|
56
|
-
should "allow passing a maximum length using `string`" do
|
57
|
-
assert_equal 1, subject.string(1).length
|
58
|
-
end
|
59
|
-
|
60
|
-
should "return a random string using `text`" do
|
61
|
-
assert_kind_of String, subject.text
|
62
|
-
assert_equal 20, subject.text.length
|
63
|
-
end
|
64
|
-
|
65
|
-
should "allow passing a maximum length using `text`" do
|
66
|
-
assert_equal 1, subject.text(1).length
|
67
|
-
end
|
68
|
-
|
69
|
-
should "return a random string using `slug`" do
|
70
|
-
assert_kind_of String, subject.slug
|
71
|
-
assert_equal 5, subject.slug.length
|
72
|
-
end
|
73
|
-
|
74
|
-
should "allow passing a maximum length using `slug`" do
|
75
|
-
assert_equal 1, subject.slug(1).length
|
76
|
-
end
|
77
|
-
|
78
|
-
should "return a random hex string using `hex`" do
|
79
|
-
assert_kind_of String, subject.hex
|
80
|
-
assert_match /\A[0-9a-f]{10}\Z/, subject.hex
|
81
|
-
end
|
82
|
-
|
83
|
-
should "allow passing a maximum length using `hex`" do
|
84
|
-
assert_equal 1, subject.hex(1).length
|
85
|
-
end
|
86
|
-
|
87
|
-
should "return a random file name string using `file_name`" do
|
88
|
-
assert_kind_of String, subject.file_name
|
89
|
-
assert_match /\A[a-z]{6}\.[a-z]{3}\Z/, subject.file_name
|
90
|
-
end
|
91
|
-
|
92
|
-
should "allow passing a name length using `file_name`" do
|
93
|
-
assert_match /\A[a-z]{1}.[a-z]{3}\Z/, subject.file_name(1)
|
94
|
-
end
|
12
|
+
should "include and extend MuchFactory" do
|
13
|
+
assert_includes MuchFactory, subject
|
95
14
|
|
96
|
-
|
97
|
-
|
98
|
-
path_segments = subject.dir_path.split('/')
|
99
|
-
assert_equal 3, path_segments.size
|
100
|
-
path_segments.each{ |s| assert_match /\A[a-z]{4}\Z/, s }
|
15
|
+
# https://stackoverflow.com/questions/5197166/ruby-get-a-list-of-extended-modules
|
16
|
+
assert_includes MuchFactory, subject_metaclass.included_modules
|
101
17
|
end
|
102
18
|
|
103
|
-
|
104
|
-
assert_equal 1, subject.dir_path(1).length
|
105
|
-
end
|
106
|
-
|
107
|
-
should "return a random folder path and file name using `file_path`" do
|
108
|
-
assert_kind_of String, subject.file_path
|
109
|
-
segments = subject.file_path.split('/')
|
110
|
-
assert_equal 4, segments.size
|
111
|
-
segments[0..-2].each{ |s| assert_match /\A[a-z]{4}\Z/, s }
|
112
|
-
assert_match /\A[a-z]{6}\.[a-z]{3}\Z/, segments.last
|
113
|
-
end
|
114
|
-
|
115
|
-
should "return a random url string using `url`" do
|
116
|
-
u = subject.url
|
117
|
-
segments = u.split('/')
|
118
|
-
|
119
|
-
assert_kind_of String, u
|
120
|
-
assert_match /\A\//, u
|
121
|
-
assert_equal 4, segments.size
|
122
|
-
segments[1..-1].each{ |s| assert_match /\A[a-z]{4}\Z/, s }
|
123
|
-
end
|
124
|
-
|
125
|
-
should "allow passing a host string using `url`" do
|
126
|
-
host = "example.com"
|
127
|
-
assert_match /\A#{host}\//, subject.url(host)
|
128
|
-
end
|
129
|
-
|
130
|
-
should "allow passing a maximum length using `url`" do
|
131
|
-
assert_equal 2, subject.url('', 1).length # plus leading '/'
|
132
|
-
end
|
133
|
-
|
134
|
-
should "return a random email string using `email`" do
|
135
|
-
e = subject.email
|
136
|
-
assert_kind_of String, e
|
137
|
-
assert_match /\A\w+@\w+\.com\Z/, e
|
138
|
-
end
|
139
|
-
|
140
|
-
should "allow passing a custom domain to `email`" do
|
141
|
-
e = subject.email('example.org')
|
142
|
-
assert_match /@example\.org\Z/, e
|
143
|
-
end
|
144
|
-
|
145
|
-
should "allow passing a mailbox length using `email`" do
|
146
|
-
assert_equal 2, subject.email(nil, 2).split('@').first.size
|
147
|
-
end
|
148
|
-
|
149
|
-
should "return a random binary string using `binary`" do
|
150
|
-
assert_kind_of String, subject.binary
|
151
|
-
end
|
152
|
-
|
153
|
-
should "return a random boolean using `boolean`" do
|
154
|
-
assert_includes subject.boolean.class, [ TrueClass, FalseClass ]
|
155
|
-
end
|
156
|
-
|
157
|
-
should "type cast values to a specified type using `type_cast`" do
|
158
|
-
expected = Date.parse('2013-01-01')
|
159
|
-
assert_equal expected, subject.type_cast('2013-01-01', :date)
|
160
|
-
end
|
19
|
+
private
|
161
20
|
|
162
|
-
|
163
|
-
|
21
|
+
def subject_metaclass
|
22
|
+
class << subject
|
23
|
+
self
|
24
|
+
end
|
164
25
|
end
|
165
26
|
|
166
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.16.
|
4
|
+
version: 2.16.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelly Redding
|
@@ -10,9 +10,27 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2018-
|
14
|
-
dependencies:
|
15
|
-
|
13
|
+
date: 2018-06-06 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: much-factory
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- &id002 !ruby/object:Gem::Version
|
22
|
+
version: 0.1.0
|
23
|
+
type: :runtime
|
24
|
+
version_requirements: *id001
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: much-stub
|
27
|
+
prerelease: false
|
28
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- *id002
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id003
|
16
34
|
description: Assertion style testing framework.
|
17
35
|
email:
|
18
36
|
- kelly@kellyredding.com
|
@@ -97,7 +115,6 @@ files:
|
|
97
115
|
- test/unit/macro_tests.rb
|
98
116
|
- test/unit/result_tests.rb
|
99
117
|
- test/unit/runner_tests.rb
|
100
|
-
- test/unit/stub_tests.rb
|
101
118
|
- test/unit/suite_tests.rb
|
102
119
|
- test/unit/test_tests.rb
|
103
120
|
- test/unit/utils_tests.rb
|
@@ -116,13 +133,13 @@ require_paths:
|
|
116
133
|
- lib
|
117
134
|
required_ruby_version: !ruby/object:Gem::Requirement
|
118
135
|
requirements:
|
119
|
-
- &
|
136
|
+
- &id004
|
120
137
|
- ">="
|
121
138
|
- !ruby/object:Gem::Version
|
122
139
|
version: "0"
|
123
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
141
|
requirements:
|
125
|
-
- *
|
142
|
+
- *id004
|
126
143
|
requirements: []
|
127
144
|
|
128
145
|
rubyforge_project:
|
@@ -168,7 +185,6 @@ test_files:
|
|
168
185
|
- test/unit/macro_tests.rb
|
169
186
|
- test/unit/result_tests.rb
|
170
187
|
- test/unit/runner_tests.rb
|
171
|
-
- test/unit/stub_tests.rb
|
172
188
|
- test/unit/suite_tests.rb
|
173
189
|
- test/unit/test_tests.rb
|
174
190
|
- test/unit/utils_tests.rb
|
data/test/unit/stub_tests.rb
DELETED
@@ -1,375 +0,0 @@
|
|
1
|
-
require 'assert'
|
2
|
-
require 'assert/stub'
|
3
|
-
|
4
|
-
require 'assert/factory'
|
5
|
-
|
6
|
-
class Assert::Stub
|
7
|
-
|
8
|
-
class UnitTests < Assert::Context
|
9
|
-
desc "Assert::Stub"
|
10
|
-
setup do
|
11
|
-
@myclass = Class.new do
|
12
|
-
def mymeth; 'meth'; end
|
13
|
-
def myval(val); val; end
|
14
|
-
def myargs(*args); args; end
|
15
|
-
def myvalargs(val1, val2, *args); [val1, val2, args]; end
|
16
|
-
def myblk(&block); block.call; end
|
17
|
-
end
|
18
|
-
@myobj = @myclass.new
|
19
|
-
|
20
|
-
@stub = Assert::Stub.new(@myobj, :mymeth)
|
21
|
-
end
|
22
|
-
subject{ @stub }
|
23
|
-
|
24
|
-
should have_readers :method_name, :name, :ivar_name, :do
|
25
|
-
should have_writers :do
|
26
|
-
should have_cmeths :key
|
27
|
-
should have_imeths :call_method, :call, :with, :teardown
|
28
|
-
|
29
|
-
should "generate a key given an object and method name" do
|
30
|
-
obj = @myobj
|
31
|
-
meth = :mymeth
|
32
|
-
assert_equal "--#{obj.object_id}--#{meth}--", Assert::Stub.key(obj, meth)
|
33
|
-
end
|
34
|
-
|
35
|
-
should "know its names" do
|
36
|
-
assert_equal 'mymeth', subject.method_name
|
37
|
-
expected = "__assert_stub__#{@myobj.object_id}_#{subject.method_name}"
|
38
|
-
assert_equal expected, subject.name
|
39
|
-
expected = "@__assert_stub_#{@myobj.object_id}_" \
|
40
|
-
"#{subject.method_name.to_sym.object_id}"
|
41
|
-
assert_equal expected, subject.ivar_name
|
42
|
-
end
|
43
|
-
|
44
|
-
should "complain when called if no do block was given" do
|
45
|
-
err = assert_raises(Assert::NotStubbedError){ @myobj.mymeth }
|
46
|
-
assert_includes 'not stubbed.', err.message
|
47
|
-
assert_includes 'test/unit/stub_tests.rb', err.backtrace.first
|
48
|
-
|
49
|
-
subject.do = proc{ 'mymeth' }
|
50
|
-
assert_nothing_raised do
|
51
|
-
@myobj.mymeth
|
52
|
-
end
|
53
|
-
|
54
|
-
assert_nothing_raised do
|
55
|
-
Assert::Stub.new(@myobj, :mymeth){ 'mymeth' }
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
should "complain if stubbing a method that the object doesn't respond to" do
|
60
|
-
err = assert_raises(Assert::StubError){ Assert::Stub.new(@myobj, :some_other_meth) }
|
61
|
-
assert_includes 'does not respond to', err.message
|
62
|
-
assert_includes 'test/unit/stub_tests.rb', err.backtrace.first
|
63
|
-
end
|
64
|
-
|
65
|
-
should "complain if stubbed and called with no `do` proc given" do
|
66
|
-
assert_raises(Assert::NotStubbedError){ @myobj.mymeth }
|
67
|
-
end
|
68
|
-
|
69
|
-
should "complain if stubbed and called with mismatched arity" do
|
70
|
-
Assert::Stub.new(@myobj, :myval){ 'myval' }
|
71
|
-
err = assert_raises(Assert::StubArityError){ @myobj.myval }
|
72
|
-
assert_includes 'arity mismatch on', err.message
|
73
|
-
assert_includes 'test/unit/stub_tests.rb', err.backtrace.first
|
74
|
-
|
75
|
-
assert_nothing_raised { @myobj.myval(1) }
|
76
|
-
assert_raises(Assert::StubArityError){ @myobj.myval(1,2) }
|
77
|
-
|
78
|
-
Assert::Stub.new(@myobj, :myargs){ 'myargs' }
|
79
|
-
assert_nothing_raised { @myobj.myargs }
|
80
|
-
assert_nothing_raised { @myobj.myargs(1) }
|
81
|
-
assert_nothing_raised { @myobj.myargs(1,2) }
|
82
|
-
|
83
|
-
Assert::Stub.new(@myobj, :myvalargs){ 'myvalargs' }
|
84
|
-
assert_raises(Assert::StubArityError){ @myobj.myvalargs }
|
85
|
-
assert_raises(Assert::StubArityError){ @myobj.myvalargs(1) }
|
86
|
-
assert_nothing_raised { @myobj.myvalargs(1,2) }
|
87
|
-
assert_nothing_raised { @myobj.myvalargs(1,2,3) }
|
88
|
-
end
|
89
|
-
|
90
|
-
should "complain if stubbed with mismatched arity" do
|
91
|
-
err = assert_raises(Assert::StubArityError) do
|
92
|
-
Assert::Stub.new(@myobj, :myval).with(){ 'myval' }
|
93
|
-
end
|
94
|
-
assert_includes 'arity mismatch on', err.message
|
95
|
-
assert_includes 'test/unit/stub_tests.rb', err.backtrace.first
|
96
|
-
|
97
|
-
assert_raises(Assert::StubArityError) do
|
98
|
-
Assert::Stub.new(@myobj, :myval).with(1,2){ 'myval' }
|
99
|
-
end
|
100
|
-
assert_nothing_raised do
|
101
|
-
Assert::Stub.new(@myobj, :myval).with(1){ 'myval' }
|
102
|
-
end
|
103
|
-
|
104
|
-
assert_nothing_raised do
|
105
|
-
Assert::Stub.new(@myobj, :myargs).with(){ 'myargs' }
|
106
|
-
end
|
107
|
-
assert_nothing_raised do
|
108
|
-
Assert::Stub.new(@myobj, :myargs).with(1,2){ 'myargs' }
|
109
|
-
end
|
110
|
-
assert_nothing_raised do
|
111
|
-
Assert::Stub.new(@myobj, :myargs).with(1){ 'myargs' }
|
112
|
-
end
|
113
|
-
|
114
|
-
assert_raises(Assert::StubArityError) do
|
115
|
-
Assert::Stub.new(@myobj, :myvalargs).with(){ 'myvalargs' }
|
116
|
-
end
|
117
|
-
assert_raises(Assert::StubArityError) do
|
118
|
-
Assert::Stub.new(@myobj, :myvalargs).with(1){ 'myvalargs' }
|
119
|
-
end
|
120
|
-
assert_nothing_raised do
|
121
|
-
Assert::Stub.new(@myobj, :myvalargs).with(1,2){ 'myvalargs' }
|
122
|
-
end
|
123
|
-
assert_nothing_raised do
|
124
|
-
Assert::Stub.new(@myobj, :myvalargs).with(1,2,3){ 'myvalargs' }
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
should "stub methods with no args" do
|
129
|
-
subject.teardown
|
130
|
-
|
131
|
-
assert_equal 'meth', @myobj.mymeth
|
132
|
-
Assert::Stub.new(@myobj, :mymeth){ 'mymeth' }
|
133
|
-
assert_equal 'mymeth', @myobj.mymeth
|
134
|
-
end
|
135
|
-
|
136
|
-
should "stub methods with required arg" do
|
137
|
-
assert_equal 1, @myobj.myval(1)
|
138
|
-
stub = Assert::Stub.new(@myobj, :myval){ |val| val.to_s }
|
139
|
-
assert_equal '1', @myobj.myval(1)
|
140
|
-
assert_equal '2', @myobj.myval(2)
|
141
|
-
stub.with(2){ 'two' }
|
142
|
-
assert_equal 'two', @myobj.myval(2)
|
143
|
-
end
|
144
|
-
|
145
|
-
should "stub methods with variable args" do
|
146
|
-
assert_equal [1,2], @myobj.myargs(1,2)
|
147
|
-
stub = Assert::Stub.new(@myobj, :myargs){ |*args| args.join(',') }
|
148
|
-
assert_equal '1,2', @myobj.myargs(1,2)
|
149
|
-
assert_equal '3,4,5', @myobj.myargs(3,4,5)
|
150
|
-
stub.with(3,4,5){ 'three-four-five' }
|
151
|
-
assert_equal 'three-four-five', @myobj.myargs(3,4,5)
|
152
|
-
end
|
153
|
-
|
154
|
-
should "stub methods with required args and variable args" do
|
155
|
-
assert_equal [1,2, [3]], @myobj.myvalargs(1,2,3)
|
156
|
-
stub = Assert::Stub.new(@myobj, :myvalargs){ |*args| args.join(',') }
|
157
|
-
assert_equal '1,2,3', @myobj.myvalargs(1,2,3)
|
158
|
-
assert_equal '3,4,5', @myobj.myvalargs(3,4,5)
|
159
|
-
stub.with(3,4,5){ 'three-four-five' }
|
160
|
-
assert_equal 'three-four-five', @myobj.myvalargs(3,4,5)
|
161
|
-
end
|
162
|
-
|
163
|
-
should "stub methods that yield blocks" do
|
164
|
-
blkcalled = false
|
165
|
-
blk = proc{ blkcalled = true }
|
166
|
-
@myobj.myblk(&blk)
|
167
|
-
assert_equal true, blkcalled
|
168
|
-
|
169
|
-
blkcalled = false
|
170
|
-
Assert::Stub.new(@myobj, :myblk){ blkcalled = 'true' }
|
171
|
-
@myobj.myblk(&blk)
|
172
|
-
assert_equal 'true', blkcalled
|
173
|
-
end
|
174
|
-
|
175
|
-
should "stub methods even if they are not local to the object" do
|
176
|
-
mydelegatorclass = Class.new do
|
177
|
-
def initialize(delegateclass)
|
178
|
-
@delegate = delegateclass.new
|
179
|
-
end
|
180
|
-
def respond_to?(meth)
|
181
|
-
@delegate.respond_to?(meth) || super
|
182
|
-
end
|
183
|
-
def method_missing(meth, *args, &block)
|
184
|
-
respond_to?(meth) ? @delegate.send(meth, *args, &block) : super
|
185
|
-
end
|
186
|
-
end
|
187
|
-
mydelegator = mydelegatorclass.new(@myclass)
|
188
|
-
|
189
|
-
assert_equal [1,2,[3]], mydelegator.myvalargs(1,2,3)
|
190
|
-
stub = Assert::Stub.new(mydelegator, :myvalargs){ |*args| args.inspect }
|
191
|
-
assert_equal '[1, 2, 3]', mydelegator.myvalargs(1,2,3)
|
192
|
-
assert_equal '[4, 5, 6]', mydelegator.myvalargs(4,5,6)
|
193
|
-
stub.with(4,5,6){ 'four-five-six' }
|
194
|
-
assert_equal 'four-five-six', mydelegator.myvalargs(4,5,6)
|
195
|
-
end
|
196
|
-
|
197
|
-
should "call to the original method" do
|
198
|
-
subject.teardown
|
199
|
-
|
200
|
-
# no args
|
201
|
-
stub = Assert::Stub.new(@myobj, :mymeth){ 'mymeth' }
|
202
|
-
assert_equal 'mymeth', stub.call([])
|
203
|
-
assert_equal 'meth', stub.call_method([])
|
204
|
-
|
205
|
-
# static args
|
206
|
-
stub = Assert::Stub.new(@myobj, :myval){ |val| val.to_s }
|
207
|
-
assert_equal '1', stub.call([1])
|
208
|
-
assert_equal 1, stub.call_method([1])
|
209
|
-
assert_equal '2', stub.call([2])
|
210
|
-
assert_equal 2, stub.call_method([2])
|
211
|
-
stub.with(2){ 'two' }
|
212
|
-
assert_equal 'two', stub.call([2])
|
213
|
-
assert_equal 2, stub.call_method([2])
|
214
|
-
|
215
|
-
# dynamic args
|
216
|
-
stub = Assert::Stub.new(@myobj, :myargs){ |*args| args.join(',') }
|
217
|
-
assert_equal '1,2', stub.call([1,2])
|
218
|
-
assert_equal [1,2], stub.call_method([1,2])
|
219
|
-
assert_equal '3,4,5', stub.call([3,4,5])
|
220
|
-
assert_equal [3,4,5], stub.call_method([3,4,5])
|
221
|
-
stub.with(3,4,5){ 'three-four-five' }
|
222
|
-
assert_equal 'three-four-five', stub.call([3,4,5])
|
223
|
-
assert_equal [3,4,5], stub.call_method([3,4,5])
|
224
|
-
|
225
|
-
# mixed static/dynamic args
|
226
|
-
stub = Assert::Stub.new(@myobj, :myvalargs){ |*args| args.join(',') }
|
227
|
-
assert_equal '1,2,3', stub.call([1,2,3])
|
228
|
-
assert_equal [1,2, [3]], stub.call_method([1,2,3])
|
229
|
-
assert_equal '3,4,5', stub.call([3,4,5])
|
230
|
-
assert_equal [3,4,[5]], stub.call_method([3,4,5])
|
231
|
-
stub.with(3,4,5){ 'three-four-five' }
|
232
|
-
assert_equal 'three-four-five', stub.call([3,4,5])
|
233
|
-
assert_equal [3,4,[5]], stub.call_method([3,4,5])
|
234
|
-
|
235
|
-
# blocks
|
236
|
-
blkcalled = false
|
237
|
-
blk = proc{ blkcalled = true }
|
238
|
-
stub = Assert::Stub.new(@myobj, :myblk){ blkcalled = 'true' }
|
239
|
-
stub.call([], &blk)
|
240
|
-
assert_equal 'true', blkcalled
|
241
|
-
stub.call_method([], &blk)
|
242
|
-
assert_equal true, blkcalled
|
243
|
-
end
|
244
|
-
|
245
|
-
should "store and remove itself on asserts main module" do
|
246
|
-
assert_equal subject, Assert.instance_variable_get(subject.ivar_name)
|
247
|
-
subject.teardown
|
248
|
-
assert_nil Assert.instance_variable_get(subject.ivar_name)
|
249
|
-
end
|
250
|
-
|
251
|
-
should "be removable" do
|
252
|
-
assert_equal 1, @myobj.myval(1)
|
253
|
-
stub = Assert::Stub.new(@myobj, :myval){ |val| val.to_s }
|
254
|
-
assert_equal '1', @myobj.myval(1)
|
255
|
-
stub.teardown
|
256
|
-
assert_equal 1, @myobj.myval(1)
|
257
|
-
end
|
258
|
-
|
259
|
-
should "have a readable inspect" do
|
260
|
-
expected = "#<#{subject.class}:#{'0x0%x' % (subject.object_id << 1)}" \
|
261
|
-
" @method_name=#{subject.method_name.inspect}>"
|
262
|
-
assert_equal expected, subject.inspect
|
263
|
-
end
|
264
|
-
|
265
|
-
end
|
266
|
-
|
267
|
-
class MutatingArgsStubTests < UnitTests
|
268
|
-
desc "with args that are mutated after they've been used to stub"
|
269
|
-
setup do
|
270
|
-
@arg = ChangeHashObject.new
|
271
|
-
|
272
|
-
@stub = Assert::Stub.new(@myobj, :myval)
|
273
|
-
@stub.with(@arg){ true }
|
274
|
-
|
275
|
-
@arg.change!
|
276
|
-
end
|
277
|
-
subject{ @stub }
|
278
|
-
|
279
|
-
should "not raise a stub error when called" do
|
280
|
-
assert_nothing_raised{ @stub.call([@arg]) }
|
281
|
-
end
|
282
|
-
|
283
|
-
end
|
284
|
-
|
285
|
-
class StubInheritedMethodAfterStubbedOnParentTests < UnitTests
|
286
|
-
desc "stubbing an inherited method after its stubbed on the parent class"
|
287
|
-
setup do
|
288
|
-
@parent_class = Class.new
|
289
|
-
@child_class = Class.new(@parent_class)
|
290
|
-
|
291
|
-
@parent_stub = Assert::Stub.new(@parent_class, :new)
|
292
|
-
@child_stub = Assert::Stub.new(@child_class, :new)
|
293
|
-
end
|
294
|
-
|
295
|
-
should "allow stubbing them independently of each other" do
|
296
|
-
assert_nothing_raised do
|
297
|
-
@parent_stub.with(1, 2){ 'parent' }
|
298
|
-
@child_stub.with(1, 2){ 'child' }
|
299
|
-
end
|
300
|
-
assert_equal 'parent', @parent_class.new(1, 2)
|
301
|
-
assert_equal 'child', @child_class.new(1, 2)
|
302
|
-
end
|
303
|
-
|
304
|
-
should "not raise any errors when tearing down the parent before the child" do
|
305
|
-
assert_nothing_raised do
|
306
|
-
@parent_stub.teardown
|
307
|
-
@child_stub.teardown
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
end
|
312
|
-
|
313
|
-
class NullStubTests < UnitTests
|
314
|
-
desc "NullStub"
|
315
|
-
setup do
|
316
|
-
@ns = NullStub.new
|
317
|
-
end
|
318
|
-
subject{ @ns }
|
319
|
-
|
320
|
-
should have_imeths :teardown
|
321
|
-
|
322
|
-
end
|
323
|
-
|
324
|
-
class ParameterListTests < UnitTests
|
325
|
-
desc "ParameterList"
|
326
|
-
setup do
|
327
|
-
many_args = (0..ParameterList::LETTERS.size).map do
|
328
|
-
Assert::Factory.string
|
329
|
-
end.join(', ')
|
330
|
-
@object = Class.new
|
331
|
-
# use `class_eval` with string to easily define methods with many params
|
332
|
-
@object.class_eval <<-methods
|
333
|
-
def self.noargs; end
|
334
|
-
def self.anyargs(*args); end
|
335
|
-
def self.manyargs(#{many_args}); end
|
336
|
-
def self.minargs(#{many_args}, *args); end
|
337
|
-
methods
|
338
|
-
end
|
339
|
-
subject{ ParameterList }
|
340
|
-
|
341
|
-
should "build a parameter list for a method that takes no args" do
|
342
|
-
assert_equal '&block', subject.new(@object, 'noargs')
|
343
|
-
end
|
344
|
-
|
345
|
-
should "build a parameter list for a method that takes any args" do
|
346
|
-
assert_equal '*args, &block', subject.new(@object, 'anyargs')
|
347
|
-
end
|
348
|
-
|
349
|
-
should "build a parameter list for a method that takes many args" do
|
350
|
-
expected = "#{ParameterList::LETTERS.join(', ')}, aa, &block"
|
351
|
-
assert_equal expected, subject.new(@object, 'manyargs')
|
352
|
-
end
|
353
|
-
|
354
|
-
should "build a parameter list for a method that takes a minimum number of args" do
|
355
|
-
expected = "#{ParameterList::LETTERS.join(', ')}, aa, *args, &block"
|
356
|
-
assert_equal expected, subject.new(@object, 'minargs')
|
357
|
-
end
|
358
|
-
|
359
|
-
end
|
360
|
-
|
361
|
-
class ChangeHashObject
|
362
|
-
def initialize
|
363
|
-
@value = nil
|
364
|
-
end
|
365
|
-
|
366
|
-
def hash
|
367
|
-
@value.hash
|
368
|
-
end
|
369
|
-
|
370
|
-
def change!
|
371
|
-
@value = 1
|
372
|
-
end
|
373
|
-
end
|
374
|
-
|
375
|
-
end
|