Skip to content

Commit 45fdfa2

Browse files
author
Jessica Xie
committed
testing script
1 parent 2649b9e commit 45fdfa2

File tree

8 files changed

+112
-2
lines changed

8 files changed

+112
-2
lines changed

fixtures/io.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A banana contains 75% water.
2+
The most consumed fruit in America is the banana
3+
Bananas are a good source of vitamin C, potassium and fiber.
4+
Fresh apples float because they contain 25% air.
5+
Bananas contain no fat, cholesterol or sodium.

fixtures/number.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12345

fixtures/read_all.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
file = io.open("fixtures/io.txt", "r")
2+
print(file:read("*a"))
3+
file:close()

fixtures/read_bytes.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
file = io.open("fixtures/io.txt", "r")
2+
print(file:read(20))
3+
file:close()

fixtures/read_lines.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
file = io.open("fixtures/io.txt", "r")
2+
print(file:read("*l"))
3+
print(file:read("*l"))
4+
file:close()

fixtures/read_number.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
file = io.open("fixtures/number.txt", "r")
2+
print(file:read("*n"))
3+
file:close()

io.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,24 @@ func readAll(l *State, f *os.File) error {
125125
}
126126

127127
func readLineHelper(l *State, f *os.File) error {
128-
// TODO: you can only read one line. After one line, the file is closed.
128+
originalFileOffset, err := f.Seek(0, 1)
129+
if err != nil {
130+
return err
131+
}
132+
129133
reader := bufio.NewReader(f)
130134
bytes, err := reader.ReadBytes('\n')
131135
if err == nil {
132136
l.PushString(string(bytes))
137+
length := int64(len(bytes))
138+
f.Seek(originalFileOffset+length, 0) // bufio loads the entire file. This is a lazy hack to get around the problem.
133139
}
134140
return err
135141
}
136142

137143
func readBytes(l *State, f *os.File, i int) error {
138144
buf := make([]byte, i)
139-
_, err := io.ReadFull(f, buf)
145+
_, err := f.Read(buf)
140146
if err == nil {
141147
l.PushString(string(buf))
142148
}

io_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package lua
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"strings"
8+
"testing"
9+
)
10+
11+
// TODO: add missing tests
12+
13+
func TestReadAll(t *testing.T) {
14+
l := NewState()
15+
OpenLibraries(l)
16+
output := captureOutput(func() {
17+
DoFile(l, "fixtures/read_all.lua")
18+
})
19+
20+
expected := `A banana contains 75% water.
21+
The most consumed fruit in America is the banana
22+
Bananas are a good source of vitamin C, potassium and fiber.
23+
Fresh apples float because they contain 25% air.
24+
Bananas contain no fat, cholesterol or sodium.`
25+
26+
if strings.Trim(output, "\n") != expected {
27+
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
28+
}
29+
}
30+
31+
func TestReadLines(t *testing.T) {
32+
l := NewState()
33+
OpenLibraries(l)
34+
output := captureOutput(func() {
35+
DoFile(l, "fixtures/read_lines.lua")
36+
})
37+
38+
expected := `A banana contains 75% water.
39+
40+
The most consumed fruit in America is the banana`
41+
42+
if strings.Trim(output, "\n") != expected {
43+
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
44+
}
45+
}
46+
47+
func TestReadNumber(t *testing.T) {
48+
l := NewState()
49+
OpenLibraries(l)
50+
output := captureOutput(func() {
51+
DoFile(l, "fixtures/read_number.lua")
52+
})
53+
54+
expected := "12345"
55+
if strings.Trim(output, "\n") != expected {
56+
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
57+
}
58+
}
59+
60+
func TestReadBytes(t *testing.T) {
61+
l := NewState()
62+
OpenLibraries(l)
63+
output := captureOutput(func() {
64+
DoFile(l, "fixtures/read_bytes.lua")
65+
})
66+
67+
expected := "A banana contains 75"
68+
if strings.Trim(output, "\n") != expected {
69+
t.Errorf("Expecting:\n%s\nbut received:\n%s\n", expected, output)
70+
}
71+
}
72+
73+
func captureOutput(f func()) string {
74+
rescueStdout := os.Stdout
75+
r, w, _ := os.Pipe()
76+
os.Stdout = w
77+
78+
f()
79+
80+
w.Close()
81+
out, _ := ioutil.ReadAll(r)
82+
fmt.Println(string(out))
83+
os.Stdout = rescueStdout
84+
return string(out)
85+
}

0 commit comments

Comments
 (0)