-
Notifications
You must be signed in to change notification settings - Fork 13
/
alphabet_test.go
101 lines (83 loc) · 2.11 KB
/
alphabet_test.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package sqids
import (
"reflect"
"testing"
)
func TestMultibyteAlphabet(t *testing.T) {
_, err := New(Options{
Alphabet: "ë1092",
})
if err != errAlphabetMultibyte {
t.Fatalf("unexpected error: %v", err)
}
}
func TestAlphabetSimple(t *testing.T) {
numbers := []uint64{1, 2, 3}
id := "489158"
alphabet := "0123456789abcdef"
s, err := New(Options{
Alphabet: alphabet,
})
if err != nil {
t.Fatal(err)
}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
if id != generatedID {
t.Errorf("Encoding `%v` should produce `%v`, but instead produced `%v`", numbers, id, generatedID)
}
decodedNumbers := s.Decode(generatedID)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}
func TestAlphabetShort(t *testing.T) {
s, err := New(Options{
Alphabet: "abc",
})
if err != nil {
t.Fatal(err)
}
numbers := []uint64{1, 2, 3}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
decodedNumbers := s.Decode(generatedID)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", generatedID, numbers, decodedNumbers)
}
}
func TestAlphabetLong(t *testing.T) {
s, err := New(Options{
Alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_+|{}[];:'\"/?.>,<`~",
})
if err != nil {
t.Fatal(err)
}
numbers := []uint64{1, 2, 3}
generatedID, err := s.Encode(numbers)
if err != nil {
t.Fatal(err)
}
decodedNumbers := s.Decode(generatedID)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", generatedID, numbers, decodedNumbers)
}
}
func TestRepeatingAlphabetCharacters(t *testing.T) {
if _, err := New(Options{
Alphabet: "aabcdefg",
}); err == nil {
t.Errorf("Should not accept alphabet with repeating characters")
}
}
func TestTooShortOfAnAlphabet(t *testing.T) {
if _, err := New(Options{
Alphabet: "ab",
}); err == nil {
t.Errorf("Should not accept too short of an alphabet")
}
}