app_mode 0.0.1
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 +5 -0
- data/Gemfile.lock +10 -0
- data/README +151 -0
- data/app_mode.gemspec +24 -0
- data/lib/app_mode.rb +3 -0
- data/lib/app_mode/app_mode.rb +184 -0
- data/rakefile +16 -0
- data/test/app_mode_test.rb +289 -0
- data/test/lib/app_mode.rb +61 -0
- data/test/lib/app_mode_support.rb +133 -0
- data/test/lib/test_case.rb +502 -0
- data/test/require.rb +9 -0
- metadata +95 -0
@@ -0,0 +1,61 @@
|
|
1
|
+
# This file overrides the main class during tests.
|
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
|
+
class AppMode
|
36
|
+
############################################################################
|
37
|
+
private
|
38
|
+
############################################################################
|
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
|
+
def origin
|
49
|
+
case @valid_states[0]
|
50
|
+
when /^dev_/
|
51
|
+
"./file_name.rb:00:in `<main>'"
|
52
|
+
when /^test_/
|
53
|
+
"/root/ruby/gems/ruby-9.8.7-p123/gems/rake-6.5.4/lib/rake/" +
|
54
|
+
"rake_test_loader.rb:5:in `<main>'"
|
55
|
+
when /^rake_/
|
56
|
+
"/root/ruby/gems/ruby-9.8.7-p123/bin/rake:19:in `<main>'"
|
57
|
+
else
|
58
|
+
"./root/file_name.rb:00:in `<main>'"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
# This file contains a module which bridges the gap between test code
|
2
|
+
# and applciation code.
|
3
|
+
|
4
|
+
#--
|
5
|
+
################################################################################
|
6
|
+
# Copyright (C) 2011 Travis Herrick #
|
7
|
+
################################################################################
|
8
|
+
# #
|
9
|
+
# \v^V,^!v\^/ #
|
10
|
+
# ~% %~ #
|
11
|
+
# { _ _ } #
|
12
|
+
# ( * - ) #
|
13
|
+
# | / | #
|
14
|
+
# \ _, / #
|
15
|
+
# \__.__/ #
|
16
|
+
# #
|
17
|
+
################################################################################
|
18
|
+
# This program is free software: you can redistribute it #
|
19
|
+
# and/or modify it under the terms of the GNU General Public License #
|
20
|
+
# as published by the Free Software Foundation, #
|
21
|
+
# either version 3 of the License, or (at your option) any later version. #
|
22
|
+
# #
|
23
|
+
# Commercial licensing may be available for a fee under a different license. #
|
24
|
+
################################################################################
|
25
|
+
# This program is distributed in the hope that it will be useful, #
|
26
|
+
# but WITHOUT ANY WARRANTY; #
|
27
|
+
# without even the implied warranty of MERCHANTABILITY #
|
28
|
+
# or FITNESS FOR A PARTICULAR PURPOSE. #
|
29
|
+
# See the GNU General Public License for more details. #
|
30
|
+
# #
|
31
|
+
# You should have received a copy of the GNU General Public License #
|
32
|
+
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
|
33
|
+
################################################################################
|
34
|
+
#++
|
35
|
+
|
36
|
+
# This module is provided to be used as an include in any necessary class.
|
37
|
+
#
|
38
|
+
# This serves as a bridge between test code and application code.
|
39
|
+
module AppModeSupport
|
40
|
+
# These are things that will be used throughout testing in multiple locations.
|
41
|
+
ITEMS = {
|
42
|
+
:states => {
|
43
|
+
:default => [:development, :test, :rake, :production],
|
44
|
+
:dev => [:dev_dev, :dev_test, :dev_rake, :dev_prod],
|
45
|
+
:test => [:test_dev, :test_test, :test_rake, :test_prod],
|
46
|
+
:rake => [:rake_dev, :rake_test, :rake_rake, :rake_prod],
|
47
|
+
:prod => [:prod_dev, :prod_test, :prod_rake, :prod_prod],
|
48
|
+
}, # :states
|
49
|
+
|
50
|
+
:method_list => {
|
51
|
+
:valid_states => :valid_states,
|
52
|
+
:state => :state,
|
53
|
+
}, # :methods
|
54
|
+
} # ITEMS
|
55
|
+
|
56
|
+
ITEMS.each_key do |key|
|
57
|
+
new_method = <<-DOC
|
58
|
+
def #{key}(*args)
|
59
|
+
method_missing(:#{key}, *args)
|
60
|
+
end
|
61
|
+
module_function :#{key}
|
62
|
+
DOC
|
63
|
+
|
64
|
+
module_eval <<-EOT, __FILE__, __LINE__ + 1
|
65
|
+
eval new_method
|
66
|
+
EOT
|
67
|
+
end
|
68
|
+
|
69
|
+
# Black magic.
|
70
|
+
#
|
71
|
+
# This is used for the following purposes:
|
72
|
+
# * To return elements from the ITEMS hash.
|
73
|
+
# * To return messages (from the ITEMS hash),
|
74
|
+
# possibly with string substitutions.
|
75
|
+
# ==== Input
|
76
|
+
# [method : Symbol] The method that was called.
|
77
|
+
# [*args : Array] Any arguments that were passed in.
|
78
|
+
# [&block : Block] A block, if specified.
|
79
|
+
# ==== Output
|
80
|
+
# [Any] It depends on the method.
|
81
|
+
def method_missing(method, *args, &block)
|
82
|
+
# Check if the method is a key in the ITEMS hash.
|
83
|
+
if ITEMS.has_key? method
|
84
|
+
# Initialize the variable that will hold the return value.
|
85
|
+
value = nil
|
86
|
+
|
87
|
+
if args.nil? or args.count == 0
|
88
|
+
# If no arguments have been specified, return the element as is.
|
89
|
+
value = ITEMS[method]
|
90
|
+
elsif ITEMS[method][args[0]].is_a?(String) &&
|
91
|
+
ITEMS[method][args[0]].index('%s')
|
92
|
+
# The first parameter is the message.
|
93
|
+
msg = args.shift
|
94
|
+
|
95
|
+
if args.count == 0
|
96
|
+
# If no arguments are left, return the message.
|
97
|
+
value = ITEMS[method][msg]
|
98
|
+
else
|
99
|
+
# Use any remaining arguments to make substitutions.
|
100
|
+
value = ITEMS[method][msg] % args
|
101
|
+
end
|
102
|
+
else # All other methods - which are expected to have one parameter.
|
103
|
+
# Get the element to return.
|
104
|
+
item = args[0].to_sym
|
105
|
+
|
106
|
+
# Return the indicated element.
|
107
|
+
value = ITEMS[method][item]
|
108
|
+
end
|
109
|
+
|
110
|
+
# Strip all trailing line feeds from strings.
|
111
|
+
value.gsub!(/\n*\z/, '') if value.is_a?(String)
|
112
|
+
|
113
|
+
return value
|
114
|
+
else
|
115
|
+
super
|
116
|
+
end
|
117
|
+
end
|
118
|
+
module_function :method_missing
|
119
|
+
|
120
|
+
# Indicates which methods the class will respond to.
|
121
|
+
# ==== Input
|
122
|
+
# [method : Symbol] The method to check for.
|
123
|
+
# [include_private : Boolean] Whether private methods should be checked.
|
124
|
+
# ==== Output
|
125
|
+
# [Boolean] Whether the object will respond to the specified method.
|
126
|
+
def respond_to_missing?(method, include_private = false)
|
127
|
+
if ITEMS.has_key? method
|
128
|
+
return true
|
129
|
+
else
|
130
|
+
super
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,502 @@
|
|
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
|