Class: Thoreau::Model::TestCase

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/thoreau/model/test_case.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, logger, #logger

Constructor Details

#initialize(family_desc:, input:, action_block:, expectation:, asserts:, negativo:) ⇒ TestCase

Returns a new instance of TestCase.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/thoreau/model/test_case.rb', line 16

def initialize family_desc:,
               input:,
               action_block:,
               expectation:,
               asserts:,
               negativo:

  @family_desc  = family_desc
  @input        = input
  @action_block = action_block
  @negativo     = negativo

  @expectation = expectation

  @assert_proc = asserts

  @ran = false
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



13
14
15
# File 'lib/thoreau/model/test_case.rb', line 13

def actual
  @actual
end

#expectationObject

Returns the value of attribute expectation.



14
15
16
# File 'lib/thoreau/model/test_case.rb', line 14

def expectation
  @expectation
end

#family_descObject (readonly)

Returns the value of attribute family_desc.



13
14
15
# File 'lib/thoreau/model/test_case.rb', line 13

def family_desc
  @family_desc
end

#inputObject (readonly)

Returns the value of attribute input.



13
14
15
# File 'lib/thoreau/model/test_case.rb', line 13

def input
  @input
end

Instance Method Details

#descObject



39
40
41
# File 'lib/thoreau/model/test_case.rb', line 39

def desc
  "#{@family_desc} #{(@input == {} ? nil : @input.sort.to_h) || @expectation.exception || "(no args)"}"
end

#failed?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/thoreau/model/test_case.rb', line 104

def failed?
  !ok?
end

#failure_expected?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/thoreau/model/test_case.rb', line 35

def failure_expected?
  @negativo
end

#ok?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/thoreau/model/test_case.rb', line 100

def ok?
  problem.nil?
end

#problemObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/thoreau/model/test_case.rb', line 88

def problem
  if failure_expected?
    if result_analysis.nil?
      "Failure expected but didn't. Is this implemented already?"
    else
      nil
    end
  else
    result_analysis
  end
end

#result_analysisObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/thoreau/model/test_case.rb', line 43

def result_analysis

  run unless @ran

  if @expectation.exception

    logger.debug " -> Expected Exception #{@expectation.exception} @actual.exception:#{@actual.exception}"

    if @expectation.exception.is_a?(Class) &&
      @actual.exception.class == @expectation.exception
      nil
    elsif @actual.exception.to_s == @expectation.exception.to_s
      nil
    elsif @actual.exception.nil?
      "Expected exception, but none raised"
    elsif @actual.exception.is_a?(NameError)
      "Did you forget to define an input? Error: #{@actual.exception}"
    else
      "Expected '#{@expectation.exception}' exception, but raised '#{@actual.exception}' (#{@actual.exception.class.name})"
    end

  elsif @assert_proc

    logger.debug " -> Assert Proc result=#{@assert_result}"

    if @actual.exception.nil?
      @assert_result ? nil : "Assertion failed. (got '#{@assert_result}', result='#{@actual.output}')"
    else
      "Expected assertion, but raised exception '#{@actual.exception}'"
    end

  else

    logger.debug " -> Result expected: result=#{@actual.output} expected_output: #{@expectation.output} @actual.exception:#{@actual.exception}"

    if @actual.exception
      "Expected output, but raised exception '#{@actual.exception}'"
    elsif @expectation.output != @actual.output
      "Expected '#{@expectation.output}', but got '#{@actual.output}'"
    else
      nil
    end
  end
end

#runObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/thoreau/model/test_case.rb', line 108

def run
  logger.debug "## RUN #{desc}"
  context_builder = Case::ContextBuilder.new(input: @input)
  context         = context_builder.create_context
  begin
    # Only capture exceptions around the subject itself.
    output  = context.instance_exec(&(@action_block))
    @actual = Model::Outcome.new output: output
  rescue Exception => e
    logger.debug("** Exception: #{e.class.name} #{e}")
    logger.debug("Available local variables: #{@input.keys.empty? ? '(none)' : @input.keys.to_sentence}") if e.is_a? NameError
    @actual = Model::Outcome.new exception: e
    return
  ensure
    @ran = true
  end

  @expectation.evaluate(@actual.output, context) unless @expectation == :use_legacy_snapshot
  @assert_result = context.instance_exec(@actual.output, &(@assert_proc)) if @assert_proc
end