github.com/gogo/protobuf@v1.3.2/test/issue530/issue530_test.go (about) 1 // Protocol Buffers for Go with Gadgets 2 // 3 // Copyright (c) 2019, The GoGo Authors. All rights reserved. 4 // http://github.com/gogo/protobuf 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 package issue530 30 31 import ( 32 "strings" 33 "testing" 34 ) 35 36 // NN : NonNullable 37 // N : Nullable 38 // R : Repeated 39 func TestStringNNMessageNNString(t *testing.T) { 40 exp := "MessageWith&Ampersand" 41 m := &Foo5{ 42 Bar1: Bar1{Str: exp}, 43 } 44 check(t, "Bar1", m.String(), exp) 45 } 46 47 func TestStringNMessageNNString(t *testing.T) { 48 exp := "MessageWith&Ampersand" 49 m := &Foo5{ 50 Bar2: &Bar1{Str: exp}, 51 } 52 check(t, "Bar2", m.String(), exp) 53 } 54 55 func TestStringNNMessageNString(t *testing.T) { 56 exp := "MessageWith&Ampersand" 57 m := &Foo5{ 58 Bar3: Bar2{Str: &exp}, 59 } 60 check(t, "Bar3", m.String(), exp) 61 } 62 63 func TestStringNMessageNString(t *testing.T) { 64 exp := "MessageWith&Ampersand" 65 m := &Foo5{ 66 Bar4: &Bar2{Str: &exp}, 67 } 68 check(t, "Bar4", m.String(), exp) 69 } 70 71 func TestStringRNNMessageNNString(t *testing.T) { 72 exp1 := "MessageWith&Ampersand1" 73 exp2 := "MessageWith&Ampersand2" 74 m := &Foo5{ 75 Bars1: []Bar1{{Str: exp1}, {Str: exp2}}, 76 } 77 check(t, "Bars1", m.String(), exp1, exp2) 78 } 79 80 func TestStringRNMessageNNString(t *testing.T) { 81 exp1 := "MessageWith&Ampersand1" 82 exp2 := "MessageWith&Ampersand2" 83 m := &Foo5{ 84 Bars2: []*Bar1{{Str: exp1}, nil, {Str: exp2}}, 85 } 86 check(t, "Bars2", m.String(), exp1, exp2) 87 } 88 89 func TestStringRNNMessageNString(t *testing.T) { 90 exp1 := "MessageWith&Ampersand1" 91 exp2 := "MessageWith&Ampersand2" 92 m := &Foo5{ 93 Bars3: []Bar2{{Str: &exp1}, {Str: &exp2}}, 94 } 95 check(t, "Bars3", m.String(), exp1, exp2) 96 } 97 98 func TestStringRNMessageNString(t *testing.T) { 99 exp1 := "MessageWith&Ampersand1" 100 exp2 := "MessageWith&Ampersand2" 101 m := &Foo5{ 102 Bars4: []*Bar2{{Str: &exp1}, {Str: &exp2}}, 103 } 104 check(t, "Bars4", m.String(), exp1, exp2) 105 } 106 107 func TestStringDeepRNNMessageRNNMessageNNStringAndNString(t *testing.T) { 108 exp1 := "MessageWith&Ampersand1" 109 exp2 := "MessageWith&Ampersand2" 110 m := &Foo5{ 111 Barrs1: []Bar3{ 112 { 113 Bars4: []Bar4{ 114 { 115 Str: exp1, 116 }, 117 }, 118 Bars2: []Bar2{ 119 { 120 Str: &exp2, 121 }, 122 }, 123 }, 124 }, 125 } 126 check(t, "Barrs1", m.String(), exp1, exp2) 127 } 128 129 func TestStringDeepRNNMessageRNMessageNNStringAndNString(t *testing.T) { 130 exp1 := "MessageWith&Ampersand1" 131 exp2 := "MessageWith&Ampersand2" 132 m := &Foo5{ 133 Barrs2: []Bar5{ 134 { 135 Bars2: []*Bar2{ 136 { 137 Str: &exp1, 138 }, 139 }, 140 Bars1: []*Bar1{ 141 { 142 Str: exp2, 143 }, 144 }, 145 }, 146 }, 147 } 148 check(t, "Barrs2", m.String(), exp1, exp2) 149 } 150 151 func TestStringMapNMessageRNNMessageNNStringAndNString(t *testing.T) { 152 exp1 := "MessageWith&Ampersand1" 153 exp2 := "MessageWith&Ampersand2" 154 m := &Foo5{ 155 Barmap1: map[string]*Bar3{ 156 "one": { 157 Bars4: []Bar4{ 158 { 159 Str: exp1, 160 }, 161 }, 162 Bars2: []Bar2{ 163 { 164 Str: &exp2, 165 }, 166 }, 167 }, 168 }, 169 } 170 check(t, "Barmap1", m.String(), exp1, exp2) 171 } 172 173 func TestStringMapNMessageRNMessageNNStringAndNString(t *testing.T) { 174 exp1 := "MessageWith&Ampersand1" 175 exp2 := "MessageWith&Ampersand2" 176 m := &Foo5{ 177 Barmap2: map[string]*Bar5{ 178 "one": { 179 Bars2: []*Bar2{ 180 { 181 Str: &exp1, 182 }, 183 }, 184 Bars1: []*Bar1{ 185 { 186 Str: exp2, 187 }, 188 }, 189 }, 190 }, 191 } 192 check(t, "Barmap2", m.String(), exp1, exp2) 193 } 194 195 func TestStringRNNMessageNNStringRNMessageNStringNString(t *testing.T) { 196 exp1 := "MessageWith&Ampersand1" 197 exp2 := "MessageWith&Ampersand2" 198 exp3 := "MessageWith&Ampersand3" 199 m := &Bar7{ 200 Bars71: []Bar7{ 201 { 202 Bars72: []*Bar7{ 203 { 204 Str2: &exp3, 205 }, 206 }, 207 Str2: &exp2, 208 }, 209 }, 210 Str1: exp1, 211 } 212 check(t, "Bar7", m.String(), exp1, exp2, exp3) 213 } 214 215 func TestStringRNNMessageWithNoStringerNNString(t *testing.T) { 216 exp1 := "MessageWith&Ampersand1" 217 exp2 := "MessageWith&Ampersand2" 218 m := &Bar8{ 219 Bars1: []Bar9{{Str: exp1}, {Str: exp2}}, 220 } 221 check(t, "Bars1", m.String(), exp1, exp2) 222 } 223 224 func check(t *testing.T, field, result string, expects ...string) { 225 // t.Logf("result: %s \n", result) 226 for _, expect := range expects { 227 if !strings.Contains(result, expect) { 228 t.Fatalf("Expected %s to contain: %s, but got: %s\n", field, expect, result) 229 } 230 } 231 }