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, &block) ⇒ Executor

Returns a new instance of Executor.



17
18
19
20
21
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 17

def initialize(setup_fixtures: false, &block)
  @setup_fixtures = setup_fixtures
  @block = block
  @captured_ivars = []
end

Instance Attribute Details

#activeObject (readonly) Also known as: active?

Returns the value of attribute active.



11
12
13
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 11

def active
  @active
end

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 11

def block
  @block
end

#captured_ivarsObject (readonly)

Returns the value of attribute captured_ivars.



11
12
13
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 11

def captured_ivars
  @captured_ivars
end

#current_test_objectObject (readonly)

Returns the value of attribute current_test_object.



11
12
13
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 11

def current_test_object
  @current_test_object
end

#setup_fixturesObject (readonly) Also known as: setup_fixtures?

Returns the value of attribute setup_fixtures.



11
12
13
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 11

def setup_fixtures
  @setup_fixtures
end

#teardown_blockObject (readonly)

Returns the value of attribute teardown_block.



11
12
13
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 11

def teardown_block
  @teardown_block
end

Instance Method Details

#activate!(test_object) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 27

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



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

def capture!(test_object)
  return unless block

  before_ivars = test_object.instance_variables

  test_object.instance_eval(&block)

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

#deactivate!Object



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

def deactivate!
  @active = false

  current_test_object&.instance_eval(&teardown_block) if teardown_block

  @current_test_object = nil
  BeforeAll.rollback_transaction
end

#restore_ivars(test_object) ⇒ Object



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

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

#teardown(&block) ⇒ Object



23
24
25
# File 'lib/test_prof/recipes/minitest/before_all.rb', line 23

def teardown(&block)
  @teardown_block = block
end