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
|
require 'test/unit'
require 'glib2'
$KCODE = "U"
class TestGIOChannel < Test::Unit::TestCase
TEST_FILE = "test-iochannel.txt"
TEST_SJIS_FILE = "test-iochannel-sjis.txt"
TEST_WRITE_FILE = "test-write-file.txt"
def test_open
io = GLib::IOChannel.open(TEST_FILE)
io.close
io = GLib::IOChannel.open(TEST_FILE, "r")
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read)
io.close
io = GLib::IOChannel.open(TEST_WRITE_FILE, "w")
assert_raises(RuntimeError){
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read)
}
io.close
GLib::IOChannel.open(TEST_FILE) {|io|
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read)
}
GLib::IOChannel.open(TEST_FILE, "r") {|io|
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read)
}
GLib::IOChannel.open(TEST_WRITE_FILE, "w") {|io|
assert_raises(RuntimeError){
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read)
}
}
assert_raises(GLib::IOChannelError){
io.close
}
assert_raises(GLib::FileError){
GLib::IOChannel.new("foo")
}
end
def test_getc
io = GLib::IOChannel.new(TEST_FILE)
["a", "b", "c", "あ"].each do |v|
3.times do
assert_equal(v.unpack("U")[0], io.getc)
end
assert_equal("\n"[0], io.getc)
end
assert_equal(nil, io.getc)
io.close
end
def test_each_char
text = "aaa\nbbb\nccc\nあああ\n".split(//)
io = GLib::IOChannel.new(TEST_FILE)
i = 0
io.each_char {|ch|
assert_equal(text[i].unpack("U")[0], ch)
i += 1
}
io.close
end
def test_readchar
io = GLib::IOChannel.new(TEST_FILE)
text = "aaa\nbbb\nccc\nあああ\n".split(//)
text.each do |v|
assert_equal(v.unpack("U")[0], io.readchar)
end
assert_raises(EOFError) {
io.readchar
}
io.close
end
def test_gets
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("aaa\n", io.gets)
assert_equal("bbb\n", io.gets)
assert_equal("ccc\n", io.gets)
assert_equal("あああ\n", io.gets)
assert_equal(nil, io.gets)
io.close
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("aaa\nbbb\n", io.gets("bbb\n"))
assert_equal("ccc\nあああ\n", io.gets("bbb\n"))
assert_equal(nil, io.gets("bbb\n"))
io.close
end
def test_readline
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("aaa\n", io.readline)
assert_equal("bbb\n", io.readline)
assert_equal("ccc\n", io.readline)
assert_equal("あああ\n", io.readline)
assert_raises(EOFError) {
io.readline
}
io.close
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("aaa\nbbb\n", io.readline("bbb\n"))
assert_equal("ccc\nあああ\n", io.readline("bbb\n"))
assert_raises(EOFError) {
io.readline("bbb\n")
}
io.close
end
def test_each_line
lines = ["aaa\n", "bbb\n", "ccc\n", "あああ\n"]
io = GLib::IOChannel.new(TEST_FILE)
i = 0
io.each {|line|
assert_equal(lines[i], line)
i += 1
}
io.close
io = GLib::IOChannel.new(TEST_FILE)
assert_raises(RuntimeError) {
io.each {|line|
raise "test"
}
}
io.close
io = GLib::IOChannel.new(TEST_FILE)
i = 0
io.each_line {|line|
assert_equal(lines[i], line)
i += 1
}
io.close
#Test for Enumerable
GLib::IOChannel.open(TEST_FILE) {|io|
io.each_with_index {|line, i|
assert_equal(lines[i], line)
}
}
assert_raises(ArgumentError){
io.each
}
end
def test_read
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read)
io.close
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read(100))
io.close
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("aaa\nbbb\n", io.read(8))
assert_equal("ccc\n", io.read(4))
assert_equal("あああ\n", io.read(10))
assert_equal("", io.read(10))
assert_equal("", io.read(10))
io.close
end
def test_seek
text = "aaa\nbbb\nccc\nあああ\n"
io = GLib::IOChannel.new(TEST_FILE)
io.seek(5)
assert_equal(text[5], io.getc)
io.seek(6, GLib::IOChannel::SEEK_SET)
assert_equal(text[6], io.getc)
io.seek(1, GLib::IOChannel::SEEK_CUR)
assert_equal(text[8], io.getc)
io.pos = 0
assert_equal(text[0], io.getc)
io.set_pos(2)
assert_equal(text[2], io.getc)
io.close
end
def test_write
io = GLib::IOChannel.new(TEST_WRITE_FILE, "w")
io.write("a\n")
io.write("あいう\n")
io.printf("a%sa\n", "a")
io.print("a", 100, "a\n")
io.puts("b", 200, "b")
io.putc("c")
io.putc("c".unpack("U")[0])
io.putc("cc".unpack("U")[0])
io.putc("あ".unpack("U")[0])
io.putc("あ")
io.putc("あい") #Ignore 2nd char
io.putc("aあ") #Ignore 2nd char
io.close
# check them
io = GLib::IOChannel.new(TEST_WRITE_FILE, "r")
assert_equal("a\n", io.gets)
assert_equal("あいう\n", io.gets)
assert_equal("aaa\n", io.gets)
assert_equal("a100a\n", io.gets)
assert_equal("b\n", io.gets)
assert_equal("200\n", io.gets)
assert_equal("b\n", io.gets)
assert_equal("c".unpack("U")[0], io.getc)
assert_equal("c".unpack("U")[0], io.getc)
assert_equal("c".unpack("U")[0], io.getc)
assert_equal("あ".unpack("U")[0], io.getc)
assert_equal("あ".unpack("U")[0], io.getc)
assert_equal("あ".unpack("U")[0], io.getc)
assert_equal("a".unpack("U")[0], io.getc)
io.close
end
def test_encoding
io = GLib::IOChannel.new(TEST_FILE)
assert_equal("UTF-8", io.encoding)
io.encoding = "Shift_JIS"
assert_equal("Shift_JIS", io.encoding)
assert_raises(GLib::ConvertError) {
io.read
}
io.close
io = GLib::IOChannel.new(TEST_SJIS_FILE)
io.encoding = "Shift_JIS"
assert_equal("Shift_JIS", io.encoding)
assert_equal("aaa\nbbb\nccc\nあああ\n", io.read)
io.close
end
def test_error
assert_raises(GLib::FileError) {
# No such file or directory
GLib::IOChannel.new("foo")
}
end
end
|