| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package util
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- )
- // TC-0194: page=2, pageSize=10
- func TestNormalizePage(t *testing.T) {
- tests := []struct {
- name string
- page, pageSize int64
- wantP, wantSize int64
- }{
- {"normal values", 2, 10, 2, 10},
- {"page=0", 0, 10, 1, 10},
- {"page=-1", -1, 10, 1, 10},
- {"pageSize=0", 1, 0, 1, 20},
- {"pageSize=-5", 1, -5, 1, 20},
- {"pageSize>100", 1, 500, 1, 100},
- {"pageSize=100 boundary", 1, 100, 1, 100},
- {"pageSize=101 boundary", 1, 101, 1, 100},
- {"both zero", 0, 0, 1, 20},
- {"both negative", -3, -10, 1, 20},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- p, s := NormalizePage(tt.page, tt.pageSize)
- assert.Equal(t, tt.wantP, p)
- assert.Equal(t, tt.wantSize, s)
- })
- }
- }
- // TC-0202: `[email protected]`
- func TestIsValidEmail(t *testing.T) {
- tests := []struct {
- name string
- email string
- want bool
- }{
- {"standard email", "[email protected]", true},
- {"with dots", "[email protected]", true},
- {"with plus", "[email protected]", true},
- {"with hyphen domain", "[email protected]", true},
- {"missing @", "userexample.com", false},
- {"missing domain", "user@", false},
- {"missing TLD", "user@example", false},
- {"empty string", "", false},
- {"double @", "user@@example.com", false},
- {"space in email", "user @example.com", false},
- {"short TLD", "[email protected]", false},
- {"subdomain", "[email protected]", true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- assert.Equal(t, tt.want, IsValidEmail(tt.email))
- })
- }
- }
- // TC-0209: `13800138000`
- func TestIsValidPhone(t *testing.T) {
- tests := []struct {
- name string
- phone string
- want bool
- }{
- {"chinese mobile", "13800138000", true},
- {"with country code", "+8613800138000", true},
- {"too short 6 digits", "123456", false},
- {"min 7 digits", "1234567", true},
- {"max 15 digits", "+123456789012345", true},
- {"over 16 digits", "1234567890123456", false},
- {"contains letters", "1380013abc", false},
- {"empty string", "", false},
- {"only plus", "+", false},
- {"plus with short", "+123456", false},
- {"leading zero not first", "01234567890", false},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- assert.Equal(t, tt.want, IsValidPhone(tt.phone))
- })
- }
- }
|