rspec-json_api 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/lib/extentions/array.rb +8 -0
- data/lib/rspec/json_api/matchers/have_no_content.rb +22 -2
- data/lib/rspec/json_api/matchers/match_json_schema.rb +31 -10
- data/lib/rspec/json_api/version.rb +1 -1
- data/lib/rspec/json_api.rb +2 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0db3b7ce7277a3294125638f46098237819fecc4c6bd73cbcbf5b057c97c0611
|
4
|
+
data.tar.gz: e3d48decdb1c0508fdd9673ed6732a3ce96f6b59e64f7ffc41997112c35b1c4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9947651ffdfa93b139480841ff20a333efbe060e5f0d93f9d683415d63c9b871290593427e6deac5dc0554cf810ff7d239ac350664f60ed45fd25f8a521fb99b
|
7
|
+
data.tar.gz: 86bcf3883a800b1f1356bebabee5752b4470bb258e5c66d75ff2a75d0b23ae85dfeb0af68d9489ea377365ce7c7c61973fa91a1dbd5bb05c786bd580ed4a2c4c
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rspec-json_api (1.
|
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
|
-
|
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
|
-
|
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
|
-
|
15
|
-
|
26
|
+
@actual = JSON.parse(actual, symbolize_names: true)
|
27
|
+
@diff = Diffy::Diff.new(expected, @actual, context: 5)
|
16
28
|
|
17
|
-
|
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
|
-
|
24
|
-
return false unless actual.deep_keys == expected.deep_keys
|
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
|
-
|
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
|
-
|
56
|
+
"expected the JSON data not to match the provided schema, but it did."
|
36
57
|
end
|
37
58
|
end
|
38
59
|
end
|
data/lib/rspec/json_api.rb
CHANGED
@@ -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
|
@@ -11,6 +12,7 @@ require "rspec/json_api/compare_array"
|
|
11
12
|
|
12
13
|
# Load extentions
|
13
14
|
require "extentions/hash"
|
15
|
+
require "extentions/array"
|
14
16
|
|
15
17
|
# Load matchers
|
16
18
|
require "rspec/json_api/matchers"
|
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.
|
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-
|
11
|
+
date: 2024-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- Rakefile
|
74
74
|
- bin/console
|
75
75
|
- bin/setup
|
76
|
+
- lib/extentions/array.rb
|
76
77
|
- lib/extentions/hash.rb
|
77
78
|
- lib/generators/rspec/json_api/install/install_generator.rb
|
78
79
|
- lib/generators/rspec/json_api/install/templates/rspec/json_api/interfaces/.empty_directory
|