go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/bytes.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  # 'bytes' fields are represented as strings on the Starlark side.
    19  
    20  m = testprotos.SimpleFields()
    21  
    22  # Default value.
    23  assert.eq(m.bs, '')
    24  
    25  # Setter and getter works.
    26  m.bs = '\x00\x01\x02\x03'
    27  assert.eq(m.bs, '\x00\x01\x02\x03')
    28  assert.eq(proto.to_textpb(m), 'bs: "\\x00\\x01\\x02\\x03"\n')
    29  
    30  # Setting through constructor works.
    31  m2 = testprotos.SimpleFields(bs='abc')
    32  assert.eq(m2.bs, 'abc')
    33  
    34  # Clearing works.
    35  m2.bs = None
    36  assert.eq(m2.bs, '')
    37  
    38  # Setting wrong type fails.
    39  def set_bad():
    40    m2.bs = 1
    41  assert.fails(set_bad, 'got int, want string')