OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 class ListInputStream implements InputStream { | |
6 List<int> read() { | |
7 var result = _data; | |
8 _data = null; | |
9 return result; | |
10 } | |
11 | |
12 void set dataHandler(void callback()) { | |
13 _dataHandler = callback; | |
14 } | |
15 | |
16 void set closeHandler(void callback()) { | |
17 _closeHandler = callback; | |
18 } | |
19 | |
20 void set errorHandler(void callback(int error)) { | |
21 } | |
22 | |
23 void write(List<int> data) { | |
24 Expect.equals(null, _data); | |
25 _data = data; | |
26 if (_dataHandler != null) { | |
27 // Data handler is not called through the event loop here. | |
28 _dataHandler(); | |
29 } | |
30 } | |
31 | |
32 void close() { | |
33 print("Closing"); | |
Mads Ager (google)
2011/10/26 10:56:07
Remove?
Søren Gjesse
2011/10/26 12:03:31
Done.
| |
34 // Close handler is not called through the event loop here. | |
35 if (_closeHandler != null) _closeHandler(); | |
36 } | |
37 | |
38 List<int> _data; | |
39 var _dataHandler; | |
40 var _closeHandler; | |
41 } | |
42 | |
43 void testUtf8() { | |
44 List<int> data = [0x01, | |
45 0x7f, | |
46 0xc2, 0x80, | |
47 0xdf, 0xbf, | |
48 0xe0, 0xa0, 0x80, | |
49 0xef, 0xbf, 0xbf]; | |
50 InputStream s = new ListInputStream(); | |
51 StringInputStream stream = new StringInputStream(s); | |
52 void stringData() { | |
53 String s = stream.read(); | |
54 Expect.equals(6, s.length); | |
55 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | |
56 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); | |
57 Expect.equals(new String.fromCharCodes([0x80]), s[2]); | |
58 Expect.equals(new String.fromCharCodes([0x7ff]), s[3]); | |
59 Expect.equals(new String.fromCharCodes([0x800]), s[4]); | |
60 Expect.equals(new String.fromCharCodes([0xffff]), s[5]); | |
61 } | |
62 stream.dataHandler = stringData; | |
63 s.write(data); | |
64 } | |
65 | |
66 void testLatin1() { | |
67 List<int> data = [0x01, | |
68 0x7f, | |
69 0x44, 0x61, 0x72, 0x74, | |
70 0x80, | |
71 0xff]; | |
72 InputStream s = new ListInputStream(); | |
73 StringInputStream stream = new StringInputStream(s, "ISO-8859-1"); | |
74 void stringData() { | |
75 String s = stream.read(); | |
76 Expect.equals(8, s.length); | |
77 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | |
78 Expect.equals(new String.fromCharCodes([0x7f]), s[1]); | |
79 Expect.equals("Dart", s.substring(2, 6)); | |
80 Expect.equals(new String.fromCharCodes([0x80]), s[6]); | |
81 Expect.equals(new String.fromCharCodes([0xff]), s[7]); | |
82 } | |
83 stream.dataHandler = stringData; | |
84 s.write(data); | |
85 } | |
86 | |
87 void testAscii() { | |
88 List<int> data = [0x01, | |
89 0x44, 0x61, 0x72, 0x74, | |
90 0x7f]; | |
91 InputStream s = new ListInputStream(); | |
92 StringInputStream stream = new StringInputStream(s, "ASCII"); | |
93 void stringData() { | |
94 String s = stream.read(); | |
95 Expect.equals(6, s.length); | |
96 Expect.equals(new String.fromCharCodes([0x01]), s[0]); | |
97 Expect.equals("Dart", s.substring(1, 5)); | |
98 Expect.equals(new String.fromCharCodes([0x7f]), s[5]); | |
99 } | |
100 stream.dataHandler = stringData; | |
101 s.write(data); | |
102 } | |
103 | |
104 void testReadLine1() { | |
105 InputStream s = new ListInputStream(); | |
106 StringInputStream stream = new StringInputStream(s); | |
107 var stage = 0; | |
108 | |
109 void stringData() { | |
110 var line; | |
111 if (stage == 0) { | |
112 line = stream.readLine(); | |
113 Expect.equals(null, line); | |
114 | |
Mads Ager (google)
2011/10/26 10:56:07
remove blank line?
Søren Gjesse
2011/10/26 12:03:31
Done.
| |
115 stage++; | |
116 s.close(); | |
117 } else if (stage == 1) { | |
118 line = stream.readLine(); | |
119 Expect.equals("Line", line); | |
120 line = stream.readLine(); | |
121 Expect.equals(null, line); | |
122 Expect.equals(true, stream.closed); | |
123 stage++; | |
124 } | |
125 } | |
126 stream.dataHandler = stringData; | |
127 s.write("Line".charCodes()); | |
128 Expect.equals(2, stage); | |
129 } | |
130 | |
131 void testReadLine2() { | |
132 InputStream s = new ListInputStream(); | |
133 StringInputStream stream = new StringInputStream(s); | |
134 var stage = 0; | |
135 | |
136 void stringData() { | |
137 var line; | |
138 if (stage == 0) { | |
139 line = stream.readLine(); | |
140 Expect.equals("Line1", line); | |
141 line = stream.readLine(); | |
142 Expect.equals("Line2", line); | |
143 line = stream.readLine(); | |
144 Expect.equals("Line3", line); | |
145 line = stream.readLine(); | |
146 Expect.equals(null, line); | |
147 | |
Mads Ager (google)
2011/10/26 10:56:07
Remove the blank lines?
Søren Gjesse
2011/10/26 12:03:31
Done.
| |
148 stage++; | |
149 s.write("ne4\n".charCodes()); | |
150 } else if (stage == 1) { | |
151 line = stream.readLine(); | |
152 Expect.equals("Line4", line); | |
153 line = stream.readLine(); | |
154 Expect.equals(null, line); | |
155 | |
156 stage++; | |
157 s.write("\n\n\r\n\r\n\r\r".charCodes()); | |
158 } else if (stage == 2) { | |
159 // Expect 5 empty lines. As long as the stream is not closed the | |
160 // final \r cannot be interpreted as a end of line. | |
161 for (int i = 0; i < 5; i++) { | |
162 line = stream.readLine(); | |
163 Expect.equals("", line); | |
164 } | |
165 line = stream.readLine(); | |
166 Expect.equals(null, line); | |
167 | |
168 stage++; | |
169 s.close(); | |
170 } else if (stage == 3) { | |
171 // The final \r can not be interpreted as a end of line. | |
172 line = stream.readLine(); | |
173 Expect.equals("", line); | |
174 line = stream.readLine(); | |
175 Expect.equals(null, line); | |
176 Expect.equals(true, stream.closed); | |
177 | |
178 stage++; | |
179 } | |
180 } | |
181 stream.dataHandler = stringData; | |
182 s.write("Line1\nLine2\r\nLine3\rLi".charCodes()); | |
183 Expect.equals(4, stage); | |
184 } | |
185 | |
186 main() { | |
187 testUtf8(); | |
188 testLatin1(); | |
189 testAscii(); | |
190 testReadLine1(); | |
191 testReadLine2(); | |
192 } | |
OLD | NEW |