-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_test.go
73 lines (66 loc) · 1.43 KB
/
io_test.go
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
package gmnlisp
import (
"context"
"os"
"path/filepath"
"testing"
)
func TestOpenOutpuFile(t *testing.T) {
w := New()
_, err := w.Interpret(context.TODO(), `
(let ((w (open-output-file "hogehoge")))
(unwind-protect
(format w "hogehoge~%")
(close w)
)
)
`)
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove("hogehoge")
output, err := os.ReadFile("hogehoge")
if err != nil {
t.Fatal(err.Error())
}
if o := string(output); o != "hogehoge\n" {
t.Fatalf(`expect "hogehoge" but "%s"`, o)
}
}
func TestOpenOutputFile(t *testing.T) {
w := New()
_, err := w.Interpret(context.TODO(), `
(with-open-output-file (w "hogehoge")
(format w "hogehoge~%")
)
`)
if err != nil {
t.Fatal(err.Error())
}
defer os.Remove("hogehoge")
output, err := os.ReadFile("hogehoge")
if err != nil {
t.Fatal(err.Error())
}
if o := string(output); o != "hogehoge\n" {
t.Fatalf(`expect "hogehoge" but "%s"`, o)
}
}
func TestFileLength(t *testing.T) {
testFile := filepath.Join(os.TempDir(), "testfile")
fd, err := os.Create(testFile)
if err != nil {
t.Fatal(err.Error())
}
fd.WriteString("0123456789")
fd.Close()
defer os.Remove(testFile)
lisp := New()
lisp = lisp.Let(Variables{NewSymbol("fname"): String(testFile)})
if eStr := lisp.Assert(`(file-length fname 8)`, Integer(10)); eStr != "" {
t.Fatal(eStr)
}
if eStr := lisp.Assert(`(file-length fname 2)`, Integer(40)); eStr != "" {
t.Fatal(eStr)
}
}