go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/freezing.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 # Works in general. 19 m1 = testprotos.Simple(i=456, many_i=[1, 2, 3]) 20 freeze(m1) 21 assert.eq(m1.i, 456) 22 assert.eq(list(m1.many_i), [1, 2, 3]) 23 def change_m1_i_1(): 24 m1.i = 456 25 assert.fails(change_m1_i_1, 'cannot modify frozen proto.Message<testprotos.Simple>') 26 def change_m1_i_2(): 27 m1.i = None 28 assert.fails(change_m1_i_2, 'cannot modify frozen proto.Message<testprotos.Simple>') 29 30 # Freezes recursively. 31 def change_m1_many_i(): 32 m1.many_i.append(4) 33 assert.fails(change_m1_many_i, 'cannot append to frozen list') 34 35 # Auto-initialization of fields still works, but default values are frozen. 36 m2 = testprotos.Simple() 37 freeze(m2) 38 assert.eq(m2.i, 0) 39 assert.eq(len(m2.many_i), 0) 40 def change_m2_many_i(): 41 m2.many_i.append(4) 42 assert.fails(change_m2_many_i, 'cannot append to frozen list')