name_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package generator
  5. import (
  6. "testing"
  7. "github.com/golang/protobuf/protoc-gen-go/descriptor"
  8. )
  9. func TestCamelCase(t *testing.T) {
  10. tests := []struct {
  11. in, want string
  12. }{
  13. {"one", "One"},
  14. {"one_two", "OneTwo"},
  15. {"_my_field_name_2", "XMyFieldName_2"},
  16. {"Something_Capped", "Something_Capped"},
  17. {"my_Name", "My_Name"},
  18. {"OneTwo", "OneTwo"},
  19. {"_", "X"},
  20. {"_a_", "XA_"},
  21. }
  22. for _, tc := range tests {
  23. if got := CamelCase(tc.in); got != tc.want {
  24. t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
  25. }
  26. }
  27. }
  28. func TestGoPackageOption(t *testing.T) {
  29. tests := []struct {
  30. in string
  31. impPath GoImportPath
  32. pkg GoPackageName
  33. ok bool
  34. }{
  35. {"", "", "", false},
  36. {"foo", "", "foo", true},
  37. {"github.com/golang/bar", "github.com/golang/bar", "bar", true},
  38. {"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
  39. {"github.com/golang/string", "github.com/golang/string", "string", true},
  40. }
  41. for _, tc := range tests {
  42. d := &FileDescriptor{
  43. FileDescriptorProto: &descriptor.FileDescriptorProto{
  44. Options: &descriptor.FileOptions{
  45. GoPackage: &tc.in,
  46. },
  47. },
  48. }
  49. impPath, pkg, ok := d.goPackageOption()
  50. if impPath != tc.impPath || pkg != tc.pkg || ok != tc.ok {
  51. t.Errorf("go_package = %q => (%q, %q, %t), want (%q, %q, %t)", tc.in,
  52. impPath, pkg, ok, tc.impPath, tc.pkg, tc.ok)
  53. }
  54. }
  55. }
  56. func TestPackageNames(t *testing.T) {
  57. g := New()
  58. g.packageNames = make(map[GoImportPath]GoPackageName)
  59. g.usedPackageNames = make(map[GoPackageName]bool)
  60. for _, test := range []struct {
  61. importPath GoImportPath
  62. want GoPackageName
  63. }{
  64. {"github.com/golang/foo", "foo"},
  65. {"github.com/golang/second/package/named/foo", "foo1"},
  66. {"github.com/golang/third/package/named/foo", "foo2"},
  67. {"github.com/golang/conflicts/with/predeclared/ident/string", "string1"},
  68. } {
  69. if got := g.GoPackageName(test.importPath); got != test.want {
  70. t.Errorf("GoPackageName(%v) = %v, want %v", test.importPath, got, test.want)
  71. }
  72. }
  73. }
  74. func TestUnescape(t *testing.T) {
  75. tests := []struct {
  76. in string
  77. out string
  78. }{
  79. // successful cases, including all kinds of escapes
  80. {"", ""},
  81. {"foo bar baz frob nitz", "foo bar baz frob nitz"},
  82. {`\000\001\002\003\004\005\006\007`, string([]byte{0, 1, 2, 3, 4, 5, 6, 7})},
  83. {`\a\b\f\n\r\t\v\\\?\'\"`, string([]byte{'\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '?', '\'', '"'})},
  84. {`\x10\x20\x30\x40\x50\x60\x70\x80`, string([]byte{16, 32, 48, 64, 80, 96, 112, 128})},
  85. // variable length octal escapes
  86. {`\0\018\222\377\3\04\005\6\07`, string([]byte{0, 1, '8', 0222, 255, 3, 4, 5, 6, 7})},
  87. // malformed escape sequences left as is
  88. {"foo \\g bar", "foo \\g bar"},
  89. {"foo \\xg0 bar", "foo \\xg0 bar"},
  90. {"\\", "\\"},
  91. {"\\x", "\\x"},
  92. {"\\xf", "\\xf"},
  93. {"\\777", "\\777"}, // overflows byte
  94. }
  95. for _, tc := range tests {
  96. s := unescape(tc.in)
  97. if s != tc.out {
  98. t.Errorf("doUnescape(%q) = %q; should have been %q", tc.in, s, tc.out)
  99. }
  100. }
  101. }