test_internals 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -0
- data/Gemfile.lock +12 -0
- data/README +34 -0
- data/lib/test_internals/app_state.rb +39 -0
- data/lib/test_internals/kernel.rb +42 -0
- data/lib/test_internals/test_case.rb +551 -0
- data/lib/test_internals.rb +8 -0
- data/rakefile +2 -0
- data/test_internals.gemspec +27 -0
- metadata +87 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
== Welcome to TestInternals
|
2
|
+
|
3
|
+
TestInternals patches Test::Unit::TestCase to allow testing of private methods,
|
4
|
+
as well as variables.
|
5
|
+
|
6
|
+
The stack trace is also available, including the ability to check
|
7
|
+
that specific parameter values were sent to a method.
|
8
|
+
|
9
|
+
It will also prevent the application from stopping tests if exit is called.
|
10
|
+
assert_alive and assert_dead have been included to assist in testing.
|
11
|
+
|
12
|
+
There are also a number of assertions that have been added.
|
13
|
+
|
14
|
+
== Getting Started
|
15
|
+
|
16
|
+
1. Install TestInternals at the command prompt if you haven't yet:
|
17
|
+
|
18
|
+
gem install test_internals
|
19
|
+
|
20
|
+
2. Require the gem in your gemfile:
|
21
|
+
|
22
|
+
gem 'test_internals', '~> 0.0.1'
|
23
|
+
|
24
|
+
3. Require the gem wherever you need shell commands:
|
25
|
+
|
26
|
+
require 'test_internals'
|
27
|
+
|
28
|
+
== Additional Documentation
|
29
|
+
|
30
|
+
rake rdoc:app
|
31
|
+
|
32
|
+
== License
|
33
|
+
|
34
|
+
TestInternals is released under the GPLv3 license.
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# This file contains the constant that is used to determine whether the
|
2
|
+
# application is still running or not.
|
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
|
+
module TestInternals
|
37
|
+
# This constant is used to track whether the application is still running.
|
38
|
+
AppState = AppMode.new(:alive, [:alive, :dead])
|
39
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# This file contains a monkey patch of Kernel functions for use during testing.
|
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 Kernel classes and functions for testing.
|
36
|
+
module Kernel
|
37
|
+
# Patch exit so that it sets a flag rather than ends the application.
|
38
|
+
# (A call to exit will cause testing to stop otherwise).
|
39
|
+
def exit(*args)
|
40
|
+
TestInternals::AppState.state = :dead
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,551 @@
|
|
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
|
+
|
103
|
+
# If initializing the class with @class.new above kills the app,
|
104
|
+
# we need to set it back to running, but we want to do this regardless.
|
105
|
+
reset_app_state
|
106
|
+
end
|
107
|
+
|
108
|
+
# Sets up functionality for all tests.
|
109
|
+
#
|
110
|
+
# Tracing is set up here so that it is only running during tests.
|
111
|
+
#
|
112
|
+
# If you want to disable tracing, simply override the setup method
|
113
|
+
# without calling super. (It would be good form to also override teardown).
|
114
|
+
def setup
|
115
|
+
set_trace_func proc { |event, file, line, id, binding, class_name|
|
116
|
+
if class_name == @class and
|
117
|
+
@stack_trace.last != {:class => class_name.name, :method => id}
|
118
|
+
@stack_trace << {
|
119
|
+
:class => class_name.name,
|
120
|
+
:method => id,
|
121
|
+
}
|
122
|
+
end
|
123
|
+
}
|
124
|
+
end
|
125
|
+
|
126
|
+
# Clean up after each test.
|
127
|
+
#
|
128
|
+
# If you disable tracing, it would be good form to override this method
|
129
|
+
# as well without calling super.
|
130
|
+
def teardown
|
131
|
+
set_trace_func nil
|
132
|
+
end
|
133
|
+
|
134
|
+
############################################################################
|
135
|
+
private
|
136
|
+
############################################################################
|
137
|
+
|
138
|
+
############################################################################
|
139
|
+
# Monkey patching methods for the class being tested.
|
140
|
+
############################################################################
|
141
|
+
|
142
|
+
# Monkey patch the class's initializer to enable tracing
|
143
|
+
# with parameters and results.
|
144
|
+
def set_initializer
|
145
|
+
@class.class_eval do
|
146
|
+
attr_accessor :trace
|
147
|
+
|
148
|
+
alias :test_case_initialize :initialize
|
149
|
+
def initialize(*args)
|
150
|
+
@trace = []
|
151
|
+
result = test_case_initialize(*args)
|
152
|
+
return result
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# Loop through the instance methods, calling set_instance_methods for each.
|
158
|
+
def set_instance_method_wrappers
|
159
|
+
[
|
160
|
+
:public_instance_methods,
|
161
|
+
:protected_instance_methods,
|
162
|
+
:private_instance_methods
|
163
|
+
].each do |method_id|
|
164
|
+
|
165
|
+
scope = method_id.to_s.gsub(/_.*/, '')
|
166
|
+
|
167
|
+
set_instance_methods(@class.send(method_id) -
|
168
|
+
@class.superclass.send(method_id), scope)
|
169
|
+
end
|
170
|
+
|
171
|
+
# If this is not at the end, the loop will attempt to do it's thing
|
172
|
+
# with the constructor created in this method, which is not necessary.
|
173
|
+
set_initializer
|
174
|
+
end
|
175
|
+
|
176
|
+
# Loop through the list of methods that are passed in,
|
177
|
+
# creating a wrapper method that enables tracing.
|
178
|
+
#
|
179
|
+
# Tracing data includes method name, parameters, and result.
|
180
|
+
# ==== Input
|
181
|
+
# [method_list : Array] A list of methods that will have wrapping functions
|
182
|
+
# created to enable tracing.
|
183
|
+
# [scope : String] The scope of the original function.
|
184
|
+
def set_instance_methods(method_list, scope)
|
185
|
+
method_list.each do |method_id|
|
186
|
+
# Setters and methods that accept blocks do not appear to work.
|
187
|
+
next if method_id =~ /=/ or method_id =~ /wrap_output/
|
188
|
+
|
189
|
+
# Build the method.
|
190
|
+
new_method = <<-DOC
|
191
|
+
alias :test_case_#{method_id} :#{method_id}
|
192
|
+
def #{method_id}(*args)
|
193
|
+
result = test_case_#{method_id}(*args)
|
194
|
+
@trace << {
|
195
|
+
:method => '#{method_id}',
|
196
|
+
:args => args,
|
197
|
+
:result => result
|
198
|
+
}
|
199
|
+
return result
|
200
|
+
end
|
201
|
+
#{scope} :#{method_id}
|
202
|
+
DOC
|
203
|
+
|
204
|
+
# Add the method to the class.
|
205
|
+
@class.class_eval do
|
206
|
+
eval(new_method)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Expose the private methods that are passed in. New methods will be created
|
212
|
+
# with the old method name followed by '_public_test'. If the original
|
213
|
+
# method contained a '?', it will be removed in the new method.
|
214
|
+
# ==== Input
|
215
|
+
# [type : Symbol] Indicates whether to handle instance or class methods.
|
216
|
+
#
|
217
|
+
# Only :class and :instance are supported.
|
218
|
+
# [methods : Array] An array of the methods to expose.
|
219
|
+
def expose_private_methods(type, methods)
|
220
|
+
# Get the text that the method should be wrapped in.
|
221
|
+
method_wrapper = wrapper(type)
|
222
|
+
|
223
|
+
# Loop through the methods.
|
224
|
+
methods.each do |method|
|
225
|
+
# Remove ?s.
|
226
|
+
new_method = method.to_s.gsub(/\?/, '')
|
227
|
+
|
228
|
+
# This is the new method.
|
229
|
+
new_method = <<-DOC
|
230
|
+
def #{new_method}_public_test(*args)
|
231
|
+
#{method}(*args)
|
232
|
+
end
|
233
|
+
DOC
|
234
|
+
|
235
|
+
# Add the wrapping text.
|
236
|
+
new_method = method_wrapper % [new_method]
|
237
|
+
|
238
|
+
# Add the method to the class.
|
239
|
+
@class.class_eval do
|
240
|
+
eval(new_method)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
# Expose the variables.
|
246
|
+
#
|
247
|
+
# New methods will be created (a getter and a setter) for each variable.
|
248
|
+
#
|
249
|
+
# Regardless of the type of variable, these methods are only available
|
250
|
+
# via an instance.
|
251
|
+
# ==== Input
|
252
|
+
# [variables : Array] An array of variables to expose.
|
253
|
+
def expose_variables(variables)
|
254
|
+
# Get the text that the methods should be wrapped in.
|
255
|
+
var_wrapper = wrapper(:instance)
|
256
|
+
|
257
|
+
# Loop through the variables
|
258
|
+
variables.each do |var|
|
259
|
+
# Remove any @s.
|
260
|
+
new_method = var.to_s.gsub(/@/, '')
|
261
|
+
|
262
|
+
# These are the new getter and setters.
|
263
|
+
new_method = <<-DOC
|
264
|
+
def #{new_method}_variable_method
|
265
|
+
#{var}
|
266
|
+
end
|
267
|
+
|
268
|
+
def #{new_method}_variable_method=(value)
|
269
|
+
#{var} = value
|
270
|
+
end
|
271
|
+
DOC
|
272
|
+
|
273
|
+
# Add the wrapping text.
|
274
|
+
new_method = var_wrapper % [new_method]
|
275
|
+
|
276
|
+
# Add the methods to the class.
|
277
|
+
@class.class_eval do
|
278
|
+
eval(new_method)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
# Returns the wrapping text for the specified type of method.
|
284
|
+
# ==== Input
|
285
|
+
# [type : Symbol] Indicates whether to handle instance or class methods.
|
286
|
+
#
|
287
|
+
# Only :class & :instance are supported.
|
288
|
+
# ==== Output
|
289
|
+
# [String] The text that the specified type of method should be wrapped in.
|
290
|
+
def wrapper(type)
|
291
|
+
case type
|
292
|
+
when :class then 'class << self;%s;end'
|
293
|
+
when :instance then '%s'
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
############################################################################
|
298
|
+
# I/O support methods.
|
299
|
+
############################################################################
|
300
|
+
|
301
|
+
# Return the actual output to stdout and stderr.
|
302
|
+
# ==== Output
|
303
|
+
# [Array] Two element array of strings.
|
304
|
+
#
|
305
|
+
# The first element is from stdout.
|
306
|
+
#
|
307
|
+
# The second element is from stderr.
|
308
|
+
def real_finis
|
309
|
+
return out, err
|
310
|
+
end
|
311
|
+
|
312
|
+
# Wrap a block to capture the output to stdout and stderr.
|
313
|
+
# ==== Input
|
314
|
+
# [&block : Block] The block of code that will have stdout and stderr trapped.
|
315
|
+
def wrap_output(&block)
|
316
|
+
begin
|
317
|
+
$stdout = @out
|
318
|
+
$stderr = @err
|
319
|
+
yield
|
320
|
+
ensure
|
321
|
+
$stdout = STDOUT
|
322
|
+
$stderr = STDERR
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
# Returns the output from stdout as a string.
|
327
|
+
# ==== Output
|
328
|
+
# [String] The output from stdout.
|
329
|
+
#
|
330
|
+
# All trailing line feeds are removed.
|
331
|
+
def out
|
332
|
+
@out.respond_to?(:string) ? @out.string.gsub(/\n*\z/, '') : ''
|
333
|
+
end
|
334
|
+
|
335
|
+
# Returns the output from stderr as a string.
|
336
|
+
# ==== Output
|
337
|
+
# [String] The output from stderr.
|
338
|
+
#
|
339
|
+
# All trailing line feeds are removed.
|
340
|
+
def err
|
341
|
+
@err.respond_to?(:string) ? @err.string.gsub(/\n*\z/, '') : ''
|
342
|
+
end
|
343
|
+
|
344
|
+
# Reset the stdout and stderr stream variables.
|
345
|
+
def reset_io
|
346
|
+
@out = StringIO.new
|
347
|
+
@err = StringIO.new
|
348
|
+
end
|
349
|
+
|
350
|
+
############################################################################
|
351
|
+
# Support methods.
|
352
|
+
############################################################################
|
353
|
+
|
354
|
+
# Indicates whether the specified method has been called on a given class.
|
355
|
+
# ==== Input
|
356
|
+
# [method_name : String] The name of the method.
|
357
|
+
#
|
358
|
+
# This value may be a string or a symbol.
|
359
|
+
# [class_name : String : @class.name] The name of the class that the method
|
360
|
+
# should have been invoked from.
|
361
|
+
def method_called?(method_name, class_name = @class.name)
|
362
|
+
!@stack_trace.index(
|
363
|
+
{:method => method_name.to_sym, :class => class_name}).nil?
|
364
|
+
end
|
365
|
+
|
366
|
+
# Set the application state to alive.
|
367
|
+
def reset_app_state
|
368
|
+
TestInternals::AppState.state = :alive unless TestInternals::AppState.alive
|
369
|
+
end
|
370
|
+
|
371
|
+
# Resets the trace arrays.
|
372
|
+
#
|
373
|
+
# This is intended for use in cases where code may be called multiple
|
374
|
+
# times in a single test.
|
375
|
+
def reset_trace
|
376
|
+
@stack_trace = []
|
377
|
+
@obj.trace = [] if @obj.respond_to?(:trace=)
|
378
|
+
end
|
379
|
+
|
380
|
+
# Shows the trace history as it stands, if the object supports it.
|
381
|
+
def show_trace
|
382
|
+
return unless defined? @obj
|
383
|
+
puts @obj.trace.join("\n" + '-' * 80 + "\n") if @obj.respond_to?(:trace)
|
384
|
+
end
|
385
|
+
|
386
|
+
############################################################################
|
387
|
+
# Assertions.
|
388
|
+
############################################################################
|
389
|
+
|
390
|
+
# Asserts that a value is equal to false.
|
391
|
+
# ==== Input
|
392
|
+
# [value : Any] The value to check for equality against false.
|
393
|
+
# [message : String : nil] The message to display if the value is not false.
|
394
|
+
def assert_false(value, message = nil)
|
395
|
+
assert_equal false, value, message
|
396
|
+
end
|
397
|
+
|
398
|
+
# Asserts that a value is equal to true.
|
399
|
+
# ==== Input
|
400
|
+
# [value : Any] The value to check for equality against true.
|
401
|
+
# [message : String : nil] The message to display if the value is not true.
|
402
|
+
def assert_true(value, message = nil)
|
403
|
+
assert_equal true, value, message
|
404
|
+
end
|
405
|
+
|
406
|
+
# Asserts that the negation of a value is true.
|
407
|
+
# ==== Input
|
408
|
+
# [value : Any] The value which will be negated and then asserted.
|
409
|
+
# [message : String : nil] The message to display if the assertion fails.
|
410
|
+
def assert_not(value, message = nil)
|
411
|
+
assert !value, message
|
412
|
+
end
|
413
|
+
|
414
|
+
# Assert that an array has a specified number of elements.
|
415
|
+
# ==== Input
|
416
|
+
# [array : Array] The array that will have it's length checked.
|
417
|
+
# [length : Fixnum] The length that the array should be.
|
418
|
+
# [message : String : nil] The message to display if the assertion fails.
|
419
|
+
def assert_array_count(array, length, message = nil)
|
420
|
+
if message.nil?
|
421
|
+
message = "#{array} has #{array.length} item(s), " +
|
422
|
+
"but was expected to have #{length}."
|
423
|
+
end
|
424
|
+
|
425
|
+
assert array.length == length, message
|
426
|
+
end
|
427
|
+
|
428
|
+
############################################################################
|
429
|
+
# Assertions - Stack trace.
|
430
|
+
############################################################################
|
431
|
+
|
432
|
+
# Asserts that a method was called on a class.
|
433
|
+
# ==== Input
|
434
|
+
# [method_name : String] The name of the method to check for.
|
435
|
+
# [class_name : String : @class.name] The name of the class
|
436
|
+
# on which <tt>method_name</tt>
|
437
|
+
# should have been invoked.
|
438
|
+
def assert_method(method_name, class_name = @class.name)
|
439
|
+
assert method_called?(method_name.to_sym, class_name),
|
440
|
+
"#{class_name}.#{method_name} has not been called."
|
441
|
+
end
|
442
|
+
|
443
|
+
# Asserts that a method was not called on a class.
|
444
|
+
# ==== Input
|
445
|
+
# [method_name : String] The name of the method to check for.
|
446
|
+
# [class_name : String : @class.name] The name of the class
|
447
|
+
# on which <tt>method_name</tt>
|
448
|
+
# should not have been invoked.
|
449
|
+
def assert_not_method(method_name, class_name = @class.name)
|
450
|
+
assert !method_called?(method_name.to_sym, class_name),
|
451
|
+
"#{class_name}.#{method_name} should not be called."
|
452
|
+
end
|
453
|
+
|
454
|
+
# Asserts that a method was called with the specified parameters.
|
455
|
+
# ==== Input
|
456
|
+
# [method_name : String] The name of the method to check.
|
457
|
+
# [*args : Array] The parameters that were passed in to the method.
|
458
|
+
def assert_trace_args(method_name, *args)
|
459
|
+
match = false
|
460
|
+
|
461
|
+
list = []
|
462
|
+
|
463
|
+
# Loop through the stack trace to see if the method was called
|
464
|
+
# with the specified arguments.
|
465
|
+
@obj.trace.each do |trace|
|
466
|
+
if trace[:method] == method_name and trace[:args] == args
|
467
|
+
match = true
|
468
|
+
break
|
469
|
+
elsif trace[:method] == method_name
|
470
|
+
list << trace[:args]
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
474
|
+
assert match,
|
475
|
+
"#{method_name} was not called with the following parameters:\n" +
|
476
|
+
"#{args.join("\n" + '-' * 80 + "\n")}\n" +
|
477
|
+
'*' * 80 + "\n" +
|
478
|
+
"#{method_name} was recorded as follows:\n" +
|
479
|
+
"#{list.join("\n" + '-' * 80 + "\n")}"
|
480
|
+
end
|
481
|
+
|
482
|
+
# Asserts that a method was called with the specified parameters
|
483
|
+
# and returned the specified result.
|
484
|
+
# ==== Input
|
485
|
+
# [method_name : String] The name of the method to check.
|
486
|
+
# [result : Any] The expected result of the method call.
|
487
|
+
# [*args : Array] The parameters that were passed in to the method.
|
488
|
+
def assert_trace_info(method_name, result, *args)
|
489
|
+
match = (@obj.trace.index(
|
490
|
+
{:methd => method_name, :args => args, :result => result}))
|
491
|
+
|
492
|
+
list = []
|
493
|
+
|
494
|
+
# Only get a list of possible results if a match was not found.
|
495
|
+
unless match
|
496
|
+
@obj.trace.each do |trace|
|
497
|
+
if trace[:method] == method_name
|
498
|
+
list << {:args => trace[:args], :result => trace[:result]}
|
499
|
+
end
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
assert match,
|
504
|
+
"#{method_name} was not called with the following parameters:\n" +
|
505
|
+
"#{args}\n" +
|
506
|
+
"or did not return the following result:\n" +
|
507
|
+
"#{result}\n" +
|
508
|
+
"#{method_name} was recorded as follows:\n" +
|
509
|
+
"#{list.join("\n" + '-' * 80 + "\n")}"
|
510
|
+
end
|
511
|
+
|
512
|
+
############################################################################
|
513
|
+
# Assertions - Application state.
|
514
|
+
############################################################################
|
515
|
+
|
516
|
+
# Asserts that the application has been stopped.
|
517
|
+
# ==== Input
|
518
|
+
# [message : String : nil] The message to show if the assertion fails.
|
519
|
+
def assert_dead(message = nil)
|
520
|
+
message = "#{@class} was not stopped as expected" if message.nil?
|
521
|
+
|
522
|
+
# Hold the state in a local variable so that the state can be reset
|
523
|
+
# prior to the assertion.
|
524
|
+
dead = TestInternals::AppState.dead
|
525
|
+
|
526
|
+
# Reset the application state so that later tests are not adversely
|
527
|
+
# affected if this assertion fails, which otherwise could leave
|
528
|
+
# the application in an incorrect state.
|
529
|
+
reset_app_state
|
530
|
+
|
531
|
+
assert_true dead, message
|
532
|
+
end
|
533
|
+
|
534
|
+
# Asserts that the application is still running.
|
535
|
+
# ==== Input
|
536
|
+
# [message : String : nil] The message to show if the assertion fails.
|
537
|
+
def assert_alive(message = nil)
|
538
|
+
message = "#{@class} is not running as expected" if message.nil?
|
539
|
+
|
540
|
+
# Hold the state in a local variable so that the state can be reset
|
541
|
+
# prior to the assertion.
|
542
|
+
alive = TestInternals::AppState.alive
|
543
|
+
|
544
|
+
# Reset the application state so that later tests are not adversely
|
545
|
+
# affected if this assertion fails, which otherwise could leave
|
546
|
+
# the application in an incorrect state.
|
547
|
+
reset_app_state
|
548
|
+
|
549
|
+
assert_true alive, message
|
550
|
+
end
|
551
|
+
end
|
data/rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'test_internals'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
|
5
|
+
s.summary = 'Allows tests to check the stack trace, ' +
|
6
|
+
'parameters, private methods, and class variables.'
|
7
|
+
s.description = 'TestInternals patches Test::Unit::TestCase to allow ' +
|
8
|
+
'testing of private methods, as well as variables. The stack trace ' +
|
9
|
+
'is also available, including the ability to check that specific ' +
|
10
|
+
'parameters were sent to a method.'
|
11
|
+
|
12
|
+
s.author = 'Travis Herrick'
|
13
|
+
s.email = 'tthetoad@gmail.com'
|
14
|
+
s.homepage = 'http://www.bitbucket.org/ToadJamb/gems_test_internals'
|
15
|
+
|
16
|
+
s.license = 'GPLV3'
|
17
|
+
|
18
|
+
s.extra_rdoc_files << 'README'
|
19
|
+
|
20
|
+
s.require_paths = ['lib']
|
21
|
+
s.files = Dir['lib/**/*.rb', '*']
|
22
|
+
s.test_files = Dir['test/**/*.rb']
|
23
|
+
|
24
|
+
s.add_development_dependency 'rake_tasks', '~> 0.0.1'
|
25
|
+
|
26
|
+
s.has_rdoc = true
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: test_internals
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Travis Herrick
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-08-08 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake_tasks
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 0
|
30
|
+
- 1
|
31
|
+
version: 0.0.1
|
32
|
+
type: :development
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
description: TestInternals patches Test::Unit::TestCase to allow testing of private methods, as well as variables. The stack trace is also available, including the ability to check that specific parameters were sent to a method.
|
36
|
+
email: tthetoad@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
files:
|
44
|
+
- lib/test_internals/test_case.rb
|
45
|
+
- lib/test_internals/app_state.rb
|
46
|
+
- lib/test_internals/kernel.rb
|
47
|
+
- lib/test_internals.rb
|
48
|
+
- Gemfile.lock
|
49
|
+
- Gemfile
|
50
|
+
- rakefile
|
51
|
+
- README
|
52
|
+
- test_internals.gemspec
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://www.bitbucket.org/ToadJamb/gems_test_internals
|
55
|
+
licenses:
|
56
|
+
- GPLV3
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: -4421005185209381903
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.7
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Allows tests to check the stack trace, parameters, private methods, and class variables.
|
86
|
+
test_files: []
|
87
|
+
|