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