David Symonds | c37ad66 | 2010-04-07 09:25:13 +1000 | [diff] [blame^] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| 3 | // Copyright 2010 Google Inc. All rights reserved. |
| 4 | // http://code.google.com/p/goprotobuf/ |
| 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 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | // Test that we can use protocol buffers that use extensions. |
| 33 | |
| 34 | package main |
| 35 | |
| 36 | import ( |
| 37 | "testing" |
| 38 | |
| 39 | "goprotobuf.googlecode.com/hg/proto" |
| 40 | base "extension_base.pb" |
| 41 | user "extension_user.pb" |
| 42 | ) |
| 43 | |
| 44 | func TestSingleFieldExtension(t *testing.T) { |
| 45 | bm := base.NewBaseMessage() |
| 46 | bm.Height = proto.Int32(178) |
| 47 | |
| 48 | // Use extension within scope of another type. |
| 49 | vol := proto.Uint32(11) |
| 50 | t.Logf("bm: %T, user.LoudMessage_Volume: %T", bm, user.LoudMessage_Volume) |
| 51 | err := proto.SetExtension(bm, user.LoudMessage_Volume, vol) |
| 52 | if err != nil { |
| 53 | t.Fatal("Failed setting extension:", err) |
| 54 | } |
| 55 | buf, err := proto.Marshal(bm) |
| 56 | if err != nil { |
| 57 | t.Fatal("Failed encoding message with extension:", err) |
| 58 | } |
| 59 | bm_new := base.NewBaseMessage() |
| 60 | if err := proto.Unmarshal(buf, bm_new); err != nil { |
| 61 | t.Fatal("Failed decoding message with extension:", err) |
| 62 | } |
| 63 | if !proto.HasExtension(bm_new, user.LoudMessage_Volume) { |
| 64 | t.Fatal("Decoded message didn't contain extension.") |
| 65 | } |
| 66 | vol_out, err := proto.GetExtension(bm_new, user.LoudMessage_Volume) |
| 67 | if err != nil { |
| 68 | t.Fatal("Failed getting extension:", err) |
| 69 | } |
| 70 | if v := vol_out.(*uint32); *v != *vol { |
| 71 | t.Errorf("vol_out = %v, expected %v", *v, *vol) |
| 72 | } |
| 73 | proto.ClearExtension(bm_new, user.LoudMessage_Volume) |
| 74 | if proto.HasExtension(bm_new, user.LoudMessage_Volume) { |
| 75 | t.Fatal("Failed clearing extension.") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestMessageExtension(t *testing.T) { |
| 80 | bm := base.NewBaseMessage() |
| 81 | bm.Height = proto.Int32(179) |
| 82 | |
| 83 | // Use extension that is itself a message. |
| 84 | um := &user.UserMessage{ |
| 85 | Name: proto.String("Dave"), |
| 86 | Rank: proto.String("Major"), |
| 87 | } |
| 88 | err := proto.SetExtension(bm, user.LoginMessage_UserMessage, um) |
| 89 | if err != nil { |
| 90 | t.Fatal("Failed setting extension:", err) |
| 91 | } |
| 92 | buf, err := proto.Marshal(bm) |
| 93 | if err != nil { |
| 94 | t.Fatal("Failed encoding message with extension:", err) |
| 95 | } |
| 96 | bm_new := base.NewBaseMessage() |
| 97 | if err := proto.Unmarshal(buf, bm_new); err != nil { |
| 98 | t.Fatal("Failed decoding message with extension:", err) |
| 99 | } |
| 100 | if !proto.HasExtension(bm_new, user.LoginMessage_UserMessage) { |
| 101 | t.Fatal("Decoded message didn't contain extension.") |
| 102 | } |
| 103 | um_out, err := proto.GetExtension(bm_new, user.LoginMessage_UserMessage) |
| 104 | if err != nil { |
| 105 | t.Fatal("Failed getting extension:", err) |
| 106 | } |
| 107 | if n := um_out.(*user.UserMessage).Name; *n != *um.Name { |
| 108 | t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name) |
| 109 | } |
| 110 | if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank { |
| 111 | t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank) |
| 112 | } |
| 113 | proto.ClearExtension(bm_new, user.LoginMessage_UserMessage) |
| 114 | if proto.HasExtension(bm_new, user.LoginMessage_UserMessage) { |
| 115 | t.Fatal("Failed clearing extension.") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestTopLevelExtension(t *testing.T) { |
| 120 | bm := base.NewBaseMessage() |
| 121 | bm.Height = proto.Int32(179) |
| 122 | |
| 123 | width := proto.Int32(17) |
| 124 | err := proto.SetExtension(bm, user.E_Width, width) |
| 125 | if err != nil { |
| 126 | t.Fatal("Failed setting extension:", err) |
| 127 | } |
| 128 | buf, err := proto.Marshal(bm) |
| 129 | if err != nil { |
| 130 | t.Fatal("Failed encoding message with extension:", err) |
| 131 | } |
| 132 | bm_new := base.NewBaseMessage() |
| 133 | if err := proto.Unmarshal(buf, bm_new); err != nil { |
| 134 | t.Fatal("Failed decoding message with extension:", err) |
| 135 | } |
| 136 | if !proto.HasExtension(bm_new, user.E_Width) { |
| 137 | t.Fatal("Decoded message didn't contain extension.") |
| 138 | } |
| 139 | width_out, err := proto.GetExtension(bm_new, user.E_Width) |
| 140 | if err != nil { |
| 141 | t.Fatal("Failed getting extension:", err) |
| 142 | } |
| 143 | if w := width_out.(*int32); *w != *width { |
| 144 | t.Errorf("width_out = %v, expected %v", *w, *width) |
| 145 | } |
| 146 | proto.ClearExtension(bm_new, user.E_Width) |
| 147 | if proto.HasExtension(bm_new, user.E_Width) { |
| 148 | t.Fatal("Failed clearing extension.") |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func main() { |
| 153 | // simpler than rigging up gotest |
| 154 | testing.Main([]testing.Test{ |
| 155 | testing.Test{"TestSingleFieldExtension", TestSingleFieldExtension}, |
| 156 | testing.Test{"TestMessageExtension", TestMessageExtension}, |
| 157 | testing.Test{"TestTopLevelExtension", TestTopLevelExtension}, |
| 158 | }) |
| 159 | } |