]> gitweb.factorcode.org Git - factor.git/blob - core/io/encodings/encodings-tests.factor
c0807d1bff8976ddc775ea0efb8ee19279465f6c
[factor.git] / core / io / encodings / encodings-tests.factor
1 USING: accessors io io.encodings io.encodings.ascii
2 io.encodings.string io.encodings.utf8 io.files
3 io.streams.byte-array io.streams.string kernel namespaces
4 tools.test ;
5 IN: io.encodings.tests
6
7 { { } }
8 [ "vocab:io/test/empty-file.txt" ascii file-lines ]
9 unit-test
10
11 : lines-test ( file encoding -- line1 line2 )
12     [ readln readln ] with-file-reader ;
13
14 {
15     "This is a line."
16     "This is another line."
17 } [
18     "vocab:io/test/windows-eol.txt"
19     ascii lines-test
20 ] unit-test
21
22 {
23     "This is a line."
24     "This is another line."
25 } [
26     "vocab:io/test/mac-os-eol.txt"
27     ascii lines-test
28 ] unit-test
29
30 {
31     "This is a line."
32     "This is another line."
33 } [
34     "vocab:io/test/unix-eol.txt"
35     ascii lines-test
36 ] unit-test
37
38 {
39     "1234"
40 } [
41      "Hello world\r\n1234" <string-reader>
42      dup stream-readln drop
43      4 swap stream-read
44 ] unit-test
45
46 {
47     "1234"
48 } [
49      "Hello world\r\n1234" <string-reader>
50      dup stream-readln drop
51      4 swap stream-read-partial
52 ] unit-test
53
54 {
55     CHAR: 1
56 } [
57      "Hello world\r\n1234" <string-reader>
58      dup stream-readln drop
59      stream-read1
60 ] unit-test
61
62 { utf8 ascii } [
63     "foo" utf8 [
64         input-stream get code>>
65         ascii decode-input
66         input-stream get code>>
67     ] with-byte-reader
68 ] unit-test
69
70 { utf8 ascii } [
71     utf8 [
72         output-stream get code>>
73         ascii encode-output
74         output-stream get code>>
75     ] with-byte-writer drop
76 ] unit-test
77
78 ! Bug 1177.
79 {
80     "! lol"
81     "! wat"
82     13
83 } [
84     "! lol\r\n! wat\r\n" utf8 encode
85     utf8 [
86         readln
87         "\r\n" read-until
88     ] with-byte-reader
89 ] unit-test
90
91 {
92     "! lol"
93     "! wa"
94     116
95 } [
96     "! lol\r\n! wat\r\n" utf8 encode
97     utf8 [
98         readln
99         "t" read-until
100     ] with-byte-reader
101 ] unit-test
102
103 ! shouldn't be able to tell on (underlying) stream of a decoder
104 ! because it's confusing when you read1 character and tell is
105 ! greater than 1.
106 [
107     "hello world" utf8 encode
108     utf8 [ tell-input ] with-byte-reader
109 ] must-fail