-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg_test.go
91 lines (77 loc) · 1.58 KB
/
pkg_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
82
83
84
85
86
87
88
89
90
91
package memlayout
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
const testSource = `
package main
func Foo() int { return 5 }
type Bar struct {
A int
B string
}
type Baz interface {
Bazer() string
}
type Qux struct {
A int
B bool
C struct {
D int64
E []byte
}
F *string
}
`
func TestStructsFromFile(t *testing.T) {
require := require.New(t)
tmp, err := ioutil.TempDir(os.TempDir(), "tmp-memlayout")
require.NoError(err)
defer func() {
require.NoError(os.RemoveAll(tmp))
}()
path := filepath.Join(tmp, "test.go")
require.NoError(ioutil.WriteFile(path, []byte(testSource), 0755))
structs, err := StructsFromFile(path, []byte(testSource))
require.NoError(err)
expected := []Struct{
{"Bar", Pos{6, 9}, []Field{
f("A", "int", 0, 8, 8, 8, false),
f("B", "string", 8, 24, 16, 8, false),
}},
{"Qux", Pos{15, 23}, []Field{
f("A", "int", 0, 8, 8, 8, false),
f("B", "bool", 8, 9, 1, 1, false),
f("", "", 9, 16, 7, 0, true),
f("C", "struct{D int64; E []byte}", 16, 48, 32, 8, false,
f("D", "int64", 16, 24, 8, 8, false),
f("E", "[]byte", 24, 48, 24, 8, false),
),
f("F", "*string", 48, 56, 8, 8, false),
}},
}
require.Len(structs, len(expected))
for i := range expected {
structsEqual(t, expected[i], structs[i])
}
}
func f(
name, typ string,
start, end, size, align int64,
padding bool,
children ...Field,
) Field {
return Field{
Name: name,
Type: typ,
Start: start,
End: end,
Size: size,
Align: align,
IsPadding: padding,
Children: children,
}
}