Class: TestProf::BeforeAll::Minitest::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/test_prof/recipes/minitest/before_all.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(setup_fixtures: false, parent: nil, &block) ⇒ Executor

Returns a new instance of Executor.



38
39
40
41
42
43
44
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 38

def initialize(setup_fixtures: false, parent: nil, &block)
  @parent = parent
  # Fixtures must be instantiated if any of the executors needs them
  @setup_fixtures = setup_fixtures || parent&.setup_fixtures
  @block = block
  @captured_ivars = []
end

Instance Attribute Details

#active ⇒ Object (readonly) Also known as: active?

Returns the value of attribute active.



32
33
34
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 32

def active
  @active
end

#block ⇒ Object (readonly)

Returns the value of attribute block.



32
33
34
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 32

def block
  @block
end

#captured_ivars ⇒ Object (readonly)

Returns the value of attribute captured_ivars.



32
33
34
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 32

def captured_ivars
  @captured_ivars
end

#current_test_object ⇒ Object (readonly)

Returns the value of attribute current_test_object.



32
33
34
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 32

def current_test_object
  @current_test_object
end

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



32
33
34
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 32

def parent
  @parent
end

#setup_fixtures ⇒ Object (readonly) Also known as: setup_fixtures?

Returns the value of attribute setup_fixtures.



32
33
34
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 32

def setup_fixtures
  @setup_fixtures
end

#teardown_block ⇒ Object (readonly)

Returns the value of attribute teardown_block.



32
33
34
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 32

def teardown_block
  @teardown_block
end

Instance Method Details

#activate!(test_object) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 50

def activate!(test_object)
  @current_test_object = test_object

  return restore_ivars(test_object) if active?

  @active = true

  BeforeAll.setup_fixtures(test_object) if setup_fixtures?
  BeforeAll.begin_transaction do
    capture!(test_object)
  end
end

#capture!(test_object) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 74

def capture!(test_object)
  before_ivars = test_object.instance_variables

  perform_setup(test_object)

  (test_object.instance_variables - before_ivars).each do |ivar|
    captured_ivars << [ivar, test_object.instance_variable_get(ivar)]
  end
end

#deactivate! ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 63

def deactivate!
  return unless active

  @active = false

  perform_teardown(current_test_object)

  @current_test_object = nil
  BeforeAll.rollback_transaction
end

#perform_setup(test_object) ⇒ Object



93
94
95
96
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 93

def perform_setup(test_object)
  parent&.perform_setup(test_object)
  test_object.instance_eval(&block) if block
end

#perform_teardown(test_object) ⇒ Object



98
99
100
101
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 98

def perform_teardown(test_object)
  current_test_object&.instance_eval(&teardown_block) if teardown_block
  parent&.perform_teardown(test_object)
end

#restore_ivars(test_object) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 84

def restore_ivars(test_object)
  captured_ivars.each do |(ivar, val)|
    test_object.instance_variable_set(
      ivar,
      val
    )
  end
end

#teardown(&block) ⇒ Object



46
47
48
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 46

def teardown(&block)
  @teardown_block = block
end