rspec-twirp 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/lib/rspec/twirp/error_matcher.rb +14 -1
- data/lib/rspec/twirp/helpers.rb +16 -0
- data/lib/rspec/twirp/make_request_matcher.rb +169 -0
- data/lib/rspec/twirp/message_matcher.rb +61 -0
- data/lib/rspec/twirp/mock_client.rb +59 -0
- data/lib/rspec/twirp/mock_connection.rb +54 -0
- data/lib/rspec/twirp/response_matcher.rb +36 -16
- data/lib/rspec/twirp/version.rb +1 -1
- data/lib/rspec/twirp.rb +42 -20
- data/spec/error_spec.rb +30 -0
- data/spec/make_request_spec.rb +181 -0
- data/spec/message_spec.rb +83 -0
- data/spec/mock_connection_spec.rb +103 -0
- data/spec/response_spec.rb +100 -32
- metadata +24 -19
- data/lib/rspec/twirp/client_response_matcher.rb +0 -48
- data/lib/rspec/twirp/request_matcher.rb +0 -37
- data/spec/client_response_spec.rb +0 -112
- data/spec/request_spec.rb +0 -75
@@ -1,37 +0,0 @@
|
|
1
|
-
RSpec::Matchers.define :be_a_twirp_request do |type = nil, **attrs|
|
2
|
-
match do |actual|
|
3
|
-
# ensure type is a valid twirp request type
|
4
|
-
if type && !(type < Google::Protobuf::MessageExts)
|
5
|
-
raise ArgumentError, "Expected `type` to be a Twirp request, found: #{type}"
|
6
|
-
end
|
7
|
-
|
8
|
-
@fail_msg = "Expected a Twirp request, found #{actual}"
|
9
|
-
return false unless actual.is_a?(Google::Protobuf::MessageExts)
|
10
|
-
|
11
|
-
# match expected request type
|
12
|
-
@fail_msg = "Expected a Twirp request of type #{type}, found #{actual.class}"
|
13
|
-
return false if type && actual.class != type
|
14
|
-
|
15
|
-
return true if attrs.empty?
|
16
|
-
|
17
|
-
RSpec::Twirp.validate_types(attrs, actual.class)
|
18
|
-
|
19
|
-
# match attributes which are present
|
20
|
-
attrs.each do |attr_name, expected_attr|
|
21
|
-
actual_attr = actual.send(attr_name)
|
22
|
-
|
23
|
-
@fail_msg = "Expected #{actual} to have #{attr_name}: #{expected_attr.inspect}, found #{actual_attr}"
|
24
|
-
return false unless values_match?(expected_attr, actual_attr)
|
25
|
-
end
|
26
|
-
|
27
|
-
true
|
28
|
-
end
|
29
|
-
|
30
|
-
description do
|
31
|
-
type ? "a #{type} Twirp request" : "a Twirp request"
|
32
|
-
end
|
33
|
-
|
34
|
-
failure_message { @fail_msg }
|
35
|
-
end
|
36
|
-
|
37
|
-
RSpec::Matchers.alias_matcher :a_twirp_request, :be_a_twirp_request
|
@@ -1,112 +0,0 @@
|
|
1
|
-
describe "be_a_twirp_client_response" do
|
2
|
-
context "with a response" do
|
3
|
-
subject { Twirp::ClientResp.new(GoodbyeResponse.new, nil) }
|
4
|
-
|
5
|
-
it { is_expected.to be_a_twirp_client_response }
|
6
|
-
|
7
|
-
it "catches non-twirp response" do
|
8
|
-
expect {
|
9
|
-
expect(Object).to be_a_twirp_client_response
|
10
|
-
}.to fail_with /found Object/
|
11
|
-
end
|
12
|
-
|
13
|
-
it "matches a specific response type" do
|
14
|
-
is_expected.to be_a_twirp_client_response(GoodbyeResponse)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "catches type mismatches" do
|
18
|
-
expect {
|
19
|
-
is_expected.to be_a_twirp_client_response(HelloResponse)
|
20
|
-
}.to fail_with /of type HelloResponse/
|
21
|
-
end
|
22
|
-
|
23
|
-
it "catches erroneous response types" do
|
24
|
-
expect {
|
25
|
-
is_expected.to be_a_twirp_client_response(Object)
|
26
|
-
}.to raise_error(ArgumentError, /Object/)
|
27
|
-
end
|
28
|
-
|
29
|
-
context "with attributes" do
|
30
|
-
subject { Twirp::ClientResp.new(GoodbyeResponse.new(**attrs), nil) }
|
31
|
-
|
32
|
-
let(:attrs) { { message: "bye", name: "Bob" } }
|
33
|
-
|
34
|
-
it "can match attributes" do
|
35
|
-
is_expected.to be_a_twirp_client_response(GoodbyeResponse, **attrs)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "supports regex matches" do
|
39
|
-
is_expected.to be_a_twirp_client_response(name: /^B/)
|
40
|
-
end
|
41
|
-
|
42
|
-
it "catches mismatches" do
|
43
|
-
expect {
|
44
|
-
is_expected.to be_a_twirp_client_response(name: "nope")
|
45
|
-
}.to fail_with /to have name: "nope"/
|
46
|
-
|
47
|
-
expect {
|
48
|
-
is_expected.to be_a_twirp_client_response(name: /no/)
|
49
|
-
}.to fail_with /to have name: \/no\//
|
50
|
-
end
|
51
|
-
|
52
|
-
it "catches the erroneous attributes" do
|
53
|
-
expect {
|
54
|
-
is_expected.to be_a_twirp_client_response(namezzz: "Bob")
|
55
|
-
}.to raise_error(ArgumentError, /namezzz/)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "catches type mismatches" do
|
59
|
-
expect {
|
60
|
-
is_expected.to be_a_twirp_client_response(name: 123)
|
61
|
-
}.to raise_error(TypeError, /string field.*given Integer/)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "with error" do
|
67
|
-
subject { Twirp::ClientResp.new(nil, error) }
|
68
|
-
|
69
|
-
let(:error) { Twirp::Error.new(code, msg, meta) }
|
70
|
-
let(:code) { :not_found }
|
71
|
-
let(:msg) { "Not Found" }
|
72
|
-
let(:meta) { { is_meta: "true" } }
|
73
|
-
|
74
|
-
it { is_expected.to be_a_twirp_client_response.with_error }
|
75
|
-
it { is_expected.to be_a_twirp_client_response.with_error(code) }
|
76
|
-
it { is_expected.to be_a_twirp_client_response.with_error(msg) }
|
77
|
-
it { is_expected.to be_a_twirp_client_response.with_error(**meta) }
|
78
|
-
it { is_expected.to be_a_twirp_client_response.with_error(/Not/) }
|
79
|
-
|
80
|
-
it "catches mismatches" do
|
81
|
-
expect {
|
82
|
-
is_expected.to be_a_twirp_client_response.with_error(:internal)
|
83
|
-
}.to fail_with /code: :internal/
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
context "with neither response nor error" do
|
88
|
-
subject { Twirp::ClientResp.new(nil, nil) }
|
89
|
-
|
90
|
-
it "fails the response match" do
|
91
|
-
expect {
|
92
|
-
is_expected.to be_a_twirp_client_response
|
93
|
-
}.to fail_with /to have data/
|
94
|
-
end
|
95
|
-
|
96
|
-
it "fails the error match" do
|
97
|
-
expect {
|
98
|
-
is_expected.to be_a_twirp_client_response.with_error
|
99
|
-
}.to fail_with /to have an error/
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context "with both response and error" do
|
104
|
-
subject { Twirp::ClientResp.new(GoodbyeResponse.new, Twirp::Error.not_found("Not Found")) }
|
105
|
-
|
106
|
-
it "fails" do
|
107
|
-
expect {
|
108
|
-
is_expected.to be_a_twirp_client_response(name: "Bob").with_error
|
109
|
-
}.to raise_error(ArgumentError, /but not both/)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
data/spec/request_spec.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
describe "be_a_twirp_request" do
|
2
|
-
subject { HelloRequest.new(**attrs) }
|
3
|
-
|
4
|
-
let(:attrs) { {} }
|
5
|
-
|
6
|
-
it { is_expected.to be_a_twirp_request }
|
7
|
-
|
8
|
-
it "catches non-twirp requests" do
|
9
|
-
expect {
|
10
|
-
expect(Object).to be_a_twirp_request
|
11
|
-
}.to fail_with /Expected a Twirp request, found Object/
|
12
|
-
end
|
13
|
-
|
14
|
-
it "matches a specific request type" do
|
15
|
-
is_expected.to be_a_twirp_request(HelloRequest)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "catches type mismatches" do
|
19
|
-
expect {
|
20
|
-
is_expected.to be_a_twirp_request(GoodbyeRequest)
|
21
|
-
}.to fail_with /request of type GoodbyeRequest/
|
22
|
-
end
|
23
|
-
|
24
|
-
it "catches erroneous request types" do
|
25
|
-
expect {
|
26
|
-
is_expected.to be_a_twirp_request(Object)
|
27
|
-
}.to raise_error(ArgumentError, /Object/)
|
28
|
-
end
|
29
|
-
|
30
|
-
context "with attributes" do
|
31
|
-
let(:attrs) { { name: "Bob", count: 3 } }
|
32
|
-
|
33
|
-
it "can match attributes" do
|
34
|
-
is_expected.to be_a_twirp_request(HelloRequest, **attrs)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "supports regex matches" do
|
38
|
-
is_expected.to be_a_twirp_request(name: /^B/)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "supports range matches" do
|
42
|
-
is_expected.to be_a_twirp_request(count: 1..5)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "catches mismatches" do
|
46
|
-
expect {
|
47
|
-
is_expected.to be_a_twirp_request(GoodbyeRequest, name: "Bob")
|
48
|
-
}.to fail_with /request of type/
|
49
|
-
|
50
|
-
expect {
|
51
|
-
is_expected.to be_a_twirp_request(name: "nope")
|
52
|
-
}.to fail_with /to have name: "nope"/
|
53
|
-
|
54
|
-
expect {
|
55
|
-
is_expected.to be_a_twirp_request(name: /no/)
|
56
|
-
}.to fail_with /to have name: \/no\//
|
57
|
-
|
58
|
-
expect {
|
59
|
-
is_expected.to be_a_twirp_request(count: 1)
|
60
|
-
}.to fail_with /to have count: 1/
|
61
|
-
end
|
62
|
-
|
63
|
-
it "catches the erroneous attribute matches" do
|
64
|
-
expect {
|
65
|
-
is_expected.to be_a_twirp_request(namezzz: "Bob")
|
66
|
-
}.to raise_error(ArgumentError, /namezzz/)
|
67
|
-
end
|
68
|
-
|
69
|
-
it "catches type mismatches" do
|
70
|
-
expect {
|
71
|
-
is_expected.to be_a_twirp_request(name: 123)
|
72
|
-
}.to raise_error(TypeError, /string field.*given Integer/)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|