go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/bytes_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 # Repeated 'bytes' field is represented by a list of strings. 19 20 m = testprotos.SimpleFields() 21 22 # Default value. 23 assert.eq(len(m.bs_rep), 0) 24 25 # Setter and getter works. 26 m.bs_rep = ['', '\x01\x02\x03'] 27 assert.eq(list(m.bs_rep), ['', '\x01\x02\x03']) 28 assert.eq(proto.to_textpb(m), 'bs_rep: ""\nbs_rep: "\\x01\\x02\\x03"\n') 29 30 # Setting through constructor works. 31 m2 = testprotos.SimpleFields(bs_rep=['a', 'b']) 32 assert.eq(list(m2.bs_rep), ['a', 'b']) 33 34 # Clearing works. 35 m2.bs_rep = None 36 assert.eq(len(m2.bs_rep), 0) 37 38 # Setting wrong type fails. 39 def set_bad(): 40 m2.bs_rep = 1 41 assert.fails(set_bad, 'got int, want an iterable') 42 43 # Trying to put a wrong type into the list fails. 44 def set_bad_item1(): 45 m2.bs_rep = [1] 46 assert.fails(set_bad_item1, 'item #0: got int, want string') 47 48 49 # Trying to put a wrong type into the list fails. 50 def set_bad_item2(): 51 m2.bs_rep = [None] 52 assert.fails(set_bad_item2, 'item #0: got NoneType, want string')