aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2025-12-05 20:58:47 +0000
committerGopher Robot <gobot@golang.org>2025-12-08 06:34:29 -0800
commitc3ac91df4d42d837427c71d2c894fe17f334aa71 (patch)
tree80879422cc39e81e913481e72ce68901f020ae7e
parentd74adeade195e94064b1dc38fd14b93037ad7694 (diff)
downloadgo-x-pkgsite-c3ac91df4d42d837427c71d2c894fe17f334aa71.tar.xz
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 <ethanalee@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> kokoro-CI: kokoro <noreply+kokoro@google.com>
-rw-r--r--internal/godoc/godoc_test.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/internal/godoc/godoc_test.go b/internal/godoc/godoc_test.go
index 1627361b..2e7664e9 100644
--- a/internal/godoc/godoc_test.go
+++ b/internal/godoc/godoc_test.go
@@ -6,12 +6,14 @@ package godoc
import (
"bytes"
+ "go/ast"
"go/format"
"go/parser"
"go/token"
"testing"
"github.com/google/go-cmp/cmp"
+ "golang.org/x/pkgsite/internal/godoc/codec"
)
func TestRemoveUnusedASTNodes(t *testing.T) {
@@ -110,3 +112,32 @@ func (t) U()
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
}
+
+func TestDecodeBasicLit(t *testing.T) {
+ // byte slice representing an encoded ast.BasicLit from Go 1.25.
+ golden := []byte{
+ 0xf6, 0x1, 0xf7, 0xd, 0x2a, 0x61, 0x73, 0x74, 0x2e, 0x42, 0x61, 0x73, 0x69,
+ 0x63, 0x4c, 0x69, 0x74, 0xf6, 0x2, 0x0, 0xf4, 0x1, 0xa, 0x2, 0xf7, 0x3,
+ 0x31, 0x32, 0x33, 0xf3,
+ }
+
+ d := codec.NewDecoder(golden)
+ val, err := d.Decode()
+ if err != nil {
+ t.Fatalf("Decode() failed: %v", err)
+ }
+
+ got, ok := val.(*ast.BasicLit)
+ if !ok {
+ t.Fatalf("decoded value is not an ast.BasicLit, got %T", val)
+ }
+
+ want := ast.BasicLit{
+ Kind: token.INT,
+ Value: "123",
+ }
+
+ if got.Kind != want.Kind || got.Value != want.Value {
+ t.Errorf("Decode(...) = %+v, want %+v", got, want)
+ }
+}