go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/reflectutil/shallow_copy_test.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package reflectutil 16 17 import ( 18 "testing" 19 20 . "github.com/smartystreets/goconvey/convey" 21 . "go.chromium.org/luci/common/testing/assertions" 22 ) 23 24 func TestShallowCopy(t *testing.T) { 25 t.Parallel() 26 27 Convey(`ShallowCopy`, t, func() { 28 Convey(`nil`, func() { 29 msg := (*TestShallowCopyMessage)(nil) 30 newMsg := ShallowCopy(msg).(*TestShallowCopyMessage) 31 So(newMsg, ShouldBeNil) 32 }) 33 34 Convey(`basic fields`, func() { 35 msg := &TestShallowCopyMessage{ 36 Field: "hello", 37 RepeatedField: []string{ 38 "this", "is", "a", "test", 39 }, 40 MappedField: map[string]string{ 41 "this": "is", 42 "a": "test", 43 }, 44 } 45 newMsg := ShallowCopy(msg).(*TestShallowCopyMessage) 46 So(newMsg, ShouldResembleProto, msg) 47 48 msg.RepeatedField[0] = "meepmorp" 49 So(newMsg.RepeatedField[0], ShouldEqual, "meepmorp") 50 51 msg.MappedField["this"] = "meepmorp" 52 So(newMsg.MappedField["this"], ShouldEqual, "meepmorp") 53 }) 54 55 Convey(`msg fields`, func() { 56 msg := &TestShallowCopyMessage{ 57 InnerMsg: &TestShallowCopyMessage_Inner{Field: "hello"}, 58 RepeatedMsg: []*TestShallowCopyMessage_Inner{ 59 {Field: "this"}, {Field: "is"}, {Field: "a"}, {Field: "test"}, 60 }, 61 MappedMsg: map[string]*TestShallowCopyMessage_Inner{ 62 "this": {Field: "is"}, 63 "a": {Field: "test"}, 64 }, 65 } 66 newMsg := ShallowCopy(msg).(*TestShallowCopyMessage) 67 So(newMsg, ShouldResembleProto, msg) 68 69 msg.InnerMsg.Field = "meepmorp" 70 So(newMsg.InnerMsg.Field, ShouldEqual, "meepmorp") 71 72 msg.RepeatedMsg[0].Field = "meepmorp" 73 So(newMsg.RepeatedMsg[0].Field, ShouldEqual, "meepmorp") 74 75 msg.MappedMsg["this"].Field = "meepmorp" 76 So(newMsg.MappedMsg["this"].Field, ShouldEqual, "meepmorp") 77 }) 78 }) 79 }