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 +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/rspec/json_matcher/abstract_comparer.rb +19 -7
- data/lib/rspec/json_matcher/abstract_matcher.rb +9 -4
- data/lib/rspec/json_matcher/exact_matcher.rb +2 -2
- data/lib/rspec/json_matcher/fuzzy_matcher.rb +2 -2
- data/lib/rspec/json_matcher/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f505a037e0c0116b2d25d3fddb4e3f0b1e670c2
|
4
|
+
data.tar.gz: 83051af724876596f2fb4073f256c0b1bf66c8fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f55d366344aa0a69c356417bf155aa2b9af6eb9b07b0f4602f8f65116f6ea4feaa69fd8c86e401236d6f3c32a2e068ed584ce8fd17b69323338cdc9702c5ed7
|
7
|
+
data.tar.gz: 81abea6d7b76bb531e16139e4a2c3fee26d290f278b2c65cd9bcf99cdd0bcf6bb415de5fc5d139afd74d6e95f2355d907a67f8e96dc70f4cfffb3018ceccd6fa
|
data/CHANGELOG.md
CHANGED
@@ -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
|
-
|
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)
|
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
|
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
|
+
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-
|
11
|
+
date: 2014-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|