| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [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 | package proto_test |
| 33 | |
| 34 | import ( |
| 35 | "bytes" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 36 | "testing" |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 37 | |
| 38 | "goprotobuf.googlecode.com/hg/proto" |
| 39 | |
| 40 | pb "./testdata/_obj/test_proto" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 41 | ) |
| 42 | |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 43 | func newTestMessage() *pb.MyMessage { |
| 44 | msg := &pb.MyMessage{ |
| 45 | Count: proto.Int32(42), |
| 46 | Name: proto.String("Dave"), |
| 47 | Quote: proto.String(`"I didn't want to go."`), |
| Rob Pike | 9caa5b9 | 2010-05-11 16:04:57 -0700 | [diff] [blame] | 48 | Pet: []string{"bunny", "kitty", "horsey"}, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 49 | Inner: &pb.InnerMessage{ |
| 50 | Host: proto.String("footrest.syd"), |
| 51 | Port: proto.Int32(7001), |
| 52 | Connected: proto.Bool(true), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 53 | }, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 54 | Others: []*pb.OtherMessage{ |
| 55 | &pb.OtherMessage{ |
| 56 | Key: proto.Int64(0xdeadbeef), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 57 | Value: []byte{1, 65, 7, 12}, |
| 58 | }, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 59 | &pb.OtherMessage{ |
| 60 | Weight: proto.Float32(6.022), |
| 61 | Inner: &pb.InnerMessage{ |
| 62 | Host: proto.String("lesha.mtv"), |
| 63 | Port: proto.Int32(8002), |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 64 | }, |
| 65 | }, |
| 66 | }, |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 67 | Bikeshed: pb.NewMyMessage_Color(pb.MyMessage_BLUE), |
| 68 | Somegroup: &pb.MyMessage_SomeGroup{ |
| 69 | GroupField: proto.Int32(8), |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 70 | }, |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 71 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 72 | ext := &pb.Ext{ |
| 73 | Data: proto.String("Big gobs for big rats"), |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 74 | } |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 75 | if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 76 | panic(err) |
| 77 | } |
| 78 | return msg |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | const text = `count: 42 |
| 82 | name: "Dave" |
| 83 | quote: "\"I didn't want to go.\"" |
| 84 | pet: "bunny" |
| 85 | pet: "kitty" |
| 86 | pet: "horsey" |
| 87 | inner: < |
| 88 | host: "footrest.syd" |
| 89 | port: 7001 |
| 90 | connected: true |
| 91 | > |
| 92 | others: < |
| 93 | key: 3735928559 |
| 94 | value: "\x01A\a\f" |
| 95 | > |
| 96 | others: < |
| 97 | weight: 6.022 |
| 98 | inner: < |
| 99 | host: "lesha.mtv" |
| 100 | port: 8002 |
| 101 | > |
| 102 | > |
| 103 | bikeshed: BLUE |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 104 | SomeGroup { |
| 105 | group_field: 8 |
| 106 | } |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 107 | [test_proto.more]: < |
| 108 | data: "Big gobs for big rats" |
| 109 | > |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 110 | ` |
| 111 | |
| 112 | func TestMarshalTextFull(t *testing.T) { |
| 113 | buf := new(bytes.Buffer) |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 114 | proto.MarshalText(buf, newTestMessage()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 115 | s := buf.String() |
| 116 | if s != text { |
| 117 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func compact(src string) string { |
| David Symonds | e37856c | 2011-06-22 12:52:53 +1000 | [diff] [blame] | 122 | // s/[ \n]+/ /g; s/ $//; |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 123 | dst := make([]byte, len(src)) |
| 124 | space := false |
| 125 | j := 0 |
| 126 | for i := 0; i < len(src); i++ { |
| 127 | c := src[i] |
| 128 | if c == ' ' || c == '\n' { |
| 129 | space = true |
| 130 | continue |
| 131 | } |
| David Symonds | 9f40281 | 2011-04-28 18:08:44 +1000 | [diff] [blame] | 132 | if j > 0 && (dst[j-1] == ':' || dst[j-1] == '<' || dst[j-1] == '{') { |
| 133 | space = false |
| 134 | } |
| 135 | if c == '{' { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 136 | space = false |
| 137 | } |
| 138 | if space { |
| 139 | dst[j] = ' ' |
| 140 | j++ |
| 141 | space = false |
| 142 | } |
| 143 | dst[j] = c |
| 144 | j++ |
| 145 | } |
| 146 | if space { |
| 147 | dst[j] = ' ' |
| 148 | j++ |
| 149 | } |
| 150 | return string(dst[0:j]) |
| 151 | } |
| 152 | |
| 153 | var compactText = compact(text) |
| 154 | |
| 155 | func TestCompactText(t *testing.T) { |
| David Symonds | aa922ff | 2011-07-19 14:58:06 +1000 | [diff] [blame^] | 156 | s := proto.CompactTextString(newTestMessage()) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 157 | if s != compactText { |
| 158 | t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, compactText) |
| 159 | } |
| 160 | } |