go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/textpb/textpb_test.go (about) 1 // Copyright 2021 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 package textpb 16 17 import ( 18 "testing" 19 20 "go.chromium.org/luci/common/proto/textpb/internal" 21 22 . "github.com/smartystreets/goconvey/convey" 23 . "go.chromium.org/luci/common/testing/assertions" 24 ) 25 26 func TestFormat(t *testing.T) { 27 Convey("Works", t, func() { 28 leaf := &internal.Leaf{ 29 Str: "{\n\"not a json really\":\n\"zzz\"\n}", 30 Json: "{\n\"this is json 1\":\n\"zzz\"\n}", 31 JsonRep: []string{ 32 "{\n\"this is json 2\":\n\"zzz\"\n}", 33 "{\n\"this is json 3\":\n\"zzz\"\n}", 34 }, 35 } 36 37 msg := internal.Container{ 38 Leaf: leaf, 39 LeafRep: []*internal.Leaf{leaf, leaf}, 40 Mapping: map[string]*internal.Leaf{"key": leaf}, 41 } 42 43 out, err := Marshal(&msg) 44 So(err, ShouldBeNil) 45 So(string(out), ShouldEqual, `leaf { 46 str: "{\n\"not a json really\":\n\"zzz\"\n}" 47 json: 48 '{' 49 ' "this is json 1": "zzz"' 50 '}' 51 json_rep: 52 '{' 53 ' "this is json 2": "zzz"' 54 '}' 55 json_rep: 56 '{' 57 ' "this is json 3": "zzz"' 58 '}' 59 } 60 leaf_rep { 61 str: "{\n\"not a json really\":\n\"zzz\"\n}" 62 json: 63 '{' 64 ' "this is json 1": "zzz"' 65 '}' 66 json_rep: 67 '{' 68 ' "this is json 2": "zzz"' 69 '}' 70 json_rep: 71 '{' 72 ' "this is json 3": "zzz"' 73 '}' 74 } 75 leaf_rep { 76 str: "{\n\"not a json really\":\n\"zzz\"\n}" 77 json: 78 '{' 79 ' "this is json 1": "zzz"' 80 '}' 81 json_rep: 82 '{' 83 ' "this is json 2": "zzz"' 84 '}' 85 json_rep: 86 '{' 87 ' "this is json 3": "zzz"' 88 '}' 89 } 90 mapping { 91 key: "key" 92 value { 93 str: "{\n\"not a json really\":\n\"zzz\"\n}" 94 json: 95 '{' 96 ' "this is json 1": "zzz"' 97 '}' 98 json_rep: 99 '{' 100 ' "this is json 2": "zzz"' 101 '}' 102 json_rep: 103 '{' 104 ' "this is json 3": "zzz"' 105 '}' 106 } 107 } 108 `) 109 }) 110 111 Convey("Malformed JSON", t, func() { 112 _, err := Format([]byte(` 113 str: "nah don't care about that one" 114 json: "not a json, boom" 115 `), (&internal.Leaf{}).ProtoReflect().Descriptor()) 116 So(err, ShouldErrLike, "value for 'json' must be valid JSON, got value 'not a json, boom'") 117 }) 118 }