rspec-json_expectations 1.1.1 → 1.2.0

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: b7be8f6d84a11ad85c20dfdb8a792db48587ed6e
4
- data.tar.gz: fce85bc76ad20f75eeb897eae740841e2ffd9fec
3
+ metadata.gz: 1983e0ca2363deb0443b471931f99b0d8d02dc1a
4
+ data.tar.gz: 71b98df45860660e1ec5d496657915ef33975b7c
5
5
  SHA512:
6
- metadata.gz: e06b381b60dc36b9153f80bc8840c8d60d332cc4d03025155b2966408475719d9ebbdfea1b59207f662730a5bf36adb7df9f6a00ff82997df544900ad298cb42
7
- data.tar.gz: 14f572322abeeb992f142360504a659984d351d1837f4e94b033546b2119a2a294e69976ad33952f38e196f6617f4769c8a5f7d02376dd4f041bbaa04e21b449
6
+ metadata.gz: b1ad5e58d0b7f8d27dab022070a7da4e2e6358fffa072e6196b8b98c8a67a1fd8a79d19ac3325d3b9ed8683bcf4b58390c83f7803bf749f1992053ab7d3229dc
7
+ data.tar.gz: 17ebe93df1ddbc066ce55d1908be380458bc9d84f300116395a99846f105764aa4eefb15c7ea2334c6a99fbe74b48def359c066270fabeed6e2dff8c6f84ff4b
@@ -0,0 +1,76 @@
1
+ Feature: RSpec matcher support for include_json matcher
2
+
3
+ As a developer extensively testing my APIs
4
+ I want to utilize the power of available RSpec matchers
5
+
6
+ Background:
7
+ Given a file "spec/spec_helper.rb" with:
8
+ """ruby
9
+ require "rspec"
10
+ require "rspec/expectations"
11
+ require "rspec/json_expectations"
12
+ """
13
+ And a local "SIMPLE_JSON" with:
14
+ """json
15
+ {
16
+ "id": 25,
17
+ "email": "john.smith@example.com",
18
+ "name": "John",
19
+ "score": 55
20
+ }
21
+ """
22
+
23
+ Scenario: Expecting json string to include simple json using an rspec matcher
24
+ Given a file "spec/matcher_example_spec.rb" with:
25
+ """ruby
26
+ require "spec_helper"
27
+
28
+ RSpec.describe "A json response" do
29
+ subject { '%{SIMPLE_JSON}' }
30
+
31
+ it "has basic info about user" do
32
+ expect(subject).to include_json(
33
+ id: 25,
34
+ email: "john.smith@example.com",
35
+ name: "John",
36
+ score: (be > 30)
37
+ )
38
+ end
39
+ end
40
+ """
41
+ When I run "rspec spec/matcher_example_spec.rb"
42
+ Then I see:
43
+ """
44
+ 1 example, 0 failures
45
+ """
46
+
47
+ Scenario: Expecting json string to include simple json using an rspec matcher with failure
48
+ Given a file "spec/matcher_example_fail_spec.rb" with:
49
+ """ruby
50
+ require "spec_helper"
51
+
52
+ RSpec.describe "A json response" do
53
+ subject { '%{SIMPLE_JSON}' }
54
+
55
+ it "has basic info about user" do
56
+ matcher = be < 30
57
+ puts "\'matcher.inspect\'"
58
+ expect(subject).to include_json(
59
+ id: 25,
60
+ email: "john.smith@example.com",
61
+ name: "John",
62
+ score: (be < 30)
63
+ )
64
+ end
65
+ end
66
+ """
67
+ When I run "rspec spec/matcher_example_fail_spec.rb"
68
+ Then I see:
69
+ """
70
+ expected: "be < 30"
71
+ got: 55
72
+ """
73
+ And I see:
74
+ """
75
+ 1 example, 1 failure
76
+ """
@@ -8,7 +8,8 @@ module RSpec
8
8
  # json atom paths.
9
9
  class JsonTraverser
10
10
  HANDLED_BY_SIMPLE_VALUE_HANDLER = [String, Numeric, FalseClass, TrueClass, NilClass]
11
- SUPPORTED_VALUES = [Hash, Regexp, Array] + HANDLED_BY_SIMPLE_VALUE_HANDLER
11
+ RSPECMATCHERS = defined?(RSpec::Matchers) ? [RSpec::Matchers::BuiltIn::BaseMatcher] : []
12
+ SUPPORTED_VALUES = [Hash, Regexp, Array] + HANDLED_BY_SIMPLE_VALUE_HANDLER + RSPECMATCHERS
12
13
 
13
14
  class << self
14
15
  def traverse(errors, expected, actual, negate=false, prefix=[])
@@ -17,6 +18,7 @@ module RSpec
17
18
  handle_array(errors, expected, actual, negate, prefix),
18
19
  handle_value(errors, expected, actual, negate, prefix),
19
20
  handle_regex(errors, expected, actual, negate, prefix),
21
+ handle_rspec_matcher(errors, expected, actual, negate, prefix),
20
22
  handle_unsupported(expected)
21
23
  ].any?
22
24
  end
@@ -80,6 +82,21 @@ module RSpec
80
82
  end
81
83
  end
82
84
 
85
+ def handle_rspec_matcher(errors, expected, actual, negate=false, prefix=[])
86
+ return nil unless defined?(RSpec::Matchers)
87
+ return nil unless expected.is_a?(RSpec::Matchers::BuiltIn::BaseMatcher)
88
+
89
+ if conditionally_negate(!!expected.matches?(actual), negate)
90
+ true
91
+ else
92
+ errors[prefix.join("/")] = {
93
+ actual: actual,
94
+ expected: expected.description
95
+ }
96
+ false
97
+ end
98
+ end
99
+
83
100
  def handle_unsupported(expected)
84
101
  unless SUPPORTED_VALUES.any? { |type| expected.is_a?(type) }
85
102
  raise NotImplementedError,
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module JsonExpectations
3
- VERSION = "1.1.1"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-json_expectations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Fedorov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-05 00:00:00.000000000 Z
11
+ date: 2015-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,6 +61,7 @@ files:
61
61
  - features/rspec/json_expectations/negation_matching.feature
62
62
  - features/rspec/json_expectations/nested_json_support.feature
63
63
  - features/rspec/json_expectations/regular_expressions_support.feature
64
+ - features/rspec/json_expectations/rspec_matchers_support.feature
64
65
  - features/rspec/json_expectations/simple_hash_match.feature
65
66
  - features/step_definitions/steps.rb
66
67
  - features/support/env.rb
@@ -104,6 +105,8 @@ test_files:
104
105
  - features/rspec/json_expectations/negation_matching.feature
105
106
  - features/rspec/json_expectations/nested_json_support.feature
106
107
  - features/rspec/json_expectations/regular_expressions_support.feature
108
+ - features/rspec/json_expectations/rspec_matchers_support.feature
107
109
  - features/rspec/json_expectations/simple_hash_match.feature
108
110
  - features/step_definitions/steps.rb
109
111
  - features/support/env.rb
112
+ has_rdoc: