app_mode 0.0.2 → 0.0.3
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.lock +2 -1
- data/app_mode.gemspec +1 -1
- data/lib/app_mode/app_mode.rb +9 -9
- data/test/app_mode_test.rb +46 -0
- data/test/lib/app_mode.rb +6 -8
- data/test/lib/app_mode_support.rb +5 -0
- data/test/require.rb +0 -1
- metadata +8 -10
- data/test/lib/test_case.rb +0 -502
data/Gemfile.lock
CHANGED
data/app_mode.gemspec
CHANGED
data/lib/app_mode/app_mode.rb
CHANGED
@@ -125,16 +125,16 @@ class AppMode
|
|
125
125
|
def dynamic_state
|
126
126
|
call = origin
|
127
127
|
return @valid_states[0] unless call.sub(/^\.\//, '').match(/\//)
|
128
|
-
return @valid_states[1] if call.match(/rake_test_loader\.rb/)
|
129
|
-
return @valid_states[2] if call.match(%r[/bin/rake])
|
130
|
-
return @valid_states.last
|
131
|
-
end
|
132
128
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
129
|
+
case call
|
130
|
+
when /rake_test_loader\.rb/,
|
131
|
+
%r[/tests?/test_\w+?.rb], %r[/tests?/\w+?_test.rb]
|
132
|
+
return @valid_states[1]
|
133
|
+
when %r[/bin/rake]
|
134
|
+
return @valid_states[2]
|
135
|
+
end
|
136
|
+
|
137
|
+
return @valid_states.last
|
138
138
|
end
|
139
139
|
|
140
140
|
# Returns the first call in the stack.
|
data/test/app_mode_test.rb
CHANGED
@@ -36,7 +36,9 @@ class AppModeTest < Test::Unit::TestCase
|
|
36
36
|
include AppModeSupport
|
37
37
|
|
38
38
|
def setup
|
39
|
+
@class = AppMode
|
39
40
|
@class.setup
|
41
|
+
@obj = nil
|
40
42
|
super
|
41
43
|
end
|
42
44
|
|
@@ -134,6 +136,30 @@ class AppModeTest < Test::Unit::TestCase
|
|
134
136
|
|
135
137
|
assert_raise(NoMethodError) { @obj.dynamic }
|
136
138
|
assert_raise(NoMethodError) { @obj.test }
|
139
|
+
|
140
|
+
assert_nothing_raised { create :dynamic, states(:test_test_class) }
|
141
|
+
assert_false @obj.test_test_class_dev
|
142
|
+
assert_true @obj.test_ttc
|
143
|
+
assert_false @obj.rake
|
144
|
+
assert_false @obj.prod
|
145
|
+
|
146
|
+
assert_nothing_raised { create :dynamic, states(:tests_test_class) }
|
147
|
+
assert_false @obj.tests_test_class_dev
|
148
|
+
assert_true @obj.test_stc
|
149
|
+
assert_false @obj.rake
|
150
|
+
assert_false @obj.prod
|
151
|
+
|
152
|
+
assert_nothing_raised { create :dynamic, states(:test_class_test) }
|
153
|
+
assert_false @obj.test_class_test_dev
|
154
|
+
assert_true @obj.test_tct
|
155
|
+
assert_false @obj.rake
|
156
|
+
assert_false @obj.prod
|
157
|
+
|
158
|
+
assert_nothing_raised { create :dynamic, states(:tests_class_test) }
|
159
|
+
assert_false @obj.tests_class_test_dev
|
160
|
+
assert_true @obj.test_sct
|
161
|
+
assert_false @obj.rake
|
162
|
+
assert_false @obj.prod
|
137
163
|
end
|
138
164
|
|
139
165
|
def test_dynamic_state_with_less_than_ideal_number_of_states
|
@@ -286,4 +312,24 @@ class AppModeTest < Test::Unit::TestCase
|
|
286
312
|
def create(*args)
|
287
313
|
@obj = @class.new(*args)
|
288
314
|
end
|
315
|
+
|
316
|
+
############################################################################
|
317
|
+
# Assertions - stolen from test_internals.
|
318
|
+
############################################################################
|
319
|
+
|
320
|
+
# Asserts that a value is equal to false.
|
321
|
+
# ==== Input
|
322
|
+
# [value : Any] The value to check for equality against false.
|
323
|
+
# [message : String : nil] The message to display if the value is not false.
|
324
|
+
def assert_false(value, message = nil)
|
325
|
+
assert_equal false, value, message
|
326
|
+
end
|
327
|
+
|
328
|
+
# Asserts that a value is equal to true.
|
329
|
+
# ==== Input
|
330
|
+
# [value : Any] The value to check for equality against true.
|
331
|
+
# [message : String : nil] The message to display if the value is not true.
|
332
|
+
def assert_true(value, message = nil)
|
333
|
+
assert_equal true, value, message
|
334
|
+
end
|
289
335
|
end
|
data/test/lib/app_mode.rb
CHANGED
@@ -37,18 +37,16 @@ class AppMode
|
|
37
37
|
private
|
38
38
|
############################################################################
|
39
39
|
|
40
|
-
def getwd
|
41
|
-
if @valid_states.include?(:dynamic_dev)
|
42
|
-
return '/root/path/development'
|
43
|
-
else
|
44
|
-
return '/root/path/production'
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
40
|
def origin
|
49
41
|
case @valid_states[0]
|
50
42
|
when /^dev_/
|
51
43
|
"./file_name.rb:00:in `<main>'"
|
44
|
+
when /^test_test_class/, /^test_class_test/,
|
45
|
+
/^tests_test_class/, /^tests_class_test/
|
46
|
+
path = @valid_states[0].to_s
|
47
|
+
folder = path.match(/^(\w+?)_/)[1].to_s
|
48
|
+
file = path.match(/^\w+?_(\w+?_\w+?)_/)[1].to_s
|
49
|
+
"./#{folder}/#{file}.rb:33:in `<main>'"
|
52
50
|
when /^test_/
|
53
51
|
"/root/ruby/gems/ruby-9.8.7-p123/gems/rake-6.5.4/lib/rake/" +
|
54
52
|
"rake_test_loader.rb:5:in `<main>'"
|
@@ -45,6 +45,11 @@ module AppModeSupport
|
|
45
45
|
:test => [:test_dev, :test_test, :test_rake, :test_prod],
|
46
46
|
:rake => [:rake_dev, :rake_test, :rake_rake, :rake_prod],
|
47
47
|
:prod => [:prod_dev, :prod_test, :prod_rake, :prod_prod],
|
48
|
+
|
49
|
+
:test_test_class => [:test_test_class_dev, :test_ttc, :rake, :prod],
|
50
|
+
:test_class_test => [:test_class_test_dev, :test_tct, :rake, :prod],
|
51
|
+
:tests_test_class => [:tests_test_class_dev, :test_stc, :rake, :prod],
|
52
|
+
:tests_class_test => [:tests_class_test_dev, :test_sct, :rake, :prod],
|
48
53
|
}, # :states
|
49
54
|
|
50
55
|
:method_list => {
|
data/test/require.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Travis Herrick
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-08-
|
17
|
+
date: 2011-08-09 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -49,10 +49,9 @@ files:
|
|
49
49
|
- app_mode.gemspec
|
50
50
|
- README
|
51
51
|
- test/require.rb
|
52
|
-
- test/lib/test_case.rb
|
53
|
-
- test/lib/app_mode_support.rb
|
54
|
-
- test/lib/app_mode.rb
|
55
52
|
- test/app_mode_test.rb
|
53
|
+
- test/lib/app_mode.rb
|
54
|
+
- test/lib/app_mode_support.rb
|
56
55
|
has_rdoc: true
|
57
56
|
homepage: http://www.bitbucket.org/ToadJamb/gems_app_mode
|
58
57
|
licenses:
|
@@ -67,7 +66,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
66
|
requirements:
|
68
67
|
- - ">="
|
69
68
|
- !ruby/object:Gem::Version
|
70
|
-
hash:
|
69
|
+
hash: 2217336874542399798
|
71
70
|
segments:
|
72
71
|
- 0
|
73
72
|
version: "0"
|
@@ -88,7 +87,6 @@ specification_version: 3
|
|
88
87
|
summary: Application state management.
|
89
88
|
test_files:
|
90
89
|
- test/require.rb
|
91
|
-
- test/lib/test_case.rb
|
92
|
-
- test/lib/app_mode_support.rb
|
93
|
-
- test/lib/app_mode.rb
|
94
90
|
- test/app_mode_test.rb
|
91
|
+
- test/lib/app_mode.rb
|
92
|
+
- test/lib/app_mode_support.rb
|
data/test/lib/test_case.rb
DELETED
@@ -1,502 +0,0 @@
|
|
1
|
-
# This file contains a monkey patch of Test::Unit::TestCase.
|
2
|
-
|
3
|
-
#--
|
4
|
-
################################################################################
|
5
|
-
# Copyright (C) 2011 Travis Herrick #
|
6
|
-
################################################################################
|
7
|
-
# #
|
8
|
-
# \v^V,^!v\^/ #
|
9
|
-
# ~% %~ #
|
10
|
-
# { _ _ } #
|
11
|
-
# ( * - ) #
|
12
|
-
# | / | #
|
13
|
-
# \ _, / #
|
14
|
-
# \__.__/ #
|
15
|
-
# #
|
16
|
-
################################################################################
|
17
|
-
# This program is free software: you can redistribute it #
|
18
|
-
# and/or modify it under the terms of the GNU General Public License #
|
19
|
-
# as published by the Free Software Foundation, #
|
20
|
-
# either version 3 of the License, or (at your option) any later version. #
|
21
|
-
# #
|
22
|
-
# Commercial licensing may be available for a fee under a different license. #
|
23
|
-
################################################################################
|
24
|
-
# This program is distributed in the hope that it will be useful, #
|
25
|
-
# but WITHOUT ANY WARRANTY; #
|
26
|
-
# without even the implied warranty of MERCHANTABILITY #
|
27
|
-
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
28
|
-
# See the GNU General Public License for more details. #
|
29
|
-
# #
|
30
|
-
# You should have received a copy of the GNU General Public License #
|
31
|
-
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
32
|
-
################################################################################
|
33
|
-
#++
|
34
|
-
|
35
|
-
# Monkey patch Test::Unit::TestCase to make it do cool stuff.
|
36
|
-
Test::Unit::TestCase.class_eval do
|
37
|
-
# Since this is in a class_eval, instance methods need to be wrapped up
|
38
|
-
# in class_variable_set or ruby will throw warnings.
|
39
|
-
|
40
|
-
# Indicates whether the class has already been initialized.
|
41
|
-
# This combined with @@class_name prevents duplicate patching.
|
42
|
-
class_variable_set(:@@initialized, false)
|
43
|
-
|
44
|
-
# Keeps track of the class that has most recently been initialized.
|
45
|
-
# This combined with @@initialized prevents duplicate patching.
|
46
|
-
class_variable_set(:@@class_name, '')
|
47
|
-
|
48
|
-
# Initializes the class
|
49
|
-
# and exposes private methods and variables of the class that is being tested.
|
50
|
-
def initialize(*args)
|
51
|
-
# Call initialize on the superclass.
|
52
|
-
super
|
53
|
-
|
54
|
-
@obj = nil
|
55
|
-
|
56
|
-
reset_io
|
57
|
-
reset_trace
|
58
|
-
|
59
|
-
# This block ensures that tests still work if there is not a class that
|
60
|
-
# corresponds with the test file/class.
|
61
|
-
@class = nil
|
62
|
-
begin
|
63
|
-
# Get the class that is being tested.
|
64
|
-
# Assume that the name of the class is found by removing 'Test'
|
65
|
-
# from the test class.
|
66
|
-
@class = Kernel.const_get(self.class.name.gsub(/Test$/, ''))
|
67
|
-
@@initialized = ((@class.name == @@class_name) && @@initialized)
|
68
|
-
@@class_name = @class.name
|
69
|
-
rescue
|
70
|
-
@@initialized = true
|
71
|
-
@@class_name = ''
|
72
|
-
end
|
73
|
-
|
74
|
-
# Only patch if this code has not yet been run.
|
75
|
-
if !@@initialized and @class.class.name != 'Module'
|
76
|
-
set_instance_method_wrappers
|
77
|
-
|
78
|
-
# Expose private class methods.
|
79
|
-
# We will only expose the methods we are responsible for creating.
|
80
|
-
# (i.e. subtracting the superclass's private methods)
|
81
|
-
expose_private_methods(:class,
|
82
|
-
@class.private_methods -
|
83
|
-
@class.superclass.private_methods)
|
84
|
-
|
85
|
-
# Expose private instance methods.
|
86
|
-
# We will only expose the methods we are responsible for creating.
|
87
|
-
# (i.e. subtracting the superclass's private methods)
|
88
|
-
expose_private_methods(:instance,
|
89
|
-
@class.private_instance_methods -
|
90
|
-
@class.superclass.private_instance_methods)
|
91
|
-
|
92
|
-
# Expose variables.
|
93
|
-
# Requires that variables are assigned to in the constructor.
|
94
|
-
wrap_output {
|
95
|
-
expose_variables(@class.class_variables +
|
96
|
-
@class.new.instance_variables)
|
97
|
-
}
|
98
|
-
|
99
|
-
# Indicate that this code has been run.
|
100
|
-
@@initialized = true
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
# Sets up functionality for all tests.
|
105
|
-
#
|
106
|
-
# Tracing is set up here so that it is only running during tests.
|
107
|
-
#
|
108
|
-
# If you want to disable tracing, simply override the setup method
|
109
|
-
# without calling super. (It would be good form to also override teardown).
|
110
|
-
def setup
|
111
|
-
set_trace_func proc { |event, file, line, id, binding, class_name|
|
112
|
-
if class_name == @class and
|
113
|
-
@stack_trace.last != {:class => class_name.name, :method => id}
|
114
|
-
@stack_trace << {
|
115
|
-
:class => class_name.name,
|
116
|
-
:method => id,
|
117
|
-
}
|
118
|
-
end
|
119
|
-
}
|
120
|
-
end
|
121
|
-
|
122
|
-
# Clean up after each test.
|
123
|
-
#
|
124
|
-
# If you disable tracing, it would be good form to override this method
|
125
|
-
# as well without calling super.
|
126
|
-
def teardown
|
127
|
-
set_trace_func nil
|
128
|
-
end
|
129
|
-
|
130
|
-
############################################################################
|
131
|
-
private
|
132
|
-
############################################################################
|
133
|
-
|
134
|
-
############################################################################
|
135
|
-
# Monkey patching methods for the class being tested.
|
136
|
-
############################################################################
|
137
|
-
|
138
|
-
# Monkey patch the class's initializer to enable tracing
|
139
|
-
# with parameters and results.
|
140
|
-
def set_initializer
|
141
|
-
@class.class_eval do
|
142
|
-
attr_accessor :trace
|
143
|
-
|
144
|
-
alias :test_case_initialize :initialize
|
145
|
-
def initialize(*args)
|
146
|
-
@trace = []
|
147
|
-
result = test_case_initialize(*args)
|
148
|
-
return result
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
# Loop through the instance methods, calling set_instance_methods for each.
|
154
|
-
def set_instance_method_wrappers
|
155
|
-
[
|
156
|
-
:public_instance_methods,
|
157
|
-
:protected_instance_methods,
|
158
|
-
:private_instance_methods
|
159
|
-
].each do |method_id|
|
160
|
-
|
161
|
-
scope = method_id.to_s.gsub(/_.*/, '')
|
162
|
-
|
163
|
-
set_instance_methods(@class.send(method_id) -
|
164
|
-
@class.superclass.send(method_id), scope)
|
165
|
-
end
|
166
|
-
|
167
|
-
# If this is not at the end, the loop will attempt to do it's thing
|
168
|
-
# with the constructor created in this method, which is not necessary.
|
169
|
-
set_initializer
|
170
|
-
end
|
171
|
-
|
172
|
-
# Loop through the list of methods that are passed in,
|
173
|
-
# creating a wrapper method that enables tracing.
|
174
|
-
#
|
175
|
-
# Tracing data includes method name, parameters, and result.
|
176
|
-
# ==== Input
|
177
|
-
# [method_list : Array] A list of methods that will have wrapping functions
|
178
|
-
# created to enable tracing.
|
179
|
-
# [scope : String] The scope of the original function.
|
180
|
-
def set_instance_methods(method_list, scope)
|
181
|
-
method_list.each do |method_id|
|
182
|
-
# Setters and methods that accept blocks do not appear to work.
|
183
|
-
next if method_id =~ /=/ or method_id =~ /wrap_output/
|
184
|
-
|
185
|
-
# Build the method.
|
186
|
-
new_method = <<-DOC
|
187
|
-
alias :test_case_#{method_id} :#{method_id}
|
188
|
-
def #{method_id}(*args)
|
189
|
-
result = test_case_#{method_id}(*args)
|
190
|
-
@trace << {
|
191
|
-
:method => '#{method_id}',
|
192
|
-
:args => args,
|
193
|
-
:result => result
|
194
|
-
}
|
195
|
-
return result
|
196
|
-
end
|
197
|
-
#{scope} :#{method_id}
|
198
|
-
DOC
|
199
|
-
|
200
|
-
# Add the method to the class.
|
201
|
-
@class.class_eval do
|
202
|
-
eval(new_method)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
# Expose the private methods that are passed in. New methods will be created
|
208
|
-
# with the old method name followed by '_public_test'. If the original
|
209
|
-
# method contained a '?', it will be removed in the new method.
|
210
|
-
# ==== Input
|
211
|
-
# [type : Symbol] Indicates whether to handle instance or class methods.
|
212
|
-
#
|
213
|
-
# Only :class and :instance are supported.
|
214
|
-
# [methods : Array] An array of the methods to expose.
|
215
|
-
def expose_private_methods(type, methods)
|
216
|
-
# Get the text that the method should be wrapped in.
|
217
|
-
method_wrapper = wrapper(type)
|
218
|
-
|
219
|
-
# Loop through the methods.
|
220
|
-
methods.each do |method|
|
221
|
-
# Remove ?s.
|
222
|
-
new_method = method.to_s.gsub(/\?/, '')
|
223
|
-
|
224
|
-
# This is the new method.
|
225
|
-
new_method = <<-DOC
|
226
|
-
def #{new_method}_public_test(*args)
|
227
|
-
#{method}(*args)
|
228
|
-
end
|
229
|
-
DOC
|
230
|
-
|
231
|
-
# Add the wrapping text.
|
232
|
-
new_method = method_wrapper % [new_method]
|
233
|
-
|
234
|
-
# Add the method to the class.
|
235
|
-
@class.class_eval do
|
236
|
-
eval(new_method)
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
# Expose the variables.
|
242
|
-
#
|
243
|
-
# New methods will be created (a getter and a setter) for each variable.
|
244
|
-
#
|
245
|
-
# Regardless of the type of variable, these methods are only available
|
246
|
-
# via an instance.
|
247
|
-
# ==== Input
|
248
|
-
# [variables : Array] An array of variables to expose.
|
249
|
-
def expose_variables(variables)
|
250
|
-
# Get the text that the methods should be wrapped in.
|
251
|
-
var_wrapper = wrapper(:instance)
|
252
|
-
|
253
|
-
# Loop through the variables
|
254
|
-
variables.each do |var|
|
255
|
-
# Remove any @s.
|
256
|
-
new_method = var.to_s.gsub(/@/, '')
|
257
|
-
|
258
|
-
# These are the new getter and setters.
|
259
|
-
new_method = <<-DOC
|
260
|
-
def #{new_method}_variable_method
|
261
|
-
#{var}
|
262
|
-
end
|
263
|
-
|
264
|
-
def #{new_method}_variable_method=(value)
|
265
|
-
#{var} = value
|
266
|
-
end
|
267
|
-
DOC
|
268
|
-
|
269
|
-
# Add the wrapping text.
|
270
|
-
new_method = var_wrapper % [new_method]
|
271
|
-
|
272
|
-
# Add the methods to the class.
|
273
|
-
@class.class_eval do
|
274
|
-
eval(new_method)
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
# Returns the wrapping text for the specified type of method.
|
280
|
-
# ==== Input
|
281
|
-
# [type : Symbol] Indicates whether to handle instance or class methods.
|
282
|
-
#
|
283
|
-
# Only :class & :instance are supported.
|
284
|
-
# ==== Output
|
285
|
-
# [String] The text that the specified type of method should be wrapped in.
|
286
|
-
def wrapper(type)
|
287
|
-
case type
|
288
|
-
when :class then 'class << self;%s;end'
|
289
|
-
when :instance then '%s'
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
############################################################################
|
294
|
-
# I/O support methods.
|
295
|
-
############################################################################
|
296
|
-
|
297
|
-
# Return the actual output to stdout and stderr.
|
298
|
-
# ==== Output
|
299
|
-
# [Array] Two element array of strings.
|
300
|
-
#
|
301
|
-
# The first element is from stdout.
|
302
|
-
#
|
303
|
-
# The second element is from stderr.
|
304
|
-
def real_finis
|
305
|
-
return out, err
|
306
|
-
end
|
307
|
-
|
308
|
-
# Wrap a block to capture the output to stdout and stderr.
|
309
|
-
# ==== Input
|
310
|
-
# [&block : Block] The block of code that will have stdout and stderr trapped.
|
311
|
-
def wrap_output(&block)
|
312
|
-
begin
|
313
|
-
$stdout = @out
|
314
|
-
$stderr = @err
|
315
|
-
yield
|
316
|
-
ensure
|
317
|
-
$stdout = STDOUT
|
318
|
-
$stderr = STDERR
|
319
|
-
end
|
320
|
-
end
|
321
|
-
|
322
|
-
# Returns the output from stdout as a string.
|
323
|
-
# ==== Output
|
324
|
-
# [String] The output from stdout.
|
325
|
-
#
|
326
|
-
# All trailing line feeds are removed.
|
327
|
-
def out
|
328
|
-
@out.respond_to?(:string) ? @out.string.gsub(/\n*\z/, '') : ''
|
329
|
-
end
|
330
|
-
|
331
|
-
# Returns the output from stderr as a string.
|
332
|
-
# ==== Output
|
333
|
-
# [String] The output from stderr.
|
334
|
-
#
|
335
|
-
# All trailing line feeds are removed.
|
336
|
-
def err
|
337
|
-
@err.respond_to?(:string) ? @err.string.gsub(/\n*\z/, '') : ''
|
338
|
-
end
|
339
|
-
|
340
|
-
# Reset the stdout and stderr stream variables.
|
341
|
-
def reset_io
|
342
|
-
@out = StringIO.new
|
343
|
-
@err = StringIO.new
|
344
|
-
end
|
345
|
-
|
346
|
-
############################################################################
|
347
|
-
# Support methods.
|
348
|
-
############################################################################
|
349
|
-
|
350
|
-
# Indicates whether the specified method has been called on a given class.
|
351
|
-
# ==== Input
|
352
|
-
# [method_name : String] The name of the method.
|
353
|
-
#
|
354
|
-
# This value may be a string or a symbol.
|
355
|
-
# [class_name : String : @class.name] The name of the class that the method
|
356
|
-
# should have been invoked from.
|
357
|
-
def method_called?(method_name, class_name = @class.name)
|
358
|
-
!@stack_trace.index(
|
359
|
-
{:method => method_name.to_sym, :class => class_name}).nil?
|
360
|
-
end
|
361
|
-
|
362
|
-
# Resets the trace arrays.
|
363
|
-
#
|
364
|
-
# This is intended for use in cases where code may be called multiple
|
365
|
-
# times in a single test.
|
366
|
-
def reset_trace
|
367
|
-
@stack_trace = []
|
368
|
-
@obj.trace = [] if @obj.respond_to?(:trace=)
|
369
|
-
end
|
370
|
-
|
371
|
-
# Shows the trace history as it stands, if the object supports it.
|
372
|
-
def show_trace
|
373
|
-
return unless defined? @obj
|
374
|
-
puts @obj.trace.join("\n" + '-' * 80 + "\n") if @obj.respond_to?(:trace)
|
375
|
-
end
|
376
|
-
|
377
|
-
############################################################################
|
378
|
-
# Assertions.
|
379
|
-
############################################################################
|
380
|
-
|
381
|
-
# Asserts that a value is equal to false.
|
382
|
-
# ==== Input
|
383
|
-
# [value : Any] The value to check for equality against false.
|
384
|
-
# [message : String : nil] The message to display if the value is not false.
|
385
|
-
def assert_false(value, message = nil)
|
386
|
-
assert_equal false, value, message
|
387
|
-
end
|
388
|
-
|
389
|
-
# Asserts that a value is equal to true.
|
390
|
-
# ==== Input
|
391
|
-
# [value : Any] The value to check for equality against true.
|
392
|
-
# [message : String : nil] The message to display if the value is not true.
|
393
|
-
def assert_true(value, message = nil)
|
394
|
-
assert_equal true, value, message
|
395
|
-
end
|
396
|
-
|
397
|
-
# Asserts that the negation of a value is true.
|
398
|
-
# ==== Input
|
399
|
-
# [value : Any] The value which will be negated and then asserted.
|
400
|
-
# [message : String : nil] The message to display if the assertion fails.
|
401
|
-
def assert_not(value, message = nil)
|
402
|
-
assert !value, message
|
403
|
-
end
|
404
|
-
|
405
|
-
# Assert that an array has a specified number of elements.
|
406
|
-
# ==== Input
|
407
|
-
# [array : Array] The array that will have it's length checked.
|
408
|
-
# [length : Fixnum] The length that the array should be.
|
409
|
-
# [message : String : nil] The message to display if the assertion fails.
|
410
|
-
def assert_array_count(array, length, message = nil)
|
411
|
-
if message.nil?
|
412
|
-
message = "#{array} has #{array.length} item(s), " +
|
413
|
-
"but was expected to have #{length}."
|
414
|
-
end
|
415
|
-
|
416
|
-
assert array.length == length, message
|
417
|
-
end
|
418
|
-
|
419
|
-
############################################################################
|
420
|
-
# Assertions - Stack trace.
|
421
|
-
############################################################################
|
422
|
-
|
423
|
-
# Asserts that a method was called on a class.
|
424
|
-
# ==== Input
|
425
|
-
# [method_name : String] The name of the method to check for.
|
426
|
-
# [class_name : String : @class.name] The name of the class
|
427
|
-
# on which <tt>method_name</tt>
|
428
|
-
# should have been invoked.
|
429
|
-
def assert_method(method_name, class_name = @class.name)
|
430
|
-
assert method_called?(method_name.to_sym, class_name),
|
431
|
-
"#{class_name}.#{method_name} has not been called."
|
432
|
-
end
|
433
|
-
|
434
|
-
# Asserts that a method was not called on a class.
|
435
|
-
# ==== Input
|
436
|
-
# [method_name : String] The name of the method to check for.
|
437
|
-
# [class_name : String : @class.name] The name of the class
|
438
|
-
# on which <tt>method_name</tt>
|
439
|
-
# should not have been invoked.
|
440
|
-
def assert_not_method(method_name, class_name = @class.name)
|
441
|
-
assert !method_called?(method_name.to_sym, class_name),
|
442
|
-
"#{class_name}.#{method_name} should not be called."
|
443
|
-
end
|
444
|
-
|
445
|
-
# Asserts that a method was called with the specified parameters.
|
446
|
-
# ==== Input
|
447
|
-
# [method_name : String] The name of the method to check.
|
448
|
-
# [*args : Array] The parameters that were passed in to the method.
|
449
|
-
def assert_trace_args(method_name, *args)
|
450
|
-
match = false
|
451
|
-
|
452
|
-
list = []
|
453
|
-
|
454
|
-
# Loop through the stack trace to see if the method was called
|
455
|
-
# with the specified arguments.
|
456
|
-
@obj.trace.each do |trace|
|
457
|
-
if trace[:method] == method_name and trace[:args] == args
|
458
|
-
match = true
|
459
|
-
break
|
460
|
-
elsif trace[:method] == method_name
|
461
|
-
list << trace[:args]
|
462
|
-
end
|
463
|
-
end
|
464
|
-
|
465
|
-
assert match,
|
466
|
-
"#{method_name} was not called with the following parameters:\n" +
|
467
|
-
"#{args.join("\n" + '-' * 80 + "\n")}\n" +
|
468
|
-
'*' * 80 + "\n" +
|
469
|
-
"#{method_name} was recorded as follows:\n" +
|
470
|
-
"#{list.join("\n" + '-' * 80 + "\n")}"
|
471
|
-
end
|
472
|
-
|
473
|
-
# Asserts that a method was called with the specified parameters
|
474
|
-
# and returned the specified result.
|
475
|
-
# ==== Input
|
476
|
-
# [method_name : String] The name of the method to check.
|
477
|
-
# [result : Any] The expected result of the method call.
|
478
|
-
# [*args : Array] The parameters that were passed in to the method.
|
479
|
-
def assert_trace_info(method_name, result, *args)
|
480
|
-
match = (@obj.trace.index(
|
481
|
-
{:methd => method_name, :args => args, :result => result}))
|
482
|
-
|
483
|
-
list = []
|
484
|
-
|
485
|
-
# Only get a list of possible results if a match was not found.
|
486
|
-
unless match
|
487
|
-
@obj.trace.each do |trace|
|
488
|
-
if trace[:method] == method_name
|
489
|
-
list << {:args => trace[:args], :result => trace[:result]}
|
490
|
-
end
|
491
|
-
end
|
492
|
-
end
|
493
|
-
|
494
|
-
assert match,
|
495
|
-
"#{method_name} was not called with the following parameters:\n" +
|
496
|
-
"#{args}\n" +
|
497
|
-
"or did not return the following result:\n" +
|
498
|
-
"#{result}\n" +
|
499
|
-
"#{method_name} was recorded as follows:\n" +
|
500
|
-
"#{list.join("\n" + '-' * 80 + "\n")}"
|
501
|
-
end
|
502
|
-
end
|