|
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 |
|
0 commit comments