json_rspec_match_maker 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4bae227cfdefaa6a79eb4f3000bd7288dbfe98d1959245811f5e5bdf831838e5
4
- data.tar.gz: c4bb1ec682a23d791ee73caba8775242c3a6c90e64a10e77887556b244cc841b
3
+ metadata.gz: 1b5b140faa0ef46843473e4c028a1d612bfb0321cd63538d36277f97cf9b129e
4
+ data.tar.gz: b59d16bc22e7f57d7f2b6fadc5faf71aaa1e2a2b0daf902ae194d656eefba551
5
5
  SHA512:
6
- metadata.gz: 98cd96ca937e649b76f97957c7291344e1b64a968548dba869641f6499bbfb207f3e7b8e21b4c0dc179808e6c9bfc78c39dff7711c6004af4de24edd4b3ff8d7
7
- data.tar.gz: 78f0bb747f35df684811b0b2c94814e1f542af1c9e2c0ae12c8c780bb8afc8881b09a4aedac45b4c611c74a0107db7c994a637016af08ef0bd8b14912b7b0b9c
6
+ metadata.gz: c9f543a7915141e2faf82ba1e4da26f0c347be97ac87ec44e5c3b2635807f4e1c8b774d4d13c480d1364de61b85c1ea3248523e963a093bdcfd4c787d9c1fe41
7
+ data.tar.gz: e64e5696fb2227dff2483c48dea976a9e8934aaf3bb529e776a73abc17c010d7fa2e2cfc6cf581d5bec05df24a21b4ae94aab5050cd07fb2c540c8f9d6558776
data/.rubocop.yml CHANGED
@@ -1,4 +1,7 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
+ AllCops:
4
+ TargetRubyVersion: 2.5.1
5
+
3
6
  Metrics/LineLength:
4
7
  Max: 100
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-03-10 16:28:20 -0500 using RuboCop version 0.52.1.
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_rspec_match_maker (0.1.2)
4
+ json_rspec_match_maker (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rake/testtask'
3
5
 
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'bundler/setup'
4
5
  require 'json_rspec_match_maker'
@@ -1,4 +1,6 @@
1
1
 
2
+ # frozen_string_literal: true
3
+
2
4
  lib = File.expand_path('lib', __dir__)
3
5
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
6
  require 'json_rspec_match_maker/version'
@@ -1,14 +1,6 @@
1
- module JsonRspecMatchMaker
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
- raise MatchDefinitionNotFound, self.class.name unless @match_definition
69
- check_definition(@match_definition)
83
+ check_definition(@match_definition, expected)
70
84
  end
71
85
 
72
- def check_definition(definition, current_expected = expected, current_key = nil)
86
+ def check_definition(definition, current_expected, current_key = nil)
73
87
  definition.each do |error_key, match_def|
74
- key = [current_key, error_key].compact.join('.')
75
- if match_def.respond_to? :call
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 = match_function.call(expected_instance)
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JsonRspecMatchMaker
2
4
  # Handles fetching the target value from the target object
3
5
  class TargetValue
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module JsonRspecMatchMaker
2
- VERSION = '0.1.2'.freeze
4
+ VERSION = '1.0.0'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json_rspec_match_maker/version'
2
4
  require 'json_rspec_match_maker/expected_value'
3
5
  require 'json_rspec_match_maker/target_value'
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.1.2
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-10 00:00:00.000000000 Z
11
+ date: 2018-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler