rspec-json_matcher 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c118f3bdb6fbfdf772d1ff11708526712b5d9433
4
- data.tar.gz: 593f3029576c3bea31c6fa07c7e43612eb09cb88
3
+ metadata.gz: 5f505a037e0c0116b2d25d3fddb4e3f0b1e670c2
4
+ data.tar.gz: 83051af724876596f2fb4073f256c0b1bf66c8fc
5
5
  SHA512:
6
- metadata.gz: a4f7fd837378811fc74192a3c6142f0dc9f8aba28dbf0b128d7c1e55b9b732463b9add86da78d66db438a0a04a8a4cb726a72b3321a484a8438d9cf6b34b6a06
7
- data.tar.gz: 6b39800a44b9e15771ea98f6e136eb1a31853af7d0dacc5cdf12123f80f0d5baa3a97120602e8b7ef1eff90b328be8cd548faaae4c5f7ac9881de38bb0814073
6
+ metadata.gz: 1f55d366344aa0a69c356417bf155aa2b9af6eb9b07b0f4602f8f65116f6ea4feaa69fd8c86e401236d6f3c32a2e068ed584ce8fd17b69323338cdc9702c5ed7
7
+ data.tar.gz: 81abea6d7b76bb531e16139e4a2c3fee26d290f278b2c65cd9bcf99cdd0bcf6bb415de5fc5d139afd74d6e95f2355d907a67f8e96dc70f4cfffb3018ceccd6fa
@@ -1,3 +1,6 @@
1
+ ## 0.1.5
2
+ * Show reasons in failure case
3
+
1
4
  ## 0.1.4
2
5
  * Support RSpec 3 style negative failure message
3
6
 
@@ -1,10 +1,10 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
3
  class AbstractComparer
4
- attr_reader :actual, :expected
4
+ attr_reader :actual, :expected, :reason
5
5
 
6
- def self.compare(*args)
7
- new(*args).compare
6
+ def self.compare(*args, &reason)
7
+ new(*args, &reason).compare
8
8
  end
9
9
 
10
10
  def self.extract_keys(array_or_hash)
@@ -15,9 +15,10 @@ module RSpec
15
15
  end
16
16
  end
17
17
 
18
- def initialize(actual, expected)
18
+ def initialize(actual, expected, &reason)
19
19
  @actual = actual
20
20
  @expected = expected
21
+ @reason = reason
21
22
  end
22
23
 
23
24
  def compare
@@ -31,7 +32,14 @@ module RSpec
31
32
  end
32
33
 
33
34
  def has_same_keys?
34
- self.class.extract_keys(actual).sort == self.class.extract_keys(expected).sort
35
+ actual_keys = self.class.extract_keys(actual).sort
36
+ expected_keys = self.class.extract_keys(expected).sort
37
+ (actual_keys == expected_keys).tap do |success|
38
+ unless success
39
+ diff_keys = (actual_keys - expected_keys) + (expected_keys - actual_keys)
40
+ reason[diff_keys.ai]
41
+ end
42
+ end
35
43
  end
36
44
 
37
45
  def has_same_value?
@@ -49,11 +57,15 @@ module RSpec
49
57
  def has_same_values?
50
58
  if expected.is_a?(Array)
51
59
  expected.each_index.all? do |index|
52
- index < actual.size && self.class.compare(actual[index], expected[index])
60
+ (index < actual.size && self.class.compare(actual[index], expected[index], &reason)).tap do |success|
61
+ reason["[#{index}]"] unless success
62
+ end
53
63
  end
54
64
  else
55
65
  expected.keys.all? do |key|
56
- actual.has_key?(key.to_s) && self.class.compare(actual[key.to_s], expected[key])
66
+ (actual.has_key?(key.to_s) && self.class.compare(actual[key.to_s], expected[key], &reason)).tap do |success|
67
+ reason[key.to_s] unless success
68
+ end
57
69
  end
58
70
  end
59
71
  end
@@ -1,16 +1,19 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
3
  class AbstractMatcher
4
- attr_reader :expected, :parsed
4
+ attr_reader :expected, :parsed, :reasons
5
5
 
6
6
  def initialize(expected = nil)
7
7
  @expected = expected
8
+ @reasons = []
8
9
  end
9
10
 
10
11
  def matches?(json)
11
12
  @parsed = JSON.parse(json)
12
13
  if has_expectation?
13
- compare
14
+ compare do |reason|
15
+ @reasons << reason
16
+ end
14
17
  else
15
18
  true
16
19
  end
@@ -19,7 +22,7 @@ module RSpec
19
22
  false
20
23
  end
21
24
 
22
- def compare
25
+ def compare(&reason)
23
26
  raise NotImplementedError, "You must implement #{self.class}#compare"
24
27
  end
25
28
 
@@ -55,7 +58,9 @@ module RSpec
55
58
  end
56
59
 
57
60
  def inspection(prefix = nil)
58
- ["expected #{prefix}to match:", expected.ai(indent: -2), "", "actual:", parsed.ai(indent: -2)].join("\n")
61
+ messages = ["expected #{prefix}to match:", expected.ai(indent: -2), "", "actual:", parsed.ai(indent: -2), ""]
62
+ messages.push "reason: #{reasons.reverse.join(".")}" unless reasons.empty?
63
+ messages.join("\n")
59
64
  end
60
65
  end
61
66
  end
@@ -1,8 +1,8 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
3
  class ExactMatcher < AbstractMatcher
4
- def compare
5
- ExactComparer.compare(parsed, expected)
4
+ def compare(&reason)
5
+ ExactComparer.compare(parsed, expected, &reason)
6
6
  end
7
7
  end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
3
  class FuzzyMatcher < AbstractMatcher
4
- def compare
5
- FuzzyComparer.compare(parsed, expected)
4
+ def compare(&reason)
5
+ FuzzyComparer.compare(parsed, expected, &reason)
6
6
  end
7
7
  end
8
8
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-json_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-26 00:00:00.000000000 Z
11
+ date: 2014-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json