rspec-json_api 1.2.2 → 1.3.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
  SHA256:
3
- metadata.gz: 7bb2dd6447fbb645e88da6ca6650a19bc60c53ac6279e24bbd440fe291eceff3
4
- data.tar.gz: 9c42396046bcd95029c1744cb37322246e74696b353a2e58404de15bf90e9d72
3
+ metadata.gz: 0db3b7ce7277a3294125638f46098237819fecc4c6bd73cbcbf5b057c97c0611
4
+ data.tar.gz: e3d48decdb1c0508fdd9673ed6732a3ce96f6b59e64f7ffc41997112c35b1c4a
5
5
  SHA512:
6
- metadata.gz: 923b1ee38754ea86b17c1aca69348ae1122de82b759cb0231a41ac02887a34bb14b8e445965409e21f133ed74054ac7754bba14cba6cbca4e9bc31b26a83cbbf
7
- data.tar.gz: 77311bfdc2ff60e1c978ca1f49a68c328f98aec2e762f167087a7c2204b4d372d10ac2412d595cbbe25c22966fd478435c644b8f095a10b44965559ad14efe9e
6
+ metadata.gz: 9947651ffdfa93b139480841ff20a333efbe060e5f0d93f9d683415d63c9b871290593427e6deac5dc0554cf810ff7d239ac350664f60ed45fd25f8a521fb99b
7
+ data.tar.gz: 86bcf3883a800b1f1356bebabee5752b4470bb258e5c66d75ff2a75d0b23ae85dfeb0af68d9489ea377365ce7c7c61973fa91a1dbd5bb05c786bd580ed4a2c4c
data/Gemfile CHANGED
@@ -7,6 +7,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
7
7
  gemspec
8
8
 
9
9
  gem "activesupport", "~> 6.1", ">= 6.1.4.1"
10
+ gem "diffy"
10
11
  gem "rake", "~> 13.0", ">= 13.0.6"
11
12
  gem "rspec-rails", "~> 5.0", ">= 5.0.2"
12
13
  gem "rubocop"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-json_api (1.2.2)
4
+ rspec-json_api (1.3.0)
5
5
  activesupport (>= 6.1.4.1)
6
6
  rails (>= 6.1.4.1)
7
7
  rspec-rails (>= 5.0.2)
@@ -75,6 +75,7 @@ GEM
75
75
  crass (1.0.6)
76
76
  date (3.3.4)
77
77
  diff-lcs (1.5.0)
78
+ diffy (3.4.2)
78
79
  erubi (1.10.0)
79
80
  globalid (1.2.1)
80
81
  activesupport (>= 6.1)
@@ -201,6 +202,7 @@ PLATFORMS
201
202
 
202
203
  DEPENDENCIES
203
204
  activesupport (~> 6.1, >= 6.1.4.1)
205
+ diffy
204
206
  rake (~> 13.0, >= 13.0.6)
205
207
  rspec-json_api!
206
208
  rspec-rails (~> 5.0, >= 5.0.2)
@@ -3,17 +3,37 @@
3
3
  module RSpec
4
4
  module JsonApi
5
5
  module Matchers
6
+ # The HaveNoContent class is designed to verify that a given string is empty.
7
+ #
8
+ # This matcher is primarily used within RSpec tests to assert that a given
9
+ # string lacks content, effectively ensuring that expected content is absent,
10
+ # which can be particularly useful in testing API responses or other outputs
11
+ # where the absence of content signifies a specific state or outcome.
6
12
  class HaveNoContent
13
+ # Determines whether the actual string is empty, signifying no content.
14
+ #
15
+ # @param actual [String] The string to be evaluated.
16
+ # @return [Boolean] Returns true if the string is empty, indicating no content; false otherwise.
7
17
  def matches?(actual)
8
18
  actual == ""
9
19
  end
10
20
 
21
+ # Provides a failure message for when the actual string contains content,
22
+ # contrary to the expectation of being empty.
23
+ #
24
+ # @return [String] A descriptive message indicating the expectation was for
25
+ # the string to have no content, yet content was found.
11
26
  def failure_message
12
- self
27
+ "expected the string to be empty but it contained content"
13
28
  end
14
29
 
30
+ # Provides a failure message for when the expectation is negated (i.e.,
31
+ # expecting content) and the actual string is unexpectedly empty.
32
+ #
33
+ # @return [String] A descriptive message indicating that content was expected
34
+ # in the string, but it was found to be empty.
15
35
  def failure_message_when_negated
16
- self
36
+ "expected the string not to be empty, yet it was devoid of content"
17
37
  end
18
38
  end
19
39
  end
@@ -3,36 +3,57 @@
3
3
  module RSpec
4
4
  module JsonApi
5
5
  module Matchers
6
+ # MatchJsonSchema class is designed to match a given JSON against a predefined JSON schema.
7
+ #
8
+ # This matcher is useful for validating JSON structures in API responses or other JSON data
9
+ # against a schema defined either as a Hash, an Array, or another JSON structure.
6
10
  class MatchJsonSchema
11
+ # @return [Object] the expected JSON schema to match against
7
12
  attr_reader :expected
13
+ # @return [Object] the actual JSON data being tested
14
+ attr_reader :actual
8
15
 
16
+ # Initializes the matcher with the expected JSON schema.
17
+ # @param expected [Object] The expected JSON schema as a Hash, Array, or other JSON-compatible structure.
9
18
  def initialize(expected)
10
19
  @expected = expected
11
20
  end
12
21
 
22
+ # Matches the actual JSON data against the expected schema.
23
+ # @param actual [String] The JSON string to test against the expected schema.
24
+ # @return [Boolean] true if the actual JSON matches the expected schema, false otherwise.
13
25
  def matches?(actual)
14
- # Parse JSON to ruby object
15
- actual = JSON.parse(actual, symbolize_names: true)
26
+ @actual = JSON.parse(actual, symbolize_names: true)
27
+ @diff = Diffy::Diff.new(expected, @actual, context: 5)
16
28
 
17
- # Compare types
18
- return false unless actual.instance_of?(expected.class)
29
+ return false unless @actual.instance_of?(expected.class)
19
30
 
20
31
  if expected.instance_of?(Array)
21
- RSpec::JsonApi::CompareArray.compare(actual, expected)
32
+ RSpec::JsonApi::CompareArray.compare(@actual, expected)
22
33
  else
23
- # Compare actual and expected schema
24
- return false unless actual.deep_keys.deep_sort == expected.deep_keys.deep_sort
34
+ return false unless @actual.deep_keys.deep_sort == expected.deep_keys.deep_sort
25
35
 
26
- RSpec::JsonApi::CompareHash.compare(actual, expected)
36
+ RSpec::JsonApi::CompareHash.compare(@actual, expected)
27
37
  end
28
38
  end
29
39
 
40
+ # Provides a failure message for when the JSON data does not match the expected schema.
41
+ # @return [String] A descriptive message detailing the mismatch between expected and actual JSON.
30
42
  def failure_message
31
- self
43
+ <<~MSG
44
+ expected: #{expected}
45
+ got: #{actual}
46
+
47
+ Diff:
48
+ #{@diff}
49
+ MSG
32
50
  end
33
51
 
52
+ # Provides a failure message for when the JSON data matches the expected schema, but it was expected not to.
53
+ # This is used in negative matchers.
54
+ # @return [self] Returns itself, but typically this method should be implemented to return a descriptive message
34
55
  def failure_message_when_negated
35
- self
56
+ "expected the JSON data not to match the provided schema, but it did."
36
57
  end
37
58
  end
38
59
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module JsonApi
5
- VERSION = "1.2.2"
5
+ VERSION = "1.3.0"
6
6
  end
7
7
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  # Load 3th party libraries
4
4
  require "json"
5
+ require "diffy"
5
6
  require "active_support/core_ext/object/blank"
6
7
 
7
8
  # Load the json_api parts
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-json_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Gajowiak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-23 00:00:00.000000000 Z
11
+ date: 2024-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport