go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/struct_to_pbtext.star (about)

     1  # Copyright 2019 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  
    16  # Empty struct.
    17  assert.eq(proto.struct_to_textpb(struct()), '')
    18  
    19  # All the supported field types.
    20  assert.eq(
    21    proto.struct_to_textpb(struct(
    22      int=1, float=3.141, str="hello", bool=True, intlist=[1, 2, 3])),
    23    '''bool: true
    24  float: 3.141
    25  int: 1
    26  intlist: 1
    27  intlist: 2
    28  intlist: 3
    29  str: "hello"
    30  ''')
    31  
    32  # Nested structs.
    33  assert.eq(
    34    proto.struct_to_textpb(struct(
    35      x=struct(), y=[struct(a=1), struct(b=2, c=struct(p=1, q=2))])),
    36    '''x: <
    37  >
    38  y: <
    39    a: 1
    40  >
    41  y: <
    42    b: 2
    43    c: <
    44      p: 1
    45      q: 2
    46    >
    47  >
    48  ''')
    49  
    50  # Dict fields not supported
    51  def struct_to_textpb_dict_field():
    52    proto.struct_to_textpb(struct(d={1: 'one'}))
    53  assert.fails(struct_to_textpb_dict_field, 'struct_to_textpb: cannot convert dict to proto')
    54  
    55  # struct_to_textpb expects an argument.
    56  assert.fails(proto.struct_to_textpb, 'missing argument for struct')
    57  
    58  # Too many arguments.
    59  def struct_to_textpb_too_many_args():
    60    proto.struct_to_textpb(struct(), struct())
    61  assert.fails(struct_to_textpb_too_many_args, 'struct_to_textpb: got 2 arguments, want at most 1')
    62  
    63  # None argument.
    64  def struct_to_textpb_with_none():
    65    proto.struct_to_textpb(None)
    66  assert.fails(struct_to_textpb_with_none, 'struct_to_textpb: got NoneType, expecting a struct')
    67  
    68  # Wrongly typed argument.
    69  def struct_to_textpb_with_int():
    70    proto.struct_to_textpb(1)
    71  assert.fails(struct_to_textpb_with_int, 'struct_to_textpb: got int, expecting a struct')