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

     1  # Copyright 2018 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  # Behavior and implementation if message-valued repeated fields isn't much
    19  # different from other repeated fields, so we test only special cases.
    20  
    21  m = testprotos.MessageFields()
    22  
    23  # Default value.
    24  assert.eq(type(m.rep), 'list<proto.Message<testprotos.Simple>>')
    25  assert.eq(len(m.rep), 0)
    26  
    27  # Can append to it, it is just like a list.
    28  m.rep.append(testprotos.Simple(i=123))
    29  assert.eq(m.rep[0].i, 123)
    30  
    31  # Can replace it.
    32  m.rep = [testprotos.Simple(i=456)]
    33  assert.eq(m.rep[0].i, 456)
    34  
    35  # Assigning None initializes a new empty message.
    36  m.rep[0] = None
    37  assert.eq(m.rep[0].i, 0)
    38  
    39  # Assigning a dict initializes a new message.
    40  m.rep[0] = {'i': 999}
    41  assert.eq(m.rep[0].i, 999)
    42  
    43  # Does type checks when updating the list.
    44  def append_bad_value():
    45    m.rep.append(testprotos.MessageFields())
    46  assert.fails(append_bad_value,
    47      'append: got proto.Message<testprotos.MessageFields>, want proto.Message<testprotos.Simple>')
    48  def set_bad_value():
    49    m.rep[0] = testprotos.MessageFields()
    50  assert.fails(set_bad_value,
    51      'item #0: got proto.Message<testprotos.MessageFields>, want proto.Message<testprotos.Simple>')
    52  
    53  # Checks types of elements when constructing from a list.
    54  def copy_bad_list():
    55    m.rep = [testprotos.Simple(i=456), testprotos.MessageFields()]
    56  assert.fails(copy_bad_list,
    57      'item #1: got proto.Message<testprotos.MessageFields>, want proto.Message<testprotos.Simple>')
    58  
    59  # Serialization to text proto works.
    60  text = proto.to_textpb(testprotos.MessageFields(
    61    rep=[
    62      testprotos.Simple(i=123),
    63      testprotos.Simple(i=456),
    64    ],
    65  ))
    66  assert.eq(text, """rep {
    67    i: 123
    68  }
    69  rep {
    70    i: 456
    71  }
    72  """)