GUnit 0.1.2 → 0.2.0

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
data/gunit.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{GUnit}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Greg Sterndale"]
@@ -44,6 +44,7 @@ Gem::Specification.new do |s|
44
44
  "test/test_helper.rb",
45
45
  "test/unit/assertions_test.rb",
46
46
  "test/unit/context_test.rb",
47
+ "test/unit/exercise_test.rb",
47
48
  "test/unit/fail_response_test.rb",
48
49
  "test/unit/proc_extensions_test.rb",
49
50
  "test/unit/setup_test.rb",
@@ -63,6 +64,7 @@ Gem::Specification.new do |s|
63
64
  "test/test_helper.rb",
64
65
  "test/unit/assertions_test.rb",
65
66
  "test/unit/context_test.rb",
67
+ "test/unit/exercise_test.rb",
66
68
  "test/unit/fail_response_test.rb",
67
69
  "test/unit/proc_extensions_test.rb",
68
70
  "test/unit/setup_test.rb",
data/lib/gunit/context.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module GUnit
2
2
  # Context instance has message, setups, exercises, teardowns, and parent (another Context)
3
3
  class Context
4
- attr_accessor :task, :parent, :message, :setups, :teardowns
4
+ attr_accessor :task, :parent, :message, :setups, :exercise, :teardowns
5
5
 
6
6
  # Context.new("my message")
7
7
  # Context.new("my message") do
@@ -28,17 +28,17 @@ module GUnit
28
28
  end
29
29
 
30
30
  def all_message
31
- parent_message = self.parent.message if self.parent
31
+ parent_message = self.parent.all_message if self.parent
32
32
  parent_message = nil if parent_message == ''
33
33
  [parent_message, @message].compact.join(' ')
34
34
  end
35
35
 
36
36
  def all_setups
37
- (self.parent ? self.parent.setups : []) + @setups
37
+ (self.parent ? self.parent.all_setups : []) + @setups
38
38
  end
39
39
 
40
40
  def all_teardowns
41
- (self.parent ? self.parent.teardowns : []) + @teardowns
41
+ (self.parent ? self.parent.all_teardowns : []) + @teardowns
42
42
  end
43
43
 
44
44
  end
@@ -0,0 +1,41 @@
1
+ module GUnit
2
+
3
+ class Exercise
4
+ attr_writer :message
5
+ attr_accessor :task
6
+
7
+ # Exercise.new("my message")
8
+ # Exercise.new("my message") { @foo.bar() }
9
+ # Exercise.new() { @foo.bar() }
10
+ def initialize(*args, &blk)
11
+ self.message = args[0]
12
+ self.task = blk if blk
13
+ end
14
+
15
+ def run(binding=self)
16
+ begin
17
+ if @task.is_a?(Proc)
18
+ bound_task = @task.bind(binding)
19
+ bound_task.call
20
+ end
21
+ return true
22
+ rescue GUnit::AssertionFailure => e
23
+ FailResponse.new
24
+ rescue ::StandardError => e
25
+ ExceptionResponse.new
26
+ end
27
+ end
28
+
29
+ def message
30
+ @message || default_message
31
+ end
32
+
33
+ def default_message
34
+ "Exercise"
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
41
+
@@ -17,7 +17,8 @@ module GUnit
17
17
 
18
18
  TEST_METHOD_PREFIX = 'test'
19
19
 
20
- attr_accessor :method_name, :context
20
+ attr_accessor :method_name
21
+ attr_writer :context
21
22
 
22
23
  @@method_count = 0
23
24
  @@context_stack = [ GUnit::Context.new ]
@@ -33,6 +34,7 @@ module GUnit
33
34
 
34
35
  def run
35
36
  self.run_setups
37
+ self.run_excercise
36
38
  response = self.send(self.method_name.to_sym)
37
39
  self.run_teardowns
38
40
  response
@@ -77,6 +79,15 @@ module GUnit
77
79
  current_context.setups << setup
78
80
  end
79
81
 
82
+ def self.exercise(*args, &blk)
83
+ exercise = if blk
84
+ GUnit::Exercise.new(args.first, &blk)
85
+ else
86
+ GUnit::Exercise.new(args.first)
87
+ end
88
+ current_context.exercise = exercise
89
+ end
90
+
80
91
  def self.teardown(*args, &blk)
81
92
  teardown = if blk
82
93
  GUnit::Teardown.new(args.first, &blk)
@@ -112,11 +123,15 @@ module GUnit
112
123
  protected
113
124
 
114
125
  def run_setups
115
- self.context.setups.each {|s| s.run(self) }
126
+ self.context.all_setups.each {|s| s.run(self) }
116
127
  end
117
-
128
+
129
+ def run_excercise
130
+ self.context.exercise.run(self) if self.context && self.context.exercise
131
+ end
132
+
118
133
  def run_teardowns
119
- self.context.teardowns.reverse.each {|t| t.run(self) } if self.context
134
+ self.context.all_teardowns.reverse.each {|t| t.run(self) } if self.context
120
135
  end
121
136
 
122
137
  def self.current_context
@@ -71,13 +71,13 @@ class FooGUnitTest < GUnit::TestCase
71
71
  context "An instance of Foo" do
72
72
  # One setup per context
73
73
  setup do
74
- @foo = 'abc'
74
+ @foo = ''
75
75
  end
76
76
 
77
- # # One exercise per context
78
- # exercise do
79
- # @foo.do_something
80
- # end
77
+ # One exercise per context
78
+ exercise do
79
+ @foo.replace 'abc'
80
+ end
81
81
 
82
82
  # One teardown per context
83
83
  teardown do
@@ -89,6 +89,13 @@ class FooGUnitTest < GUnit::TestCase
89
89
  assert @foo == 'abc'
90
90
  end
91
91
 
92
+ # Exercise outside of this context should not apply
93
+ context "doing something else" do
94
+ verify "emptiness of foo" do
95
+ assert @foo == '', "#{@foo.class} is not empty"
96
+ end
97
+ end
98
+
92
99
  # many nested contexts per context
93
100
  context "doing something else" do
94
101
  setup do
@@ -173,7 +180,7 @@ class FooGUnitTestTest < Test::Unit::TestCase
173
180
 
174
181
  def test_run_test_runner
175
182
  @test_runner.run
176
- assert_equal 6, @test_runner.passes.length
183
+ assert_equal 7, @test_runner.passes.length
177
184
  assert_equal 5, @test_runner.fails.length
178
185
  assert_equal 1, @test_runner.exceptions.length
179
186
  assert_equal 1, @test_runner.to_dos.length
@@ -16,7 +16,7 @@ class GUnit::ContextTest < Test::Unit::TestCase
16
16
  @context1 = GUnit::Context.new
17
17
  end
18
18
 
19
- def test_it_is_a_setup
19
+ def test_it_is_a_context
20
20
  assert @context1.is_a?(GUnit::Context)
21
21
  end
22
22
 
@@ -31,6 +31,12 @@ class GUnit::ContextTest < Test::Unit::TestCase
31
31
  assert_equal message, @context1.message
32
32
  end
33
33
 
34
+ def test_exercise_setter
35
+ exercise = GUnit::Exercise.new
36
+ @context1.exercise = exercise
37
+ assert_equal exercise, @context1.exercise
38
+ end
39
+
34
40
  def test_setups_setter
35
41
  setups = [GUnit::Setup.new, GUnit::Setup.new]
36
42
  @context1.setups = setups
@@ -49,31 +55,40 @@ class GUnit::ContextTest < Test::Unit::TestCase
49
55
  assert_equal parent, @context1.parent
50
56
  end
51
57
 
52
- def test_message_with_parent
58
+ def test_message_with_parents
59
+ nana = GUnit::Context.new
60
+ nana.message = "Nana"
53
61
  parent = GUnit::Context.new
54
62
  parent.message = "Mom"
63
+ parent.parent = nana
55
64
  message = "Kid"
56
65
  @context1.parent = parent
57
66
  @context1.message = message
58
- assert_equal parent.message + ' ' + message, @context1.all_message
67
+ assert_equal nana.message + ' ' + parent.message + ' ' + message, @context1.all_message
59
68
  end
60
69
 
61
- def test_setups_with_parent
70
+ def test_setups_with_parents
71
+ pops = GUnit::Context.new
72
+ pops.setups = [GUnit::Setup.new, GUnit::Setup.new]
62
73
  parent = GUnit::Context.new
63
74
  parent.setups = [GUnit::Setup.new, GUnit::Setup.new]
75
+ parent.parent = pops
64
76
  setups = [GUnit::Setup.new, GUnit::Setup.new]
65
77
  @context1.parent = parent
66
78
  @context1.setups = setups
67
- assert_equal parent.setups + setups, @context1.all_setups
79
+ assert_equal pops.setups + parent.setups + setups, @context1.all_setups
68
80
  end
69
81
 
70
- def test_teardowns_with_parent
82
+ def test_teardowns_with_parents
83
+ pops = GUnit::Context.new
84
+ pops.teardowns = [GUnit::Teardown.new, GUnit::Teardown.new]
71
85
  parent = GUnit::Context.new
72
86
  parent.teardowns = [GUnit::Teardown.new, GUnit::Teardown.new]
87
+ parent.parent = pops
73
88
  teardowns = [GUnit::Teardown.new, GUnit::Teardown.new]
74
89
  @context1.parent = parent
75
90
  @context1.teardowns = teardowns
76
- assert_equal parent.teardowns + teardowns, @context1.all_teardowns
91
+ assert_equal pops.teardowns + parent.teardowns + teardowns, @context1.all_teardowns
77
92
  end
78
93
 
79
94
  # Context.new('my fixtures')
@@ -0,0 +1,100 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ # class MyClassTest < GUnit::TestCase
4
+ # exercise "Some fixtures should be created here"
5
+ #
6
+ # context "An instance of MyClass"
7
+ # setup "a Foo with a name" do
8
+ # @foo = Foo.new("a foo")
9
+ # end
10
+ # exercise "renamed" do
11
+ # @foo.rename("the foo")
12
+ # end
13
+ # end
14
+ # end
15
+
16
+ class GUnit::ExerciseTest < Test::Unit::TestCase
17
+
18
+ def setup
19
+ @exercise1 = GUnit::Exercise.new
20
+ end
21
+
22
+ def test_it_is_a_exercise
23
+ assert @exercise1.is_a?(GUnit::Exercise)
24
+ end
25
+
26
+ def test_has_default_message
27
+ assert_not_nil @exercise1.message
28
+ end
29
+
30
+ def test_message_setter
31
+ message = "Exercise some code"
32
+ @exercise1.message = message
33
+ assert_equal message, @exercise1.message
34
+ end
35
+
36
+ # Exercise.new('some feature')
37
+ def test_initialize_with_one_arg
38
+ message = 'some feature'
39
+ @exercise2 = GUnit::Exercise.new(message)
40
+ assert @exercise2.message === message
41
+ end
42
+
43
+ # Exercise.new('some feature'){ @foo = "bar" }
44
+ def test_initialize_with_one_arg_and_block
45
+ message = 'some feature'
46
+ task = (@foo = "bar")
47
+ @exercise2 = GUnit::Exercise.new(message) { (@foo = "bar") }
48
+ assert message === @exercise2.message
49
+ assert task === @exercise2.task.call
50
+ end
51
+
52
+ def test_run_with_task_called_returns_true
53
+ @exercise1.task = Proc.new { true }
54
+ response = @exercise1.run
55
+ assert response === true
56
+ end
57
+
58
+ def test_run_with_task_called_returns_false
59
+ @exercise1.task = Proc.new { false }
60
+ response = @exercise1.run
61
+ assert response === true
62
+ end
63
+
64
+ def test_run_with_task_is_false
65
+ @exercise1.task = false
66
+ response = @exercise1.run
67
+ assert response === true
68
+ end
69
+
70
+ def test_run_with_task_is_nil
71
+ @exercise1.task = nil
72
+ response = @exercise1.run
73
+ assert response === true
74
+ end
75
+
76
+ def test_run_with_assertion_failure_exception
77
+ @exercise1.task = lambda { raise GUnit::AssertionFailure }
78
+ response = @exercise1.run
79
+ assert response.is_a?(GUnit::TestResponse)
80
+ assert response.is_a?(GUnit::FailResponse)
81
+ end
82
+
83
+ def test_run_with_random_exception
84
+ @exercise1.task = lambda { raise 'Boom' }
85
+ response = @exercise1.run
86
+ assert response.is_a?(GUnit::TestResponse)
87
+ assert response.is_a?(GUnit::ExceptionResponse)
88
+ end
89
+
90
+ def test_run_with_binding
91
+ obj = Object.new
92
+ obj.instance_variable_set("@foo", "bar")
93
+ @exercise1.task = Proc.new { instance_variable_set("@foo", "zip") }
94
+ @exercise1.run
95
+ assert_equal "bar", obj.instance_variable_get("@foo")
96
+ @exercise1.run(obj)
97
+ assert_equal "zip", obj.instance_variable_get("@foo")
98
+ end
99
+
100
+ end
@@ -97,6 +97,15 @@ class GUnit::TestCaseTest < Test::Unit::TestCase
97
97
  assert_equal "bar", @my_test_case1.instance_variable_get("@foo")
98
98
  end
99
99
 
100
+ def test_run_runs_exercise
101
+ MyClassTest.setup { @foo = "abc" }
102
+ MyClassTest.exercise { @foo.replace "bar" }
103
+ method_name = "test_one"
104
+ @my_test_case1 = MyClassTest.new(method_name)
105
+ @my_test_case1.run
106
+ assert_equal "bar", @my_test_case1.instance_variable_get("@foo")
107
+ end
108
+
100
109
  def test_run_runs_teardowns
101
110
  @my_test_case2 = MyClassTest.new
102
111
  MyClassTest.setup { @foo = "bar" }
@@ -166,16 +175,19 @@ class GUnit::TestCaseTest < Test::Unit::TestCase
166
175
  def test_setup_and_teardown_inside_context_creates_context_with_setup_and_teardown
167
176
  context_msg = "In some context"
168
177
  setup_msg = "Given this"
178
+ exercise_msg = "Doing this"
169
179
  teardown_msg = "Housekeeping"
170
180
  verify_msg = "This should be the case B"
171
181
  MyClassTest.context(context_msg) do
172
182
  setup(setup_msg) { @foo = "abc" }
183
+ exercise(exercise_msg) { @foo.replace "xyz" }
173
184
  teardown(teardown_msg) { @foo = "def" }
174
185
  verify(verify_msg) { true }
175
186
  end
176
187
  method_name = MyClassTest.message_to_test_method_name(verify_msg)
177
188
  context = MyClassTest.context_for_method(method_name)
178
189
  assert_equal setup_msg, context.setups.last.message
190
+ assert_equal exercise_msg, context.exercise.message
179
191
  assert_equal teardown_msg, context.teardowns.last.message
180
192
  end
181
193
 
@@ -198,4 +210,72 @@ class GUnit::TestCaseTest < Test::Unit::TestCase
198
210
  assert context.is_a?(GUnit::Context)
199
211
  end
200
212
 
213
+ def test_run_runs_contexts_setups
214
+ setup = GUnit::Setup.new { @foo = @foo.nil? ? 1 : @foo+1 }
215
+ pops = GUnit::Context.new
216
+ pops.setups = [setup, setup]
217
+ parent = GUnit::Context.new
218
+ parent.setups = [setup, setup]
219
+ parent.parent = pops
220
+ context1 = GUnit::Context.new
221
+ context1.setups = [setup, setup]
222
+ context1.parent = parent
223
+ method_name = "test_one"
224
+ @my_test_case1 = MyClassTest.new(method_name)
225
+ @my_test_case1.stubs(:context).returns(context1)
226
+ @my_test_case1.run
227
+ assert_equal 6, @my_test_case1.instance_variable_get("@foo")
228
+ end
229
+
230
+ def test_run_runs_context_exercise_only
231
+ exercise = GUnit::Exercise.new { @foo = @foo.nil? ? 1 : @foo+1 }
232
+ pops = GUnit::Context.new
233
+ pops.exercise = exercise
234
+ parent = GUnit::Context.new
235
+ parent.exercise = exercise
236
+ parent.parent = pops
237
+ context1 = GUnit::Context.new
238
+ context1.exercise = exercise
239
+ context1.parent = parent
240
+ method_name = "test_one"
241
+ @my_test_case1 = MyClassTest.new(method_name)
242
+ @my_test_case1.stubs(:context).returns(context1)
243
+ @my_test_case1.run
244
+ assert_equal 1, @my_test_case1.instance_variable_get("@foo")
245
+ end
246
+
247
+ def test_run_runs_contexts_teardowns
248
+ teardown = GUnit::Teardown.new { @foo = @foo.nil? ? 1 : @foo+1 }
249
+ pops = GUnit::Context.new
250
+ pops.teardowns = [teardown, teardown]
251
+ parent = GUnit::Context.new
252
+ parent.teardowns = [teardown, teardown]
253
+ parent.parent = pops
254
+ context1 = GUnit::Context.new
255
+ context1.teardowns = [teardown, teardown]
256
+ context1.parent = parent
257
+ method_name = "test_one"
258
+ @my_test_case1 = MyClassTest.new(method_name)
259
+ @my_test_case1.stubs(:context).returns(context1)
260
+ @my_test_case1.run
261
+ assert_equal 6, @my_test_case1.instance_variable_get("@foo")
262
+ end
263
+
264
+ def test_run_runs_contexts_teardowns_in_reverse_order
265
+ teardown = GUnit::Teardown.new { @foo = @foo.nil? ? 1 : @foo+1 }
266
+ last_teardown = GUnit::Teardown.new { @foo = -1 }
267
+ pops = GUnit::Context.new
268
+ pops.teardowns = [last_teardown, teardown]
269
+ parent = GUnit::Context.new
270
+ parent.teardowns = [teardown, teardown]
271
+ parent.parent = pops
272
+ context1 = GUnit::Context.new
273
+ context1.teardowns = [teardown, teardown]
274
+ context1.parent = parent
275
+ method_name = "test_one"
276
+ @my_test_case1 = MyClassTest.new(method_name)
277
+ @my_test_case1.stubs(:context).returns(context1)
278
+ @my_test_case1.run
279
+ assert_equal -1, @my_test_case1.instance_variable_get("@foo")
280
+ end
201
281
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: GUnit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Sterndale
@@ -50,6 +50,7 @@ files:
50
50
  - test/test_helper.rb
51
51
  - test/unit/assertions_test.rb
52
52
  - test/unit/context_test.rb
53
+ - test/unit/exercise_test.rb
53
54
  - test/unit/fail_response_test.rb
54
55
  - test/unit/proc_extensions_test.rb
55
56
  - test/unit/setup_test.rb
@@ -91,6 +92,7 @@ test_files:
91
92
  - test/test_helper.rb
92
93
  - test/unit/assertions_test.rb
93
94
  - test/unit/context_test.rb
95
+ - test/unit/exercise_test.rb
94
96
  - test/unit/fail_response_test.rb
95
97
  - test/unit/proc_extensions_test.rb
96
98
  - test/unit/setup_test.rb