json_rspec_match_maker 0.1.2 → 1.0.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/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +1 -1
- data/Gemfile +2 -0
- data/Gemfile.lock +1 -1
- data/Rakefile +2 -0
- data/bin/console +1 -0
- data/json_rspec_match_maker.gemspec +2 -0
- data/lib/json_rspec_match_maker/base.rb +35 -21
- data/lib/json_rspec_match_maker/expected_value.rb +18 -2
- data/lib/json_rspec_match_maker/target_value.rb +2 -0
- data/lib/json_rspec_match_maker/version.rb +3 -1
- data/lib/json_rspec_match_maker.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b5b140faa0ef46843473e4c028a1d612bfb0321cd63538d36277f97cf9b129e
|
4
|
+
data.tar.gz: b59d16bc22e7f57d7f2b6fadc5faf71aaa1e2a2b0daf902ae194d656eefba551
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9f543a7915141e2faf82ba1e4da26f0c347be97ac87ec44e5c3b2635807f4e1c8b774d4d13c480d1364de61b85c1ea3248523e963a093bdcfd4c787d9c1fe41
|
7
|
+
data.tar.gz: e64e5696fb2227dff2483c48dea976a9e8934aaf3bb529e776a73abc17c010d7fa2e2cfc6cf581d5bec05df24a21b4ae94aab5050cd07fb2c540c8f9d6558776
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2018-
|
3
|
+
# on 2018-05-15 14:13:45 -0400 using RuboCop version 0.52.1.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,14 +1,6 @@
|
|
1
|
-
|
2
|
-
# Error raised when the child class has failed to set @match_definition
|
3
|
-
# @api private
|
4
|
-
class MatchDefinitionNotFound < StandardError
|
5
|
-
# Create an error message for a child class
|
6
|
-
# @param class_name [String] the name of the matcher class
|
7
|
-
def initialize(class_name)
|
8
|
-
super("Expected instance variable @match_defintion to be set for #{class_name}")
|
9
|
-
end
|
10
|
-
end
|
1
|
+
# frozen_string_literal: true
|
11
2
|
|
3
|
+
module JsonRspecMatchMaker
|
12
4
|
# Base class that abstracts away all of the work of using the @match_definition
|
13
5
|
class Base
|
14
6
|
# The object being expected against
|
@@ -32,8 +24,9 @@ module JsonRspecMatchMaker
|
|
32
24
|
# @example
|
33
25
|
# JsonRspecMatchMaker.new(active_record_model)
|
34
26
|
# JsonRspecMatchMaker.new(presenter_instance)
|
35
|
-
def initialize(expected)
|
27
|
+
def initialize(expected, match_definition)
|
36
28
|
@expected = expected
|
29
|
+
@match_definition = expand_definition(match_definition)
|
37
30
|
@errors = {}
|
38
31
|
end
|
39
32
|
|
@@ -60,22 +53,43 @@ module JsonRspecMatchMaker
|
|
60
53
|
|
61
54
|
private
|
62
55
|
|
56
|
+
# expands simple arrays into full hash definitions
|
57
|
+
# @api private
|
58
|
+
def expand_definition(definition)
|
59
|
+
return definition if definition.is_a? Hash
|
60
|
+
definition.each_with_object({}) do |key, result|
|
61
|
+
if key.is_a? String
|
62
|
+
result[key] = :default
|
63
|
+
elsif key.is_a? Hash
|
64
|
+
result.merge!(expand_sub_definition(key))
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# expands nested simple definition into a full hash
|
70
|
+
# @api private
|
71
|
+
def expand_sub_definition(sub_definition)
|
72
|
+
subkey = sub_definition.keys.first
|
73
|
+
return sub_definition if sub_definition[subkey].respond_to? :call
|
74
|
+
sub_definition[subkey][:attributes] = expand_definition(sub_definition[subkey][:attributes])
|
75
|
+
sub_definition
|
76
|
+
end
|
77
|
+
|
63
78
|
# Walks through the match definition, collecting errors for each field
|
64
79
|
# @api private
|
65
80
|
# @raise [MatchDefinitionNotFound] if child class does not set @match_definition
|
66
81
|
# @return [nil] returns nothing, adds to error list as side effect
|
67
82
|
def check_target_against_expected
|
68
|
-
|
69
|
-
check_definition(@match_definition)
|
83
|
+
check_definition(@match_definition, expected)
|
70
84
|
end
|
71
85
|
|
72
|
-
def check_definition(definition, current_expected
|
86
|
+
def check_definition(definition, current_expected, current_key = nil)
|
73
87
|
definition.each do |error_key, match_def|
|
74
|
-
|
75
|
-
|
76
|
-
check_values(key, match_def, current_expected)
|
77
|
-
else
|
88
|
+
if match_def.is_a? Hash
|
89
|
+
key = [current_key, error_key].compact.join('.')
|
78
90
|
check_each(key, match_def, current_expected)
|
91
|
+
else
|
92
|
+
check_values(current_key, error_key, match_def, current_expected)
|
79
93
|
end
|
80
94
|
end
|
81
95
|
end
|
@@ -111,9 +125,9 @@ module JsonRspecMatchMaker
|
|
111
125
|
# :error_key the subfield reported in the error
|
112
126
|
# the index if iterating through a list, otherwise nil
|
113
127
|
# @return [nil] returns nothing, adds to error list as side effect
|
114
|
-
def check_values(error_key, match_function, expected_instance = expected)
|
115
|
-
expected_value = ExpectedValue.new(match_function, expected_instance)
|
116
|
-
target_value = TargetValue.new(error_key, target)
|
128
|
+
def check_values(key_prefix, error_key, match_function, expected_instance = expected)
|
129
|
+
expected_value = ExpectedValue.new(match_function, expected_instance, error_key)
|
130
|
+
target_value = TargetValue.new([key_prefix, error_key].compact.join('.'), target)
|
117
131
|
add_error(expected_value, target_value) unless expected_value == target_value
|
118
132
|
end
|
119
133
|
|
@@ -1,14 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module JsonRspecMatchMaker
|
2
4
|
# Handles fetching the expected value from the expected instance
|
3
5
|
class ExpectedValue
|
4
6
|
attr_reader :value
|
5
|
-
def initialize(match_function, expected_instance)
|
6
|
-
@value =
|
7
|
+
def initialize(match_function, expected_instance, error_key)
|
8
|
+
@value = fetch_expected_value(expected_instance, match_function, error_key)
|
7
9
|
end
|
8
10
|
|
9
11
|
def ==(other)
|
10
12
|
raise ArgumentError unless other.is_a? TargetValue
|
11
13
|
other.value == value
|
12
14
|
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def fetch_expected_value(instance, function, key)
|
19
|
+
if function == :default
|
20
|
+
key.split('.').inject(instance) do |expected, k|
|
21
|
+
expected&.send k
|
22
|
+
rescue NoMethodError
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
else
|
26
|
+
function.call(instance)
|
27
|
+
end
|
28
|
+
end
|
13
29
|
end
|
14
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_rspec_match_maker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick McGee
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|