-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemit_test.go
More file actions
135 lines (117 loc) · 4.07 KB
/
Copy pathemit_test.go
File metadata and controls
135 lines (117 loc) · 4.07 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
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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestBind(t *testing.T) {
srcDir := "testdata/src"
dstDir := "testdata/dst"
entries, err := os.ReadDir(srcDir)
if err != nil {
t.Fatal(err)
}
var found int
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".h") {
continue
}
name := strings.TrimSuffix(e.Name(), ".h")
t.Run(name, func(t *testing.T) {
srcPath := filepath.Join(srcDir, e.Name())
dstPath := filepath.Join(dstDir, name+".go")
compare(t, srcPath, dstPath, Options{Package: "main"})
})
found++
}
if found == 0 {
t.Fatal("no .h files found in", srcDir)
}
}
func TestBindBody(t *testing.T) {
srcPath := filepath.Join("testdata/src", "funcs.h")
dstPath := filepath.Join("testdata/dst", "funcs_body.go")
compare(t, srcPath, dstPath, Options{Package: "main", Body: true})
}
func TestBindDefine(t *testing.T) {
srcPath := filepath.Join("testdata/src", "define.h")
dstPath := filepath.Join("testdata/dst", "define_audio.go")
compare(t, srcPath, dstPath, Options{Package: "main", Define: []string{"WITH_AUDIO"}})
}
func TestBindStyleCap(t *testing.T) {
srcPath := filepath.Join("testdata/src", "style.h")
dstPath := filepath.Join("testdata/dst", "style_cap.go")
opts := Options{Package: "main", Style: styleCap, Strip: []string{"uv_"}}
compare(t, srcPath, dstPath, opts)
}
func TestBindStyleGo(t *testing.T) {
srcPath := filepath.Join("testdata/src", "style.h")
dstPath := filepath.Join("testdata/dst", "style_go.go")
opts := Options{Package: "main", Style: styleGo, Strip: []string{"uv_"}}
compare(t, srcPath, dstPath, opts)
}
func TestBindScope(t *testing.T) {
// -scope emits the headers under a directory, so an umbrella header that
// only includes them produces a binding. The default TestBind case covers
// the same header without -scope, where it emits nothing.
srcPath := filepath.Join("testdata/src", "umbrella.h")
dstPath := filepath.Join("testdata/dst", "umbrella_scope.go")
opts := Options{Package: "main", Scope: []string{"testdata/src/umbrella"}}
compare(t, srcPath, dstPath, opts)
}
func TestBindCollision(t *testing.T) {
// Check that two C names mapping to one Solod name fail.
srcPath := filepath.Join("testdata/src/collide", "collide.h")
opts := Options{Package: "main", Style: styleGo, Strip: []string{"uv_"}}
_, err := Emit([]string{srcPath}, opts)
if err == nil {
t.Fatal("expected a name collision error")
}
want := `C names collide; copy these into a -rename file and edit the Solod names apart:
struct stat Stat
stat Stat
UV_TIMEOUT Timeout
uv_timeout Timeout`
if err.Error() != want {
t.Errorf("got %q, want %q", err, want)
}
}
func TestBindRename(t *testing.T) {
// A rename file gives distinct Solod names to the two colliding symbols.
rename, err := parseRenames(filepath.Join("testdata/src/collide", "collide.rename"))
if err != nil {
t.Fatal(err)
}
srcPath := filepath.Join("testdata/src/collide", "collide.h")
dstPath := filepath.Join("testdata/dst", "collide_rename.go")
opts := Options{Package: "main", Style: styleGo, Strip: []string{"uv_"}, Rename: rename}
compare(t, srcPath, dstPath, opts)
}
func TestBindExclude(t *testing.T) {
// A rename file with a bare C name drops that symbol, so the pair no
// longer collides.
rename, err := parseRenames(filepath.Join("testdata/src/collide", "collide_exclude.rename"))
if err != nil {
t.Fatal(err)
}
srcPath := filepath.Join("testdata/src/collide", "collide.h")
dstPath := filepath.Join("testdata/dst", "collide_exclude.go")
opts := Options{Package: "main", Style: styleGo, Strip: []string{"uv_"}, Rename: rename}
compare(t, srcPath, dstPath, opts)
}
// compare emits the header and diffs it against the expected output.
func compare(t *testing.T, srcPath, dstPath string, opts Options) {
t.Helper()
got, err := Emit([]string{srcPath}, opts)
if err != nil {
t.Fatal(err)
}
want, err := os.ReadFile(dstPath)
if err != nil {
t.Fatalf("missing expected output %s: %v", dstPath, err)
}
if string(got) != string(want) {
t.Errorf("output mismatch for %s\n--- want\n%s\n--- got\n%s", srcPath, want, got)
}
}