json_expressions 0.7.2 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,14 @@
1
+ ### v0.8.0 [view commit logs](https://github.com/chancancode/json_expressions/compare/0.7.3...0.8.0)
2
+
3
+ * Added Test::Unit support.
4
+ * Added MiniTest::Spec support.
5
+ * BREAKING: Changed internal structure of MiniTest support code. This shouldn't affect you unless you have been manually requiring and including the MiniTest helpers yourself.
6
+ * Use of `WILDCARD_MATCHER` (the constant) inside a `MiniTest::Unit::TestCase` is now discouraged. Instead, you are encouraged to use `wildcard_matcher` (the method) instead. README has been updated.
7
+
8
+ ### v0.7.3 [view commit logs](https://github.com/chancancode/json_expressions/compare/0.7.2...0.7.3)
9
+
10
+ * Removed WILDCARD_MATCHER#match and the corresponding test. Since support for Object#match has been removed in v0.7.0, this should no longer be necessary.
11
+
1
12
  ### v0.7.2 [view commit logs](https://github.com/chancancode/json_expressions/compare/0.7.1...0.7.2)
2
13
 
3
14
  * Bugfix: Corrected a misbehaving require statement in minitest helpers (Fixes #2)
data/README.md CHANGED
@@ -89,8 +89,8 @@ class UsersControllerTest < MiniTest::Unit::TestCase
89
89
  type: 'Administrator',
90
90
  points: Fixnum, # Any integer value
91
91
  homepage: /\Ahttps?\:\/\/.*\z/i, # Let's get serious
92
- created_at: WILDCARD_MATCHER, # Don't care as long as it exists
93
- updated_at: WILDCARD_MATCHER,
92
+ created_at: wildcard_matcher, # Don't care as long as it exists
93
+ updated_at: wildcard_matcher,
94
94
  posts: [
95
95
  {
96
96
  id: Fixnum,
@@ -119,21 +119,28 @@ class UsersControllerTest < MiniTest::Unit::TestCase
119
119
  end
120
120
  end
121
121
 
122
- # RSpec example
122
+ # MiniTest::Spec example
123
123
  describe UsersController, "#show" do
124
124
  it "returns a user" do
125
125
  pattern = # See above...
126
126
 
127
127
  server_response = get '/users/chancancode.json'
128
128
 
129
- server_response.body.should match_json_expression(pattern)
129
+ server_response.body.must_match_json_expression(pattern)
130
130
  end
131
131
  end
132
- ```
133
132
 
134
- ### `RSpec` Integration
133
+ # RSpec example
134
+ describe UsersController, "#show" do
135
+ it "returns a user" do
136
+ pattern = # See above...
135
137
 
138
+ server_response = get '/users/chancancode.json'
136
139
 
140
+ server_response.body.should match_json_expression(pattern)
141
+ end
142
+ end
143
+ ```
137
144
 
138
145
  ### Basic Matching
139
146
 
@@ -164,11 +171,11 @@ matches the JSON object
164
171
 
165
172
  ### Wildcard Matching
166
173
 
167
- You can use the WILDCARD_MATCHER to ignore keys that you don't care about (other than the fact that they exist).
174
+ You can use `wildcard_matcher` to ignore keys that you don't care about (other than the fact that they exist).
168
175
 
169
176
  This pattern
170
177
  ```ruby
171
- [ WILDCARD_MATCHER, WILDCARD_MATCHER, WILDCARD_MATCHER, WILDCARD_MATCHER, WILDCARD_MATCHER, WILDCARD_MATCHER, WILDCARD_MATCHER ]
178
+ [ wildcard_matcher, wildcard_matcher, wildcard_matcher, wildcard_matcher, wildcard_matcher, wildcard_matcher, wildcard_matcher ]
172
179
  ```
173
180
  matches the JSON array
174
181
  ```json
@@ -177,12 +184,14 @@ matches the JSON array
177
184
 
178
185
  Furthermore, because the pattern is just plain old Ruby code, you can also write:
179
186
  ```ruby
180
- [ WILDCARD_MATCHER ] * 7
187
+ [ wildcard_matcher ] * 7
181
188
  ```
182
189
 
190
+ Note: Previously, the examples here uses `WILDCARD_MATCHER` which is a constant defined on `MiniTest::Unit::TestCase`. Since 0.8.0, the use of this constant is discouraged because it doesn't work for `MiniTest::Spec` and `RSpec` due to how Ruby scoping works for blocks. Instead, `wildcard_matcher` (a method) has been added. This is now the preferred way to retrieve the wildcard matcher in order to maintain consistency among the different test frameworks.
191
+
183
192
  ### Object Equality
184
193
 
185
- By default, json_expressions uses `Object#===` to match against the corresponding value in the target JSON. In most cases, this method behaves exactly the same as `Object#==`. However, certain classes override this method to provide specialized behavior (notably `Regexp` and `Module`, see below). If you find this undesirable for certain classes, you can explicitly opt them out and json_expressions will call `Object#==` instead:
194
+ By default, json_expressions uses `Object#===` to match against the corresponding value in the target JSON. In most cases, this method behaves exactly the same as `Object#==`. However, certain classes override this method to provide specialized behavior (notably `Regexp`, `Module` and `Range`, see below). If you find this undesirable for certain classes, you can explicitly opt them out and json_expressions will call `Object#==` instead:
186
195
 
187
196
  ```ruby
188
197
  # This is the default setting
@@ -237,6 +246,21 @@ matches the JSON object
237
246
  }
238
247
  ```
239
248
 
249
+ ### Ranges
250
+
251
+ `Range` overrides `===` to mean `include?`. Therefore,
252
+ ```ruby
253
+ { day: (1..31), month: (1..12) }
254
+ ```
255
+ matches the JSON object
256
+ ```json
257
+ { "day": 3, "month": 11 }
258
+ ```
259
+ but not
260
+ ```json
261
+ { "day": -1, "month": 13 }
262
+ ```
263
+
240
264
  ### Capturing
241
265
 
242
266
  Similar to how "captures" work in Regexp, you can capture the value of certain keys for later use:
@@ -301,10 +325,10 @@ will match
301
325
  You can change this behavior in a case-by-case manner:
302
326
  ```ruby
303
327
  {
304
- "unordered_array" => [1,2,3,4,5].unordered!, # calling unordered! is optional as it's the default
305
- "ordered_array" => [1,2,3,4,5].ordered!,
306
- "unordered_hash" => {'a'=>1, 'b'=>2}.unordered!,
307
- "ordered_hash" => {'a'=>1, 'b'=>2}.ordered!
328
+ unordered_array: [1,2,3,4,5].unordered!, # calling unordered! is optional as it's the default
329
+ ordered_array: [1,2,3,4,5].ordered!,
330
+ unordered_hash: {a: 1, b: 2}.unordered!,
331
+ ordered_hash: {a: 1, b: 2}.ordered!
308
332
  }
309
333
  ```
310
334
 
@@ -339,8 +363,8 @@ You can change this behavior in a case-by-case manner:
339
363
  {
340
364
  strict_array: [1,2,3,4,5].strict!, # calling strict! is optional as it's the default
341
365
  forgiving_array: [1,2,3,4,5].forgiving!,
342
- strict_hash: {'a'=>1, 'b'=>2}.strict!,
343
- forgiving_hash: {'a'=>1, 'b'=>2}.forgiving!
366
+ strict_hash: {a: 1, b: 2}.strict!,
367
+ forgiving_hash: {a: 1, b: 2}.forgiving!
344
368
  }
345
369
  ```
346
370
 
@@ -349,8 +373,8 @@ They also come with some more sensible aliases:
349
373
  {
350
374
  strict_array: [1,2,3,4,5].reject_extra_values!,
351
375
  forgiving_array: [1,2,3,4,5].ignore_extra_values!,
352
- strict_hash: {'a'=>1, 'b'=>2}.reject_extra_keys!,
353
- forgiving_hash: {'a'=>1, 'b'=>2}.ignore_extra_keys!
376
+ strict_hash: {a: 1, b: 2}.reject_extra_keys!,
377
+ forgiving_hash: {a: 1, b: 2}.ignore_extra_keys!
354
378
  }
355
379
  ```
356
380
 
@@ -363,7 +387,7 @@ JsonExpressions::Matcher.assume_strict_hashes = false
363
387
 
364
388
  ## Support for `MiniTest::Spec` (and other testing frameworks)
365
389
 
366
- The `Matcher` class itself is written in a testing-framework-agnostic manner. This allows you to easily write custom helpers/matchers for your favorite testing framework. `MiniTest::Spec` is on my TODO list, but it is not a high priority for me personally, as I currently don't use it. If you need this now, write it yourself and submit a pull request - it's really easy, I promise (see `lib/json_expressions/minitest/unit/helpers.rb` for inspiration).
390
+ The `Matcher` class itself is written in a testing-framework-agnostic manner. This allows you to easily write custom helpers/matchers for your favorite testing framework. `MiniTest::Spec` is on my TODO list, but it is not a high priority for me personally, as I don't use it myself. If you need this now, write it yourself and submit a pull request - it's really easy, I promise (see `lib/json_expressions/minitest/unit/helpers.rb` for inspiration).
367
391
 
368
392
  ## Contributing
369
393
 
@@ -15,10 +15,6 @@ module JsonExpressions
15
15
  true
16
16
  end
17
17
 
18
- def WILDCARD_MATCHER.match(other)
19
- true
20
- end
21
-
22
18
  def WILDCARD_MATCHER.to_s
23
19
  'WILDCARD_MATCHER'
24
20
  end
@@ -1,8 +1,19 @@
1
1
  require 'minitest/unit'
2
+ require 'minitest/spec'
2
3
  require 'json_expressions'
3
- require 'json_expressions/minitest/unit/helpers'
4
+ require 'json_expressions/minitest/assertions'
5
+
6
+ # module MiniTest::Assertions
7
+ # include JsonExpressions::MiniTest::Assertions
8
+ # end
4
9
 
5
10
  class MiniTest::Unit::TestCase
6
- include JsonExpressions::MiniTest::Unit::Helpers
7
- WILDCARD_MATCHER = JsonExpressions::WILDCARD_MATCHER
8
- end
11
+ WILDCARD_MATCHER = JsonExpressions::WILDCARD_MATCHER
12
+
13
+ def wildcard_matcher
14
+ ::JsonExpressions::WILDCARD_MATCHER
15
+ end
16
+ end
17
+
18
+ Object.infect_an_assertion :assert_json_match, :must_match_json_expression
19
+ Object.infect_an_assertion :refute_json_match, :wont_match_json_expression
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+
3
+ module MiniTest
4
+ module Assertions
5
+ def assert_json_match(exp, act, msg = nil)
6
+ unless JsonExpressions::Matcher === exp
7
+ exp = JsonExpressions::Matcher.new(exp)
8
+ end
9
+
10
+ if String === act
11
+ assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
12
+ end
13
+
14
+ assert exp =~ act, ->{ "Expected #{mu_pp(exp)} to match #{mu_pp(act)}\n" + exp.last_error}
15
+
16
+ # Return the matcher
17
+ return exp
18
+ end
19
+
20
+ def refute_json_match(exp, act, msg = nil)
21
+ unless JsonExpressions::Matcher === exp
22
+ exp = JsonExpressions::Matcher.new(exp)
23
+ end
24
+
25
+ if String === act
26
+ assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
27
+ end
28
+
29
+ refute exp =~ act, "Expected #{mu_pp(exp)} to not match #{mu_pp(act)}"
30
+
31
+ # Return the matcher
32
+ return exp
33
+ end
34
+ end
35
+ end
@@ -4,4 +4,14 @@ require 'json_expressions/rspec/matchers'
4
4
 
5
5
  RSpec::configure do |config|
6
6
  config.include(JsonExpressions::RSpec::Matchers)
7
+ end
8
+
9
+ module RSpec
10
+ module Core
11
+ class ExampleGroup
12
+ def wildcard_matcher
13
+ ::JsonExpressions::WILDCARD_MATCHER
14
+ end
15
+ end
16
+ end
7
17
  end
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+ require 'pp'
3
+
4
+ module JsonExpressions
5
+ module Test
6
+ module Unit
7
+ module Helpers
8
+ def assert_json_match(exp, act, msg = nil)
9
+ unless JsonExpressions::Matcher === exp
10
+ exp = JsonExpressions::Matcher.new(exp)
11
+ end
12
+
13
+ if String === act
14
+ begin
15
+ act = JSON.parse(act)
16
+ rescue
17
+ assert false, "Expected #{pp(act)} to be valid JSON"
18
+ end
19
+ end
20
+
21
+ unless exp =~ act
22
+ assert false, "Expected #{pp(exp)} to match #{pp(act)}\n #{exp.last_error}"
23
+ end
24
+
25
+ # Return the matcher
26
+ return exp
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+ require 'json_expressions'
3
+ require 'json_expressions/test/unit/helpers'
4
+
5
+ class Test::Unit::TestCase
6
+ include JsonExpressions::Test::Unit::Helpers
7
+ WILDCARD_MATCHER = JsonExpressions::WILDCARD_MATCHER
8
+
9
+ def wildcard_matcher
10
+ ::JsonExpressions::WILDCARD_MATCHER
11
+ end
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_expressions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-12 00:00:00.000000000 Z
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: JSON matchmaking for all your API testing needs.
15
15
  email:
@@ -20,11 +20,13 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/json_expressions/core_extensions.rb
22
22
  - lib/json_expressions/matcher.rb
23
- - lib/json_expressions/minitest/unit/helpers.rb
23
+ - lib/json_expressions/minitest/assertions.rb
24
24
  - lib/json_expressions/minitest.rb
25
25
  - lib/json_expressions/rspec/matchers/match_json_expression.rb
26
26
  - lib/json_expressions/rspec/matchers.rb
27
27
  - lib/json_expressions/rspec.rb
28
+ - lib/json_expressions/test/unit/helpers.rb
29
+ - lib/json_expressions/testunit.rb
28
30
  - lib/json_expressions.rb
29
31
  - README.md
30
32
  - CHANGELOG.md
@@ -1,39 +0,0 @@
1
- require 'json'
2
-
3
- module JsonExpressions
4
- module MiniTest
5
- module Unit
6
- module Helpers
7
- def assert_json_match(exp, act, msg = nil)
8
- unless JsonExpressions::Matcher === exp
9
- exp = JsonExpressions::Matcher.new(exp)
10
- end
11
-
12
- if String === act
13
- assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
14
- end
15
-
16
- assert exp =~ act, ->{ "Expected #{mu_pp(exp)} to match #{mu_pp(act)}\n" + exp.last_error}
17
-
18
- # Return the matcher
19
- return exp
20
- end
21
-
22
- def refute_json_match(exp, act, msg = nil)
23
- unless JsonExpressions::Matcher === exp
24
- exp = JsonExpressions::Matcher.new(exp)
25
- end
26
-
27
- if String === act
28
- assert act = JSON.parse(act), "Expected #{mu_pp(act)} to be valid JSON"
29
- end
30
-
31
- refute exp =~ act, "Expected #{mu_pp(exp)} to not match #{mu_pp(act)}"
32
-
33
- # Return the matcher
34
- return exp
35
- end
36
- end
37
- end
38
- end
39
- end