-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
81 lines (63 loc) · 1.6 KB
/
main_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
74
75
76
77
78
79
80
81
package main
import (
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testConversion(t *testing.T, dir string) {
dir = filepath.Join("testdata", dir)
cmd := exec.Command("lingo")
cmd.Dir = dir
err := cmd.Run()
require.NoError(t, err)
expectedDir := filepath.Join(dir, "expected")
expected, err := os.ReadDir(expectedDir)
require.NoError(t, err)
expectedFiles := make(map[string][]byte, len(expected))
for _, entry := range expected {
if !entry.IsDir() {
contents, err := os.ReadFile(filepath.Join(expectedDir, entry.Name()))
require.NoError(t, err)
expectedFiles[entry.Name()] = contents
}
}
actual, err := os.ReadDir(dir)
require.NoError(t, err)
for _, entry := range actual {
if !entry.IsDir() {
expected, ok := expectedFiles[entry.Name()]
if !assert.Truef(t, ok, "unexpected file %v", entry.Name()) {
continue
}
contents, err := os.ReadFile(filepath.Join(dir, entry.Name()))
require.NoError(t, err)
assert.Equal(t, expected, contents)
delete(expectedFiles, entry.Name())
}
}
if len(expectedFiles) != 0 {
missing := make([]string, 0, len(expectedFiles))
for k := range expectedFiles {
missing = append(missing, k)
}
sort.Strings(missing)
assert.Failf(t, "missing files: %v", strings.Join(missing, ", "))
}
}
func TestLingo(t *testing.T) {
testConversion(t, "lingo")
}
func TestTour(t *testing.T) {
testConversion(t, "tour")
}
func TestHanoi(t *testing.T) {
testConversion(t, "hanoi")
}
func TestEmpty(t *testing.T) {
testConversion(t, "empty")
}