nest-unit 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/README.textile +58 -0
- data/lib/nest-unit.rb +62 -0
- data/test/nest-unit_test.rb +90 -0
- data/test/test_helper.rb +5 -0
- metadata +56 -0
data/README.textile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
h1. nest-unit
|
2
|
+
|
3
|
+
Simple nested contexts for Test::Unit.
|
4
|
+
|
5
|
+
h3. Usage
|
6
|
+
|
7
|
+
<pre>
|
8
|
+
class FooTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
test "should be true" do
|
11
|
+
assert true
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when there is a thing" do
|
15
|
+
before do
|
16
|
+
@thing = Object.new
|
17
|
+
end
|
18
|
+
|
19
|
+
test "bar-ing" do
|
20
|
+
assert Foo.bar(@thing)
|
21
|
+
end
|
22
|
+
|
23
|
+
test "fizz-ing" do
|
24
|
+
assert Foo.fizz(@thing)
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when there are two things" do
|
28
|
+
before do
|
29
|
+
@another_thing = Object.new
|
30
|
+
end
|
31
|
+
|
32
|
+
test "bar-ing" do
|
33
|
+
assert Foo.bar(@thing, @another_thing)
|
34
|
+
end
|
35
|
+
|
36
|
+
test "fizz-ing" do
|
37
|
+
assert Foo.fizz(@thing, @another_thing)
|
38
|
+
end
|
39
|
+
|
40
|
+
after do
|
41
|
+
Foo.some_more_cleanup!
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
after do
|
46
|
+
Foo.cleanup!
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
</pre>
|
51
|
+
|
52
|
+
h3. Note
|
53
|
+
|
54
|
+
You must use the @test@ helper. Tests defined with the usual
|
55
|
+
@def test_*@ don't work yet. I need to hook up some @method_added@
|
56
|
+
juice or something.
|
57
|
+
|
58
|
+
(c) Copyright 2008 Pat Nakajima, released under MIT License.
|
data/lib/nest-unit.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
class Test::Unit::TestCase
|
4
|
+
def self.test(name, &block)
|
5
|
+
define_method("test_" + name.gsub(/[^\w]/, '_'), &block)
|
6
|
+
end unless respond_to?(:test)
|
7
|
+
|
8
|
+
class Context
|
9
|
+
attr_reader :parent_context
|
10
|
+
|
11
|
+
def initialize(test_case, name, parent_context=nil)
|
12
|
+
@test_case, @name, @parent_context = test_case, name, parent_context
|
13
|
+
end
|
14
|
+
|
15
|
+
def before(&block)
|
16
|
+
block_given? ? (@before = block) : @before
|
17
|
+
end
|
18
|
+
|
19
|
+
def after(&block)
|
20
|
+
block_given? ? (@after = block) : @after
|
21
|
+
end
|
22
|
+
|
23
|
+
def context(name=nil, &block)
|
24
|
+
Context.new(@test_case, name, self).instance_eval(&block)
|
25
|
+
end
|
26
|
+
|
27
|
+
def run_callback(position, kase)
|
28
|
+
parent_context.run_callback(position, kase) if has_parent?
|
29
|
+
|
30
|
+
if callback = send(position)
|
31
|
+
kase.instance_eval(&callback)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def name
|
36
|
+
[parent_name, @name].compact.join('_')
|
37
|
+
end
|
38
|
+
|
39
|
+
def test(test_name, &block)
|
40
|
+
context = self
|
41
|
+
@test_case.test [name, test_name].compact.join('_') do
|
42
|
+
context.run_callback(:before, self)
|
43
|
+
instance_eval(&block)
|
44
|
+
context.run_callback(:after, self)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def parent_name
|
51
|
+
has_parent? ? parent_context.name : nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def has_parent?
|
55
|
+
parent_context
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.context(name=nil, &block)
|
60
|
+
Context.new(self, name).instance_eval(&block)
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w[test_helper])
|
2
|
+
|
3
|
+
class ContextTest < Test::Unit::TestCase
|
4
|
+
attr_reader :callable
|
5
|
+
|
6
|
+
include RR::Adapters::TestUnit
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@callable = Object.new
|
10
|
+
stub(callable).after! { true }
|
11
|
+
stub(callable).call!
|
12
|
+
stub(callable).called? { false }
|
13
|
+
end
|
14
|
+
|
15
|
+
test "has test helper" do
|
16
|
+
assert true
|
17
|
+
end
|
18
|
+
|
19
|
+
test "doesn't have before" do
|
20
|
+
assert !callable.called?
|
21
|
+
end
|
22
|
+
|
23
|
+
context "within a context block" do
|
24
|
+
before do
|
25
|
+
stub(callable).called? { true }
|
26
|
+
end
|
27
|
+
|
28
|
+
test "has before" do
|
29
|
+
assert callable.called?
|
30
|
+
end
|
31
|
+
|
32
|
+
test "has after" do
|
33
|
+
mock(callable).call!
|
34
|
+
end
|
35
|
+
|
36
|
+
after do
|
37
|
+
callable.call!
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test "isn't called outside of context" do
|
42
|
+
assert !callable.called?
|
43
|
+
end
|
44
|
+
|
45
|
+
context "with nested contexts" do
|
46
|
+
before do
|
47
|
+
@call_count = 1
|
48
|
+
end
|
49
|
+
|
50
|
+
test "should perform original before" do
|
51
|
+
assert_equal 1, @call_count
|
52
|
+
end
|
53
|
+
|
54
|
+
test "should before original after" do
|
55
|
+
mock(callable).after!
|
56
|
+
end
|
57
|
+
|
58
|
+
context "within nested context" do
|
59
|
+
before do
|
60
|
+
@call_count += 2
|
61
|
+
end
|
62
|
+
|
63
|
+
test "should perform both original before and nested" do
|
64
|
+
assert_equal 3, @call_count
|
65
|
+
end
|
66
|
+
|
67
|
+
test "should perform both original after and nested" do
|
68
|
+
mock(callable).after!.twice
|
69
|
+
end
|
70
|
+
|
71
|
+
after do
|
72
|
+
callable.after!
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when not using `test` helper" do
|
77
|
+
before do
|
78
|
+
@before_called = callable.before!
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_calls_before_block
|
82
|
+
assert @before_called, "before not called"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
after do
|
87
|
+
callable.after!
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nest-unit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pat Nakajima
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-15 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: patnakajima@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.textile
|
26
|
+
- lib/nest-unit.rb
|
27
|
+
- test/nest-unit_test.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
has_rdoc: false
|
30
|
+
homepage:
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.1
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: The lightest way to add contexts to Test::Unit
|
55
|
+
test_files: []
|
56
|
+
|