minitest-suite_callbacks 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/minitest/suite_callbacks.rb +63 -41
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 029402ce9007c610b3b062614b6637f57813ddc0
|
4
|
+
data.tar.gz: 9401049b7d036592f8ea7cd98dbc9648a4c6bf36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32a11cc44a5243542c91cb0772ccd352202ade4fdab6ade4be11c2d3553f4267898e2ab22ebd22161e69aa223682007fcd84398d9f68631069be510d8e00b32c
|
7
|
+
data.tar.gz: 4768466c103aa42148daefd2d1247ae59d341cfb959a058a0090d534be408f2f4585374f3db1a8117246c454cec075c8f79229b78ff4bac703953d89c4aa8e55
|
@@ -4,56 +4,78 @@
|
|
4
4
|
# It just doesn't work at all with contexts & suite-wide setup
|
5
5
|
module Minitest::SuiteCallbacks
|
6
6
|
def self.extended(base)
|
7
|
-
base
|
8
|
-
|
9
|
-
|
7
|
+
class << base
|
8
|
+
attr_reader :sandbox
|
9
|
+
attr_accessor :counter
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
original_singleton_run =
|
17
|
-
|
18
|
-
context.setup_before_suite
|
19
|
-
original_singleton_run.call(*args, &block)
|
20
|
-
end
|
21
|
-
|
22
|
-
# TODO what if we call before_suite multiple times?
|
23
|
-
original_instance_run = instance_method(:run)
|
11
|
+
# Define before_suite on the test class context
|
12
|
+
def before_suite(&before_suite_proc)
|
13
|
+
# Setup state before suite
|
14
|
+
context = self
|
15
|
+
singleton_class.class_eval do
|
16
|
+
original_singleton_run = instance_method(:run)
|
17
|
+
# Wrap the run method on the singleton class of the test class
|
24
18
|
define_method :run do |*args, &block|
|
25
|
-
|
26
|
-
|
19
|
+
# TODO this is kind of hacky; it only runs before_suite once
|
20
|
+
# I don't think this will support other use cases (e.g. multiple before_suites/nested before_suites)
|
21
|
+
context.instance_eval do
|
22
|
+
if @sandbox.nil?
|
23
|
+
@sandbox = new('sandbox')
|
24
|
+
@sandbox.instance_eval &before_suite_proc
|
25
|
+
end
|
26
|
+
end
|
27
|
+
original_singleton_run.bind(self).call(*args, &block)
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
context.sandbox.instance_eval &after_suite_proc
|
36
|
-
end
|
31
|
+
# TODO what if we call before_suite multiple times?
|
32
|
+
original_instance_run = instance_method(:run)
|
33
|
+
define_method :run do |*args, &block|
|
34
|
+
context.transfer_sandbox_state_to(self)
|
35
|
+
original_instance_run.bind(self).call(*args, &block)
|
37
36
|
end
|
37
|
+
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
# TODO: because of how Minitest separates its suites, this method gets called after EACH suite,
|
40
|
+
# not after all related suites are finished
|
41
|
+
def after_suite(&after_suite_proc)
|
42
|
+
@counter = 0
|
43
|
+
context = self
|
44
|
+
singleton_class.class_eval do
|
45
|
+
original_singleton_run = instance_method(:run)
|
46
|
+
define_method :run do |*args, &block|
|
47
|
+
original_singleton_run.bind(self).call(*args, &block)
|
48
|
+
context.instance_eval do
|
49
|
+
@counter += 1
|
50
|
+
if has_run_all_related_suites?
|
51
|
+
@sandbox.instance_eval &after_suite_proc
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
42
55
|
end
|
56
|
+
end
|
43
57
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
58
|
+
def has_run_all_related_suites?
|
59
|
+
# Get the number of subclasses + the class itself
|
60
|
+
@counter == descendant_count + 1
|
61
|
+
end
|
62
|
+
|
63
|
+
def descendant_count
|
64
|
+
ObjectSpace.each_object(::Class).select{|k| k < self}.size
|
65
|
+
end
|
66
|
+
|
67
|
+
def transfer_sandbox_state_to(dest_context)
|
68
|
+
# TODO: generate these
|
69
|
+
ivs_to_skip = [:@NAME, :@assertions, :@failures]
|
70
|
+
iv_map = {}
|
71
|
+
@sandbox.instance_variables.each do |iv|
|
72
|
+
next if ivs_to_skip.include?(iv)
|
73
|
+
iv_map[iv] = @sandbox.instance_variable_get(iv.to_sym)
|
74
|
+
end
|
75
|
+
|
76
|
+
dest_context.instance_eval do
|
77
|
+
iv_map.each do |k, v|
|
78
|
+
instance_variable_set k, v
|
57
79
|
end
|
58
80
|
end
|
59
81
|
end
|