rspec-protobuf 0.2.4 → 0.3.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: d635dd29059d952dc1e0db41c3cef03aa4d9e18f098db7a61f34736821317831
4
- data.tar.gz: db9151a95b2f9d72898eaa5fedaa2b9ecbf0dd1d4bf5cb16506442f7cfcbba88
3
+ metadata.gz: 067f4cadf677ad482da31b4d3dbcb66cd53e79f434807d67e0e9522144109b32
4
+ data.tar.gz: fdc42f1600c8cbc1f97388a413ae81bded5cc8451e84128907854a453953d709
5
5
  SHA512:
6
- metadata.gz: 55abd020cfdbf915a4b6db65e674367333957e59ff86a0dab804d4807dad3e0d4e250c20b5a2043fc76ac88931293206719720e1844563125a46e56224705a33
7
- data.tar.gz: a485ad9cfd09055229c816f83baa2105659c3e50ebc5b3a053de6ec758c3bdd854969c606efc681a2f1cfda80821129efb7448cbe64d2bfd828e62ed54cf890b
6
+ metadata.gz: 548a3253d3e1c1f36e1bc00472822c61eaf6f96a02865ac71533aec137958a34bbd2d24dc5f4a170eb4949b4fcbf31f1966b62d546caf3bd3d3086b2a520a163
7
+ data.tar.gz: 514ea10c3836b87c2f82bc7b6e9cefa8c94514238791b76c462cad5fdbed82fe21af8ba1ade18dc7a616846e8c863aad4d118621e50dd2f1d792179930216ccf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ### v0.3.1 (2023-03-27)
2
+ - bugfix
3
+
4
+ ### v0.3.0 (2023-03-25)
5
+ - match repeated field
6
+ - match default values
7
+ - test coverage
8
+ - type check
9
+ - simplify
10
+ - symbol matching
11
+
1
12
  ### v0.2.4 (2023-03-15)
2
13
  - repeated field bug
3
14
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-protobuf (0.2.4)
4
+ rspec-protobuf (0.3.1)
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,50 @@ 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
+ expected === actual || 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
+ end
94
+ end
85
95
  end
86
96
  end
87
97
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module Protobuf
3
- VERSION = "0.2.4"
3
+ VERSION = "0.3.1"
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
 
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.4
4
+ version: 0.3.1
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-15 00:00:00.000000000 Z
11
+ date: 2023-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-protobuf