Skip to content

Commit 292b9e1

Browse files
committed
Make Status comparable.
1 parent ff7f9d5 commit 292b9e1

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

lib/protocol/grpc/header/message.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module Header
1414
# This header is optional and typically only present when there's an error (non-zero status code).
1515
# This header can appear both as an initial header (for trailers-only responses) and as a trailer.
1616
class Message < String
17+
# Parse a message from a header value.
18+
#
19+
# @parameter value [String] The header value to parse.
20+
# @returns [Message] A new Message instance.
1721
def self.parse(value)
1822
new(value)
1923
end

lib/protocol/grpc/header/status.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ module Header
1212
# Status code 0 indicates success (OK), while other codes indicate various error conditions.
1313
# This header can appear both as an initial header (for trailers-only responses) and as a trailer.
1414
class Status
15+
# Parse a status code from a header value.
16+
#
17+
# @parameter value [String] The header value to parse.
18+
# @returns [Status] A new Status instance.
1519
def self.parse(value)
1620
new(value.to_i)
1721
end
@@ -37,6 +41,30 @@ def to_s
3741
@value.to_s
3842
end
3943

44+
# Check equality with another status or integer value.
45+
#
46+
# @parameter other [Status | Integer] The value to compare with.
47+
# @returns [Boolean] if the status codes are equal.
48+
def ==(other)
49+
@value == other.to_i
50+
end
51+
52+
alias eql? ==
53+
54+
# Generate hash for use in Hash/Set collections.
55+
#
56+
# @returns [Integer] The hash value based on the status code.
57+
def hash
58+
@value.hash
59+
end
60+
61+
# Check if this status represents success (status code 0).
62+
#
63+
# @returns [Boolean] `true` if the status code is 0 (OK).
64+
def ok?
65+
@value == 0
66+
end
67+
4068
# Merge another status value (takes the new value, as status should only appear once)
4169
# @parameter value [String | Integer] The new status code
4270
def <<(value)

test/protocol/grpc/header/status.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,69 @@
4949
end
5050
end
5151

52+
with "#==" do
53+
it "compares equal status instances" do
54+
status1 = subject.new(0)
55+
status2 = subject.new(0)
56+
expect(status1).to be == status2
57+
end
58+
59+
it "compares unequal status instances" do
60+
status1 = subject.new(0)
61+
status2 = subject.new(1)
62+
expect(status1).not.to be == status2
63+
end
64+
65+
it "compares status with integer" do
66+
status = subject.new(5)
67+
expect(status).to be == 5
68+
end
69+
70+
it "compares status with different integer" do
71+
status = subject.new(5)
72+
expect(status).not.to be == 3
73+
end
74+
end
75+
76+
with "#eql?" do
77+
it "works same as ==" do
78+
status1 = subject.new(0)
79+
status2 = subject.new(0)
80+
expect(status1.eql?(status2)).to be == true
81+
end
82+
end
83+
84+
with "#hash" do
85+
it "returns consistent hash for same status code" do
86+
status1 = subject.new(0)
87+
status2 = subject.new(0)
88+
expect(status1.hash).to be == status2.hash
89+
end
90+
91+
it "can be used as hash key" do
92+
status = subject.new(0)
93+
hash = {status => "success"}
94+
expect(hash[subject.new(0)]).to be == "success"
95+
end
96+
end
97+
98+
with "#ok?" do
99+
it "returns true for status code 0" do
100+
status = subject.new(0)
101+
expect(status).to be(:ok?)
102+
end
103+
104+
it "returns false for non-zero status code" do
105+
status = subject.new(1)
106+
expect(status.ok?).to be == false
107+
end
108+
109+
it "returns false for error status" do
110+
status = subject.new(14)
111+
expect(status.ok?).to be == false
112+
end
113+
end
114+
52115
with "#<<" do
53116
it "merges new integer value" do
54117
status = subject.new(0)

0 commit comments

Comments
 (0)