go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/simple_repeated.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 scalar types, since the implementation 19 # of repeated fields is identical for all of them. 20 21 m = testprotos.SimpleFields() 22 23 # Default value. 24 assert.eq(type(m.i64_rep), 'list<int64>') 25 assert.eq(len(m.i64_rep), 0) 26 27 # Can append to it, it is just like a list. 28 m.i64_rep.append(1) 29 assert.eq(list(m.i64_rep), [1]) 30 31 # Can completely recreated the field by replacing with default. 32 m.i64_rep = None 33 assert.eq(len(m.i64_rep), 0) 34 35 # Fields of the exact same type can point to a single object. 36 m2 = testprotos.SimpleFields() 37 m2.i64_rep = m.i64_rep 38 assert.true(m2.i64_rep == m.i64_rep) 39 m.i64_rep.append(4444) 40 assert.eq(m2.i64_rep[-1], 4444) 41 42 # Assigning a regular list makes a copy. 43 lst = [1, 2, 3] 44 m.i64_rep = lst 45 assert.true(m.i64_rep != lst) 46 lst.append(4) 47 assert.eq(m.i64_rep[-1], 3) # old one 48 49 # Rejects list<T> with different T. 50 def set_wrong_list(): 51 m.i64_rep = m.bs_rep 52 assert.fails(set_wrong_list, 'want list<int64> or just list') 53 54 # int64 and sint64 (etc) are actually same type on Starlark side and thus can 55 # be assigned to one another. 56 m.si64_rep = [1, 2, 3] 57 m.i64_rep = m.si64_rep 58 assert.true(m.i64_rep == m.si64_rep) 59 60 # Same with bytes and string. 61 m.bs_rep = ['a', 'b', 'c'] 62 m.str_rep = m.bs_rep 63 assert.true(m.str_rep == m.bs_rep) 64 65 # Trying to replace with a completely wrong type is an error. 66 def set_int(): 67 m.i64_rep = 123 68 assert.fails(set_int, 'got int, want an iterable') 69 70 # Does type checks when updating the list. 71 def append_bad_value(): 72 m.i64_rep.append('zzz') 73 assert.fails(append_bad_value, 'append: got string, want int') 74 m.i64_rep = [0] 75 def set_bad_value(): 76 m.i64_rep[0] = None 77 assert.fails(set_bad_value, 'item #0: got NoneType, want int') 78 79 # Checks types of elements when constructing from a list. 80 def copy_bad_list(): 81 m.i64_rep = [1, 2, None] 82 assert.fails(copy_bad_list, 'when constructing list<int64>: item #2: got NoneType, want int') 83 84 # Serialization to text proto works. 85 text = proto.to_textpb(testprotos.SimpleFields(i64_rep=[1, 2, 3])) 86 assert.eq(text, """i64_rep: 1 87 i64_rep: 2 88 i64_rep: 3 89 """)