forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gonative_test.go
143 lines (130 loc) · 3.38 KB
/
gonative_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
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
136
137
138
139
140
141
142
143
package gno
import (
"bytes"
"fmt"
"reflect"
"testing"
"github.com/gnolang/gno/pkgs/crypto"
"github.com/jaekwon/testify/assert"
)
// args is an even number of elements,
// the even index items are package nodes,
// and the odd index items are corresponding package values.
func gonativeTestStore(args ...interface{}) Store {
store := NewStore(nil, nil, nil)
store.SetPackageGetter(func(pkgPath string) (*PackageNode, *PackageValue) {
for i := 0; i < len(args)/2; i++ {
pn := args[i*2].(*PackageNode)
pv := args[i*2+1].(*PackageValue)
if pkgPath == pv.PkgPath {
return pn, pv
}
}
return nil, nil
})
store.SetStrictGo2GnoMapping(false)
return store
}
type Foo struct {
A int
B int32
C int64
D string
}
func TestGoNativeDefine(t *testing.T) {
// Create package foo and define Foo.
pkg := NewPackageNode("foo", "test.foo", nil)
rt := reflect.TypeOf(Foo{})
pkg.DefineGoNativeType(rt)
nt := pkg.GetValueRef(nil, Name("Foo")).GetType().(*NativeType)
assert.Equal(t, nt.Type, rt)
path := pkg.GetPathForName(nil, Name("Foo"))
assert.Equal(t, path.Depth, uint8(1))
assert.Equal(t, path.Index, uint16(0))
pv := pkg.NewPackage()
nt = pv.GetBlock(nil).GetPointerTo(nil, path).TV.GetType().(*NativeType)
assert.Equal(t, nt.Type, rt)
store := gonativeTestStore(pkg, pv)
// Import above package and evaluate foo.Foo.
m := NewMachineWithOptions(MachineOptions{
PkgPath: "test",
Store: store,
})
m.RunDeclaration(ImportD("foo", "test.foo"))
tvs := m.Eval(Sel(Nx("foo"), "Foo"))
assert.Equal(t, len(tvs), 1)
assert.Equal(t, tvs[0].V.(TypeValue).Type, nt)
}
func TestGoNativeDefine2(t *testing.T) {
// Create package foo and define Foo.
pkg := NewPackageNode("foo", "test.foo", nil)
rt := reflect.TypeOf(Foo{})
pkg.DefineGoNativeType(rt)
pv := pkg.NewPackage()
store := gonativeTestStore(pkg, pv)
// Import above package and run file.
out := new(bytes.Buffer)
m := NewMachineWithOptions(MachineOptions{
PkgPath: "main",
Output: out,
Store: store,
})
c := `package main
import foo "test.foo"
func main() {
f := foo.Foo{A:1}
println("A:", f.A)
println("B:", f.B)
println("C:", f.C)
println("D:", f.D)
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()
assert.Equal(t, string(out.Bytes()), `A: 1
B: 0
C: 0
D:
`)
}
func TestGoNativeDefine3(t *testing.T) {
// Create package foo and define Foo.
out := new(bytes.Buffer)
pkg := NewPackageNode("foo", "test.foo", nil)
pkg.DefineGoNativeType(reflect.TypeOf(Foo{}))
pkg.DefineGoNativeValue("PrintFoo", func(f Foo) {
out.Write([]byte(fmt.Sprintf("A: %v\n", f.A)))
out.Write([]byte(fmt.Sprintf("B: %v\n", f.B)))
out.Write([]byte(fmt.Sprintf("C: %v\n", f.C)))
out.Write([]byte(fmt.Sprintf("D: %v\n", f.D)))
})
pv := pkg.NewPackage()
store := gonativeTestStore(pkg, pv)
// Import above package and run file.
m := NewMachineWithOptions(MachineOptions{
PkgPath: "main",
Output: out,
Store: store,
})
c := `package main
import foo "test.foo"
func main() {
f := foo.Foo{A:1}
foo.PrintFoo(f)
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()
assert.Equal(t, string(out.Bytes()), `A: 1
B: 0
C: 0
D:
`)
}
func TestCrypto(t *testing.T) {
addr := crypto.Address{}
store := gonativeTestStore()
tv := Go2GnoValue(nilAllocator, store, reflect.ValueOf(addr))
assert.Equal(t, tv.String(),
`(array[0x0000000000000000000000000000000000000000] github.com/gnolang/gno/pkgs/crypto.Address)`)
}