strutils_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package strutils
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestAllBlank(t *testing.T) {
  7. allBlank := AllBlank("", "", "")
  8. if !allBlank {
  9. t.Fatal("字符串全为空但是检测失败")
  10. }
  11. allBlank = AllBlank("", "foo", "")
  12. if allBlank {
  13. t.Fatal("字符串不全为空但是检测失败")
  14. }
  15. }
  16. func TestHasBlank(t *testing.T) {
  17. hasBlank := HasBlank("a", "", "b")
  18. if !hasBlank {
  19. t.Fatal("字符串包含空字符串检测失败")
  20. }
  21. hasBlank = HasBlank("a", "b", "c")
  22. if hasBlank {
  23. t.Fatal("字符串不包含空字符串但是检测失败")
  24. }
  25. }
  26. func TestHasText(t *testing.T) {
  27. hasText := HasText("a", "", "b")
  28. if !hasText {
  29. t.Fatal("字符串包含字符串检测失败")
  30. }
  31. hasText = HasText("", "", "")
  32. if hasText {
  33. t.Fatal("字符串不包含字符串但是检测失败")
  34. }
  35. }
  36. func TestIsStringEmpty(t *testing.T) {
  37. stringEmpty := IsStringEmpty("")
  38. if !stringEmpty {
  39. t.Fatal("字符串为空但是检测失败")
  40. }
  41. stringEmpty = IsStringEmpty("foo")
  42. if stringEmpty {
  43. t.Fatal("字符串不为空但是检测失败")
  44. }
  45. }
  46. func TestIsStringNotEmpty(t *testing.T) {
  47. stringNotEmpty := IsStringNotEmpty("")
  48. if stringNotEmpty {
  49. t.Fatal("字符串为空但是检测失败")
  50. }
  51. stringNotEmpty = IsStringNotEmpty("foo")
  52. if !stringNotEmpty {
  53. t.Fatal("字符串不为空但是检测失败")
  54. }
  55. }
  56. func TestUUID(t *testing.T) {
  57. UUID := GetUUID()
  58. UUIDParts := strings.Split(UUID, "-")
  59. if len(UUIDParts) != 5 {
  60. t.Fatal("生成uuid格式不正确")
  61. }
  62. if len(UUIDParts[0]) != 8 {
  63. t.Fatal("生成uuid第一段长度不为8")
  64. }
  65. if len(UUIDParts[1]) != 4 {
  66. t.Fatal("生成uuid第二段长度不为4")
  67. }
  68. if len(UUIDParts[2]) != 4 {
  69. t.Fatal("生成uuid第三段长度不为4")
  70. }
  71. if len(UUIDParts[3]) != 4 {
  72. t.Fatal("生成uuid第四段长度不为4")
  73. }
  74. if len(UUIDParts[4]) != 12 {
  75. t.Fatal("生成uuid第五段长度不为12")
  76. }
  77. simpleUUID := SimpleUUID()
  78. if len(simpleUUID) != 32 {
  79. t.Fatal("生成uuid长度不为32")
  80. }
  81. }
  82. func TestGenerateUpperLetters(t *testing.T) {
  83. letters := GenerateUpperLetters(3)
  84. if letters[0] != "A" || letters[1] != "B" || letters[2] != "C" {
  85. t.Fatal("生成字母错误")
  86. }
  87. }
  88. func TestGenerateLowerLetters(t *testing.T) {
  89. letters := GenerateLowerLetters(3)
  90. if letters[0] != "a" || letters[1] != "b" || letters[2] != "c" {
  91. t.Fatal("生成字母错误")
  92. }
  93. }