go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/ints.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  # Note: this test also covers all other integer types, since their
    19  # implementation is almost identical. The differences are tested in
    20  # int_ranges.star.
    21  
    22  m = testprotos.SimpleFields()
    23  
    24  # Default value.
    25  assert.eq(m.i64, 0)
    26  
    27  # Setter and getter works.
    28  m.i64 = 123
    29  assert.eq(m.i64, 123)
    30  
    31  # Setting through constructor works.
    32  m2 = testprotos.SimpleFields(i64=456)
    33  assert.eq(m2.i64, 456)
    34  
    35  # Clearing works.
    36  m2.i64 = None
    37  assert.eq(m2.i64, 0)
    38  
    39  # Setting wrong type fails.
    40  def set_bad():
    41    m2.i64 = [1, 2, 3]
    42  assert.fails(set_bad, 'got list, want int')
    43  
    44  # Setting to a message fails.
    45  def set_msg():
    46    m2.i64 = testprotos.SimpleFields()
    47  assert.fails(set_msg, 'got proto.Message<testprotos.SimpleFields>, want int')
    48  
    49  # We don't support implicit conversions from float to int. Callers should use
    50  # int(...) cast explicitly.
    51  def set_float():
    52    m2.i64 = 123.4
    53  assert.fails(set_float, 'got float, want int')
    54  
    55  # Serialization to text proto works.
    56  text = proto.to_textpb(testprotos.SimpleFields(i64=987))
    57  assert.eq(text, "i64: 987\n")