test_extensions 0.1.1 → 0.1.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/Readme CHANGED
@@ -1,9 +1,8 @@
1
- = QualitySmith Test Extensions
1
+ = Test Extensions
2
2
 
3
- [<b>Home page</b>:] soon: http://test-extensions.rubyforge.org/
3
+ [<b>Home page</b>:] http://test-extensions.rubyforge.org/
4
4
  [<b>Project site</b>:] http://rubyforge.org/projects/test-extensions
5
5
  [<b>Gem install</b>:] <tt>gem install test_extensions</tt>
6
- [<b>Wiki</b>:] http://wiki.qualitysmith.com/
7
6
  [<b>Author</b>:] Tyler Rick, and others
8
7
  [<b>Copyright</b>:] 2007 QualitySmith, Inc.
9
8
  [<b>License</b>:] {GNU General Public License}[http://www.gnu.org/copyleft/gpl.html]
@@ -28,7 +27,7 @@ After:
28
27
 
29
28
  link:include/assert_equal_with_difference_highlighting-there_he_is.png
30
29
 
31
- See http://qualitysmithext.rubyforge.org/classes/Test/Unit/Assertions.html#M000082 for full details.
30
+ See http://quality-ext.rubyforge.org/classes/Test/Unit/Assertions.html#M000082 for full details.
32
31
 
33
32
  === Useful assertion methods
34
33
 
@@ -38,7 +37,7 @@ See http://qualitysmithext.rubyforge.org/classes/Test/Unit/Assertions.html#M0000
38
37
  assert_match Regexp.loose_join('key phrase 1', 'key phrase 2'), "Some really long string that you want to make sure contains both key phrase 1 and key phrase 2, in that order, but you don't care too much about the stuff in between, and you don't really want to hand-craft the regular expression or worry about escaping things like '.'s and '*'s yourself."
39
38
  * <b><tt>assert_changed</tt></b>: assert that a variable changed state during the course of the block (not really useful)
40
39
 
41
- See the individual method documentation for each method for details. These methods are currently part of http://qualitysmithext.rubyforge.org/.
40
+ See the individual method documentation for each method for details. These methods are currently part of http://quality-ext.rubyforge.org.
42
41
 
43
42
  Rails-specific:
44
43
  * assert_user_error
@@ -48,6 +47,10 @@ Rails-specific:
48
47
  * <b><tt>capture_output</tt></b> -- captures the standard output/error from the method under test (rather than letting it print to the screen) so that you can make assertions about it.
49
48
  * <b><tt>simulate_input</tt></b> -- if your application is expecting some kind of user input on stdin, this method can be used to simulate the input, so that your test is completely automated and self-contained.
50
49
 
50
+ === FreshTestCaseCreator
51
+
52
+ ...
53
+
51
54
  == Installation
52
55
 
53
56
  sudo gem install test_extensions --include-dependencies
@@ -65,7 +68,7 @@ Then just throw this in your <tt>test_helper.rb</tt>:
65
68
  Gems:
66
69
  * colored
67
70
  * facets
68
- * qualitysmith_extensions
71
+ * quality_extensions
69
72
 
70
73
  == Problems, questions, comments, or contributions?
71
74
 
@@ -1,13 +1,18 @@
1
1
  require 'test/unit'
2
+
2
3
  require 'rubygems'
3
4
 
4
5
  gem 'facets'
5
6
  require 'facets/core/kernel/require_local'
7
+
6
8
  require_local 'test_extensions/test_colorizer'
9
+ require_local 'test_extensions/runnable'
10
+ require_local 'test_extensions/remove_constants_when_test_suite_finished'
11
+ require_local 'test_extensions/fresh_test_environment_creator'
7
12
 
8
- gem 'qualitysmith_extensions'
9
- require 'qualitysmith_extensions/regexp/join'
10
- require 'qualitysmith_extensions/kernel/capture_output.rb'
11
- require 'qualitysmith_extensions/kernel/simulate_input.rb'
13
+ gem 'quality_extensions'
14
+ require 'quality_extensions/regexp/join'
15
+ require 'quality_extensions/kernel/capture_output.rb'
16
+ require 'quality_extensions/kernel/simulate_input.rb'
12
17
 
13
- require 'qualitysmith_extensions/test/all'
18
+ require 'quality_extensions/test/all'
@@ -0,0 +1,204 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2007 QualitySmith, Inc.
4
+ # License:: GPL
5
+ # Developer notes::
6
+ # Changes::
7
+ #++
8
+
9
+ require 'test/unit'
10
+ require 'test/unit/ui/console/testrunner'
11
+ require 'rubygems'
12
+ require 'facets/core/kernel/require_local'
13
+ require 'facets/core/kernel/load_local'
14
+ require 'facets/core/kernel/with'
15
+ require 'quality_extensions/module/remove_const'
16
+ require 'quality_extensions/module/create'
17
+ require 'quality_extensions/object/ignore_access'
18
+
19
+ # useful if you want to try multiple different configurations of the same class
20
+ # and make sure that they all are compatabible in some way (document in your common tests)
21
+ # creates a clean slate by removing constants...
22
+ # Lets you cleanly *document* both all the *commonalities* and all the *differences* between two different behaviors (caused by two different configurations)
23
+ #
24
+ # Example:
25
+ # # Common setup
26
+ # test_environment_creator = FreshTestEnvironmentCreator.new(:classes_to_remove => [MyModel]) do
27
+ # def setup
28
+ # @object = ClassWithSomeConfiguration.new
29
+ # end
30
+ # def test_common_stuff
31
+ # # These tests should hold true whether we're acting acts_one_way *or* acts_another_way
32
+ # ...
33
+ # end
34
+ # end
35
+ #
36
+ # # The setup for TestWhenActingOneWay
37
+ # class MyModel < ActiveRecord::Base
38
+ # acts_one_way
39
+ # end
40
+ # # The TestWhenActingOneWay TestCase itself
41
+ # test_environment_creator.create(:TestWhenActingOneWay) do
42
+ # def test_1
43
+ # ...
44
+ # end
45
+ # end.run!
46
+ #
47
+ # # The setup for TestWhenActingAnotherWay
48
+ # class MyModel < ActiveRecord::Base
49
+ # acts_another_way
50
+ # end
51
+ # # The TestWhenActingAnotherWay TestCase itself
52
+ # test_environment_creator.create(:TestWhenActingAnotherWay) do
53
+ # def test_1
54
+ # ...
55
+ # end
56
+ # end.run!
57
+ #
58
+ class FreshTestEnvironmentCreator
59
+ def initialize(options = {}, &common_tests)
60
+ @classes_to_remove = options[:classes_to_remove] || []
61
+ @common_tests = Module.create(:CommonTests, &common_tests) if block_given?
62
+ end
63
+ def create(test_case_name, &contents_of_class)
64
+ returning test_case_class = Class.create(test_case_name, :superclass => Test::Unit::TestCase, &contents_of_class) do
65
+ test_case_class.send :include, @common_tests if @common_tests
66
+ test_case_class.ignore_access.class_variable_set(:@@classes_to_remove, @classes_to_remove)
67
+ end
68
+ end
69
+ end
70
+
71
+ class Test::Unit::TestCase
72
+ def self.run!
73
+ Test::Unit::UI::Console::TestRunner.run(self)
74
+
75
+ self.ignore_access.class_variable_get(:@@classes_to_remove).each do |class_to_reload|
76
+ class_to_reload.remove_const! rescue nil
77
+ end if self.ignore_access.class_variables.include?('@@classes_to_remove')
78
+ end
79
+ end
80
+
81
+
82
+
83
+
84
+ # _____ _
85
+ # |_ _|__ ___| |_
86
+ # | |/ _ \/ __| __|
87
+ # | | __/\__ \ |_
88
+ # |_|\___||___/\__|
89
+ #
90
+ =begin test
91
+ require 'test/unit'
92
+ require 'facets/core/integer/multiple'
93
+
94
+ #---------------------------------------------------------------------------------------------------------------------------------
95
+ # You don't need FreshTestEnvironmentCreator if all you want is some tests / setup common to multiple test cases...
96
+ # It provides that as a feature, but that's easy to do by simply creating a module containing your common test code...
97
+
98
+ module CommonTestsAndSetup
99
+ def setup
100
+ puts 'Common setup'
101
+ end
102
+ def test_common_1
103
+ puts 'test_common_1'
104
+ end
105
+ end
106
+
107
+ class Test1 < Test::Unit::TestCase
108
+ include CommonTestsAndSetup
109
+ def setup
110
+ super
111
+ puts 'Special setup for Test1'
112
+ end
113
+ def test_1
114
+ puts 'test_1'
115
+ end
116
+ end
117
+ Test1.run!
118
+
119
+ class Test2 < Test::Unit::TestCase
120
+ include CommonTestsAndSetup
121
+ def test_1
122
+ puts 'test_2'
123
+ end
124
+ end
125
+ Test2.run!
126
+
127
+ puts '---------------------------------------------------------------------------------------------------------------------------------'
128
+ #---------------------------------------------------------------------------------------------------------------------------------
129
+ # Beginning of actual tests for FreshTestEnvironmentCreator.
130
+
131
+ class BaseClass
132
+ def abstract?; true; end
133
+ def do_the_stuff_we_always_do; "We did the stuff that we're always supposed to do."; end
134
+ def gather_user_input; "Some really unintuitive default user interface for gather input from the user."; end
135
+ def do_computations; "Computing... " + (1..10).to_a.join(',') + ',done'; end
136
+ def behave!
137
+ gather_user_input +
138
+ do_the_stuff_we_always_do +
139
+ do_computations
140
+ end
141
+
142
+ # Configuration macros, which do any number of unknown things to the class which might be hard to undo if you wanted to create
143
+ # that class again with the same name but with a different configuration... In other words, lets assume that these macros
144
+ # do all kind of nasty things to the class, which would necessitate us doing a remove_const and redefining the class again
145
+ # from scratch if we wanted to be guaranteed of having a clean slate...
146
+ def self.behave_efficiently
147
+ define_method :do_computations do
148
+ "Computing... " + (1..10).select(&:even?).join(',') + ',done'
149
+ end
150
+ end
151
+ def self.behave_intelligently
152
+ define_method :gather_user_input do
153
+ "Read your mind and intelligently predict what you're trying to accomplish."
154
+ end
155
+ end
156
+ end
157
+
158
+ test_environment_creator = FreshTestEnvironmentCreator.new(:classes_to_remove => [ClassWithSomeConfiguration]) do
159
+ def setup
160
+ @object = ClassWithSomeConfiguration.new
161
+ end
162
+ def test_common_stuff
163
+ puts 'Testing common stuff'
164
+ assert_match Regexp.union("We did the stuff"), @object.behave!
165
+ assert_match /We did the stuff that we're always supposed to do./, @object.behave!
166
+ #' (To fix syntax highlighting in vim)
167
+ end
168
+ end
169
+
170
+ #---------------------------------------------------------------------------------------------------------------------------------
171
+
172
+ class ClassWithSomeConfiguration < BaseClass
173
+ behave_efficiently
174
+ end
175
+
176
+ test_environment_creator.create(:TestOfEfficiency) do
177
+ def test_1
178
+ puts "Testing behave_efficiently"
179
+ assert_match Regexp.union("unintuitive default user interface"), @object.behave!
180
+ assert_match Regexp.union("2,4,6,8,10,done"), @object.behave!
181
+ end
182
+ end.run!
183
+
184
+ #---------------------------------------------------------------------------------------------------------------------------------
185
+
186
+ class ClassWithSomeConfiguration < BaseClass
187
+ behave_intelligently
188
+ end
189
+
190
+ test_environment_creator.create(:TestOfIntelligence) do
191
+ def setup
192
+ puts "(A bit of setup specific to this test case...)"
193
+ super
194
+ end
195
+ def test_1
196
+ puts "Testing behave_intelligently"
197
+ assert_match Regexp.union("intelligently predict"), @object.behave!
198
+ assert_match Regexp.union("1,2,3,4,5,6,7,8,9,10,done"), @object.behave!
199
+ end
200
+ end.run!
201
+
202
+
203
+ =end
204
+
@@ -0,0 +1,76 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2007 QualitySmith, Inc.
4
+ # License:: GPL
5
+ # Developer notes::
6
+ # Changes::
7
+ #++
8
+
9
+ require 'test/unit/ui/console/testrunner'
10
+
11
+ require 'rubygems'
12
+ require 'quality_extensions/module/alias_method_chain'
13
+
14
+ class Test::Unit::UI::TestRunnerMediator
15
+ # Test::Unit uses listeners to notify the UI of test progress, failures, etc. That's great, if you're writing a new UI.
16
+ # But unfortunately, it doesn't provide an easy way (that I could find) to add your *own* listeners (for things *other* than the UI).
17
+ # The only place that I see add_listener used anywhere in Test::Unit is in the UI classes (ui/*/testrunner.rb).
18
+ #
19
+ # So how does one hook into Test::Unit to register/add a listener for things *without* writing a new UI? (Let's assume we're
20
+ # perfectly happy with the Test::Unit::UI::Console UI...)
21
+ #
22
+ # They've provided some nice "events" ("channels"?) -- like TestCase::STARTED, TestCase::FINISHED, TestResult::FAULT -- but
23
+ # how do I register with Test::Unit that *I* want to be notified of those events too?
24
+ #
25
+ # Also, add_listener is an instance method -- how do you get inside of TestCase/TestRunnerMediator so that you *have* an instance?
26
+ #
27
+ # I wanted to add my *own* listener callback. So that's why I wrote this monkey patch... (If there's an easier way, let me know...)
28
+ #
29
+ # * Test::Unit::TestCase::FINISHED -- Seems to be called not when the entire TestCase has finished running (as the name would suggest) but after every test *method* in the TestCase...?
30
+ # * Test::Unit::UI::TestRunnerMediator::FINISHED -- When all the tests for a whole suite have finished
31
+
32
+ def self.register_additional_observers(&block)
33
+ @@additional_observers_procs ||= []
34
+ @@additional_observers_procs.push block
35
+ end
36
+ def run_suite_with_additional_observers
37
+ @@additional_observers_procs.each do |proc|
38
+ proc.call(self)
39
+ end if defined?(@@additional_observers_procs)
40
+ run_suite_without_additional_observers
41
+ end
42
+ alias_method_chain :run_suite, :additional_observers
43
+ end
44
+
45
+
46
+ # _____ _
47
+ # |_ _|__ ___| |_
48
+ # | |/ _ \/ __| __|
49
+ # | | __/\__ \ |_
50
+ # |_|\___||___/\__|
51
+ #
52
+ =begin test
53
+ require 'test/unit'
54
+ require 'rubygems'
55
+ require 'facets/core/kernel/require_local'
56
+ require_local 'runnable'
57
+
58
+ Test::Unit::UI::TestRunnerMediator.register_additional_observers do |mediator|
59
+ mediator.add_listener(Test::Unit::TestCase::FINISHED) do |*args|
60
+ puts "FINISHED!"
61
+ p args
62
+ end
63
+ end
64
+
65
+
66
+ class Test1 < Test::Unit::TestCase
67
+ def test_1
68
+ end
69
+ def test_2
70
+ end
71
+ end
72
+ Test1.run!
73
+
74
+ =end
75
+
76
+
@@ -0,0 +1,70 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2007 QualitySmith, Inc.
4
+ # License:: GPL
5
+ # Developer notes::
6
+ # Changes::
7
+ #++
8
+
9
+ require 'test/unit'
10
+ require 'test/unit/ui/console/testrunner'
11
+
12
+ require 'rubygems'
13
+ require 'facets/core/kernel/require_local'
14
+ require 'quality_extensions/module/remove_const'
15
+ require 'quality_extensions/object/ignore_access'
16
+ require 'quality_extensions/module/alias_method_chain'
17
+
18
+ require_local 'runnable'
19
+
20
+ class Test::Unit::TestCase
21
+
22
+ def self.run_with_remove_constants_when_test_suite_finished!
23
+ run_without_remove_constants_when_test_suite_finished!
24
+
25
+ self.ignore_access.class_variable_get(:@@classes_to_remove).each do |class_to_reload|
26
+ #puts "removing #{class_to_reload}"
27
+ class_to_reload.remove_const! rescue nil
28
+ end if self.ignore_access.class_variables.include?('@@classes_to_remove')
29
+ end
30
+
31
+ class << self
32
+ alias_method_chain :run!, :remove_constants_when_test_suite_finished
33
+ end
34
+ end
35
+
36
+
37
+
38
+ # _____ _
39
+ # |_ _|__ ___| |_
40
+ # | |/ _ \/ __| __|
41
+ # | | __/\__ \ |_
42
+ # |_|\___||___/\__|
43
+ #
44
+ =begin test
45
+ require 'test/unit'
46
+
47
+ class Foo
48
+ def method1; end
49
+ end
50
+
51
+ class Test1 < Test::Unit::TestCase
52
+ def test_1
53
+ assert_nothing_raised { Foo.new.method1 }
54
+ end
55
+ end
56
+ Test1.ignore_access.class_variable_set(:@@classes_to_remove, [Foo])
57
+ Test1.run!
58
+
59
+ class Foo
60
+ end
61
+
62
+ class Test2 < Test::Unit::TestCase
63
+ def test_1
64
+ assert_raise(NoMethodError) { Foo.new.method1 }
65
+ end
66
+ end
67
+ Test2.run!
68
+
69
+ =end
70
+
@@ -0,0 +1,41 @@
1
+ #--
2
+ # Author:: Tyler Rick
3
+ # Copyright:: Copyright (c) 2007 QualitySmith, Inc.
4
+ # License:: GPL
5
+ # Developer notes::
6
+ # Changes::
7
+ #++
8
+
9
+ require 'test/unit'
10
+ require 'test/unit/ui/console/testrunner'
11
+
12
+ require 'rubygems'
13
+ require 'quality_extensions/module/remove_const'
14
+ require 'quality_extensions/module/create'
15
+ require 'quality_extensions/object/ignore_access'
16
+
17
+ class Test::Unit::TestCase
18
+ # Just a (much conciser) wrapper for Test::Unit::UI::Console::TestRunner.run(NameOfTestCase)
19
+ def self.run!
20
+ Test::Unit::UI::Console::TestRunner.run(self)
21
+ end
22
+ end
23
+
24
+
25
+ # _____ _
26
+ # |_ _|__ ___| |_
27
+ # | |/ _ \/ __| __|
28
+ # | | __/\__ \ |_
29
+ # |_|\___||___/\__|
30
+ #
31
+ =begin test
32
+ require 'test/unit'
33
+
34
+ class Test1 < Test::Unit::TestCase
35
+ def test_1
36
+ end
37
+ end
38
+ Test1.run!
39
+
40
+ =end
41
+
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: test_extensions
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2007-05-01 00:00:00 -07:00
6
+ version: 0.1.3
7
+ date: 2007-10-09 00:00:00 -07:00
8
8
  summary: A collection of user-contributed enhancements and new methods for Test::Unit
9
9
  require_paths:
10
10
  - lib
@@ -31,6 +31,10 @@ authors:
31
31
  files:
32
32
  - lib/test_extensions.rb
33
33
  - lib/test_extensions/test_colorizer.rb
34
+ - lib/test_extensions/remove_constants_when_test_suite_finished.rb
35
+ - lib/test_extensions/register_additional_observers.rb
36
+ - lib/test_extensions/fresh_test_environment_creator.rb
37
+ - lib/test_extensions/runnable.rb
34
38
  - Readme
35
39
  test_files: []
36
40
 
@@ -68,7 +72,7 @@ dependencies:
68
72
  version: 0.0.0
69
73
  version:
70
74
  - !ruby/object:Gem::Dependency
71
- name: qualitysmith_extensions
75
+ name: quality_extensions
72
76
  version_requirement:
73
77
  version_requirements: !ruby/object:Gem::Version::Requirement
74
78
  requirements: