names_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2018 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 protogen
  5. import "testing"
  6. func TestCamelCase(t *testing.T) {
  7. tests := []struct {
  8. in, want string
  9. }{
  10. {"", ""},
  11. {"one", "One"},
  12. {"one_two", "OneTwo"},
  13. {"_my_field_name_2", "XMyFieldName_2"},
  14. {"Something_Capped", "Something_Capped"},
  15. {"my_Name", "My_Name"},
  16. {"OneTwo", "OneTwo"},
  17. {"_", "X"},
  18. {"_a_", "XA_"},
  19. {"one.two", "OneTwo"},
  20. {"one.Two", "One_Two"},
  21. {"one_two.three_four", "OneTwoThreeFour"},
  22. {"one_two.Three_four", "OneTwo_ThreeFour"},
  23. {"_one._two", "XOne_XTwo"},
  24. {"SCREAMING_SNAKE_CASE", "SCREAMING_SNAKE_CASE"},
  25. {"double__underscore", "Double_Underscore"},
  26. {"camelCase", "CamelCase"},
  27. {"go2proto", "Go2Proto"},
  28. {"世界", "世界"},
  29. {"x世界", "X世界"},
  30. {"foo_bar世界", "FooBar世界"},
  31. }
  32. for _, tc := range tests {
  33. if got := camelCase(tc.in); got != tc.want {
  34. t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
  35. }
  36. }
  37. }
  38. func TestCleanGoName(t *testing.T) {
  39. tests := []struct {
  40. in, want string
  41. }{
  42. {"", "_"},
  43. {"boo", "boo"},
  44. {"Boo", "Boo"},
  45. {"ßoo", "ßoo"},
  46. {"default", "_default"},
  47. {"hello", "hello"},
  48. {"hello-world!!", "hello_world__"},
  49. {"hello-\xde\xad\xbe\xef\x00", "hello_____"},
  50. {"hello 世界", "hello_世界"},
  51. {"世界", "世界"},
  52. }
  53. for _, tc := range tests {
  54. if got := cleanGoName(tc.in); got != tc.want {
  55. t.Errorf("cleanGoName(%q) = %q, want %q", tc.in, got, tc.want)
  56. }
  57. }
  58. }