go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/clone.star (about)

     1  # Copyright 2020 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  l = proto.new_loader(proto.new_descriptor_set(blob=read('./testprotos/all.pb')))
    16  testprotos = l.module('go.chromium.org/luci/starlark/starlarkproto/testprotos/test.proto')
    17  
    18  
    19  # Works in general.
    20  src = testprotos.SimpleFields(i64=123, b=True)
    21  assert.eq(proto.clone(src), src)
    22  
    23  # Should return a deep copy.
    24  map_src = testprotos.MapWithMessageType(m={
    25      "msg1": testprotos.Simple(many_i=[1, 2, 3]),
    26  })
    27  
    28  copy = proto.clone(map_src)
    29  copy.m['msg1'].i = -1
    30  copy.m['msg1'].many_i[0] += 100
    31  copy.m['msg1'].many_i.append(1024)
    32  copy.m['msg2'] = testprotos.Simple()
    33  
    34  assert.eq(map_src, testprotos.MapWithMessageType(m={
    35      "msg1": testprotos.Simple(many_i=[1, 2, 3]),
    36  }))
    37  assert.eq(copy, testprotos.MapWithMessageType(m={
    38      "msg1": testprotos.Simple(i=-1, many_i=[101, 2, 3, 1024]),
    39      "msg2": testprotos.Simple(),
    40  }))
    41  
    42  # clone expects an argument.
    43  def clone_no_args():
    44    proto.clone()
    45  assert.fails(clone_no_args, 'missing argument for msg')
    46  
    47  # Too many arguments.
    48  def clone_too_many_args():
    49    proto.clone(src, src)
    50  assert.fails(clone_too_many_args, 'clone: got 2 arguments, want at most 1')
    51  
    52  # None argument.
    53  def clone_with_none():
    54    proto.clone(None)
    55  assert.fails(clone_with_none, 'clone: for parameter msg: got NoneType, want proto.Message')
    56  
    57  # Wrongly typed argument.
    58  def clone_with_int():
    59    proto.clone(1)
    60  assert.fails(clone_with_int, 'clone: for parameter msg: got int, want proto.Message')