bf394aaef7c1b7f2746645e79ef2f12a69cd6525
[nit.git] / lib / io / test_push_back_reader.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Test suites for module `push_back_reader`
12 module test_push_back_reader is test_suite
13
14 import test_suite
15 import io::push_back_reader
16
17 class TestPushBackDecorator
18 super TestSuite
19
20 private fun sample: PushBackDecorator do
21 return new PushBackDecorator(new StringIStream("""
22 abcd
23
24 efg
25 """))
26 end
27
28 fun test_read_char do
29 var subject = sample
30
31 assert 'a' == subject.read_char.ascii
32 end
33
34 fun test_read_char_eof do
35 var subject = new PushBackDecorator(new StringIStream(""))
36
37 assert -1 == subject.read_char
38 end
39
40 fun test_unread_read_char do
41 var subject = sample
42
43 subject.unread_char('z')
44 assert 'z' == subject.read_char.ascii
45 assert 'a' == subject.read_char.ascii
46 end
47
48 fun test_read_partial do
49 var subject = sample
50
51 assert "abcd" == subject.read(4)
52 end
53
54 fun test_read_too_much do
55 var subject = sample
56 var exp = """
57 abcd
58
59 efg
60 """
61 assert exp == subject.read(100)
62 end
63
64 fun test_unread_read do
65 var subject = sample
66
67 subject.unread("a")
68 assert "aab" == subject.read(3)
69 end
70
71 fun test_unread_read_mixed do
72 var subject = sample
73
74 subject.unread("a")
75 assert "aab" == subject.read(3)
76 end
77
78 fun test_read_all do
79 var subject = sample
80 var exp = """
81 abcd
82
83 efg
84 """
85 assert exp == subject.read_all
86 end
87
88 fun test_unread_read_all do
89 var subject = sample
90 var exp = """
91 fooabcd
92
93 efg
94 """
95 subject.unread("foo")
96 assert exp == subject.read_all
97 end
98
99 fun test_read_line do
100 var subject = sample
101
102 assert "abcd" == subject.read_line
103 assert "" == subject.read_line
104 end
105
106 fun test_unread_read_line do
107 var subject = sample
108
109 subject.unread("a\nb")
110 assert "a" == subject.read_line
111 assert "babcd" == subject.read_line
112 end
113
114 fun test_eof do
115 var subject = sample
116
117 assert not subject.eof
118 subject.read_all
119 assert subject.eof
120 end
121
122 fun test_eof_empty do
123 var subject = new PushBackDecorator(new StringIStream(""))
124
125 assert subject.eof
126 end
127
128 fun test_close do
129 var subject = sample
130
131 subject.close
132 assert subject.eof
133 end
134
135 fun test_unread_close do
136 var subject = sample
137
138 subject.unread("foo")
139 subject.close
140 assert subject.eof
141 end
142
143 fun test_unread_char_order do
144 var subject = sample
145
146 subject.unread_char('z')
147 subject.unread_char('y')
148 assert "yzab" == subject.read(4)
149 end
150
151 fun test_unread_order do
152 var subject = sample
153
154 subject.unread("bar")
155 subject.unread("foo")
156 assert "foobarab" == subject.read(8)
157 end
158 end