Skip to content

Commit c3ac91d

Browse files
ethanalee-workgopherbot
authored andcommitted
internal/godoc: add unit test for TestDecodeBasicLit
Test: go generate ./internal/godoc/... && go test -v -run TestDecodeBasicLit - This test ensures that the encoding and decoding of fields remains consistent. - A golden byte slice represesnting an ast.BasicLit should still be able to be decoded if the codec changes (i.e. a new field is added). Fixes golang/go#76350 Change-Id: I9747b1961247f2250c0377da5d1c4684fa810e92 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/727201 Auto-Submit: Ethan Lee <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent d74adea commit c3ac91d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

internal/godoc/godoc_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ package godoc
66

77
import (
88
"bytes"
9+
"go/ast"
910
"go/format"
1011
"go/parser"
1112
"go/token"
1213
"testing"
1314

1415
"github.com/google/go-cmp/cmp"
16+
"golang.org/x/pkgsite/internal/godoc/codec"
1517
)
1618

1719
func TestRemoveUnusedASTNodes(t *testing.T) {
@@ -110,3 +112,32 @@ func (t) U()
110112
t.Errorf("mismatch (-want, +got):\n%s", diff)
111113
}
112114
}
115+
116+
func TestDecodeBasicLit(t *testing.T) {
117+
// byte slice representing an encoded ast.BasicLit from Go 1.25.
118+
golden := []byte{
119+
0xf6, 0x1, 0xf7, 0xd, 0x2a, 0x61, 0x73, 0x74, 0x2e, 0x42, 0x61, 0x73, 0x69,
120+
0x63, 0x4c, 0x69, 0x74, 0xf6, 0x2, 0x0, 0xf4, 0x1, 0xa, 0x2, 0xf7, 0x3,
121+
0x31, 0x32, 0x33, 0xf3,
122+
}
123+
124+
d := codec.NewDecoder(golden)
125+
val, err := d.Decode()
126+
if err != nil {
127+
t.Fatalf("Decode() failed: %v", err)
128+
}
129+
130+
got, ok := val.(*ast.BasicLit)
131+
if !ok {
132+
t.Fatalf("decoded value is not an ast.BasicLit, got %T", val)
133+
}
134+
135+
want := ast.BasicLit{
136+
Kind: token.INT,
137+
Value: "123",
138+
}
139+
140+
if got.Kind != want.Kind || got.Value != want.Value {
141+
t.Errorf("Decode(...) = %+v, want %+v", got, want)
142+
}
143+
}

0 commit comments

Comments
 (0)