string_test.go 1.5 KB

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