go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/float64.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.f64, 0.0)
    22  
    23  # Setter and getter works.
    24  m.f64 = 1.0
    25  assert.eq(m.f64, 1.0)
    26  assert.eq(proto.to_textpb(m), 'f64: 1\n')
    27  
    28  # Setting through constructor works.
    29  m2 = testprotos.SimpleFields(f64=1.0)
    30  assert.eq(m2.f64, 1.0)
    31  
    32  # Clearing works.
    33  m2.f64 = None
    34  assert.eq(m2.f64, 0.0)
    35  
    36  # Implicit conversion from int to float is supported.
    37  m2.f64 = 123
    38  assert.eq(m2.f64, 123.0)
    39  assert.eq(proto.to_textpb(m2), 'f64: 123\n')
    40  
    41  # Setting wrong type fails.
    42  def set_bad():
    43    m2.f64 = [1, 2, 3]
    44  assert.fails(set_bad, 'got list, want float')