-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathparser_spec.cr
More file actions
169 lines (134 loc) · 4.78 KB
/
Copy pathparser_spec.cr
File metadata and controls
169 lines (134 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require "./spec_helper"
require "../src/parser"
module Redis
describe Parser do
it "reads ints as Int64" do
[1, 12, 1234, 12345678, 123456789012345678_i64, -1, -12345678, 0].each do |int|
io = IO::Memory.new(":#{int}\r\n")
Parser.new(io).read.should eq int
end
end
it "reads simple strings as String" do
io = IO::Memory.new("+OK\r\n+QUEUED\r\n+.\r\n+\r\n" * 2)
parser = Parser.new(io)
2.times do
parser.read.should eq "OK"
parser.read.should eq "QUEUED"
# With the optimization made in aafe485, we need to ensure we can read
# simple strings with a size of less than 2 bytes.
parser.read.should eq "."
parser.read.should eq ""
end
end
it "reads bulk strings as String" do
io = IO::Memory.new("$11\r\nHello world\r\n")
Parser.new(io).read.should eq "Hello world"
io = IO::Memory.new("$0\r\n\r\n")
Parser.new(io).read.should eq ""
end
it "reads nil" do
io = IO::Memory.new("$-1\r\n")
Parser.new(io).read.should eq nil
io = IO::Memory.new("*-1\r\n")
Parser.new(io).read.should eq nil
end
it "reads arrays as Array" do
io = IO::Memory.new
io << "*3\r\n"
io << "$4\r\n" # Bulk string, length 4
io << "foo!\r\n" # Value of that bulk string
io << ":12345\r\n" # Int value 12345
io << "$-1\r\n" # nil
Parser.new(io.rewind).read.should eq ["foo!", 12345, nil]
end
it "reads arrays that contain errors" do
io = IO::Memory.new
io << "*3\r\n"
io << "$3\r\n" << "foo\r\n"
io << "-OOPS The thing broke\r\n"
io << ":1234\r\n"
Parser.new(io.rewind).read.should eq ["foo", Error.new("OOPS The thing broke"), 1234]
end
it "reads nil" do
io = IO::Memory.new("_\r\n")
Parser.new(io).read.should eq nil
end
it "reads doubles as Float64" do
io = IO::Memory.new(",12.34\r\n,56.78\r\n,inf\r\n,-inf\r\n,nan\r\n,1.234567890e4\r\n")
parser = Parser.new(io)
parser.read.should eq 12.34
parser.read.should eq 56.78
parser.read.should eq Float64::INFINITY
parser.read.should eq -Float64::INFINITY
parser.read.as(Float).nan?.should eq true
parser.read.should eq 1.234567890e4
end
it "reads booleans as Bool" do
io = IO::Memory.new("#t\r\n#f\r\n")
parser = Parser.new(io)
parser.read.should eq true
parser.read.should eq false
end
it "reads simple errors as Redis::Error" do
io = IO::Memory.new("-OMG The thing broke!")
parser = Parser.new(io)
parser.read.should eq Error.new("OMG The thing broke!")
end
it "reads blob errors as Redis::Error" do
first_error_msg = "OMG Hello World!\r\nSecond line"
second_error_msg = "OK Computer"
parser = Parser.new(IO::Memory.new(<<-EOF))
!#{first_error_msg.bytesize}\r
#{first_error_msg}\r
!#{second_error_msg.bytesize}\r
#{second_error_msg}\r
EOF
parser.read.should eq Error.new(first_error_msg)
parser.read.should eq Error.new(second_error_msg)
end
it "reads verbatim strings as String" do
io = IO::Memory.new("=9\r\ntxt:Hello\r\n=10\r\nmkd:World!\r\n")
parser = Parser.new(io)
parser.read.should eq "Hello"
parser.read.should eq "World!"
end
it "reads big numbers" do
io = IO::Memory.new("(123456789012345678901234567890\r\n(98765432109876543210987654321\r\n")
parser = Parser.new(io)
parser.read.should eq BigInt.new("123456789012345678901234567890")
parser.read.should eq BigInt.new("98765432109876543210987654321")
end
it "reads maps as Hash(Redis::Value, Redis::Value)" do
io = IO::Memory.new("%2\r\n+one\r\n:1\r\n$3\r\ntwo\r\n:2\r\n%1\r\n+foo\r\n+bar\r\n")
parser = Parser.new(io)
hash = parser.read
hash.should be_a Hash(Redis::Value, Redis::Value)
hash.should eq({
"one" => 1,
"two" => 2,
})
parser.read.should eq({"foo" => "bar"})
end
it "reads sets as Set(Redis::Value)" do
io = IO::Memory.new("~3\r\n+one\r\n:2\r\n(3\r\n")
parser = Parser.new(io)
parser.read.should eq Set(Value){"one", 2i64, BigInt.new("3")}
end
it "reads attributes" do
io = IO::Memory.new("|1\r\n+foo\r\n+bar\r\n|2\r\n+one\r\n:1\r\n+two\r\n:2\r\n")
parser = Parser.new(io)
parser.read.should eq Attributes.new({"foo" => "bar"} of Value => Value)
parser.read.should eq Attributes.new({"one" => 1i64, "two" => 2i64} of Value => Value)
end
it "can read without failing if the IO is closed" do
reader, writer = IO.pipe
begin
parser = Parser.new(reader)
writer.close
parser.read?.should eq nil
ensure
reader.close
end
end
end
end