-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtype_tuple_test.go
More file actions
142 lines (131 loc) · 3.85 KB
/
type_tuple_test.go
File metadata and controls
142 lines (131 loc) · 3.85 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
136
137
138
139
140
141
142
//go:build !genjs
/*
Copyright 2026 The XGo Authors (xgo.dev)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogen_test
import (
"go/token"
"go/types"
"testing"
"github.com/goplus/gogen"
)
func TestIsTupleType(t *testing.T) {
var cb gogen.CodeBuilder
x := types.NewField(token.NoPos, nil, "x", types.Typ[types.Int], false)
y := types.NewField(token.NoPos, nil, "y", types.Typ[types.Int], false)
flds := []*types.Var{x, y}
if cb.IsTupleType(types.NewStruct(flds, nil)) {
t.Fatal("IsTupleType returned true for a struct with non-tuple field names")
}
}
func TestTupleLit(t *testing.T) {
pkg := newMainPackage()
pkg.NewFunc(nil, "foo", nil, nil, false).BodyStart(pkg).
Val(1).
Val(2).
TupleLit(nil, 2).
EndStmt().
End()
domTest(t, pkg, `package main
func foo() {
struct {
X_0 int
X_1 int
}{1, 2}
}
`)
}
func TestTupleMember(t *testing.T) {
pkg := newMainPackage()
x := types.NewField(token.NoPos, pkg.Types, "x", types.Typ[types.Int], false)
y := types.NewField(token.NoPos, pkg.Types, "y", types.Typ[types.Int], false)
typ := pkg.NewTuple(true, x, y)
pt := types.NewNamed(types.NewTypeName(token.NoPos, pkg.Types, "Point", typ), typ, nil)
a := types.NewParam(token.NoPos, pkg.Types, "a", typ)
b := types.NewParam(token.NoPos, pkg.Types, "b", pt)
typf := types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(b), false)
f := types.NewParam(token.NoPos, pkg.Types, "f", typf)
ok := types.NewParam(token.NoPos, pkg.Types, "ok", types.Typ[types.Bool])
typf2 := types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(b, ok), false)
f2 := types.NewParam(token.NoPos, pkg.Types, "f2", typf2)
pkg.NewFunc(nil, "foo", types.NewTuple(a, f, f2), nil, false).BodyStart(pkg).
Val(ctxRef(pkg, "a")).
MemberRef("0").
Val(ctxRef(pkg, "a")).
MemberVal("1", 0).
Assign(1).
EndStmt().
Val(ctxRef(pkg, "a")).
MemberRef("x").
Val(ctxRef(pkg, "a")).
MemberVal("y", 0).
Assign(1).
EndStmt().
DefineVarStart(token.NoPos, "x").
Val(ctxRef(pkg, "a")).
EndInit(1).
DefineVarStart(token.NoPos, "x2").
Val(ctxRef(pkg, "f")).Call(0).
EndInit(1).
DefineVarStart(token.NoPos, "x3", "ok").
Val(ctxRef(pkg, "f2")).Call(0).
EndInit(1).
Debug(func(cb *gogen.CodeBuilder) {
cb.Val(ctxRef(pkg, "a"))
cb.Member("unknown", 0, gogen.MemberFlagRef)
cb.Member("unknown", 0, gogen.MemberFlagVal)
cb.ResetStmt()
}).
End()
domTest(t, pkg, `package main
func foo(a struct {
X_0 int
X_1 int
}, f func() (b Point), f2 func() (b Point, ok bool)) {
a.X_0 = a.X_1
a.X_0 = a.X_1
x := a
x2 := f()
x3, ok := f2()
}
`)
}
func newFields(names ...string) []*types.Var {
ret := make([]*types.Var, len(names))
for i, name := range names {
ret[i] = types.NewField(token.NoPos, nil, name, types.Typ[types.Int], false)
}
return ret
}
func TestCodeBuilder_LookupField(t *testing.T) {
p := newMainPackage()
cb := p.CB()
tests := []struct {
name string // description of this test case
t *types.Struct
fld string
want int
}{
{"test1", types.NewStruct(newFields("a"), nil), "b", -1},
{"test2", types.NewStruct(newFields("a"), nil), "a", 0},
{"test3", p.NewTuple(true, newFields("a")...), "a", 0},
{"test4", p.NewTuple(true, newFields("a")...), "0", 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cb.LookupField(tt.t, tt.fld)
if got != tt.want {
t.Errorf("LookupField() = %v, want %v", got, tt.want)
}
})
}
}