scope 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/scope.rb +19 -24
- data/scope.gemspec +1 -1
- metadata +4 -4
data/lib/scope.rb
CHANGED
@@ -55,13 +55,13 @@ module Scope
|
|
55
55
|
@context_for_test[test_method_name] = @contexts.last
|
56
56
|
end
|
57
57
|
|
58
|
-
def self.setup(&block) @contexts.last.
|
59
|
-
def self.teardown(&block) @contexts.last.
|
58
|
+
def self.setup(&block) @contexts.last.setup= block end
|
59
|
+
def self.teardown(&block) @contexts.last.teardown = block end
|
60
60
|
|
61
61
|
# setup_once blocks are run just once for a context, and not on a per-test basis. They are useful
|
62
62
|
# for integration tests with costly setup.
|
63
|
-
def self.setup_once(&block) @contexts.last.
|
64
|
-
def self.teardown_once(&block) @contexts.last.
|
63
|
+
def self.setup_once(&block) @contexts.last.setup_once = block end
|
64
|
+
def self.teardown_once(&block) @contexts.last.teardown_once = block end
|
65
65
|
|
66
66
|
# "Focuses" the next test that's defined after this method is called, ensuring that only that test is run.
|
67
67
|
def self.focus
|
@@ -80,7 +80,7 @@ module Scope
|
|
80
80
|
context = self.class.context_for_test[test_name]
|
81
81
|
result = nil
|
82
82
|
# Unit::TestCase's implementation of run() invokes the test method (test_name) with exception handling.
|
83
|
-
context.run_setup_and_teardown(test_name) { result = super }
|
83
|
+
context.run_setup_and_teardown(self, test_name) { result = super }
|
84
84
|
result
|
85
85
|
end
|
86
86
|
end
|
@@ -91,6 +91,7 @@ module Scope
|
|
91
91
|
# We keep both tests and subcontexts in the same array because we need to know what the very last thing
|
92
92
|
# to execute inside of this context is, for the purpose of calling teardown_once at the correct time.
|
93
93
|
attr_accessor :tests_and_subcontexts
|
94
|
+
attr_accessor :setup, :teardown, :setup_once, :teardown_once
|
94
95
|
|
95
96
|
def initialize(name, parent_context = nil)
|
96
97
|
@name = name
|
@@ -100,21 +101,22 @@ module Scope
|
|
100
101
|
|
101
102
|
# Runs the setup work for this context and any parent contexts, yields to the block (which should invoke
|
102
103
|
# the actual test method), and then completes the teardown work.
|
103
|
-
def run_setup_and_teardown(test_name)
|
104
|
-
contexts = ([self] +
|
105
|
-
contexts.each
|
106
|
-
|
104
|
+
def run_setup_and_teardown(test_case_instance, test_name)
|
105
|
+
contexts = ([self] + ancestor_contexts).reverse
|
106
|
+
contexts.each { |context| context.setup_once.call if context.setup_once }
|
107
|
+
# We're using instance_eval so that instance vars set by the block are created on the test_case_instance
|
108
|
+
contexts.each { |context| test_case_instance.instance_eval(&context.setup) if context.setup }
|
107
109
|
yield
|
108
110
|
contexts.reverse!
|
109
|
-
contexts.each(
|
111
|
+
contexts.each { |context| test_case_instance.instance_eval(&context.teardown) if context.teardown }
|
110
112
|
|
111
113
|
# If this is the last context being run in any parent contexts, run their teardown_once blocks.
|
112
114
|
if tests_and_subcontexts.last == test_name
|
113
|
-
|
114
|
-
descendant_context =
|
115
|
-
|
115
|
+
teardown_once.call if teardown_once
|
116
|
+
descendant_context = self
|
117
|
+
ancestor_contexts.each do |ancestor|
|
116
118
|
break unless ancestor.tests_and_subcontexts.last == descendant_context
|
117
|
-
ancestor.teardown_once
|
119
|
+
ancestor.teardown_once.call if ancestor.teardown_once
|
118
120
|
descendant_context = ancestor
|
119
121
|
end
|
120
122
|
end
|
@@ -127,18 +129,11 @@ module Scope
|
|
127
129
|
ancestors
|
128
130
|
end
|
129
131
|
|
130
|
-
def
|
131
|
-
def
|
132
|
-
def add_setup_once(&block) @setup_once = run_only_once(&block) end
|
133
|
-
def add_teardown_once(&block) @teardown_once = run_only_once(&block) end
|
134
|
-
|
135
|
-
def setup() @setup.call if @setup end
|
136
|
-
def teardown() @teardown.call if @teardown end
|
137
|
-
def setup_once() @setup_once.call if @setup_once end
|
138
|
-
def teardown_once() @teardown_once.call if @teardown_once end
|
132
|
+
def teardown_once=(block) @teardown_once = run_only_once(block) end
|
133
|
+
def setup_once=(block) @setup_once = run_only_once(block) end
|
139
134
|
|
140
135
|
private
|
141
|
-
def run_only_once(
|
136
|
+
def run_only_once(block)
|
142
137
|
has_run = false
|
143
138
|
Proc.new { block.call unless has_run; has_run = true }
|
144
139
|
end
|
data/scope.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "scope"
|
3
|
-
s.version = "0.1.
|
3
|
+
s.version = "0.1.2"
|
4
4
|
|
5
5
|
s.required_rubygems_version = Gem::Requirement.new(">=0") if s.respond_to? :required_rubygems_version=
|
6
6
|
s.specification_version = 2 if s.respond_to? :specification_version=
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Phil Crosby
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-23 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|