validate_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package util
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. // TC-0269: page=2, pageSize=10
  7. func TestNormalizePage(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. page, pageSize int64
  11. wantP, wantSize int64
  12. }{
  13. {"normal values", 2, 10, 2, 10},
  14. {"page=0", 0, 10, 1, 10},
  15. {"page=-1", -1, 10, 1, 10},
  16. {"pageSize=0", 1, 0, 1, 20},
  17. {"pageSize=-5", 1, -5, 1, 20},
  18. {"pageSize>100", 1, 500, 1, 100},
  19. {"pageSize=100 boundary", 1, 100, 1, 100},
  20. {"pageSize=101 boundary", 1, 101, 1, 100},
  21. {"both zero", 0, 0, 1, 20},
  22. {"both negative", -3, -10, 1, 20},
  23. }
  24. for _, tt := range tests {
  25. t.Run(tt.name, func(t *testing.T) {
  26. p, s := NormalizePage(tt.page, tt.pageSize)
  27. assert.Equal(t, tt.wantP, p)
  28. assert.Equal(t, tt.wantSize, s)
  29. })
  30. }
  31. }
  32. // TC-0277: `[email protected]`
  33. func TestIsValidEmail(t *testing.T) {
  34. tests := []struct {
  35. name string
  36. email string
  37. want bool
  38. }{
  39. {"standard email", "[email protected]", true},
  40. {"with dots", "[email protected]", true},
  41. {"with plus", "[email protected]", true},
  42. {"with hyphen domain", "[email protected]", true},
  43. {"missing @", "userexample.com", false},
  44. {"missing domain", "user@", false},
  45. {"missing TLD", "user@example", false},
  46. {"empty string", "", false},
  47. {"double @", "user@@example.com", false},
  48. {"space in email", "user @example.com", false},
  49. {"short TLD", "[email protected]", false},
  50. {"subdomain", "[email protected]", true},
  51. }
  52. for _, tt := range tests {
  53. t.Run(tt.name, func(t *testing.T) {
  54. assert.Equal(t, tt.want, IsValidEmail(tt.email))
  55. })
  56. }
  57. }
  58. // TC-0284: `13800138000`
  59. func TestIsValidPhone(t *testing.T) {
  60. tests := []struct {
  61. name string
  62. phone string
  63. want bool
  64. }{
  65. {"chinese mobile", "13800138000", true},
  66. {"with country code", "+8613800138000", true},
  67. {"too short 6 digits", "123456", false},
  68. {"min 7 digits", "1234567", true},
  69. {"max 15 digits", "+123456789012345", true},
  70. {"over 16 digits", "1234567890123456", false},
  71. {"contains letters", "1380013abc", false},
  72. {"empty string", "", false},
  73. {"only plus", "+", false},
  74. {"plus with short", "+123456", false},
  75. {"leading zero not first", "01234567890", false},
  76. }
  77. for _, tt := range tests {
  78. t.Run(tt.name, func(t *testing.T) {
  79. assert.Equal(t, tt.want, IsValidPhone(tt.phone))
  80. })
  81. }
  82. }