Skip to content

Commit 1220267

Browse files
authored
Merge pull request #8 from nomtek/feature/enhance-failure-description
Feature/enhance failure description
2 parents 237ec94 + 0c8cabc commit 1220267

7 files changed

Lines changed: 62 additions & 17 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
77
gemspec
88

99
gem "activesupport", "~> 6.1", ">= 6.1.4.1"
10+
gem "diffy"
1011
gem "rake", "~> 13.0", ">= 13.0.6"
1112
gem "rspec-rails", "~> 5.0", ">= 5.0.2"
1213
gem "rubocop"

Gemfile.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
rspec-json_api (1.2.2)
4+
rspec-json_api (1.3.0)
55
activesupport (>= 6.1.4.1)
66
rails (>= 6.1.4.1)
77
rspec-rails (>= 5.0.2)
@@ -75,6 +75,7 @@ GEM
7575
crass (1.0.6)
7676
date (3.3.4)
7777
diff-lcs (1.5.0)
78+
diffy (3.4.2)
7879
erubi (1.10.0)
7980
globalid (1.2.1)
8081
activesupport (>= 6.1)
@@ -201,6 +202,7 @@ PLATFORMS
201202

202203
DEPENDENCIES
203204
activesupport (~> 6.1, >= 6.1.4.1)
205+
diffy
204206
rake (~> 13.0, >= 13.0.6)
205207
rspec-json_api!
206208
rspec-rails (~> 5.0, >= 5.0.2)

lib/rspec/json_api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Load 3th party libraries
44
require "json"
5+
require "diffy"
56
require "active_support/core_ext/object/blank"
67

78
# Load the json_api parts

lib/rspec/json_api/matchers/have_no_content.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,37 @@
33
module RSpec
44
module JsonApi
55
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.
612
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.
717
def matches?(actual)
818
actual == ""
919
end
1020

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.
1126
def failure_message
12-
self
27+
"expected the string to be empty but it contained content"
1328
end
1429

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.
1535
def failure_message_when_negated
16-
self
36+
"expected the string not to be empty, yet it was devoid of content"
1737
end
1838
end
1939
end

lib/rspec/json_api/matchers/match_json_schema.rb

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,57 @@
33
module RSpec
44
module JsonApi
55
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.
610
class MatchJsonSchema
11+
# @return [Object] the expected JSON schema to match against
712
attr_reader :expected
13+
# @return [Object] the actual JSON data being tested
14+
attr_reader :actual
815

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.
918
def initialize(expected)
1019
@expected = expected
1120
end
1221

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.
1325
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)
1628

17-
# Compare types
18-
return false unless actual.instance_of?(expected.class)
29+
return false unless @actual.instance_of?(expected.class)
1930

2031
if expected.instance_of?(Array)
21-
RSpec::JsonApi::CompareArray.compare(actual, expected)
32+
RSpec::JsonApi::CompareArray.compare(@actual, expected)
2233
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
2535

26-
RSpec::JsonApi::CompareHash.compare(actual, expected)
36+
RSpec::JsonApi::CompareHash.compare(@actual, expected)
2737
end
2838
end
2939

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.
3042
def failure_message
31-
self
43+
<<~MSG
44+
expected: #{expected}
45+
got: #{actual}
46+
47+
Diff:
48+
#{@diff}
49+
MSG
3250
end
3351

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
3455
def failure_message_when_negated
35-
self
56+
"expected the JSON data not to match the provided schema, but it did."
3657
end
3758
end
3859
end

lib/rspec/json_api/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module RSpec
44
module JsonApi
5-
VERSION = "1.2.2"
5+
VERSION = "1.3.0"
66
end
77
end

spec/rspec/json_api/matchers/match_json_schema_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
{
163163
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b",
164164
name: "Roy Mcdaniel",
165-
age: 3
165+
age: 1
166166
}
167167
]
168168
}
@@ -183,7 +183,7 @@
183183
{
184184
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4b",
185185
name: "Roy Mcdaniel",
186-
age: 3
186+
age: 1
187187
}
188188
]
189189
}.to_json
@@ -217,7 +217,7 @@
217217
end
218218
end
219219

220-
context 'when keys are in arbitrary order' do
220+
context "when keys are in arbitrary order" do
221221
let(:expected) do
222222
{
223223
id: "8eccff73-f134-42f2-aed4-751d1f4ebd4f",

0 commit comments

Comments
 (0)