string_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package util
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. type data struct {
  8. input string
  9. expected string
  10. }
  11. func TestTitle(t *testing.T) {
  12. list := []*data{
  13. {input: "_", expected: "_"},
  14. {input: "abc", expected: "Abc"},
  15. {input: "ABC", expected: "ABC"},
  16. {input: "", expected: ""},
  17. {input: " abc", expected: " abc"},
  18. }
  19. for _, e := range list {
  20. assert.Equal(t, e.expected, Title(e.input))
  21. }
  22. }
  23. func TestUntitle(t *testing.T) {
  24. list := []*data{
  25. {input: "_", expected: "_"},
  26. {input: "Abc", expected: "abc"},
  27. {input: "ABC", expected: "aBC"},
  28. {input: "", expected: ""},
  29. {input: " abc", expected: " abc"},
  30. }
  31. for _, e := range list {
  32. assert.Equal(t, e.expected, Untitle(e.input))
  33. }
  34. }
  35. func TestIndex(t *testing.T) {
  36. list := []string{"a", "b", "c"}
  37. assert.Equal(t, 1, Index(list, "b"))
  38. assert.Equal(t, -1, Index(list, "d"))
  39. }
  40. func TestSafeString(t *testing.T) {
  41. list := []*data{
  42. {input: "_", expected: "_"},
  43. {input: "a-b-c", expected: "a_b_c"},
  44. {input: "123abc", expected: "_123abc"},
  45. {input: "汉abc", expected: "_abc"},
  46. {input: "汉a字", expected: "_a_"},
  47. {input: "キャラクターabc", expected: "______abc"},
  48. {input: "-a_B-C", expected: "_a_B_C"},
  49. {input: "a_B C", expected: "a_B_C"},
  50. {input: "A#B#C", expected: "A_B_C"},
  51. {input: "_123", expected: "_123"},
  52. {input: "", expected: ""},
  53. {input: "\t", expected: "_"},
  54. {input: "\n", expected: "_"},
  55. }
  56. for _, e := range list {
  57. assert.Equal(t, e.expected, SafeString(e.input))
  58. }
  59. }
  60. func TestEscapeGoKeyword(t *testing.T) {
  61. for k := range goKeyword {
  62. assert.Equal(t, goKeyword[k], EscapeGolangKeyword(k))
  63. assert.False(t, isGolangKeyword(strings.Title(k)))
  64. }
  65. }