forked from Shopify/go-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_read_test.go
More file actions
90 lines (75 loc) · 2.39 KB
/
io_read_test.go
File metadata and controls
90 lines (75 loc) · 2.39 KB
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
package lua
import "testing"
func TestIORead(t *testing.T) {
testString(t, `
-- Test file read functionality
local tmp = os.tmpname()
-- Write test data
local f = io.open(tmp, "w")
assert(f, "cannot open temp file for writing")
f:write("hello\n")
f:write("world\n")
f:write("123\n")
f:write("45.67\n")
f:write("0xABC\n")
f:close()
-- Test read("l") - read line without EOL
f = io.open(tmp, "r")
local line = f:read("l")
assert(line == "hello", "read('l') failed: got '" .. tostring(line) .. "'")
print("read('l'):", line, "OK")
-- Test read("*l") - Lua 5.2 format
line = f:read("*l")
assert(line == "world", "read('*l') failed: got '" .. tostring(line) .. "'")
print("read('*l'):", line, "OK")
-- Test read("n") - read number (integer)
local num = f:read("n")
assert(num == 123, "read('n') for int failed: got " .. tostring(num))
print("read('n') int:", num, "OK")
-- Test read("n") - read number (float)
num = f:read("n")
assert(num == 45.67, "read('n') for float failed: got " .. tostring(num))
print("read('n') float:", num, "OK")
-- Test read("n") - read hex number
num = f:read("n")
assert(num == 0xABC, "read('n') for hex failed: got " .. tostring(num))
print("read('n') hex:", num, "OK")
f:close()
-- Test read("a") - read all
f = io.open(tmp, "r")
local all = f:read("a")
assert(#all > 0, "read('a') failed")
print("read('a'):", #all, "bytes OK")
f:close()
-- Test read("L") - read line with EOL
f = io.open(tmp, "r")
line = f:read("L")
assert(line == "hello\n", "read('L') failed: got '" .. tostring(line) .. "'")
print("read('L'):", "OK")
f:close()
-- Test read(n) - read n bytes
f = io.open(tmp, "r")
local bytes = f:read(5)
assert(bytes == "hello", "read(5) failed: got '" .. tostring(bytes) .. "'")
print("read(5):", bytes, "OK")
f:close()
-- Test read() - default is "l"
f = io.open(tmp, "r")
line = f:read()
assert(line == "hello", "read() default failed: got '" .. tostring(line) .. "'")
print("read():", line, "OK")
f:close()
-- Test read(0) - test for EOF
f = io.open(tmp, "r")
local test = f:read(0)
assert(test == "", "read(0) at start should return ''")
f:read("a") -- read all
test = f:read(0)
assert(test == nil, "read(0) at EOF should return nil")
print("read(0): OK")
f:close()
-- Cleanup
os.remove(tmp)
print("\nAll read tests passed!")
`)
}