generator_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package gen
  2. import (
  3. "testing"
  4. )
  5. func TestCamelToSnake(t *testing.T) {
  6. for i, test := range []struct {
  7. In, Out string
  8. }{
  9. {"", ""},
  10. {"A", "a"},
  11. {"SimpleExample", "simple_example"},
  12. {"internalField", "internal_field"},
  13. {"SomeHTTPStuff", "some_http_stuff"},
  14. {"WriteJSON", "write_json"},
  15. {"HTTP2Server", "http2_server"},
  16. {"Some_Mixed_Case", "some_mixed_case"},
  17. {"do_nothing", "do_nothing"},
  18. {"JSONHTTPRPCServer", "jsonhttprpc_server"}, // nothing can be done here without a dictionary
  19. } {
  20. got := camelToSnake(test.In)
  21. if got != test.Out {
  22. t.Errorf("[%d] camelToSnake(%s) = %s; want %s", i, test.In, got, test.Out)
  23. }
  24. }
  25. }
  26. func TestCamelToLowerCamel(t *testing.T) {
  27. for i, test := range []struct {
  28. In, Out string
  29. }{
  30. {"", ""},
  31. {"A", "a"},
  32. {"SimpleExample", "simpleExample"},
  33. {"internalField", "internalField"},
  34. {"SomeHTTPStuff", "someHTTPStuff"},
  35. {"WriteJSON", "writeJSON"},
  36. {"HTTP2Server", "http2Server"},
  37. {"JSONHTTPRPCServer", "jsonhttprpcServer"}, // nothing can be done here without a dictionary
  38. } {
  39. got := lowerFirst(test.In)
  40. if got != test.Out {
  41. t.Errorf("[%d] lowerFirst(%s) = %s; want %s", i, test.In, got, test.Out)
  42. }
  43. }
  44. }
  45. func TestJoinFunctionNameParts(t *testing.T) {
  46. for i, test := range []struct {
  47. keepFirst bool
  48. parts []string
  49. out string
  50. }{
  51. {false, []string{}, ""},
  52. {false, []string{"a"}, "A"},
  53. {false, []string{"simple", "example"}, "SimpleExample"},
  54. {true, []string{"first", "example"}, "firstExample"},
  55. {false, []string{"some", "UPPER", "case"}, "SomeUPPERCase"},
  56. {false, []string{"number", "123"}, "Number123"},
  57. } {
  58. got := joinFunctionNameParts(test.keepFirst, test.parts...)
  59. if got != test.out {
  60. t.Errorf("[%d] joinFunctionNameParts(%v) = %s; want %s", i, test.parts, got, test.out)
  61. }
  62. }
  63. }
  64. func TestFixVendorPath(t *testing.T) {
  65. for i, test := range []struct {
  66. In, Out string
  67. }{
  68. {"", ""},
  69. {"time", "time"},
  70. {"project/vendor/subpackage", "subpackage"},
  71. } {
  72. got := fixPkgPathVendoring(test.In)
  73. if got != test.Out {
  74. t.Errorf("[%d] fixPkgPathVendoring(%s) = %s; want %s", i, test.In, got, test.Out)
  75. }
  76. }
  77. }