match_json 0.0.7 → 0.0.8

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
- SHA1:
3
- metadata.gz: dbc46ca994f742211eef028282ffe68fd347348c
4
- data.tar.gz: 8319a12a7de9cac7164d26055df0e0f4d2c92f21
2
+ SHA256:
3
+ metadata.gz: a7885b90321caa660711df8efaefc8c68685a6dccebf016699446285438cf9e7
4
+ data.tar.gz: 67b355f32cc560b957a34323766fae402ac083866a11e94193204266a1f655bf
5
5
  SHA512:
6
- metadata.gz: a5bab51f9a0988851bb3231fda9eb38d848ef3d376eb7288812866b562d407ddf394fe0fec43163b2e40833491024e7a6890818bc3eac40224c42dd4bfb6d17b
7
- data.tar.gz: 7d78a70cfcc882073f20b7de4c55c760bacce249bcce889ff52a2555baf45e0bb5b27de5dff1c11f4e548a1237a4d043110b8a633ccaacad6a1cddcad7c700d0
6
+ metadata.gz: cdb5deeff6835ffd46ce4539fd85ad9938bfe1485c36073076c44c659811cf8a3e79ad13d37805fbace4f9fdd5af179c77f98af368fffbcd8fb517e7775cb29d
7
+ data.tar.gz: 278fb942dc789194b407821173ee4d84161972a859fe60b514faeae6dc72e945b7c7c016c929f386ab8eaa4ec45e5f620711a0aaed89daa4c0ea808207652705
data/README.md CHANGED
@@ -129,15 +129,22 @@ Just add them to `spec/support/match_json.rb`:
129
129
 
130
130
  ```ruby
131
131
  MatchJson::Matchers::IncludeJson::PATTERNS['id'] = /\A\d{6}\z/
132
+ MatchJson::Matchers::IncludeJson::PATTERNS[/id:(\w+)'] = proc { |actual, match| /\A#{match}_\z/ =~ actual }
132
133
  ```
133
134
 
134
135
  and then use it in your spec like so:
135
136
 
136
137
  ```ruby
137
138
  it 'uses patten to check value' do
139
+
140
+ # MatchJson::Matchers::IncludeJson::PATTERNS['id'] = /\A\d{6}\z/
138
141
  expect(%Q({"one": "123456"})).to match_json(%Q({"one": "{id}"}))
139
142
  # .. you can even do this:
140
143
  expect(%Q({"one": 123456})).to match_json(%Q({"one": {id}}))
144
+
145
+ # MatchJson::Matchers::IncludeJson::PATTERNS[/id:(\w+)'] = proc { |actual, match| /\A#{match}_\z/ =~ actual }
146
+ expect(%Q({"id": "usr_xxx"})).to match_json(%Q({"one": "{id:usr}"}))
147
+ expect(%Q({"id": "cus_xxx"})).to match_json(%Q({"one": "{id:cus}"}))
141
148
  end
142
149
  ```
143
150
 
@@ -84,6 +84,11 @@ module MatchJson
84
84
  when regexp?(value)
85
85
  reg_exp = value.match(/{re:(.*)}/)[1]
86
86
  actual =~ Regexp.new(reg_exp)
87
+ when defined_regexp?(value)
88
+ defined_regexps = PATTERNS.keys.find_all { |pattern| pattern.is_a? Regexp }
89
+ matches = value.match(/{(\w+):(\w+)}/)
90
+ reg_exp = defined_regexps.find {|re| re.match?("#{matches[1]}:#{matches[2]}") }
91
+ PATTERNS[reg_exp].call(actual, matches[2])
87
92
  when pattern?(value)
88
93
  actual =~ PATTERNS["#{value.tr('{}', '')}"]
89
94
  when non_string_pattern?(value) && !actual.is_a?(String)
@@ -105,6 +110,10 @@ module MatchJson
105
110
  !!(str =~ /\A\{re:.+\}\z/)
106
111
  end
107
112
 
113
+ def defined_regexp?(str)
114
+ !!(str =~ /\A\{\w+:\w+\}\z/)
115
+ end
116
+
108
117
  def compared_different_types?(actual, expected)
109
118
  actual.class != expected.class
110
119
  end
@@ -1,3 +1,3 @@
1
1
  module MatchJson
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -97,4 +97,20 @@ describe "include_json" do
97
97
  }.to fail_with(%Q("one":"{id}:non-string" was not found in\n {"one":true}))
98
98
  end
99
99
  end
100
+
101
+ context 'with custom pattern as regexp' do
102
+ let(:regexp) { /id:(\w+)/ }
103
+ before do
104
+ MatchJson::Matchers::IncludeJson::PATTERNS[regexp] = proc { |actual, match| /\A#{match}_\w+\z/ =~ actual }
105
+ end
106
+
107
+ after do
108
+ MatchJson::Matchers::IncludeJson::PATTERNS.delete(regexp)
109
+ end
110
+
111
+ it 'uses patten to check value' do
112
+ expect(%Q({"one": "cus_xxx"})).to include_json(%Q({"one": "{id:cus}"}))
113
+ expect(%Q({"one": "cust_xxx"})).to_not include_json(%Q({"one": "{id:cus}"}))
114
+ end
115
+ end
100
116
  end
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.7
4
+ version: 0.0.8
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: 2017-05-26 00:00:00.000000000 Z
12
+ date: 2018-07-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  version: '0'
151
151
  requirements: []
152
152
  rubyforge_project:
153
- rubygems_version: 2.6.8
153
+ rubygems_version: 2.7.5
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: RSpec matcher for JSON documents