Module: Maze::Compare

Defined in:
lib/maze/compare.rb

Overview

Routines for conducting comparisons

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Class Method Details

.array(array1, array2, result) ⇒ Object

Compares two arrays for value equality, traversing and comparing each element. Results are written to the given Result object.

Parameters:

  • array1 (Array)

    The first array to compare

  • array2 (Array)

    The second array to compare

  • result (Result)

    The Result to store the results



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/maze/compare.rb', line 95

def array(array1, array2, result)
  unless array1.length == array2.length
    result.equal = false
    result.reasons << "Expected #{array1.length} items in array, received #{array2.length}"
    return
  end

  array1.each_with_index do |obj1, index|
    value(obj1, array2[index], result)
    unless result.equal?
      result.keys << index.to_s
      break
    end
  end
end

.hash(hash1, hash2, result) ⇒ Object

Compares two hashes for value equality, traversing and comparing each key-value pair. Results are written to the given Result object.

Parameters:

  • hash1 (Hash)

    The first hash to compare

  • hash2 (Hash)

    The second hash to compare

  • result (Result)

    The Result to store the results



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/maze/compare.rb', line 117

def hash(hash1, hash2, result)
  unless hash1.keys.length == hash2.keys.length
    result.equal = false
    missing = hash1.keys - hash2.keys
    unexpected = hash2.keys - hash1.keys
    result.reasons << "Missing keys from hash: #{missing.join(',')}" unless missing.empty?
    result.reasons << "Unexpected keys in hash: #{unexpected.join(',')}" unless unexpected.empty?
    return
  end

  hash1.each do |key, value|
    value(value, hash2[key], result)
    unless result.equal?
      result.keys << key
      break
    end
  end
end

.regex_match(template, value) ⇒ Boolean

Matches a string against a regex

Parameters:

  • template (String)

    The regex to test with

  • value (String)

    The value to test

Returns:

  • (Boolean)

    Whether the regex produced a match



154
155
156
157
158
# File 'lib/maze/compare.rb', line 154

def regex_match(template, value)
  regex = template
  regex = "^#{regex}$" unless regex.start_with?('^') || regex.end_with?('$')
  value =~ /#{regex}/
end

.string(template, str2, result) ⇒ Object

Compares two strings, writing the results to the given Result object.

Parameters:

  • template (String)

    The expected string

  • str2 (String)

    The string to compare

  • result (Result)

    The Result to store the results



141
142
143
144
145
146
# File 'lib/maze/compare.rb', line 141

def string(template, str2, result)
  return if template == str2 || regex_match(template, str2)

  result.equal = false
  result.reasons << "'#{str2}' does not match '#{template}'"
end

.value(obj1, obj2, result = nil) ⇒ Result

Compares two objects for value equality, traversing to compare each nested object.

Parameters:

  • obj1 (Any)

    The first object to compare

  • obj2 (Any)

    The second object to compare

  • result (Result|nil) (defaults to: nil)

    Optional. Used for comparing recursively

Returns:

  • (Result)

    The result of comparing the objects



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
87
# File 'lib/maze/compare.rb', line 56

def value(obj1, obj2, result = nil)
  result ||= Result.new
  return result if obj1 == 'IGNORE'

  if obj1 == 'NUMBER'
    if obj2.is_a?(Numeric)
      return result
    else
      result.equal = false
      result.reasons << "A Number was expected, '#{obj2.class} received"
      return result
    end
  end

  unless obj1.class == obj2.class
    result.equal = false
    result.reasons << "Object types differ - expected '#{obj1.class}', received '#{obj2.class}'"
    return result
  end

  case obj1
  when Array
    array(obj1, obj2, result)
  when Hash
    hash(obj1, obj2, result)
  when String
    string(obj1, obj2, result)
  else
    result.reasons << "#{obj1} is not equal to #{obj2}" unless (result.equal = (obj1 == obj2))
  end
  result
end