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.



33
34
35
36
37
38
39
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 33

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

#activeObject (readonly) Also known as: active?

Returns the value of attribute active.



27
28
29
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

def active
  @active
end

#blockObject (readonly)

Returns the value of attribute block.



27
28
29
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

def block
  @block
end

#captured_ivarsObject (readonly)

Returns the value of attribute captured_ivars.



27
28
29
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

def captured_ivars
  @captured_ivars
end

#current_test_objectObject (readonly)

Returns the value of attribute current_test_object.



27
28
29
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

def current_test_object
  @current_test_object
end

#parentObject (readonly)

Returns the value of attribute parent.



27
28
29
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

def parent
  @parent
end

#setup_fixturesObject (readonly) Also known as: setup_fixtures?

Returns the value of attribute setup_fixtures.



27
28
29
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

def setup_fixtures
  @setup_fixtures
end

#teardown_blockObject (readonly)

Returns the value of attribute teardown_block.



27
28
29
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

def teardown_block
  @teardown_block
end

Instance Method Details

#activate!(test_object) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 45

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



69
70
71
72
73
74
75
76
77
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 69

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 58

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



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

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

#perform_teardown(test_object) ⇒ Object



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

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



79
80
81
82
83
84
85
86
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 79

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

#teardown(&block) ⇒ Object



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

def teardown(&block)
  @teardown_block = block
end