go.temporal.io/server/chasm/map_test.go

53 LOC · 0 covered · 53 uncovered · 0 ranges · 0 concepts · 0 introducers · 0 tests

1 package chasm
2
3 import (
4 "go/ast"
5 "go/parser"
6 "go/printer"
7 "go/token"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14 )
15
16 // Another approach would be to code generate string const.
17 func TestMapKeyTypesMatchConst(t *testing.T) {
18 _, currentFile, _, ok := runtime.Caller(0)
19 require.True(t, ok, "failed to get current file path")
20 srcFile := filepath.Join(filepath.Dir(currentFile), "map.go")
21
22 fset := token.NewFileSet()
23 file, err := parser.ParseFile(fset, srcFile, nil, parser.AllErrors)
24 require.NoError(t, err)
25
26 var found string
27 // Walk the top‐level declarations looking for:
28 // type Map[K ... , T any] map[K]T
29 for _, decl := range file.Decls {
30 gd, ok := decl.(*ast.GenDecl)
31 if !ok || gd.Tok != token.TYPE {
32 continue
33 }
34 for _, spec := range gd.Specs {
35 ts, ok := spec.(*ast.TypeSpec)
36 if !ok || ts.Name.Name != "Map" {
37 continue
38 }
39 // ts.TypeParams.List[0] is the field for K
40 if ts.TypeParams != nil && len(ts.TypeParams.List) > 0 {
41 field := ts.TypeParams.List[0]
42 var buf strings.Builder
43 // pretty‐print the AST node for the constraint
44 err = printer.Fprint(&buf, fset, field.Type)
45 require.NoError(t, err)
46 found = buf.String()
47 }
48 }
49 }
50
51 require.NotEmpty(t, found, "could not locate Map[K …] in AST")
52 require.Equal(t, mapKeyTypes, found)
53 }