Class: Similatron::Comparison

Inherits:
Object
  • Object
show all
Defined in:
lib/similatron/comparison.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected:, actual:, score:, diff: nil, overwrite: nil) ⇒ Comparison

Returns a new instance of Comparison.



6
7
8
9
10
11
12
# File 'lib/similatron/comparison.rb', line 6

def initialize(expected:, actual:, score:, diff: nil, overwrite: nil)
  @expected = expected
  @actual = actual
  @diff = diff
  @score = score
  @overwrite = overwrite
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



4
5
6
# File 'lib/similatron/comparison.rb', line 4

def actual
  @actual
end

#diffObject (readonly)

Returns the value of attribute diff.



4
5
6
# File 'lib/similatron/comparison.rb', line 4

def diff
  @diff
end

#expectedObject (readonly)

Returns the value of attribute expected.



4
5
6
# File 'lib/similatron/comparison.rb', line 4

def expected
  @expected
end

#scoreObject (readonly)

Returns the value of attribute score.



4
5
6
# File 'lib/similatron/comparison.rb', line 4

def score
  @score
end

Instance Method Details

#as_jsonObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/similatron/comparison.rb', line 34

def as_json
  {
    expected: expected,
    actual: actual,
    diff: diff,
    score: score,
    same: !!same?, # rubocop:disable Style/DoubleNegation
    overwrite: !!overwrite? # rubocop:disable Style/DoubleNegation
  }
end

#overwrite?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/similatron/comparison.rb', line 18

def overwrite?
  @overwrite
end

#raise_when_differentObject

Raises:

  • (StandardError)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/similatron/comparison.rb', line 22

def raise_when_different
  return if same?

  message_parts = [
    "Found #{actual} different from #{expected}\n",
    "Score: #{score}"
  ]
  message_parts << "\nDiff in #{diff}" if diff

  raise StandardError, message_parts.join
end

#same?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/similatron/comparison.rb', line 14

def same?
  @score.zero?
end