-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast_test.go
71 lines (66 loc) · 2.08 KB
/
ast_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
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGenericFunc(t *testing.T) {
t.Run("generic T any", func(t *testing.T) {
_, f := MustGenFunc(`func G[T any]() { }`)
require.True(t, IsGenericFunc(f))
})
t.Run("generic T ~Scanner", func(t *testing.T) {
_, f := MustGenFunc(`func G[T ~Scanner]() { }`)
require.True(t, IsGenericFunc(f))
})
t.Run("generic T ****any", func(t *testing.T) {
_, f := MustGenFunc(`func G[T ****any]() { }`)
require.True(t, IsGenericFunc(f))
})
t.Run("generic T, Q, K ****any", func(t *testing.T) {
_, f := MustGenFunc(`func G[T, Q, K ****any]() { }`)
require.True(t, IsGenericFunc(f))
})
t.Run("receiver T", func(t *testing.T) {
_, f := MustGenFunc(`func (s Q[T]) G() { }`)
require.True(t, IsGenericFunc(f))
})
t.Run("receiver T, Q, K", func(t *testing.T) {
_, f := MustGenFunc(`func (s Q[T, Q, K]) G() { }`)
require.True(t, IsGenericFunc(f))
})
t.Run("non-generic", func(t *testing.T) {
_, f := MustGenFunc(`func (s Q) G() { }`)
require.False(t, IsGenericFunc(f))
})
}
func TestEqualExprs(t *testing.T) {
t.Run("idents", func(t *testing.T) {
_, e0 := MustGenExpr(`a.Field.Value`)
_, e1 := MustGenExpr(`a.Field.Value`)
_, e2 := MustGenExpr(`b.Field.Value`)
_, e3 := MustGenExpr(`a.Prop.Value`)
_, e4 := MustGenExpr(`a.Field.Key`)
require.True(t, EqualExprs(e0, e1))
require.True(t, EqualExprs(e1, e1))
require.False(t, EqualExprs(e1, e2))
require.False(t, EqualExprs(e1, e3))
require.False(t, EqualExprs(e1, e4))
})
t.Run("array access", func(t *testing.T) {
_, e0 := MustGenExpr(`a.Field[0].Value`)
_, e1 := MustGenExpr(`a.Field[0].Value`)
_, e2 := MustGenExpr(`a.Field[1].Value`)
_, e3 := MustGenExpr(`a.Field[b].Value`)
require.True(t, EqualExprs(e0, e1))
require.False(t, EqualExprs(e1, e2))
require.False(t, EqualExprs(e1, e3))
})
}
func TestDeconstructSelector(t *testing.T) {
t.Run("simple selector", func(t *testing.T) {
_, e := MustGenExpr(`a.Field.Value`)
selector, ok := DeconstructSelector(e)
require.True(t, ok)
require.Equal(t, "a.Field.Value", selector)
})
}