go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/bool.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.b, False)
    22  
    23  # Setter and getter works.
    24  m.b = True
    25  assert.eq(m.b, True)
    26  assert.eq(proto.to_textpb(m), 'b: true\n')
    27  m.b = False
    28  assert.eq(m.b, False)
    29  assert.eq(proto.to_textpb(m), '')  # 'false' is default!
    30  
    31  # Setting through constructor works.
    32  m2 = testprotos.SimpleFields(b=True)
    33  assert.eq(m2.b, True)
    34  
    35  # Clearing works.
    36  m2.b = None
    37  assert.eq(m2.b, False)
    38  
    39  # Setting wrong type fails.
    40  def set_bad():
    41    m2.b = [1, 2, 3]
    42  assert.fails(set_bad, 'got list, want bool')
    43  
    44  # We don't support implicit conversions to bool. Callers should use bool(...)
    45  # cast explicitly.
    46  def set_int():
    47    m2.b = 0
    48  assert.fails(set_int, 'got int, want bool')
    49  
    50  # We don't support implicit conversions to bool. Callers should use bool(...)
    51  # cast explicitly.
    52  def set_list():
    53    m2.b = []
    54  assert.fails(set_list, 'got list, want bool')
    55  
    56  # Assiging bool to non-bool field fails.
    57  def set_bool_to_int():
    58    m2.i64 = False
    59  assert.fails(set_bool_to_int, 'got bool, want int')