Class: Must::Differ
- Inherits:
-
Object
- Object
- Must::Differ
- Defined in:
- lib/must/differ.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
readonly
Returns the value of attribute a.
-
#b ⇒ Object
readonly
Returns the value of attribute b.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #eq_float?(a, b) ⇒ Boolean
- #eq_object?(a, b) ⇒ Boolean
- #execute ⇒ Object
- #execute! ⇒ Object
- #failed(klass, msg) ⇒ Object
-
#initialize(a, b, path = nil) ⇒ Differ
constructor
A new instance of Differ.
- #message ⇒ Object
Constructor Details
#initialize(a, b, path = nil) ⇒ Differ
Returns a new instance of Differ.
5 6 7 8 |
# File 'lib/must/differ.rb', line 5 def initialize(a, b, path = nil) path ||= a.class.name.downcase @a, @b, @path = a, b, path end |
Instance Attribute Details
#a ⇒ Object (readonly)
Returns the value of attribute a.
3 4 5 |
# File 'lib/must/differ.rb', line 3 def a @a end |
#b ⇒ Object (readonly)
Returns the value of attribute b.
3 4 5 |
# File 'lib/must/differ.rb', line 3 def b @b end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/must/differ.rb', line 3 def path @path end |
Instance Method Details
#eq_float?(a, b) ⇒ Boolean
10 11 12 |
# File 'lib/must/differ.rb', line 10 def eq_float?(a, b) (a - b).abs <= 0.0000001 end |
#eq_object?(a, b) ⇒ Boolean
14 15 16 |
# File 'lib/must/differ.rb', line 14 def eq_object?(a, b) a == b end |
#execute ⇒ Object
71 72 73 74 75 76 |
# File 'lib/must/differ.rb', line 71 def execute execute! return true rescue Must::Invalid return false end |
#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 |
#failed(klass, msg) ⇒ Object
85 86 87 |
# File 'lib/must/differ.rb', line 85 def failed(klass, msg) raise klass, msg end |