jeremymcanally-context 0.5.2 → 0.5.5

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.
@@ -15,6 +15,7 @@ lib/context/test.rb
15
15
  lib/context/version.rb
16
16
  lib/context/shared_behavior.rb
17
17
  lib/context/core_ext/string.rb
18
+ lib/context/core_ext/rails_hacks.rb
18
19
  setup.rb
19
20
  tasks/deployment.rake
20
21
  tasks/environment.rake
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "context"
3
- s.version = "0.5.2"
3
+ s.version = "0.5.5"
4
4
  s.date = "2008-10-03"
5
5
  s.summary = "Contexts and DSL sugar for your tests"
6
6
  s.email = "jeremy@entp.com"
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "lib/context/test.rb",
28
28
  "lib/context/version.rb",
29
29
  "lib/context/core_ext/string.rb",
30
+ "lib/context/core_ext/rails_hacks.rb",
30
31
  "setup.rb"
31
32
  ]
32
33
 
@@ -5,9 +5,15 @@ require 'rubygems'
5
5
  require 'test/unit'
6
6
 
7
7
  require 'context/core_ext/string'
8
+ require 'context/core_ext/rails_hacks'
9
+
8
10
  require 'context/version'
9
11
  require 'context/suite'
10
12
  require 'context/test'
11
13
  require 'context/lifecycle'
12
14
  require 'context/context'
13
- require 'context/shared_behavior'
15
+ require 'context/shared_behavior'
16
+
17
+ class Test::Unit::TestCase
18
+ extend Context::Context
19
+ end
@@ -1,5 +1,5 @@
1
- class Test::Unit::TestCase
2
- class << self
1
+ module Context
2
+ module Context
3
3
  # Test::Unit uses ObjectSpace to figure out what Test::Unit:TestCase instances are running
4
4
  # Contexts are not named and therefore sometimes get garbage collected.
5
5
  # Think of #context_list as the shelter for nameless contexts
@@ -0,0 +1,10 @@
1
+ class Test::Unit::TestCase
2
+ class << self
3
+ def setup_for_shoulda
4
+ self.instance_eval do
5
+ alias :setup :before
6
+ alias :teardown :after
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,6 +1,6 @@
1
1
  class Test::Unit::TestCase
2
2
  class << self
3
- attr_accessor :before_each_callbacks, :before_all_callbacks, :after_each_callbacks, :after_all_callbacks
3
+ attr_accessor :before_each_callbacks, :before_all_callbacks, :after_each_callbacks, :after_all_callbacks, :before_should_callbacks
4
4
 
5
5
  # Add logic to run before the tests (i.e., a +setup+ method)
6
6
  #
@@ -9,6 +9,11 @@ class Test::Unit::TestCase
9
9
  # end
10
10
  #
11
11
  def before(period = :each, &block)
12
+ unless block_given?
13
+ block = period
14
+ period = :each
15
+ end
16
+
12
17
  send("before_#{period}_callbacks") << block
13
18
  end
14
19
 
@@ -21,6 +26,11 @@ class Test::Unit::TestCase
21
26
  # end
22
27
  #
23
28
  def after(period = :each, &block)
29
+ unless block_given?
30
+ block = period
31
+ period = :each
32
+ end
33
+
24
34
  send("after_#{period}_callbacks") << block
25
35
  end
26
36
 
@@ -32,22 +42,28 @@ class Test::Unit::TestCase
32
42
  end
33
43
  end
34
44
 
35
- self.before_all_callbacks = []
36
- self.before_each_callbacks = []
37
- self.after_each_callbacks = []
38
- self.after_all_callbacks = []
45
+ self.before_all_callbacks = []
46
+ self.before_each_callbacks = []
47
+ self.after_each_callbacks = []
48
+ self.after_all_callbacks = []
49
+ self.before_should_callbacks = {}
39
50
 
40
51
  def self.inherited(child) # :nodoc:
41
52
  super
42
- child.before_all_callbacks = []
43
- child.before_each_callbacks = []
44
- child.after_each_callbacks = []
45
- child.after_all_callbacks = []
53
+ child.before_all_callbacks = []
54
+ child.before_each_callbacks = []
55
+ child.after_each_callbacks = []
56
+ child.after_all_callbacks = []
57
+ child.before_should_callbacks = {}
46
58
 
47
59
  child.class_eval do
48
60
  def setup(&block)
49
61
  super
50
62
 
63
+ unless self.class.before_should_callbacks[method_name].nil?
64
+ instance_eval(&self.class.before_should_callbacks[method_name])
65
+ end
66
+
51
67
  run_each_callbacks :before
52
68
  end
53
69
 
@@ -56,11 +72,13 @@ class Test::Unit::TestCase
56
72
 
57
73
  run_each_callbacks :after
58
74
  end
59
- end
75
+ end if self == Test::Unit::TestCase
60
76
  end
61
77
 
62
78
  def run_each_callbacks(callback_type) # :nodoc:
63
- self.class.gather_callbacks(callback_type, :each).each { |c| instance_eval(&c) if c }
79
+ self.class.gather_callbacks(callback_type, :each).each do |c|
80
+ c.is_a?(Proc) ? instance_eval(&c) : send(c)
81
+ end
64
82
  end
65
83
 
66
84
  def run_all_callbacks(callback_type) # :nodoc:
@@ -69,6 +87,12 @@ class Test::Unit::TestCase
69
87
  (instance_variables - previous_ivars).inject({}) do |hash, ivar|
70
88
  hash.update ivar => instance_variable_get(ivar)
71
89
  end
90
+ rescue Object => exception
91
+ raise <<-BANG
92
+ Error running the #{callback_type}(:all) callback for #{name}
93
+ #{exception.class.name}: #{exception.message}
94
+ #{exception.backtrace.join("\n")}
95
+ BANG
72
96
  end
73
97
 
74
98
  def set_values_from_callbacks(values) # :nodoc:
@@ -76,4 +100,4 @@ class Test::Unit::TestCase
76
100
  instance_variable_set name, value
77
101
  end
78
102
  end
79
- end
103
+ end
@@ -7,11 +7,15 @@ class Test::Unit::TestCase
7
7
  # assert_false @user.can?(:delete, @other_user)
8
8
  # end
9
9
  #
10
- def test(name, &block)
11
- test_name = "test_#{((context_name == "" ? context_name : context_name + " ") + name).to_method_name}".to_sym
10
+ def test(name, opts={}, &block)
11
+ test_name = ["test:", context_name, name].reject { |n| n == "" }.join(' ')
12
12
  # puts "running test #{test_name}"
13
13
  defined = instance_method(test_name) rescue false
14
14
  raise "#{test_name} is already defined in #{self}" if defined
15
+
16
+ unless opts[:before].nil?
17
+ before_should_callbacks[test_name] = opts[:before]
18
+ end
15
19
 
16
20
  if block_given?
17
21
  define_method(test_name, &block)
@@ -23,5 +27,11 @@ class Test::Unit::TestCase
23
27
  end
24
28
 
25
29
  %w(it should tests).each {|m| alias_method m, :test}
30
+
31
+ def before_test(name, &block)
32
+ test(name, :before => block) {}
33
+ end
34
+
35
+ %w(before_it before_should before_tests).each {|m| alias_method m, :before_test}
26
36
  end
27
- end
37
+ end
@@ -2,7 +2,7 @@ module Context
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 5
5
- TINY = 2
5
+ TINY = 5
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -2,71 +2,87 @@ require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
3
  class TestLifecycle < Test::Unit::TestCase
4
4
  before do
5
- @inherited_before_each_var = 1
5
+ @inherited_before_each_var ||= 0
6
+ @inherited_before_each_var += 1
6
7
  end
7
8
 
8
9
  before do
9
- @inherited_before_each_var = 2
10
- @inherited_before_each_var_2 = 1
10
+ @inherited_before_each_var ||= 0
11
+ @inherited_before_each_var_2 ||= 0
12
+ @inherited_before_each_var += 2
13
+ @inherited_before_each_var_2 += 1
11
14
  end
12
15
 
13
16
  after do
14
- @inherited_after_each_var = 1
17
+ @inherited_after_each_var ||= 0
18
+ @inherited_after_each_var += 1
15
19
  end
16
20
 
17
21
  before :all do
18
- @inherited_before_all_var = 1
22
+ @inherited_before_all_var ||= 0
23
+ @inherited_before_all_var += 1
19
24
  end
20
25
 
21
26
  after :all do
22
- @inherited_after_all_var = 1
27
+ @inherited_after_all_var ||= 0
28
+ @inherited_after_all_var += 1
23
29
  end
24
30
 
25
31
  sample_test = context "lifecycle" do
26
32
  attr_reader :inherited_before_each_var, :inherited_before_each_var_2, :inherited_after_each_var,
27
- :after_each_var, :inherited_before_all_var, :inherited_after_all_var, :before_all_var, :after_all_var,
33
+ :after_each_var, :inherited_before_all_var, :inherited_after_all_var, :before_all_var, :after_all_var, :ivar,
28
34
  :superclass_before_each_var, :superclass_after_each_var, :superclass_before_all_var, :superclass_after_all_var, :one, :two
29
35
 
30
36
  before do
31
- @inherited_before_each_var = 3
37
+ @inherited_before_each_var ||= 0
38
+ @inherited_before_each_var += 4
32
39
  end
33
40
 
34
41
  after do
35
- @after_each_var = 1
42
+ @after_each_var ||= 0
43
+ @after_each_var += 1
36
44
  end
37
45
 
38
46
  before :all do
39
- @before_all_var = 1
47
+ @before_all_var ||= 0
48
+ @before_all_var += 1
40
49
  end
41
50
 
42
51
  after :all do
43
- @after_all_var = 1
52
+ @after_all_var ||= 0
53
+ @after_all_var += 1
44
54
  end
45
55
 
56
+ after :a_method
57
+
46
58
  test "foo" do
47
59
  end
48
60
  end
49
61
 
50
62
  before do
51
- @superclass_before_each_var = 1
63
+ @superclass_before_each_var ||= 0
64
+ @superclass_before_each_var += 1
52
65
  end
53
66
 
54
67
  after do
55
- @superclass_after_each_var = 1
68
+ @superclass_after_each_var ||= 0
69
+ @superclass_after_each_var += 1
56
70
  end
57
71
 
58
72
  before :all do
59
- @superclass_before_all_var = 1
73
+ @superclass_before_all_var ||= 0
74
+ @superclass_before_all_var += 1
60
75
  end
61
76
 
62
77
  after :all do
63
- @superclass_after_all_var = 1
78
+ @superclass_after_all_var ||= 0
79
+ @superclass_after_all_var += 1
64
80
  end
65
81
 
66
82
  context "With before/after :each blocks" do
67
83
  before do
68
84
  @result = Test::Unit::TestResult.new
69
- @test = sample_test.new("test_lifecycle_foo")
85
+ @test = sample_test.new("test: lifecycle foo")
70
86
  @test.run(@result) { |inherited_after_each_var, v| }
71
87
  end
72
88
 
@@ -75,7 +91,7 @@ class TestLifecycle < Test::Unit::TestCase
75
91
  end
76
92
 
77
93
  it "it runs inherited before callbacks in order" do
78
- assert_equal 3, @test.inherited_before_each_var
94
+ assert_equal 7, @test.inherited_before_each_var
79
95
  end
80
96
 
81
97
  it "it runs before callbacks in order" do
@@ -93,6 +109,10 @@ class TestLifecycle < Test::Unit::TestCase
93
109
  it "it runs after callbacks" do
94
110
  assert_equal 1, @test.after_each_var
95
111
  end
112
+
113
+ it "it runs after callbacks specified with method names, instead of blocks" do
114
+ assert_equal "a method ran", @test.ivar
115
+ end
96
116
  end
97
117
 
98
118
  context "With before/after :all blocks" do
@@ -132,7 +152,7 @@ class TestLifecycle < Test::Unit::TestCase
132
152
  context "With setup/teardown methods" do
133
153
  before do
134
154
  @result = Test::Unit::TestResult.new
135
- @test = sample_test.new("test_lifecycle_foo")
155
+ @test = sample_test.new("test: lifecycle foo")
136
156
 
137
157
  @test.class.setup do
138
158
  @one = 1
@@ -150,7 +170,7 @@ class TestLifecycle < Test::Unit::TestCase
150
170
  end
151
171
 
152
172
  it "runs setup method block and regular callbacks" do
153
- assert_equal 3, @test.inherited_before_each_var
173
+ assert_equal 7, @test.inherited_before_each_var
154
174
  end
155
175
 
156
176
  it "runs teardown method block a la Shoulda" do
@@ -161,4 +181,44 @@ class TestLifecycle < Test::Unit::TestCase
161
181
  assert_equal 1, @test.after_each_var
162
182
  end
163
183
  end
184
+
185
+ context "With the before option" do
186
+ setup do
187
+ @jvar = "override success!"
188
+ end
189
+
190
+ l = lambda { @ivar = "awesome" }
191
+ should "run the lambda", :before => l do
192
+ assert_equal "awesome", @ivar
193
+ end
194
+
195
+ l = lambda { @jvar = "should be overridden" }
196
+ should "run the lambda before the setup", :before => l do
197
+ assert_equal "override success!", @jvar
198
+ end
199
+ end
200
+
201
+ context "Before tests" do
202
+ # omg this is odd
203
+ setup do
204
+ assert_equal "yup, it's set", @ivar
205
+ end
206
+
207
+ before_test "run before the setup block" do
208
+ @ivar = "yup, it's set"
209
+ end
210
+ end
211
+
212
+ context "To be compatible with rails' expectations" do
213
+ setup :a_method
214
+
215
+ it "should accept a symbol for an argument to setup and run that method at setup time" do
216
+ assert_equal "a method ran", @ivar
217
+ end
218
+ end
219
+
220
+ protected
221
+ def a_method
222
+ @ivar = "a method ran"
223
+ end
164
224
  end
@@ -6,12 +6,18 @@ class TestTest < Test::Unit::TestCase
6
6
  assert self.class.respond_to?(method_alias)
7
7
  end
8
8
  end
9
+
10
+ def test_before_test_aliases
11
+ [:before_test, :before_it, :before_should, :before_tests].each do |method_alias|
12
+ assert self.class.respond_to?(method_alias), method_alias.inspect
13
+ end
14
+ end
9
15
 
10
16
  context "A test block" do
11
17
  it "should create a test_xxx method" do
12
18
  self.class.test("should create a test method") { true }
13
19
 
14
- assert self.respond_to?(:test_a_test_block_should_create_a_test_method)
20
+ assert self.respond_to?("test: A test block should create a test method")
15
21
  end
16
22
  end
17
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeremymcanally-context
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy McAnally
@@ -41,6 +41,7 @@ files:
41
41
  - lib/context/shared_behavior.rb
42
42
  - lib/context/test.rb
43
43
  - lib/context/core_ext/string.rb
44
+ - lib/context/core_ext/rails_hacks.rb
44
45
  - setup.rb
45
46
  has_rdoc: true
46
47
  homepage: http://github.com/jeremymcanally/context