github.com/gogo/protobuf@v1.3.2/test/mapdefaults/combos/both/map_test.go (about) 1 // Protocol Buffers for Go with Gadgets 2 // 3 // Copyright (c) 2017, 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 mapdefaults 30 31 import ( 32 "testing" 33 34 "github.com/gogo/protobuf/proto" 35 ) 36 37 func TestUnmarshalImplicitDefaultKeyValue1(t *testing.T) { 38 fm := &FakeMap{ 39 Entries: []*FakeMapEntry{ 40 { 41 Key: "foo", 42 Value: "", 43 }, 44 { 45 Key: "", 46 Value: "bar", 47 }, 48 { 49 Key: "as", 50 Value: "df", 51 }, 52 }, 53 } 54 55 serializedMsg, err := proto.Marshal(fm) 56 if err != nil { 57 t.Fatalf("Failed to serialize msg: %s", err) 58 } 59 60 msg := MapTest{} 61 err = proto.Unmarshal(serializedMsg, &msg) 62 63 if err != nil { 64 t.Fatalf("Unexpected error: %s", err) 65 } 66 67 strStr := msg.StrStr 68 if len(strStr) != 3 { 69 t.Fatal("StrStr map should have 3 key/value pairs") 70 } 71 72 val, ok := strStr["foo"] 73 if !ok { 74 t.Fatal("\"foo\" not found in StrStr map.") 75 } 76 if val != "" { 77 t.Fatalf("Unexpected value for \"foo\": %s", val) 78 } 79 80 val, ok = strStr[""] 81 if !ok { 82 t.Fatal("\"\" not found in StrStr map.") 83 } 84 if val != "bar" { 85 t.Fatalf("Unexpected value for \"\": %s", val) 86 } 87 88 val, ok = strStr["as"] 89 if !ok { 90 t.Fatal("\"as\" not found in StrStr map.") 91 } 92 if val != "df" { 93 t.Fatalf("Unexpected value for \"as\": %s", val) 94 } 95 } 96 97 func TestUnmarshalImplicitDefaultKeyValue2(t *testing.T) { 98 fm := &FakeMap{ 99 Entries: []*FakeMapEntry{ 100 { 101 Key: "", 102 Value: "", 103 }, 104 }, 105 } 106 107 serializedMsg, err := proto.Marshal(fm) 108 if err != nil { 109 t.Fatalf("Failed to serialize msg: %s", err) 110 } 111 112 // Sanity check 113 if string(serializedMsg) != "\n\x00" { 114 t.Fatal("Serialized bytes mismatched") 115 } 116 117 msg := MapTest{} 118 err = proto.Unmarshal(serializedMsg, &msg) 119 120 if err != nil { 121 t.Fatalf("Unexpected error: %s", err) 122 } 123 124 strStr := msg.StrStr 125 if len(strStr) != 1 { 126 t.Fatal("StrStr map should have 1 key/value pairs") 127 } 128 129 val, ok := strStr[""] 130 if !ok { 131 t.Fatal("\"\" not found in StrStr map.") 132 } 133 if val != "" { 134 t.Fatalf("Unexpected value for \"\": %s", val) 135 } 136 }