go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/to_pbtext.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 m = testprotos.Simple() 19 20 # Works in general. 21 m.i = 123 22 assert.eq(proto.to_textpb(m), 'i: 123\n') 23 24 # Omits defaults. 25 m.i = 0 26 assert.eq(proto.to_textpb(m), '') 27 28 # to_textpb expects an argument. 29 def to_textpb_no_args(): 30 proto.to_textpb() 31 assert.fails(to_textpb_no_args, 'missing argument for msg') 32 33 # Too many arguments. 34 def to_textpb_too_many_args(): 35 proto.to_textpb(m, m) 36 assert.fails(to_textpb_too_many_args, 'to_textpb: got 2 arguments, want at most 1') 37 38 # None argument. 39 def to_textpb_with_none(): 40 proto.to_textpb(None) 41 assert.fails(to_textpb_with_none, 'to_textpb: for parameter msg: got NoneType, want proto.Message') 42 43 # Wrongly typed argument. 44 def to_textpb_with_int(): 45 proto.to_textpb(1) 46 assert.fails(to_textpb_with_int, 'to_textpb: for parameter msg: got int, want proto.Message')