equaltest.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Protocol Buffers for Go with Gadgets
  2. //
  3. // Copyright (c) 2013, 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. package equal
  29. import (
  30. "github.com/gogo/protobuf/gogoproto"
  31. "github.com/gogo/protobuf/plugin/testgen"
  32. "github.com/gogo/protobuf/protoc-gen-gogo/generator"
  33. )
  34. type test struct {
  35. *generator.Generator
  36. }
  37. func NewTest(g *generator.Generator) testgen.TestPlugin {
  38. return &test{g}
  39. }
  40. func (p *test) Generate(imports generator.PluginImports, file *generator.FileDescriptor) bool {
  41. used := false
  42. randPkg := imports.NewImport("math/rand")
  43. timePkg := imports.NewImport("time")
  44. testingPkg := imports.NewImport("testing")
  45. protoPkg := imports.NewImport("github.com/gogo/protobuf/proto")
  46. unsafePkg := imports.NewImport("unsafe")
  47. if !gogoproto.ImportsGoGoProto(file.FileDescriptorProto) {
  48. protoPkg = imports.NewImport("github.com/golang/protobuf/proto")
  49. }
  50. for _, message := range file.Messages() {
  51. ccTypeName := generator.CamelCaseSlice(message.TypeName())
  52. if !gogoproto.HasVerboseEqual(file.FileDescriptorProto, message.DescriptorProto) {
  53. continue
  54. }
  55. if message.DescriptorProto.GetOptions().GetMapEntry() {
  56. continue
  57. }
  58. if gogoproto.HasTestGen(file.FileDescriptorProto, message.DescriptorProto) {
  59. used = true
  60. hasUnsafe := gogoproto.IsUnsafeMarshaler(file.FileDescriptorProto, message.DescriptorProto) ||
  61. gogoproto.IsUnsafeUnmarshaler(file.FileDescriptorProto, message.DescriptorProto)
  62. p.P(`func Test`, ccTypeName, `VerboseEqual(t *`, testingPkg.Use(), `.T) {`)
  63. p.In()
  64. if hasUnsafe {
  65. if hasUnsafe {
  66. p.P(`var bigendian uint32 = 0x01020304`)
  67. p.P(`if *(*byte)(`, unsafePkg.Use(), `.Pointer(&bigendian)) == 1 {`)
  68. p.In()
  69. p.P(`t.Skip("unsafe does not work on big endian architectures")`)
  70. p.Out()
  71. p.P(`}`)
  72. }
  73. }
  74. p.P(`popr := `, randPkg.Use(), `.New(`, randPkg.Use(), `.NewSource(`, timePkg.Use(), `.Now().UnixNano()))`)
  75. p.P(`p := NewPopulated`, ccTypeName, `(popr, false)`)
  76. p.P(`dAtA, err := `, protoPkg.Use(), `.Marshal(p)`)
  77. p.P(`if err != nil {`)
  78. p.In()
  79. p.P(`panic(err)`)
  80. p.Out()
  81. p.P(`}`)
  82. p.P(`msg := &`, ccTypeName, `{}`)
  83. p.P(`if err := `, protoPkg.Use(), `.Unmarshal(dAtA, msg); err != nil {`)
  84. p.In()
  85. p.P(`panic(err)`)
  86. p.Out()
  87. p.P(`}`)
  88. p.P(`if err := p.VerboseEqual(msg); err != nil {`)
  89. p.In()
  90. p.P(`t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)`)
  91. p.Out()
  92. p.P(`}`)
  93. p.Out()
  94. p.P(`}`)
  95. }
  96. }
  97. return used
  98. }
  99. func init() {
  100. testgen.RegisterTestPlugin(NewTest)
  101. }