rspec-protobuf 0.2.3 → 0.3.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: 329f818e153685d7b3c9295ca051543fe6915f9277478576892152359ebd6498
4
- data.tar.gz: 8b79e6616adac2977f473aac8e7dab6f12efd343b2e2f9af987e597a2ee166dc
3
+ metadata.gz: ae8739f180ed388f80da7486fb02c809a4cb81aa524c63986fa15d4a002b30f9
4
+ data.tar.gz: 11be76d911202e00bdc6084c173754d835df59eb363f239d2ae46289ce85c023
5
5
  SHA512:
6
- metadata.gz: 6597c162046f769e115efbfe4f1a83b027e1d27bf850873c97df598073707156773cd05a61ff771e6e4c626eac182403a21b8c47878adb53663b1da042599de5
7
- data.tar.gz: 83458a56e5694c6766c99b4f86ab1eb63b2871adb2c9e559cc1bd045c85855f6fd1629aca670089dd11dbf5b0462bae27da2d000248d0f8ff9c2f7d56570e387
6
+ metadata.gz: bca33b284f98f8a48fc5b5b6a69ef927d8d29212a1c093c72cfcda7425476a7d8cdaed617866f045b6b62e688b6f7165e93c4eca28b39c2476a8454f32480c67
7
+ data.tar.gz: 1972979254b796caafed01a06c298e7ad578e3171516363bb4c0aaf720527ce8cfd51f77501dd267ebd4e9f0bc97d10e9f4ce6e43095cedff19cb6cdb14e2008
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ### v0.3.0 (2023-03-25)
2
+ - match repeated field
3
+ - match default values
4
+ - test coverage
5
+ - type check
6
+ - simplify
7
+ - symbol matching
8
+
9
+ ### v0.2.4 (2023-03-15)
10
+ - repeated field bug
11
+
1
12
  ### v0.2.3 (2023-03-14)
2
13
  - repeated fields
3
14
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-protobuf (0.2.3)
4
+ rspec-protobuf (0.3.0)
5
5
  google-protobuf (>= 3)
6
6
  rspec-expectations (>= 3)
7
7
 
@@ -5,63 +5,29 @@ module RSpec
5
5
  def include?(*args)
6
6
  expected_attrs = Hash === args.last ? args.pop : {}
7
7
 
8
- # ensure all enumerated keys are present
8
+ # ensure all enumerated keys are indeed attributes
9
9
  return false unless args.all? { |attr| respond_to?(attr) }
10
10
 
11
11
  # ensure all enumerated attributes are present
12
12
  return false unless expected_attrs.keys.all? { |attr| respond_to?(attr) }
13
13
 
14
- fields = {}
15
- self.class.descriptor.each { |f| fields[f.name.to_sym] = f }
14
+ fields = Hash[
15
+ self.class.descriptor.map { |f| [ f.name.to_sym, f ] }
16
+ ]
16
17
 
17
- # check expected attribute matches
18
+ # ensure that expected attribute matches
18
19
  expected_attrs.all? do |expected_attr, expected_value|
19
- field = fields[expected_attr]
20
- actual_value = field.get(self)
21
-
22
- # convert enum to int value to match input type
23
- if field.type == :enum && expected_value.is_a?(Integer)
24
- actual_value = field.subtype.lookup_name(actual_value)
25
- end
26
-
27
- matches = expected_value === actual_value
28
-
29
- if actual_value.is_a?(Google::Protobuf::MessageExts)
30
- if expected_value.is_a?(Hash)
31
- matches ||= actual_value.match?(**expected_value)
32
- else
33
- matches ||= expected_value === actual_value.to_h
34
- end
35
- end
36
-
37
- matches
20
+ field_matches?(fields[expected_attr], expected_value)
38
21
  end
39
22
  end
40
23
 
41
- def match?(**attrs)
42
- # ensure all enumerated keys are present
24
+ def matches?(**attrs)
25
+ # ensure all enumerated attributes are present
43
26
  return false unless attrs.keys.all? { |attr| respond_to?(attr) }
44
27
 
45
- # ensure each field matches
28
+ # ensure that each field matches
46
29
  self.class.descriptor.all? do |field|
47
- actual_value = field.get(self)
48
- expected_value = attrs[field.name.to_sym]
49
-
50
- if actual_value.is_a?(Google::Protobuf::MessageExts) && expected_value.is_a?(Hash)
51
- actual_value.match?(**expected_value)
52
- else
53
- unless attrs.key?(field.name.to_sym)
54
- # fall back to default value
55
- expected_value = field.label == :repeated ? [] : field.default
56
- end
57
-
58
- # convert enum to int value to match input type
59
- if field.type == :enum && expected_value.is_a?(Integer)
60
- actual_value = field.subtype.lookup_name(actual_value)
61
- end
62
-
63
- expected_value === actual_value
64
- end
30
+ field_matches?(field, attrs[field.name.to_sym])
65
31
  end
66
32
  end
67
33
 
@@ -82,6 +48,52 @@ module RSpec
82
48
 
83
49
  res
84
50
  end
51
+
52
+ private
53
+
54
+ def field_matches?(field, expected)
55
+ actual = field.get(self)
56
+
57
+ if expected.nil?
58
+ # mimic protobuf behavior and convert to default value
59
+ expected = field.label == :repeated ? [] : field.default
60
+ elsif expected.is_a?(Symbol) && field.type == :string
61
+ # convert symbols to strings
62
+ expected = expected.to_s
63
+ end
64
+
65
+ # convert enum to int value to match input type
66
+ if field.type == :enum && expected.is_a?(Integer)
67
+ actual = field.subtype.lookup_name(actual)
68
+ end
69
+
70
+ if field.label == :repeated && expected.is_a?(Array)
71
+ return false unless actual.length == expected.length
72
+
73
+ actual.zip(expected).all? do |a_actual, a_expected|
74
+ values_match?(a_actual, a_expected)
75
+ end
76
+ else
77
+ values_match?(actual, expected)
78
+ end
79
+ end
80
+
81
+ def values_match?(actual, expected)
82
+ if actual.is_a?(Google::Protobuf::MessageExts)
83
+ case expected
84
+ when Google::Protobuf::MessageExts
85
+ expected === actual
86
+ when Hash
87
+ # recurse
88
+ actual.matches?(**expected)
89
+ else
90
+ # eg. RSpec matchers
91
+ expected === actual.to_h
92
+ end
93
+ else
94
+ expected === actual
95
+ end
96
+ end
85
97
  end
86
98
  end
87
99
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Protobuf
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -8,7 +8,7 @@ using RSpec::Protobuf::Refinements
8
8
  RSpec::Matchers.define :be_a_protobuf do |type = nil, **attrs|
9
9
  match do |actual|
10
10
  # ensure type is a valid Protobuf message type
11
- if type && !(type < Google::Protobuf::MessageExts)
11
+ if type && !(type.is_a?(Class) && type < Google::Protobuf::MessageExts)
12
12
  raise TypeError, "Expected arg to be a Google::Protobuf::MessageExts, found: #{type}"
13
13
  end
14
14
 
@@ -34,7 +34,9 @@ RSpec::Matchers.define :be_a_protobuf do |type = nil, **attrs|
34
34
 
35
35
  # customize differ output by removing unreferenced and default attrs
36
36
  @actual = actual.normalized_hash.slice(*attrs.keys).reject do |k, v|
37
- v == actual.class.descriptor.lookup(k.to_s).default
37
+ field = actual.class.descriptor.lookup(k.to_s)
38
+ default = field.label == :repeated ? [] : field.default
39
+ v == default
38
40
  end
39
41
 
40
42
  actual.include?(attrs)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-protobuf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-14 00:00:00.000000000 Z
11
+ date: 2023-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-protobuf