extension_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // Test that we can use protocol buffers that use extensions.
  32. package main
  33. import (
  34. "testing"
  35. "goprotobuf.googlecode.com/hg/proto"
  36. base "extension_base.pb"
  37. user "extension_user.pb"
  38. )
  39. func TestSingleFieldExtension(t *testing.T) {
  40. bm := &base.BaseMessage{}
  41. bm.Height = proto.Int32(178)
  42. // Use extension within scope of another type.
  43. vol := proto.Uint32(11)
  44. err := proto.SetExtension(bm, user.E_LoudMessage_Volume, vol)
  45. if err != nil {
  46. t.Fatal("Failed setting extension:", err)
  47. }
  48. buf, err := proto.Marshal(bm)
  49. if err != nil {
  50. t.Fatal("Failed encoding message with extension:", err)
  51. }
  52. bm_new := &base.BaseMessage{}
  53. if err := proto.Unmarshal(buf, bm_new); err != nil {
  54. t.Fatal("Failed decoding message with extension:", err)
  55. }
  56. if !proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
  57. t.Fatal("Decoded message didn't contain extension.")
  58. }
  59. vol_out, err := proto.GetExtension(bm_new, user.E_LoudMessage_Volume)
  60. if err != nil {
  61. t.Fatal("Failed getting extension:", err)
  62. }
  63. if v := vol_out.(*uint32); *v != *vol {
  64. t.Errorf("vol_out = %v, expected %v", *v, *vol)
  65. }
  66. proto.ClearExtension(bm_new, user.E_LoudMessage_Volume)
  67. if proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
  68. t.Fatal("Failed clearing extension.")
  69. }
  70. }
  71. func TestMessageExtension(t *testing.T) {
  72. bm := &base.BaseMessage{}
  73. bm.Height = proto.Int32(179)
  74. // Use extension that is itself a message.
  75. um := &user.UserMessage{
  76. Name: proto.String("Dave"),
  77. Rank: proto.String("Major"),
  78. }
  79. err := proto.SetExtension(bm, user.E_LoginMessage_UserMessage, um)
  80. if err != nil {
  81. t.Fatal("Failed setting extension:", err)
  82. }
  83. buf, err := proto.Marshal(bm)
  84. if err != nil {
  85. t.Fatal("Failed encoding message with extension:", err)
  86. }
  87. bm_new := &base.BaseMessage{}
  88. if err := proto.Unmarshal(buf, bm_new); err != nil {
  89. t.Fatal("Failed decoding message with extension:", err)
  90. }
  91. if !proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
  92. t.Fatal("Decoded message didn't contain extension.")
  93. }
  94. um_out, err := proto.GetExtension(bm_new, user.E_LoginMessage_UserMessage)
  95. if err != nil {
  96. t.Fatal("Failed getting extension:", err)
  97. }
  98. if n := um_out.(*user.UserMessage).Name; *n != *um.Name {
  99. t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name)
  100. }
  101. if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank {
  102. t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank)
  103. }
  104. proto.ClearExtension(bm_new, user.E_LoginMessage_UserMessage)
  105. if proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
  106. t.Fatal("Failed clearing extension.")
  107. }
  108. }
  109. func TestTopLevelExtension(t *testing.T) {
  110. bm := &base.BaseMessage{}
  111. bm.Height = proto.Int32(179)
  112. width := proto.Int32(17)
  113. err := proto.SetExtension(bm, user.E_Width, width)
  114. if err != nil {
  115. t.Fatal("Failed setting extension:", err)
  116. }
  117. buf, err := proto.Marshal(bm)
  118. if err != nil {
  119. t.Fatal("Failed encoding message with extension:", err)
  120. }
  121. bm_new := &base.BaseMessage{}
  122. if err := proto.Unmarshal(buf, bm_new); err != nil {
  123. t.Fatal("Failed decoding message with extension:", err)
  124. }
  125. if !proto.HasExtension(bm_new, user.E_Width) {
  126. t.Fatal("Decoded message didn't contain extension.")
  127. }
  128. width_out, err := proto.GetExtension(bm_new, user.E_Width)
  129. if err != nil {
  130. t.Fatal("Failed getting extension:", err)
  131. }
  132. if w := width_out.(*int32); *w != *width {
  133. t.Errorf("width_out = %v, expected %v", *w, *width)
  134. }
  135. proto.ClearExtension(bm_new, user.E_Width)
  136. if proto.HasExtension(bm_new, user.E_Width) {
  137. t.Fatal("Failed clearing extension.")
  138. }
  139. }
  140. func main() {
  141. // simpler than rigging up gotest
  142. testing.Main([]testing.Test{
  143. testing.Test{"TestSingleFieldExtension", TestSingleFieldExtension},
  144. testing.Test{"TestMessageExtension", TestMessageExtension},
  145. testing.Test{"TestTopLevelExtension", TestTopLevelExtension},
  146. })
  147. }