match_json 0.0.4 → 0.0.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: 57bf78ba08a2206400b474522a4e5945ec306e9b
4
- data.tar.gz: aa7e17daa7bab8fc1fd61e51fa694b67c3714ed2
3
+ metadata.gz: 85aee492f202a8aa740ce50b390990f23c896950
4
+ data.tar.gz: 2d60002f2d1993bbbb51aa85e2b62d1d73aa149d
5
5
  SHA512:
6
- metadata.gz: d759631412ea874063e35261a050b3652a0898b28c372a1f4669d266b852bbdca589384a87c5a66a8813b6304707a47a37aa40f09a773a0879af169748adaa96
7
- data.tar.gz: 6c7fb7d11597b029a64b42e15b474b6281acb9c7e33b990bfe771f5ef45c146894ffe1cd3f9778f81672dfffe51569c5eee9bbcedb46cfbaeea984c0d658c4dc
6
+ metadata.gz: 8e4c6e88d0c4a29f1dc0c19e5b121999a2bdf4538f70abe2ba43b0edfca6f69a45c1454501cfbd0ad5202269e9c5d519125b61d4310004c581130e355b3abb37
7
+ data.tar.gz: 7b4ba9b8fa5ea69078cecf230264f10faa463c52bf348ac15c1d3a16065e22f2e65eab96bb23ace6ad7429ce7ad248bb7dc1f98581fa80e9d3488060e088f8ab
@@ -0,0 +1,2 @@
1
+ ### 0.0.5
2
+ * allow to use pattern for non-string values (by @marocchino)
data/README.md CHANGED
@@ -68,6 +68,8 @@ and then use it in your spec:
68
68
  ```ruby
69
69
  it 'uses patten to check value' do
70
70
  expect(%Q({"one": "123456"})).to include_json(%Q({"one": "{id}"}))
71
+ # you can do even this:
72
+ expect(%Q({"one": 123456})).to include_json(%Q({"one": {id}}))
71
73
  end
72
74
  ```
73
75
 
@@ -11,13 +11,13 @@ module MatchJson
11
11
  }
12
12
 
13
13
  def initialize(expected_json)
14
- @expected_json = JSON.parse(expected_json)
14
+ @expected_json = JSON.parse(expected_json.gsub(/(?<!")(\{\w+\})(?!")/, '"\1:non-string"'))
15
15
  end
16
16
 
17
17
  def matches?(actual_json)
18
18
  @actual_json = actual_json.respond_to?(:body) ? JSON.parse(actual_json.body) : JSON.parse(actual_json)
19
19
 
20
- match = catch(:match) { json_included?(@actual_json, @expected_json) }
20
+ catch(:match) { json_included?(@actual_json, @expected_json) }
21
21
  end
22
22
 
23
23
  def failure_message
@@ -55,10 +55,10 @@ module MatchJson
55
55
 
56
56
  def array_included?(actual, expected, nested, raise_error)
57
57
  expected.each do |value|
58
- if (!actual.any? { |actual_value| equal_value?(actual_value, value, nested, false) })
58
+ if actual.nil? || (!actual.any? { |actual_value| equal_value?(actual_value, value, nested, false) })
59
59
  @failure_message = %Q("#{value}" was not found in\n )
60
60
  @failure_message << %Q("#{nested}"=>) if !nested.empty?
61
- @failure_message << "#{actual}"
61
+ @failure_message << "#{actual.nil? ? "nil" : actual}"
62
62
 
63
63
  if raise_error
64
64
  throw(:match, false)
@@ -85,7 +85,9 @@ module MatchJson
85
85
  reg_exp = value.match(/{re:(.*)}/)[1]
86
86
  actual =~ Regexp.new(reg_exp)
87
87
  when pattern?(value)
88
- actual =~ PATTERNS["#{value.gsub('{', '').gsub('}', '')}"]
88
+ actual =~ PATTERNS["#{value.tr('{}', '')}"]
89
+ when non_string_pattern?(value) && !actual.is_a?(String)
90
+ actual.to_s =~ PATTERNS["#{value.tr('{}', '').sub(':non-string', '')}"]
89
91
  else
90
92
  value == actual
91
93
  end
@@ -95,6 +97,10 @@ module MatchJson
95
97
  !!(str =~ /\A\{\w+\}\z/)
96
98
  end
97
99
 
100
+ def non_string_pattern?(str)
101
+ !!(str =~ /\A\{\w+\}:non-string\z/)
102
+ end
103
+
98
104
  def regexp?(str)
99
105
  !!(str =~ /\A\{re:.+\}\z/)
100
106
  end
@@ -1,3 +1,3 @@
1
1
  module MatchJson
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -32,6 +32,12 @@ describe "include_json" do
32
32
  expect(%Q({ "array" : [1, 2, 3] })).to include_json(%Q({ "array" : [5, 1] }))
33
33
  }.to fail_with(%Q("5" was not found in\n " > array"=>[1, 2, 3]))
34
34
  end
35
+
36
+ it 'fails with there is null instead of array' do
37
+ expect {
38
+ expect(%Q({ "array" : null })).to include_json(%Q({ "array" : [5] }))
39
+ }.to fail_with(%Q("5" was not found in\n " > array"=>nil))
40
+ end
35
41
  end
36
42
 
37
43
  context 'when array contains object' do
@@ -80,6 +86,8 @@ describe "include_json" do
80
86
 
81
87
  it 'uses patten to check value' do
82
88
  expect(%Q({"one": "123456"})).to include_json(%Q({"one": "{id}"}))
89
+ expect(%Q({"one": 123456})).to include_json(%Q({"one": {id}}))
90
+ expect(%Q({"one": "123456"})).not_to include_json(%Q({"one": {id}}))
83
91
 
84
92
  expect {
85
93
  expect(%Q({"one": "abcdef"})).to include_json(%Q({"one": "{id}"}))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: match_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Gabriel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-03 00:00:00.000000000 Z
12
+ date: 2015-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -67,6 +67,7 @@ extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
69
  - ".gitignore"
70
+ - CHANGELOG.md
70
71
  - Gemfile
71
72
  - LICENSE.txt
72
73
  - README.md
@@ -99,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  version: '0'
100
101
  requirements: []
101
102
  rubyforge_project:
102
- rubygems_version: 2.4.5
103
+ rubygems_version: 2.4.6
103
104
  signing_key:
104
105
  specification_version: 4
105
106
  summary: RSpec matcher for JSON documents