rspec-protobuf 0.1.0 → 0.2.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/CHANGELOG.md +5 -0
- data/README.md +20 -0
- data/lib/rspec/protobuf/refinements.rb +17 -18
- data/lib/rspec/protobuf/version.rb +1 -1
- data/lib/rspec/protobuf.rb +12 -2
- 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: ff057c1fafc371beb4b2d2890300d27ef142158299190715fb9bef3891a24ec6
|
4
|
+
data.tar.gz: a9fa3d68d62d37fe63f7b556e05f221afe0de2ebe34bbf47bd91d313e04d06b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5a7bd6137681147eeb3d97c9e8c162a2e71e7de3d6b2d0f1d4cac95449ea179b6e784c2b428f42fb4dc5912eb19ccbbfea5d1caec6864d7b21d339cb8d23c4e
|
7
|
+
data.tar.gz: a8bb57417121161efdcb3f0b876b93e1c5da578ab90c40d96617cb1eb8c04719d5a372d5edcf294d8edbbea2628d67312a65b94d43329b2bc2a33f3a5319690a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -17,6 +17,26 @@ it { is_expected.to be_a_protobuf(msg: /^h/) }
|
|
17
17
|
```
|
18
18
|
|
19
19
|
|
20
|
+
### Improved RSpec Errors
|
21
|
+
Before
|
22
|
+
```ruby
|
23
|
+
Failure/Error: is_expected.to have_attributes(date: { month: 2 })
|
24
|
+
expected <ComplexMessage: complex: false, date: <DateMessage: type: :DATE_DEFAULT, month: 1, day: 0, year: 0>> to have attributes {:date => {:month => 2}} but had attributes {:date => <DateMessage: type: :DATE_DEFAULT, month: 1, day: 0, year: 0>}
|
25
|
+
Diff:
|
26
|
+
@@ -1 +1 @@
|
27
|
+
-:date => {:month=>2},
|
28
|
+
+:date => <DateMessage: type: :DATE_DEFAULT, month: 1, day: 0, year: 0>,
|
29
|
+
```
|
30
|
+
|
31
|
+
After
|
32
|
+
```ruby
|
33
|
+
Failure/Error: is_expected.to be_a_protobuf(date: { month: 2 })
|
34
|
+
Diff:
|
35
|
+
@@ -1 +1 @@
|
36
|
+
-:date => {:month=>2},
|
37
|
+
+:date => {:month=>1},
|
38
|
+
```
|
39
|
+
|
20
40
|
----
|
21
41
|
## Contributing
|
22
42
|
|
@@ -2,24 +2,6 @@ module RSpec
|
|
2
2
|
module Protobuf
|
3
3
|
module Refinements
|
4
4
|
refine Google::Protobuf::MessageExts do
|
5
|
-
def normalized_hash(symbolize_keys: true)
|
6
|
-
res = {}
|
7
|
-
|
8
|
-
self.class.descriptor.each do |field|
|
9
|
-
key = symbolize_keys ? field.name.to_sym : field.name
|
10
|
-
value = field.get(self)
|
11
|
-
|
12
|
-
if value.is_a?(Google::Protobuf::MessageExts)
|
13
|
-
# recursively serialize sub-message
|
14
|
-
value = value.normalized_hash(symbolize_keys: symbolize_keys)
|
15
|
-
end
|
16
|
-
|
17
|
-
res[key] = value unless field.default == value
|
18
|
-
end
|
19
|
-
|
20
|
-
res
|
21
|
-
end
|
22
|
-
|
23
5
|
def include?(*args)
|
24
6
|
expected_attrs = Hash === args.last ? args.pop : {}
|
25
7
|
|
@@ -63,6 +45,23 @@ module RSpec
|
|
63
45
|
end
|
64
46
|
end
|
65
47
|
end
|
48
|
+
|
49
|
+
def normalized_hash
|
50
|
+
res = {}
|
51
|
+
|
52
|
+
self.class.descriptor.each do |field|
|
53
|
+
value = field.get(self)
|
54
|
+
|
55
|
+
if value.is_a?(Google::Protobuf::MessageExts)
|
56
|
+
# recursively serialize sub-message
|
57
|
+
value = value.normalized_hash
|
58
|
+
end
|
59
|
+
|
60
|
+
res[field.name.to_sym] = value unless field.default == value
|
61
|
+
end
|
62
|
+
|
63
|
+
res
|
64
|
+
end
|
66
65
|
end
|
67
66
|
end
|
68
67
|
end
|
data/lib/rspec/protobuf.rb
CHANGED
@@ -9,9 +9,10 @@ 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
11
|
if type && !(type < Google::Protobuf::MessageExts)
|
12
|
-
raise TypeError, "Expected
|
12
|
+
raise TypeError, "Expected arg to be a Google::Protobuf::MessageExts, found: #{type}"
|
13
13
|
end
|
14
14
|
|
15
|
+
@fail_msg = "#{actual} is not a Protobuf"
|
15
16
|
return false unless actual.is_a?(Google::Protobuf::MessageExts)
|
16
17
|
|
17
18
|
# match expected message type
|
@@ -29,14 +30,23 @@ RSpec::Matchers.define :be_a_protobuf do |type = nil, **attrs|
|
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
33
|
+
@fail_msg = ""
|
34
|
+
|
35
|
+
# customize differ output by removing unreferenced and default attrs
|
36
|
+
@actual = actual.normalized_hash.slice(*attrs.keys).reject do |k, v|
|
37
|
+
v == actual.class.descriptor.lookup(k.to_s).default
|
38
|
+
end
|
39
|
+
|
32
40
|
actual.include?(attrs)
|
33
41
|
end
|
34
42
|
|
35
43
|
description do
|
36
|
-
type ? "a #{type} Protobuf
|
44
|
+
type ? "a #{type} Protobuf" : "a Protobuf"
|
37
45
|
end
|
38
46
|
|
39
47
|
failure_message { @fail_msg }
|
48
|
+
|
49
|
+
diffable
|
40
50
|
end
|
41
51
|
|
42
52
|
RSpec::Matchers.alias_matcher :a_protobuf, :be_a_protobuf
|
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.
|
4
|
+
version: 0.2.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: 2022-11-
|
11
|
+
date: 2022-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-protobuf
|