hashes_equal 0.1.0 → 0.1.1

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: 723e208b4818b3724802a8fb3c54fbe6349ce1fe93ba477d4d1fd7fc4b23a73f
4
- data.tar.gz: c55f8bbb2b9c26d263b3b92f6349e4c23a1cfe5113ff3db57483627289087b14
3
+ metadata.gz: 592e8dd71868e0562bc22597481fe2976e66a1d98736b226b5081888845eee0c
4
+ data.tar.gz: 66a73f2d9ae4fadd454a25985d491fb3cab67aff4720c2c67a6173b902edcbcc
5
5
  SHA512:
6
- metadata.gz: d34411729d8d5a1b85c6f8f495946377410569afcccf53726ffeadbb69ab24e42e1677c76658c2ce5780ede5111beb33f6c32335404a0e954b372bd946d638d6
7
- data.tar.gz: 6913b25aac360fed3290b420780387db5b4bc646976e6347753e8d465d586eef0ac9a6c15c9287b4c205ddb667e284ce8f311e348727ca0848c83f08ec96ec4c
6
+ metadata.gz: 441e2cef13aad2345f16f3be8e67907731f055f1d3760ba6ca6d46e91f8ccd5e343c93d58fefd56c7a25b7490f1a4483a761a859c56c95775616b827a9686823
7
+ data.tar.gz: 255341610fea9f2e88d9f73a693396b9cf00e64b08ef8f3f5016de765d0064aa333b1a1ee8052f22c8503b5ac7d26209dd833543ab1a4d095363eb9c8959ff9c
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'hashes_equal/version'
4
4
  require 'hashes_equal/hash_compare_helper'
5
+ require 'hashes_equal/enumerable_compare_helper'
5
6
 
6
7
  module HashesEqual
7
8
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hashes_equal/enumerable_diff_displayer'
4
+
5
+ module HashesEqual
6
+ module EnumerableCompareHelper
7
+ def assert_enumerable_equal(expected, actual, verbose: true)
8
+ displayable_diff = EnumerableDiffDisplayer.new(
9
+ expected: expected,
10
+ actual: actual
11
+ ).call
12
+ if verbose
13
+ assert_equal expected, actual, displayable_diff
14
+ else
15
+ assert expected == actual, displayable_diff
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'assertable'
4
+ require 'hashdiff'
5
+ require 'hashes_equal/generic_diff_displayer'
6
+
7
+ module HashesEqual
8
+ class EnumerableDiffDisplayer < GenericDiffDisplayer
9
+ class ExpectationMustBeEnumerable < ArgumentError; end
10
+ class ActualValueMustBeEnumerable < ArgumentError; end
11
+
12
+ private
13
+
14
+ def check_args
15
+ super
16
+ raise ExpectationMustBeEnumerable unless expected.is_a?(Enumerable)
17
+ raise ActualValueMustBeEnumerable unless actual.is_a?(Enumerable)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'assertable'
4
+ require 'hashdiff'
5
+
6
+ module HashesEqual
7
+ class GenericDiffDisplayer
8
+ include Assertable
9
+
10
+ class UnprocessableHashdiff < ArgumentError; end
11
+
12
+ def initialize(expected:, actual:)
13
+ @expected = expected
14
+ @actual = actual
15
+ assert_send :expected
16
+ assert_send :actual
17
+ check_args
18
+ end
19
+
20
+ def call
21
+ compute_diff
22
+ ANSI.white do
23
+ formatted_diff
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :expected, :actual, :diff
30
+
31
+ def check_args
32
+ # Add specific checks if needed
33
+ end
34
+
35
+ def compute_diff
36
+ @diff = perform_diff_computation
37
+ end
38
+
39
+ def perform_diff_computation
40
+ Hashdiff.diff(expected, actual)
41
+ end
42
+
43
+ def formatted_diff
44
+ "\n" + diff.map do |diff_element|
45
+ format_element diff_element
46
+ end.join("\n")
47
+ end
48
+
49
+ def format_element(diff_element)
50
+ # exp_val is act_val in case of "+"
51
+ type, key, exp_val, act_val = diff_element
52
+ return missing_value_message(key, exp_val) if type == '-'
53
+ return spurious_value_message(key, exp_val) if type == '+'
54
+ return value_disagreement_message(key, exp_val, act_val) if type == '~'
55
+
56
+ raise UnprocessableHashdiff, diff_element
57
+ end
58
+
59
+ def missing_value_message(key, exp_val)
60
+ [
61
+ "actual value for #{ANSI.red { key }} is missing, expected was",
62
+ ANSI.green { exp_val.inspect }
63
+ ].join("\n\t")
64
+ end
65
+
66
+ def spurious_value_message(key, exp_val)
67
+ [
68
+ "spurious value #{ANSI.red { exp_val.inspect }}",
69
+ "for #{key} was not expected"
70
+ ].join(' ')
71
+ end
72
+
73
+ def value_disagreement_message(key, exp_val, act_val)
74
+ [
75
+ "values for #{key} differ",
76
+ "expected: #{ANSI.green { exp_val.inspect }}",
77
+ "actual: #{ANSI.red { act_val.inspect }}"
78
+ ].join("\n\t")
79
+ end
80
+ end
81
+ end
@@ -2,79 +2,19 @@
2
2
 
3
3
  require 'assertable'
4
4
  require 'hashdiff'
5
+ require 'hashes_equal/generic_diff_displayer'
5
6
 
6
7
  module HashesEqual
7
- class HashDiffDisplayer
8
- include Assertable
9
-
8
+ class HashDiffDisplayer < GenericDiffDisplayer
10
9
  class ExpectationMustBeHash < ArgumentError; end
11
10
  class ActualValueMustBeAHash < ArgumentError; end
12
- class UnprocessableHashdiff < ArgumentError; end
13
-
14
- def initialize(expected:, actual:)
15
- @expected = expected
16
- @actual = actual
17
- assert_send :expected
18
- assert_send :actual
19
- raise ExpectationMustBeHash unless expected.is_a?(Hash)
20
- raise ActualValueMustBeAHash unless actual.is_a?(Hash)
21
- end
22
-
23
- def call
24
- compute_diff
25
- ANSI.white do
26
- formatted_diff
27
- end
28
- end
29
11
 
30
12
  private
31
13
 
32
- attr_reader :expected, :actual, :diff
33
-
34
- def compute_diff
35
- @diff = perform_diff_computation
36
- end
37
-
38
- def perform_diff_computation
39
- Hashdiff.diff(expected, actual)
40
- end
41
-
42
- def formatted_diff
43
- "\n" + diff.map do |diff_element|
44
- format_element diff_element
45
- end.join("\n")
46
- end
47
-
48
- def format_element(diff_element)
49
- # exp_val is act_val in case of "+"
50
- type, key, exp_val, act_val = diff_element
51
- return missing_value_message(key, exp_val) if type == '-'
52
- return spurious_value_message(key, exp_val) if type == '+'
53
- return value_disagreement_message(key, exp_val, act_val) if type == '~'
54
-
55
- raise UnprocessableHashdiff, diff_element
56
- end
57
-
58
- def missing_value_message(key, exp_val)
59
- [
60
- "actual value for #{ANSI.red { key }} is missing, expected was",
61
- ANSI.green { exp_val.inspect }
62
- ].join("\n\t")
63
- end
64
-
65
- def spurious_value_message(key, exp_val)
66
- [
67
- "spurious value #{ANSI.red { exp_val.inspect }}",
68
- "for #{key} was not expected"
69
- ].join(' ')
70
- end
71
-
72
- def value_disagreement_message(key, exp_val, act_val)
73
- [
74
- "values for #{key} differ",
75
- "expected: #{ANSI.green { exp_val.inspect }}",
76
- "actual: #{ANSI.red { act_val.inspect }}"
77
- ].join("\n\t")
14
+ def check_args
15
+ super
16
+ raise ExpectationMustBeHash unless expected.is_a?(Hash)
17
+ raise ActualValueMustBeAHash unless actual.is_a?(Hash)
78
18
  end
79
19
  end
80
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HashesEqual
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashes_equal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shippingbo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-08 00:00:00.000000000 Z
11
+ date: 2020-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ansi
@@ -245,7 +245,6 @@ files:
245
245
  - ".travis.yml"
246
246
  - CODE_OF_CONDUCT.md
247
247
  - Gemfile
248
- - Gemfile.lock
249
248
  - Guardfile
250
249
  - LICENSE.txt
251
250
  - Makefile
@@ -255,6 +254,9 @@ files:
255
254
  - bin/setup
256
255
  - hashes_equal.gemspec
257
256
  - lib/hashes_equal.rb
257
+ - lib/hashes_equal/enumerable_compare_helper.rb
258
+ - lib/hashes_equal/enumerable_diff_displayer.rb
259
+ - lib/hashes_equal/generic_diff_displayer.rb
258
260
  - lib/hashes_equal/hash_compare_helper.rb
259
261
  - lib/hashes_equal/hash_diff_displayer.rb
260
262
  - lib/hashes_equal/version.rb