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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/minitest/suite_callbacks.rb +63 -41
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a8179e88e90edd230281e82dd434e3d4408b3c3
4
- data.tar.gz: b272d88b7a19a3d7371660dec2e77e71570186be
3
+ metadata.gz: 029402ce9007c610b3b062614b6637f57813ddc0
4
+ data.tar.gz: 9401049b7d036592f8ea7cd98dbc9648a4c6bf36
5
5
  SHA512:
6
- metadata.gz: 325840ce50324be88cc50596d8b0b7d8e0cea15547295c049711d9415f68ec72e7a8daff86d08be33d3652d3cc9b9368b6061d979af8af4c3d22136feb4bf5e6
7
- data.tar.gz: 66e13b0d486a2b5a6c974c2925a7e2a701c81c61c07b9e741a593c37361f06fbd168bb5235507a2fd300277b46d5660f1c0c7da6cff4efd358342ad682e83036
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.class_eval do
8
- class << self
9
- attr_reader :sandbox
7
+ class << base
8
+ attr_reader :sandbox
9
+ attr_accessor :counter
10
10
 
11
- # DSL method
12
- def before_suite(&before_suite_proc)
13
- @before_suite_proc = before_suite_proc
14
- # Setup state before suite
15
- context = self
16
- original_singleton_run = method(:run)
17
- define_singleton_method :run do |*args, &block|
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
- context.transfer_sandbox_state_to(self)
26
- original_instance_run.bind(self).call(*args, &block)
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
- def after_suite(&after_suite_proc)
31
- context = self
32
- original_singleton_run = method(:run)
33
- define_singleton_method :run do |*args, &block|
34
- original_singleton_run.call(*args, &block)
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
- def setup_before_suite
40
- @sandbox = new('sandbox')
41
- @sandbox.instance_eval &@before_suite_proc
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
- def transfer_sandbox_state_to(dest_context)
45
- # TODO: generate these
46
- ivs_to_skip = [:@NAME, :@assertions, :@failures]
47
- iv_map = {}
48
- @sandbox.instance_variables.each do |iv|
49
- next if ivs_to_skip.include?(iv)
50
- iv_map[iv] = @sandbox.instance_variable_get(iv.to_sym)
51
- end
52
-
53
- dest_context.instance_eval do
54
- iv_map.each do |k, v|
55
- instance_variable_set k, v
56
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-suite_callbacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - backupify