Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Jul 16, 2023
1 parent 19ea5a1 commit 5a739db
Show file tree
Hide file tree
Showing 3 changed files with 360 additions and 23 deletions.
49 changes: 49 additions & 0 deletions alphabet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package sqids

import (
"reflect"
"testing"
)

func TestSimple(t *testing.T) {
numbers := []uint64{1, 2, 3}
id := "4d9fd2"

sqids, err := NewCustom("0123456789abcdef", 0)
if err != nil {
t.Fatal(err)
}

generatedID, err := sqids.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 := sqids.Decode(id)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}

func TestShortAlphabet(t *testing.T) {
sqids, err := NewCustom("abcde", 0)
if err != nil {
t.Fatal(err)
}

numbers := []uint64{1, 2, 3}

id, err := sqids.Encode(numbers)
if err != nil {
t.Fatal(err)
}

decodedNumbers := sqids.Decode(id)
if !reflect.DeepEqual(numbers, decodedNumbers) {
t.Errorf("Decoding `%v` should produce `%v`, but instead produced `%v`", id, numbers, decodedNumbers)
}
}
9 changes: 5 additions & 4 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
)

func TestEncodeDecode(t *testing.T) {
numbers := []uint64{}
numbers := []uint64{1, 2, 3}

sqids, _ := New()
id, err := sqids.Encode(numbers)
sqids, err := New()
if err != nil {
t.Fatal(err)
}

decoded, err := sqids.Decode(id)
id, err := sqids.Encode(numbers)
if err != nil {
t.Fatal(err)
}

decoded := sqids.Decode(id)

if !reflect.DeepEqual(numbers, decoded) {
t.Errorf("Could not encode/decode `%v`", numbers)
}
Expand Down
Loading

0 comments on commit 5a739db

Please sign in to comment.