1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-04 20:54:43 +00:00
arikawa/discord/snowflake_test.go

67 lines
1.3 KiB
Go
Raw Normal View History

2020-05-08 21:11:56 +00:00
package discord
import (
"testing"
"time"
)
func TestSnowflake(t *testing.T) {
t.Run("parse", func(t *testing.T) {
_, err := ParseSnowflake("175928847299117063")
if err != nil {
t.Fatal("Failed to parse snowflake:", err)
}
})
const value = 175928847299117063
var expect = time.Date(2016, 04, 30, 11, 18, 25, 796*int(time.Millisecond), time.UTC)
t.Run("methods", func(t *testing.T) {
s := Snowflake(value)
if ts := s.Time(); !ts.Equal(expect) {
t.Fatal("Unexpected time (expected/got):", expect, ts)
}
if s.Worker() != 1 {
t.Fatal("Unexpected worker:", s.Worker())
}
if s.PID() != 0 {
t.Fatal("Unexpected PID:", s.PID())
}
if s.Increment() != 7 {
t.Fatal("Unexpected increment:", s.Increment())
}
})
t.Run("IsValid", func(t *testing.T) {
t.Run("0", func(t *testing.T) {
if Snowflake(0).IsValid() {
t.Fatal("0 isn't a valid Snowflake")
}
})
t.Run("null", func(t *testing.T) {
if NullSnowflake.IsValid() {
t.Fatal("NullSnowflake isn't a valid Snowflake")
}
})
t.Run("valid", func(t *testing.T) {
var testFlake Snowflake = 123
if !testFlake.IsValid() {
t.Fatal(testFlake, "is a valid Snowflake")
}
})
})
2020-05-08 21:11:56 +00:00
t.Run("new", func(t *testing.T) {
if s := NewSnowflake(expect); !s.Time().Equal(expect) {
t.Fatal("Unexpected new snowflake from expected time:", s)
}
})
}