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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
require "spec"
require "char/reader"
require "../../support/string"
private def assert_invalid_byte_sequence(bytes, *, file = __FILE__, line = __LINE__)
reader = Char::Reader.new(String.new bytes)
reader.current_char.should eq(Char::REPLACEMENT), file: file, line: line
reader.current_char_width.should eq(1), file: file, line: line
reader.error.should eq(bytes[0]), file: file, line: line
end
private def assert_reads_at_end(bytes, char, *, file = __FILE__, line = __LINE__)
str = String.new bytes
reader = Char::Reader.new(str, pos: bytes.size)
reader.previous_char.should eq(char), file: file, line: line
reader.current_char.should eq(char), file: file, line: line
reader.current_char_width.should eq(bytes.size), file: file, line: line
reader.pos.should eq(0), file: file, line: line
reader.error.should be_nil, file: file, line: line
end
private def assert_invalid_byte_sequence_at_end(bytes, *, file = __FILE__, line = __LINE__)
str = String.new bytes
reader = Char::Reader.new(str, pos: bytes.size)
reader.previous_char
reader.current_char.should eq(Char::REPLACEMENT), file: file, line: line
reader.current_char_width.should eq(1), file: file, line: line
reader.pos.should eq(bytes.size - 1), file: file, line: line
reader.error.should eq(bytes[-1]), file: file, line: line
end
describe "Char::Reader" do
it "iterates through empty string" do
reader = Char::Reader.new("")
reader.pos.should eq(0)
reader.current_char.ord.should eq(0)
reader.error.should be_nil
reader.has_next?.should be_false
expect_raises IndexError do
reader.next_char
end
end
it "iterates through string of size one" do
reader = Char::Reader.new("a")
reader.pos.should eq(0)
reader.current_char.should eq('a')
reader.has_next?.should be_true
reader.next_char.ord.should eq(0)
reader.has_next?.should be_false
expect_raises IndexError do
reader.next_char
end
end
it "iterates through chars" do
reader = Char::Reader.new("há日本語")
reader.pos.should eq(0)
reader.current_char.ord.should eq(104)
reader.has_next?.should be_true
reader.next_char.ord.should eq(225)
reader.pos.should eq(1)
reader.current_char.ord.should eq(225)
reader.next_char.ord.should eq(26085)
reader.next_char.ord.should eq(26412)
reader.next_char.ord.should eq(35486)
reader.has_next?.should be_true
reader.next_char.ord.should eq(0)
reader.has_next?.should be_false
expect_raises IndexError do
reader.next_char
end
end
it "peeks next char" do
reader = Char::Reader.new("há日本語")
reader.peek_next_char.ord.should eq(225)
end
it "sets pos" do
reader = Char::Reader.new("há日本語")
reader.pos = 1
reader.pos.should eq(1)
reader.current_char.ord.should eq(225)
end
describe "#each" do
it "yields chars" do
reader = Char::Reader.new("abc")
chars = [] of Char
reader.each do |char|
chars << char
end.should be_nil
chars.should eq ['a', 'b', 'c']
end
it "does not yield if empty" do
reader = Char::Reader.new("")
reader.each do |char|
fail "reader each shouldn't yield on empty string"
end.should be_nil
end
it "checks bounds after block" do
string = "f"
reader = Char::Reader.new(string)
reader.each do |c|
c.should eq 'f'
reader.next_char
end
end
end
it "starts at end" do
reader = Char::Reader.new(at_end: "")
reader.pos.should eq(0)
reader.current_char.ord.should eq(0)
reader.has_previous?.should be_false
reader.has_next?.should be_false
end
it "gets previous char (ascii)" do
reader = Char::Reader.new(at_end: "hello")
reader.pos.should eq(4)
reader.current_char.should eq('o')
reader.has_previous?.should be_true
reader.has_next?.should be_true
reader.previous_char.should eq('l')
reader.has_next?.should be_true
reader.previous_char.should eq('l')
reader.previous_char.should eq('e')
reader.previous_char.should eq('h')
reader.has_previous?.should be_false
expect_raises IndexError do
reader.previous_char
end
end
it "gets previous char (unicode)" do
reader = Char::Reader.new(at_end: "há日本語")
reader.pos.should eq(9)
reader.current_char.should eq('語')
reader.has_previous?.should be_true
reader.has_next?.should be_true
reader.previous_char.should eq('本')
reader.has_next?.should be_true
reader.previous_char.should eq('日')
reader.previous_char.should eq('á')
reader.previous_char.should eq('h')
reader.has_previous?.should be_false
end
it "starts at pos" do
reader = Char::Reader.new("há日本語", pos: 9)
reader.pos.should eq(9)
reader.current_char.should eq('語')
end
it "#current_char?" do
reader = Char::Reader.new("há日本語")
reader.current_char?.should eq('h')
reader.next_char
reader.current_char?.should eq('á')
reader.next_char
reader.current_char?.should eq('日')
reader.next_char
reader.current_char?.should eq('本')
reader.next_char
reader.current_char?.should eq('語')
reader.next_char
reader.current_char?.should be_nil
reader.previous_char
reader.current_char?.should eq('語')
end
it "#next_char?" do
reader = Char::Reader.new("há日本語")
reader.next_char?.should eq('á')
reader.pos.should eq(1)
reader.next_char?.should eq('日')
reader.pos.should eq(3)
reader.next_char?.should eq('本')
reader.pos.should eq(6)
reader.next_char?.should eq('語')
reader.pos.should eq(9)
reader.next_char?.should be_nil
reader.pos.should eq(12)
reader.next_char?.should be_nil
reader.pos.should eq(12)
end
it "#previous_char?" do
reader = Char::Reader.new("há日本語", pos: 12)
reader.previous_char?.should eq('語')
reader.pos.should eq(9)
reader.previous_char?.should eq('本')
reader.pos.should eq(6)
reader.previous_char?.should eq('日')
reader.pos.should eq(3)
reader.previous_char?.should eq('á')
reader.pos.should eq(1)
reader.previous_char?.should eq('h')
reader.pos.should eq(0)
reader.previous_char?.should be_nil
reader.pos.should eq(0)
end
it "errors on invalid UTF-8" do
{% for bytes in INVALID_UTF8_BYTE_SEQUENCES %}
assert_invalid_byte_sequence Bytes{{ bytes }}
{% end %}
end
describe "#previous_char" do
it "reads on valid UTF-8" do
{% for bytes, char in VALID_UTF8_BYTE_SEQUENCES %}
assert_reads_at_end Bytes{{ bytes }}, {{ char }}
{% end %}
end
it "errors on invalid UTF-8" do
assert_invalid_byte_sequence_at_end Bytes[0x80]
assert_invalid_byte_sequence_at_end Bytes[0xbf]
assert_invalid_byte_sequence_at_end Bytes[0xc0]
assert_invalid_byte_sequence_at_end Bytes[0xff]
assert_invalid_byte_sequence_at_end Bytes[0x00, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x7f, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x9f, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xbf, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc1, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xe0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xff, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x00, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x7f, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x80, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x8f, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x90, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xbf, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc0, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc1, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc2, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xdf, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xe0, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xe0, 0x9f, 0xbf]
assert_invalid_byte_sequence_at_end Bytes[0xf0, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xff, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x00, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x7f, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x80, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x8f, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x90, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xbf, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc0, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc1, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xc2, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xdf, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xed, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xed, 0xbf, 0xbf]
assert_invalid_byte_sequence_at_end Bytes[0xf0, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xff, 0xa0, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x00, 0x80, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xef, 0x80, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xf0, 0x80, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xf5, 0x80, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xff, 0x80, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0x00, 0x90, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xef, 0x90, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xf4, 0x90, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xf5, 0x90, 0x80, 0x80]
assert_invalid_byte_sequence_at_end Bytes[0xff, 0x90, 0x80, 0x80]
end
end
end
|