xref: /aosp_15_r20/external/golang-protobuf/internal/impl/lazy_test.go (revision 1c12ee1efe575feb122dbf939ff15148a3b3e8f2)
1// Copyright 2020 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package impl_test
6
7import (
8	"testing"
9
10	"google.golang.org/protobuf/internal/flags"
11	"google.golang.org/protobuf/internal/impl"
12	"google.golang.org/protobuf/internal/protobuild"
13	"google.golang.org/protobuf/proto"
14
15	testpb "google.golang.org/protobuf/internal/testprotos/test"
16)
17
18func TestLazyExtensions(t *testing.T) {
19	checkLazy := func(when string, m *testpb.TestAllExtensions, want bool) {
20		xd := testpb.E_OptionalNestedMessage.TypeDescriptor()
21		if got := impl.IsLazy(m.ProtoReflect(), xd); got != want {
22			t.Errorf("%v: m.optional_nested_message lazy=%v, want %v", when, got, want)
23		}
24		e := proto.GetExtension(m, testpb.E_OptionalNestedMessage).(*testpb.TestAllExtensions_NestedMessage).Corecursive
25		if got := impl.IsLazy(e.ProtoReflect(), xd); got != want {
26			t.Errorf("%v: m.optional_nested_message.corecursive.optional_nested_message lazy=%v, want %v", when, got, want)
27		}
28	}
29
30	m1 := &testpb.TestAllExtensions{}
31	protobuild.Message{
32		"optional_nested_message": protobuild.Message{
33			"a": 1,
34			"corecursive": protobuild.Message{
35				"optional_nested_message": protobuild.Message{
36					"a": 2,
37				},
38			},
39		},
40	}.Build(m1.ProtoReflect())
41	checkLazy("before unmarshal", m1, false)
42
43	w, err := proto.Marshal(m1)
44	if err != nil {
45		t.Fatal(err)
46	}
47	m := &testpb.TestAllExtensions{}
48	if err := proto.Unmarshal(w, m); err != nil {
49		t.Fatal(err)
50	}
51	checkLazy("after unmarshal", m, flags.LazyUnmarshalExtensions)
52}
53