Module: TestBench::Fixture::Actuate::Class

Defined in:
lib/test_bench/fixture/actuate/class.rb

Constant Summary collapse

Error =
::Class.new(RuntimeError)

Class Method Summary collapse

Class Method Details

.actuate(fixture, &block_actuator) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/test_bench/fixture/actuate/class.rb', line 23

def self.actuate(fixture, &block_actuator)
  if fixture.respond_to?(:call)
    fixture_actuator = fixture
  end

  if block_actuator && fixture_actuator
    raise Error, "Block argument given to fixture that has an actuator (Fixture Class: #{fixture.class})"
  end

  if block_actuator.nil? && fixture_actuator.nil?
    raise Error, "No actuator"
  end

  if not block_actuator.nil?
    block_actuator.(fixture)
  else
    fixture_actuator.()
  end
end

.assure_fixture(fixture_class) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/test_bench/fixture/actuate/class.rb', line 77

def self.assure_fixture(fixture_class)
  is_class = fixture_class.instance_of?(::Class)

  if not is_class
    raise Error, "Not a class (Class: #{fixture_class})"
  end

  if is_class
    is_fixture_class = fixture_class.included_modules.include?(Fixture)
  end

  if not is_fixture_class
    raise Error, "Not a fixture class (Class: #{fixture_class})"
  end
end

.build_fixture(fixture_class) ⇒ Object



43
44
45
46
47
# File 'lib/test_bench/fixture/actuate/class.rb', line 43

def self.build_fixture(fixture_class, ...)
  constructor = constructor(fixture_class)

  fixture_class.public_send(constructor, ...)
end

.call(fixture_class, session: nil, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/test_bench/fixture/actuate/class.rb', line 7

def self.call(fixture_class, *, session: nil, **, &block)
  if test_block?(fixture_class)
    test_block = block
  else
    block_actuator = block
  end

  fixture = build_fixture(fixture_class, *, **, &test_block)

  Session.configure(fixture, session:, attr_name: :test_session)

  actuate(fixture, &block_actuator)

  fixture
end

.constructor(fixture_class) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/test_bench/fixture/actuate/class.rb', line 67

def self.constructor(fixture_class)
  assure_fixture(fixture_class)

  if fixture_class.respond_to?(:build)
    :build
  else
    :new
  end
end

.test_block?(fixture_class) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/test_bench/fixture/actuate/class.rb', line 49

def self.test_block?(fixture_class)
  assure_fixture(fixture_class)

  constructor = constructor(fixture_class)

  if constructor == :build
    constructor_method = fixture_class.method(:build)
  else
    constructor_method = fixture_class.instance_method(:initialize)
  end

  constructor_parameters = constructor_method.parameters

  constructor_parameters.any? do |(type, _)|
    type == :block
  end
end