rspec-protobuf 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4edfb359c42bc196bc668c9d88916ed4cf05f9b0f465a39353efccd90dc73477
4
+ data.tar.gz: f5151e46350a73c03f25ac349a71ced8a295a573237aae678044711bd72328a3
5
+ SHA512:
6
+ metadata.gz: 8a7a6963bc85ed51a6bdfd7802784e5c9c9932f3d24932bbee9c46349112ec861ec8bc289bab12ef2818f1a34117023a45265899f42d66760dfa2b67bb32f062
7
+ data.tar.gz: dba4658ed4fa1cae57a4ea57a83867a9313968118a10659b0acfccd229d914c4b36df054fe70428c43eb5430496f0d319ea268ed57e1b6ec2f969cae539424bc
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### v0.1.0 (2022-11-08)
2
+ -
3
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Daniel Pepper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ rspec-protobuf
2
+ ======
3
+ ![Gem](https://img.shields.io/gem/dt/rspec-protobuf?style=plastic)
4
+ [![codecov](https://codecov.io/gh/dpep/rspec-protobuf/branch/main/graph/badge.svg)](https://codecov.io/gh/dpep/rspec-protobuf)
5
+
6
+ RSpec matchers for Protobuf.
7
+
8
+
9
+ ```ruby
10
+ require "rspec/protobuf"
11
+
12
+ subject { MyProtoMessage.new(msg: "hi") }
13
+
14
+ it { is_expected.to be_a_protobuf }
15
+ it { is_expected.to be_a_protobuf(msg: "hi") }
16
+ it { is_expected.to be_a_protobuf(msg: /^h/) }
17
+ ```
18
+
19
+
20
+ ----
21
+ ## Contributing
22
+
23
+ Yes please :)
24
+
25
+ 1. Fork it
26
+ 1. Create your feature branch (`git checkout -b my-feature`)
27
+ 1. Ensure the tests pass (`bundle exec rspec`)
28
+ 1. Commit your changes (`git commit -am 'awesome new feature'`)
29
+ 1. Push your branch (`git push origin my-feature`)
30
+ 1. Create a Pull Request
@@ -0,0 +1,69 @@
1
+ module RSpec
2
+ module Protobuf
3
+ module Refinements
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
+ def include?(*args)
24
+ expected_attrs = Hash === args.last ? args.pop : {}
25
+
26
+ # ensure all enumerated keys are present
27
+ return false unless args.all? { |attr| respond_to?(attr) }
28
+
29
+ # ensure all enumerated attributes are present
30
+ return false unless expected_attrs.keys.all? { |attr| respond_to?(attr) }
31
+
32
+ # check expected attribute matches
33
+ expected_attrs.all? do |expected_attr, expected_value|
34
+ actual_value = send(expected_attr)
35
+
36
+ matches = expected_value === actual_value
37
+
38
+ if actual_value.is_a?(Google::Protobuf::MessageExts)
39
+ if expected_value.is_a?(Hash)
40
+ matches ||= actual_value.match?(**expected_value)
41
+ else
42
+ matches ||= expected_value === actual_value.to_h
43
+ end
44
+ end
45
+
46
+ matches
47
+ end
48
+ end
49
+
50
+ def match?(**attrs)
51
+ # ensure all enumerated keys are present
52
+ return false unless attrs.keys.all? { |attr| respond_to?(attr) }
53
+
54
+ # ensure each field matches
55
+ self.class.descriptor.all? do |field|
56
+ actual_value = field.get(self)
57
+ expected_value = attrs[field.name.to_sym]
58
+
59
+ if actual_value.is_a?(Google::Protobuf::MessageExts) && expected_value.is_a?(Hash)
60
+ actual_value.match?(**expected_value)
61
+ else
62
+ attrs.fetch(field.name.to_sym, field.default) === actual_value
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ module RSpec
2
+ module Protobuf
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,42 @@
1
+ require "google/protobuf"
2
+ require "rspec"
3
+ require "rspec/protobuf/refinements"
4
+ require "rspec/protobuf/version"
5
+
6
+ using RSpec::Protobuf::Refinements
7
+
8
+ RSpec::Matchers.define :be_a_protobuf do |type = nil, **attrs|
9
+ match do |actual|
10
+ # ensure type is a valid Protobuf message type
11
+ if type && !(type < Google::Protobuf::MessageExts)
12
+ raise TypeError, "Expected areg to be a Google::Protobuf::MessageExts, found: #{type}"
13
+ end
14
+
15
+ return false unless actual.is_a?(Google::Protobuf::MessageExts)
16
+
17
+ # match expected message type
18
+ @fail_msg = "Expected a Protobuf message of type #{type}, found #{actual.class}"
19
+ return false if type && actual.class != type
20
+
21
+ return true if attrs.empty?
22
+
23
+ if type
24
+ # validate attrs
25
+ invalid_attrs = attrs.keys - type.descriptor.map { |x| x.name.to_sym }
26
+
27
+ unless invalid_attrs.empty?
28
+ raise ArgumentError, "invalid attribute for #{type}: #{invalid_attrs.join(", ")}"
29
+ end
30
+ end
31
+
32
+ actual.include?(attrs)
33
+ end
34
+
35
+ description do
36
+ type ? "a #{type} Protobuf message" : "a Protobuf message"
37
+ end
38
+
39
+ failure_message { @fail_msg }
40
+ end
41
+
42
+ RSpec::Matchers.alias_matcher :a_protobuf, :be_a_protobuf
@@ -0,0 +1 @@
1
+ require "rspec/protobuf"
@@ -0,0 +1,23 @@
1
+ package_name = File.basename(__FILE__).split(".")[0]
2
+ load Dir.glob("lib/**/version.rb")[0]
3
+
4
+ package = RSpec::Protobuf
5
+
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = package_name
9
+ s.version = package.const_get "VERSION"
10
+ s.authors = ["Daniel Pepper"]
11
+ s.summary = package.to_s
12
+ s.description = "RSpec matchers for Protobuf"
13
+ s.homepage = "https://github.com/dpep/#{package_name}"
14
+ s.license = "MIT"
15
+ s.files = `git ls-files * ':!:spec'`.split("\n")
16
+
17
+ s.add_dependency "google-protobuf", ">= 3"
18
+ s.add_dependency "rspec", ">= 3"
19
+
20
+ s.add_development_dependency "byebug"
21
+ s.add_development_dependency "codecov"
22
+ s.add_development_dependency "simplecov"
23
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-protobuf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Pepper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-protobuf
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: RSpec matchers for Protobuf
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - CHANGELOG.md
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.md
93
+ - lib/rspec-protobuf.rb
94
+ - lib/rspec/protobuf.rb
95
+ - lib/rspec/protobuf/refinements.rb
96
+ - lib/rspec/protobuf/version.rb
97
+ - rspec-protobuf.gemspec
98
+ homepage: https://github.com/dpep/rspec-protobuf
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.3.7
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Gem::Specification::RSpec::Protobuf
121
+ test_files: []