Discord: Added tests for Snowflake
This commit is contained in:
parent
2cad0e87b6
commit
f9c962fb96
|
@ -74,7 +74,7 @@ func (s Snowflake) Time() time.Time {
|
|||
}
|
||||
|
||||
func (s Snowflake) Worker() uint8 {
|
||||
return uint8(s & 0x3E0000)
|
||||
return uint8(s & 0x3E0000 >> 17)
|
||||
}
|
||||
|
||||
func (s Snowflake) PID() uint8 {
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
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("new", func(t *testing.T) {
|
||||
if s := NewSnowflake(expect); !s.Time().Equal(expect) {
|
||||
t.Fatal("Unexpected new snowflake from expected time:", s)
|
||||
}
|
||||
})
|
||||
}
|
|
@ -2,6 +2,7 @@ package discord
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -95,10 +96,22 @@ func (t UnixMsTimestamp) Time() time.Time {
|
|||
|
||||
type Seconds int
|
||||
|
||||
// NullSecond is used in cases where null should be used instead of a number or
|
||||
// omitted. This is similar to NullSnowflake.
|
||||
const NullSecond = -1
|
||||
|
||||
func DurationToSeconds(dura time.Duration) Seconds {
|
||||
return Seconds(dura.Seconds())
|
||||
}
|
||||
|
||||
func (s Seconds) MarshalJSON() ([]byte, error) {
|
||||
if s < 1 {
|
||||
return []byte("null"), nil
|
||||
} else {
|
||||
return []byte(strconv.Itoa(int(s))), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s Seconds) String() string {
|
||||
return s.Duration().String()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue