go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/string.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  m = testprotos.SimpleFields()
    19  
    20  # Default value.
    21  assert.eq(m.s, '')
    22  
    23  # Setter and getter works.
    24  m.s = 'blah'
    25  assert.eq(m.s, 'blah')
    26  assert.eq(proto.to_textpb(m), 's: "blah"\n')
    27  
    28  # Setting through constructor works.
    29  m2 = testprotos.SimpleFields(s='blah')
    30  assert.eq(m2.s, 'blah')
    31  
    32  # Clearing works.
    33  m2.s = None
    34  assert.eq(m2.s, '')
    35  
    36  # Setting wrong type fails.
    37  def set_bad():
    38    m2.s = 1
    39  assert.fails(set_bad, 'got int, want string')
    40  
    41  # Assiging string to a non-string field fails.
    42  def set_string_to_int():
    43    m2.i64 = ''
    44  assert.fails(set_string_to_int, 'got string, want int')