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) ⇒ Differ
constructor
A new instance of Differ.
Constructor Details
#initialize(a, b, path) ⇒ Differ
Returns a new instance of Differ.
5 6 7 |
# File 'lib/must/differ.rb', line 5 def initialize(a, b, path) @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
9 10 11 |
# File 'lib/must/differ.rb', line 9 def eq_float?(a, b) (a - b).abs <= 0.0000001 end |
#eq_object?(a, b) ⇒ Boolean
13 14 15 |
# File 'lib/must/differ.rb', line 13 def eq_object?(a, b) a == b end |
#execute ⇒ Object
61 62 63 64 65 66 |
# File 'lib/must/differ.rb', line 61 def execute execute! return true rescue Must::Invalid return false end |
#execute! ⇒ Object
17 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 |
# File 'lib/must/differ.rb', line 17 def execute! unless a.class == b.class failed ClassMismatch, "%s expected [%s], but got [%s]" % [path, a.class, b.class] 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}]").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 failed ValueMismatch, "%s expected %s(%s), but got %s(%s)" % [path, av, a.class, bv, a.class] 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] else raise end end |
#failed(klass, msg) ⇒ Object
68 69 70 |
# File 'lib/must/differ.rb', line 68 def failed(klass, msg) raise klass, msg end |