-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.go
More file actions
52 lines (46 loc) · 1.39 KB
/
parse_test.go
File metadata and controls
52 lines (46 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package unixid
import (
"testing"
)
func TestParse(t *testing.T) {
uid, err := NewUnixID()
if err != nil {
t.Fatal("Error creating unixid:", err)
}
testCases := []struct {
name string
input string
expectedTimestamp int64
expectedUserNum string
expectError bool
}{
{"server ID", "1624397134562544800", 1624397134562544800, "", false},
{"client ID", "1624397134562544800.42", 1624397134562544800, "42", false},
{"client ID with large userNum", "1624397134562544800.1234", 1624397134562544800, "1234", false},
{"invalid multiple dots", "1624397134562544800.42.42", 0, "", true},
{"invalid letter", "1624397134562544800a", 0, "", true},
{"invalid starts with dot", ".1624397134562544800", 0, "", true},
{"invalid ends with dot", "1624397134562544800.", 0, "", true},
{"invalid empty", "", 0, "", true},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
timestamp, userNum, err := uid.Parse(tc.input)
if tc.expectError {
if err == nil {
t.Errorf("expected error, got nil")
}
return
}
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if timestamp != tc.expectedTimestamp {
t.Errorf("timestamp: expected %d, got %d", tc.expectedTimestamp, timestamp)
}
if userNum != tc.expectedUserNum {
t.Errorf("userNum: expected %q, got %q", tc.expectedUserNum, userNum)
}
})
}
}