File: reader_spec.cr

package info (click to toggle)
crystal 1.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,956 kB
  • sloc: javascript: 1,712; sh: 592; cpp: 541; makefile: 243; ansic: 119; python: 105; xml: 32
file content (180 lines) | stat: -rw-r--r-- 4,852 bytes parent folder | download
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
require "spec"
require "char/reader"

private def assert_invalid_byte_sequence(bytes)
  reader = Char::Reader.new(String.new bytes)
  reader.current_char.should eq(Char::REPLACEMENT)
  reader.current_char_width.should eq(1)
  reader.error.should eq(bytes[0])
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

  it "is an Enumerable(Char)" do
    reader = Char::Reader.new("abc")
    sum = 0
    reader.each do |char|
      sum += char.ord
    end.should be_nil
    sum.should eq(294)
  end

  it "is an Enumerable(Char) but doesn't 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 "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
  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.previous_char.should eq('l')
    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.previous_char.should eq('本')
    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 "errors if 0x80 <= first_byte < 0xC2" do
    assert_invalid_byte_sequence Bytes[0x80]
    assert_invalid_byte_sequence Bytes[0xC1]
  end

  it "errors if (second_byte & 0xC0) != 0x80" do
    assert_invalid_byte_sequence Bytes[0xd0]
  end

  it "errors if first_byte == 0xE0 && second_byte < 0xA0" do
    assert_invalid_byte_sequence Bytes[0xe0, 0x9F, 0xA0]
  end

  it "errors if first_byte == 0xED && second_byte >= 0xA0" do
    assert_invalid_byte_sequence Bytes[0xed, 0xB0, 0xA0]
  end

  it "errors if first_byte < 0xF0 && (third_byte & 0xC0) != 0x80" do
    assert_invalid_byte_sequence Bytes[0xe0, 0xA0, 0]
  end

  it "errors if first_byte == 0xF0 && second_byte < 0x90" do
    assert_invalid_byte_sequence Bytes[0xf0, 0x8F, 0xA0]
  end

  it "errors if first_byte == 0xF4 && second_byte >= 0x90" do
    assert_invalid_byte_sequence Bytes[0xf4, 0x90, 0xA0]
  end

  it "errors if first_byte < 0xF5 && (fourth_byte & 0xC0) != 0x80" do
    assert_invalid_byte_sequence Bytes[0xf4, 0x8F, 0xA0, 0]
  end

  it "errors if first_byte >= 0xF5" do
    assert_invalid_byte_sequence Bytes[0xf5, 0x8F, 0xA0, 0xA0]
  end

  it "errors if second_byte is out of bounds" do
    assert_invalid_byte_sequence Bytes[0xf4]
  end

  it "errors if third_byte is out of bounds" do
    assert_invalid_byte_sequence Bytes[0xf4, 0x8f]
  end

  it "errors if fourth_byte is out of bounds" do
    assert_invalid_byte_sequence Bytes[0xf4, 0x8f, 0xa0]
  end
end