format_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package format
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestSplit(t *testing.T) {
  7. list, err := split("A")
  8. assert.Nil(t, err)
  9. assert.Equal(t, []string{"A"}, list)
  10. list, err = split("goZero")
  11. assert.Nil(t, err)
  12. assert.Equal(t, []string{"go", "Zero"}, list)
  13. list, err = split("Gozero")
  14. assert.Nil(t, err)
  15. assert.Equal(t, []string{"Gozero"}, list)
  16. list, err = split("go_zero")
  17. assert.Nil(t, err)
  18. assert.Equal(t, []string{"go", "zero"}, list)
  19. list, err = split("talGo_zero")
  20. assert.Nil(t, err)
  21. assert.Equal(t, []string{"tal", "Go", "zero"}, list)
  22. list, err = split("GOZERO")
  23. assert.Nil(t, err)
  24. assert.Equal(t, []string{"G", "O", "Z", "E", "R", "O"}, list)
  25. list, err = split("gozero")
  26. assert.Nil(t, err)
  27. assert.Equal(t, []string{"gozero"}, list)
  28. list, err = split("")
  29. assert.Nil(t, err)
  30. assert.Equal(t, 0, len(list))
  31. list, err = split("a_b_CD_EF")
  32. assert.Nil(t, err)
  33. assert.Equal(t, []string{"a", "b", "C", "D", "E", "F"}, list)
  34. list, err = split("_")
  35. assert.Nil(t, err)
  36. assert.Equal(t, 0, len(list))
  37. list, err = split("__")
  38. assert.Nil(t, err)
  39. assert.Equal(t, 0, len(list))
  40. list, err = split("_A")
  41. assert.Nil(t, err)
  42. assert.Equal(t, []string{"A"}, list)
  43. list, err = split("_A_")
  44. assert.Nil(t, err)
  45. assert.Equal(t, []string{"A"}, list)
  46. list, err = split("A_")
  47. assert.Nil(t, err)
  48. assert.Equal(t, []string{"A"}, list)
  49. list, err = split("welcome_to_go_zero")
  50. assert.Nil(t, err)
  51. assert.Equal(t, []string{"welcome", "to", "go", "zero"}, list)
  52. }
  53. func TestFileNamingFormat(t *testing.T) {
  54. testFileNamingFormat(t, "gozero", "welcome_to_go_zero", "welcometogozero")
  55. testFileNamingFormat(t, "_go#zero_", "welcome_to_go_zero", "_welcome#to#go#zero_")
  56. testFileNamingFormat(t, "Go#zero", "welcome_to_go_zero", "Welcome#to#go#zero")
  57. testFileNamingFormat(t, "Go#Zero", "welcome_to_go_zero", "Welcome#To#Go#Zero")
  58. testFileNamingFormat(t, "Go_Zero", "welcome_to_go_zero", "Welcome_To_Go_Zero")
  59. testFileNamingFormat(t, "go_Zero", "welcome_to_go_zero", "welcome_To_Go_Zero")
  60. testFileNamingFormat(t, "goZero", "welcome_to_go_zero", "welcomeToGoZero")
  61. testFileNamingFormat(t, "GoZero", "welcome_to_go_zero", "WelcomeToGoZero")
  62. testFileNamingFormat(t, "GOZero", "welcome_to_go_zero", "WELCOMEToGoZero")
  63. testFileNamingFormat(t, "GoZERO", "welcome_to_go_zero", "WelcomeTOGOZERO")
  64. testFileNamingFormat(t, "GOZERO", "welcome_to_go_zero", "WELCOMETOGOZERO")
  65. testFileNamingFormat(t, "GO*ZERO", "welcome_to_go_zero", "WELCOME*TO*GO*ZERO")
  66. testFileNamingFormat(t, "[GO#ZERO]", "welcome_to_go_zero", "[WELCOME#TO#GO#ZERO]")
  67. testFileNamingFormat(t, "{go###zero}", "welcome_to_go_zero", "{welcome###to###go###zero}")
  68. testFileNamingFormat(t, "{go###zerogo_zero}", "welcome_to_go_zero", "{welcome###to###go###zerogo_zero}")
  69. testFileNamingFormat(t, "GogoZerozero", "welcome_to_go_zero", "WelcomegoTogoGogoZerozero")
  70. testFileNamingFormat(t, "前缀GoZero后缀", "welcome_to_go_zero", "前缀WelcomeToGoZero后缀")
  71. testFileNamingFormat(t, "GoZero", "welcometogozero", "Welcometogozero")
  72. testFileNamingFormat(t, "GoZero", "WelcomeToGoZero", "WelcomeToGoZero")
  73. testFileNamingFormat(t, "gozero", "WelcomeToGoZero", "welcometogozero")
  74. testFileNamingFormat(t, "go_zero", "WelcomeToGoZero", "welcome_to_go_zero")
  75. testFileNamingFormat(t, "Go_Zero", "WelcomeToGoZero", "Welcome_To_Go_Zero")
  76. testFileNamingFormat(t, "Go_Zero", "", "")
  77. testFileNamingFormatErr(t, "go", "")
  78. testFileNamingFormatErr(t, "gOZero", "")
  79. testFileNamingFormatErr(t, "zero", "")
  80. testFileNamingFormatErr(t, "goZEro", "welcome_to_go_zero")
  81. testFileNamingFormatErr(t, "goZERo", "welcome_to_go_zero")
  82. testFileNamingFormatErr(t, "zerogo", "welcome_to_go_zero")
  83. }
  84. func testFileNamingFormat(t *testing.T, format, in, expected string) {
  85. format, err := FileNamingFormat(format, in)
  86. assert.Nil(t, err)
  87. assert.Equal(t, expected, format)
  88. }
  89. func testFileNamingFormatErr(t *testing.T, format, in string) {
  90. _, err := FileNamingFormat(format, in)
  91. assert.Error(t, err)
  92. }