name_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2013 The Go Authors. All rights reserved.
  4. // https://github.com/golang/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. // * 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. package generator
  32. import (
  33. "testing"
  34. "github.com/golang/protobuf/protoc-gen-go/descriptor"
  35. )
  36. func TestCamelCase(t *testing.T) {
  37. tests := []struct {
  38. in, want string
  39. }{
  40. {"one", "One"},
  41. {"one_two", "OneTwo"},
  42. {"_my_field_name_2", "XMyFieldName_2"},
  43. {"Something_Capped", "Something_Capped"},
  44. {"my_Name", "My_Name"},
  45. {"OneTwo", "OneTwo"},
  46. {"_", "X"},
  47. {"_a_", "XA_"},
  48. }
  49. for _, tc := range tests {
  50. if got := CamelCase(tc.in); got != tc.want {
  51. t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
  52. }
  53. }
  54. }
  55. func TestGoPackageOption(t *testing.T) {
  56. tests := []struct {
  57. in string
  58. impPath GoImportPath
  59. pkg GoPackageName
  60. ok bool
  61. }{
  62. {"", "", "", false},
  63. {"foo", "", "foo", true},
  64. {"github.com/golang/bar", "github.com/golang/bar", "bar", true},
  65. {"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
  66. {"github.com/golang/string", "github.com/golang/string", "string", true},
  67. }
  68. for _, tc := range tests {
  69. d := &FileDescriptor{
  70. FileDescriptorProto: &descriptor.FileDescriptorProto{
  71. Options: &descriptor.FileOptions{
  72. GoPackage: &tc.in,
  73. },
  74. },
  75. }
  76. impPath, pkg, ok := d.goPackageOption()
  77. if impPath != tc.impPath || pkg != tc.pkg || ok != tc.ok {
  78. t.Errorf("go_package = %q => (%q, %q, %t), want (%q, %q, %t)", tc.in,
  79. impPath, pkg, ok, tc.impPath, tc.pkg, tc.ok)
  80. }
  81. }
  82. }
  83. func TestPackageNames(t *testing.T) {
  84. g := New()
  85. g.packageNames = make(map[GoImportPath]GoPackageName)
  86. g.usedPackageNames = make(map[GoPackageName]bool)
  87. for _, test := range []struct {
  88. importPath GoImportPath
  89. want GoPackageName
  90. }{
  91. {"github.com/golang/foo", "foo"},
  92. {"github.com/golang/second/package/named/foo", "foo1"},
  93. {"github.com/golang/third/package/named/foo", "foo2"},
  94. {"github.com/golang/conflicts/with/predeclared/ident/string", "string1"},
  95. } {
  96. if got := g.GoPackageName(test.importPath); got != test.want {
  97. t.Errorf("GoPackageName(%v) = %v, want %v", test.importPath, got, test.want)
  98. }
  99. }
  100. }
  101. func TestUnescape(t *testing.T) {
  102. tests := []struct {
  103. in string
  104. out string
  105. }{
  106. // successful cases, including all kinds of escapes
  107. {"", ""},
  108. {"foo bar baz frob nitz", "foo bar baz frob nitz"},
  109. {`\000\001\002\003\004\005\006\007`, string([]byte{0, 1, 2, 3, 4, 5, 6, 7})},
  110. {`\a\b\f\n\r\t\v\\\?\'\"`, string([]byte{'\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '?', '\'', '"'})},
  111. {`\x10\x20\x30\x40\x50\x60\x70\x80`, string([]byte{16, 32, 48, 64, 80, 96, 112, 128})},
  112. // variable length octal escapes
  113. {`\0\018\222\377\3\04\005\6\07`, string([]byte{0, 1, '8', 0222, 255, 3, 4, 5, 6, 7})},
  114. // malformed escape sequences left as is
  115. {"foo \\g bar", "foo \\g bar"},
  116. {"foo \\xg0 bar", "foo \\xg0 bar"},
  117. {"\\", "\\"},
  118. {"\\x", "\\x"},
  119. {"\\xf", "\\xf"},
  120. {"\\777", "\\777"}, // overflows byte
  121. }
  122. for _, tc := range tests {
  123. s := unescape(tc.in)
  124. if s != tc.out {
  125. t.Errorf("doUnescape(%q) = %q; should have been %q", tc.in, s, tc.out)
  126. }
  127. }
  128. }