rspec-json_api 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitattributes +28 -0
- data/.github/workflows/main.yml +45 -20
- data/.gitignore +6 -0
- data/.rubocop.yml +41 -50
- data/CHANGELOG.md +55 -28
- data/Gemfile +14 -14
- data/Gemfile.lock +182 -262
- data/README.md +319 -319
- data/gemfiles/rails_6_1.gemfile +7 -0
- data/gemfiles/rails_7_1.gemfile +7 -0
- data/gemfiles/rails_7_2.gemfile +7 -0
- data/gemfiles/rails_8_0.gemfile +7 -0
- data/gemfiles/rails_8_1.gemfile +7 -0
- data/lib/generators/rspec/json_api/interface/interface_generator.rb +3 -1
- data/lib/generators/rspec/json_api/type/type_generator.rb +25 -23
- data/lib/rspec/json_api/constraints.rb +56 -0
- data/lib/rspec/json_api/matchers/match_json_schema.rb +14 -11
- data/lib/rspec/json_api/schema_match.rb +135 -0
- data/lib/rspec/json_api/traversal.rb +49 -0
- data/lib/rspec/json_api/types/uuid.rb +1 -1
- data/lib/rspec/json_api/version.rb +7 -7
- data/lib/rspec/json_api.rb +23 -25
- data/rspec-json_api.gemspec +42 -38
- metadata +13 -13
- data/lib/extensions/array.rb +0 -8
- data/lib/extensions/hash.rb +0 -36
- data/lib/generators/rspec/json_api/interface/templates/interface.erb +0 -9
- data/lib/rspec/json_api/compare_array.rb +0 -39
- data/lib/rspec/json_api/compare_hash.rb +0 -114
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module RSpec
|
|
4
|
-
module JsonApi
|
|
5
|
-
module CompareArray
|
|
6
|
-
extend self
|
|
7
|
-
|
|
8
|
-
def compare(actual, expected)
|
|
9
|
-
if interface?(expected)
|
|
10
|
-
actual.all? do |actual_elem|
|
|
11
|
-
return false unless actual_elem.deep_keys == expected[0].deep_keys
|
|
12
|
-
|
|
13
|
-
CompareHash.compare(actual_elem, expected[0])
|
|
14
|
-
end
|
|
15
|
-
else
|
|
16
|
-
actual.each_with_index.all? do |actual_elem, index|
|
|
17
|
-
compare_primitive_type_element(actual, expected, actual_elem, index)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
private
|
|
23
|
-
|
|
24
|
-
def interface?(expected_value)
|
|
25
|
-
expected_value.size == 1 && expected_value[0].is_a?(Hash)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def compare_primitive_type_element(actual, expected, actual_elem, index)
|
|
29
|
-
if actual[index].respond_to?(:deep_keys) && expected[index].respond_to?(:deep_keys)
|
|
30
|
-
return false unless actual[index].deep_keys == expected[index].deep_keys
|
|
31
|
-
|
|
32
|
-
CompareHash.compare(actual_elem, expected[index])
|
|
33
|
-
else
|
|
34
|
-
CompareHash.compare_values(actual[index], expected[index])
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module RSpec
|
|
4
|
-
module JsonApi
|
|
5
|
-
module CompareHash
|
|
6
|
-
module_function
|
|
7
|
-
|
|
8
|
-
SUPPORTED_OPTIONS = %i[allow_blank type value min max inclusion regex lambda].freeze
|
|
9
|
-
|
|
10
|
-
def compare(actual, expected)
|
|
11
|
-
return false if actual.blank? && expected.present?
|
|
12
|
-
|
|
13
|
-
keys = expected.deep_key_paths | actual.deep_key_paths
|
|
14
|
-
|
|
15
|
-
compare_key_paths_and_values(keys, actual, expected)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def compare_key_paths_and_values(keys, actual, expected)
|
|
19
|
-
keys.all? do |key_path|
|
|
20
|
-
actual_value = actual.dig(*key_path)
|
|
21
|
-
expected_value = expected.dig(*key_path)
|
|
22
|
-
|
|
23
|
-
compare_values(actual_value, expected_value)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def compare_values(actual_value, expected_value)
|
|
28
|
-
case expected_value
|
|
29
|
-
when Class
|
|
30
|
-
compare_class(actual_value, expected_value)
|
|
31
|
-
when Regexp
|
|
32
|
-
compare_regexp(actual_value, expected_value)
|
|
33
|
-
when Proc
|
|
34
|
-
compare_proc(actual_value, expected_value)
|
|
35
|
-
when Array
|
|
36
|
-
compare_array(actual_value, expected_value)
|
|
37
|
-
else
|
|
38
|
-
compare_simple_value(actual_value, expected_value)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def compare_class(actual_value, expected_value)
|
|
43
|
-
actual_value.instance_of?(expected_value)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def compare_regexp(actual_value, expected_value)
|
|
47
|
-
actual_value.to_s =~ expected_value
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def compare_proc(actual_value, expected_value)
|
|
51
|
-
payload = expected_value.call
|
|
52
|
-
payload.sanitize!(SUPPORTED_OPTIONS)
|
|
53
|
-
payload = payload.sort_by { |k, _v| k == :allow_blank ? 0 : 1 }.to_h
|
|
54
|
-
|
|
55
|
-
payload.all? do |condition_key, condition_value|
|
|
56
|
-
case condition_key
|
|
57
|
-
when :allow_blank
|
|
58
|
-
return true if actual_value.blank? && condition_value
|
|
59
|
-
|
|
60
|
-
true
|
|
61
|
-
when :type
|
|
62
|
-
compare_class(actual_value, condition_value)
|
|
63
|
-
when :value
|
|
64
|
-
compare_simple_value(actual_value, condition_value)
|
|
65
|
-
when :inclusion
|
|
66
|
-
condition_value.include?(actual_value)
|
|
67
|
-
when :min
|
|
68
|
-
return false if !condition_value.is_a?(Numeric) || !actual_value.is_a?(Numeric)
|
|
69
|
-
|
|
70
|
-
actual_value >= condition_value
|
|
71
|
-
when :max
|
|
72
|
-
return false if !condition_value.is_a?(Numeric) || !actual_value.is_a?(Numeric)
|
|
73
|
-
|
|
74
|
-
actual_value <= condition_value
|
|
75
|
-
when :regex
|
|
76
|
-
compare_regexp(actual_value, condition_value)
|
|
77
|
-
when :lambda
|
|
78
|
-
condition_value.call(actual_value)
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def compare_array(actual_value, expected_value)
|
|
84
|
-
if simple_type?(expected_value)
|
|
85
|
-
type = expected_value[0]
|
|
86
|
-
|
|
87
|
-
actual_value.all? { |elem| compare_class(elem, type) }
|
|
88
|
-
elsif interface?(expected_value)
|
|
89
|
-
interface = expected_value[0]
|
|
90
|
-
|
|
91
|
-
actual_value.all? { |elem| compare(elem, interface) }
|
|
92
|
-
else
|
|
93
|
-
return false if actual_value&.size != expected_value&.size
|
|
94
|
-
|
|
95
|
-
expected_value.each_with_index.all? do |elem, index|
|
|
96
|
-
elem.is_a?(Hash) ? compare(actual_value[index], elem) : compare_simple_value(actual_value[index], elem)
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
def compare_simple_value(actual_value, expected_value)
|
|
102
|
-
actual_value == expected_value
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def simple_type?(expected_value)
|
|
106
|
-
expected_value.size == 1 && expected_value[0].instance_of?(Class)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def interface?(expected_value)
|
|
110
|
-
expected_value.size == 1 && expected_value[0].is_a?(Hash)
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
end
|