go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/cmp.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  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  m1 = testprotos.Complex()
    19  
    20  # Trivial cases.
    21  assert.true(m1 == m1)
    22  assert.true(m1 == testprotos.Complex())
    23  assert.true(m1 != testprotos.Simple())
    24  assert.true(m1 != testprotos.Complex(i64=123))
    25  assert.true(testprotos.Complex(enum_val=1) != testprotos.Complex(i64=123))
    26  
    27  # Comparison operation has no observable side effects (like appearance of
    28  # optional message-valued fields).
    29  assert.eq(str(m1), '')
    30  
    31  # Empty submessage and unset submessage are NOT the same thing.
    32  assert.true(testprotos.Complex(msg_val={}) != testprotos.Complex())
    33  
    34  # Singular fields are checked.
    35  assert.true(testprotos.Complex(i64=123) == testprotos.Complex(i64=123))
    36  assert.true(testprotos.Complex(i64=123) != testprotos.Complex(i64=456))
    37  
    38  # Assigning a field to its default value doesn't influence the comparison.
    39  m1.i64 = 0
    40  assert.true(m1 == testprotos.Complex())
    41  m1.i64_rep = []
    42  assert.true(m1 == testprotos.Complex())
    43  m1.msg_val_rep = []
    44  assert.true(m1 == testprotos.Complex())
    45  m1.mp = {}
    46  assert.true(m1 == testprotos.Complex())
    47  
    48  # Singular message fields are checked recursively.
    49  assert.true(testprotos.Complex(msg_val={'i':1}) == testprotos.Complex(msg_val={'i':1}))
    50  assert.true(testprotos.Complex(msg_val={'i':1}) != testprotos.Complex(msg_val={'i':2}))
    51  
    52  # Repeated fields are checked recursively.
    53  assert.true(testprotos.Complex(i64_rep=[1]) == testprotos.Complex(i64_rep=[1]))
    54  assert.true(testprotos.Complex(i64_rep=[1]) != testprotos.Complex(i64_rep=[2]))
    55  
    56  # Repeated message fields are checked recursively.
    57  assert.true(testprotos.Complex(msg_val_rep=[{'i':1}]) == testprotos.Complex(msg_val_rep=[{'i':1}]))
    58  assert.true(testprotos.Complex(msg_val_rep=[{'i':1}]) != testprotos.Complex(msg_val_rep=[{'i':2}]))
    59  
    60  # Map fields are checked recursively.
    61  assert.true(testprotos.Complex(mp={'1':{'i':1}}) == testprotos.Complex(mp={'1':{'i':1}}))
    62  assert.true(testprotos.Complex(mp={'1':{'i':1}}) != testprotos.Complex(mp={'1':{'i':2}}))
    63  
    64  # Oneof fields are tested correctly.
    65  assert.true(testprotos.Complex(simple={}) == testprotos.Complex(simple={}))
    66  assert.true(testprotos.Complex(simple={}) != testprotos.Complex(another_simple={}))
    67  
    68  # Even when doing the "force" switch of the oneof or when resetting it.
    69  m2 = testprotos.Complex(simple={})
    70  m2.another_simple = {}
    71  assert.true(m2 == testprotos.Complex(another_simple={}))
    72  m2.another_simple = None
    73  assert.true(m2 == testprotos.Complex())
    74  
    75  # Visits all fields.
    76  def msg(x):
    77    return testprotos.Complex(
    78        enum_val = 1,
    79        i64 = 123,
    80        i64_rep = [1, 2, 3],
    81        mp = {'1': {}, '2': {}},
    82        msg_val = {'i': 123},
    83        msg_val_rep = [{}, {}, {'i': x}, {}],
    84    )
    85  assert.true(msg(1) == msg(1))
    86  assert.true(msg(1) != msg(2))
    87  
    88  
    89  # Floats are weird: https://play.golang.org/p/UeQgkdxnnyv. At least this
    90  # peculiarity is documented here now.
    91  assert.true(testprotos.SimpleFields(f32=1.7) == testprotos.SimpleFields(f32=1.7))
    92  assert.true(testprotos.SimpleFields(f32=1.7) != proto.from_textpb(testprotos.SimpleFields, 'f32: 1.7'))
    93  
    94  
    95  # Other comparison operators are not supported.
    96  assert.fails(lambda: msg(0) < msg(0), '"<" is not implemented for proto.Message<testprotos.Complex>')