Method: Must::Differ#execute!

Defined in:
lib/must/differ.rb

#execute!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/must/differ.rb', line 18

def execute!
  unless a.class == b.class
    if a == nil or b == nil
      failed ValueMismatch, "%s expected %s, but got %s" % [path, a.class, b.class]
    else
      failed ClassMismatch, "%s expected %s, but got %s" % [path, a.class, b.class]
    end
  end

  if a.is_a?(Array)
    max = [a.size, b.size].max
    max.times do |i|
      Differ.new(a[i], b[i], "#{path}[#{i}]").execute!
    end
    return true
  end

  if a.is_a?(Hash)
    (a.keys | b.keys).each do |key|
      Differ.new(a[key], b[key], "#{path}[#{key.inspect}]").execute!
    end
    return true
  end

  eq = 
    case a
    when Float; eq_float?(a, b)
    else      ; eq_object?(a, b)
    end

  unless eq
    av = a.inspect.split(//)[0..50].join
    bv = b.inspect.split(//)[0..50].join
    if a.class == b.class
      failed ValueMismatch, "%s expected %s, but got %s" % [path, av, bv]
    else
      failed ValueMismatch, "%s expected %s(%s), but got %s(%s)" % [path, av, a.class, bv, a.class]
    end
  end

  return true

rescue Must::ClassMismatch => err
  if a.class == b.class
#        as = Must::StructInfo.new(a).inspect
#        bs = Must::StructInfo.new(b).inspect
#        raise Must::StructMismatch, "%s expected %s, but got %s" % [path, as, bs]
    raise Must::StructMismatch, err.to_s
  else
    raise
  end
end