Tests for platform.ID

Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
pull/10616/head
Leonardo Di Donato 2018-07-23 16:04:33 +02:00 committed by Leonardo Di Donato
parent 60eb7dd8ad
commit bc03eb2d2b
1 changed files with 65 additions and 27 deletions

View File

@ -39,47 +39,85 @@ func TestIDFromString(t *testing.T) {
}
func TestDecodeFromString(t *testing.T) {
var id1 ID
err := id1.DecodeFromString("020f755c3c082000")
var id ID
err := id.DecodeFromString("020f755c3c082000")
if err != nil {
t.Errorf(err.Error())
}
want1 := []byte{48, 50, 48, 102, 55, 53, 53, 99, 51, 99, 48, 56, 50, 48, 48, 48}
byte1 := id1.Encode()
if !bytes.Equal(want1, byte1) {
want := []byte{48, 50, 48, 102, 55, 53, 53, 99, 51, 99, 48, 56, 50, 48, 48, 48}
got, _ := id.Encode()
if !bytes.Equal(want, got) {
t.Errorf("encoding error")
}
if id.String() != "020f755c3c082000" {
t.Errorf("expecting string representation to contain the right value")
}
}
func TestEncode(t *testing.T) {
var id ID
if !id.Empty() {
t.Errorf("ID must be empty at first stage")
}
got, err := id.Encode()
if len(got) != 0 {
t.Errorf("encoding an empty ID must result in an empty byte array plus an error")
}
if err == nil {
t.Errorf("encoding an empty ID must result in an empty byte array plus an error")
}
id.DecodeFromString("5ca1ab1eba5eba11")
want := []byte{53, 99, 97, 49, 97, 98, 49, 101, 98, 97, 53, 101, 98, 97, 49, 49}
got, err = id.Encode()
if !bytes.Equal(want, got) {
t.Errorf("encoding error")
}
if id.String() != "5ca1ab1eba5eba11" {
t.Errorf("expecting string representation to contain the right value")
}
}
func TestDecodeFromShorterString(t *testing.T) {
var id1 ID
err := id1.DecodeFromString("020f75")
if err != nil {
t.Errorf(err.Error())
var id ID
err := id.DecodeFromString("020f75")
if err == nil {
t.Errorf("expecting shorter inputs to error")
}
want1 := []byte{48, 50, 48, 102, 55, 53, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48}
byte1 := id1.Encode()
if !bytes.Equal(want1, byte1) {
t.Errorf("encoding error")
}
if id1.String() != "020f750000000000" {
t.Errorf("wrong id")
if id.String() != "" {
t.Errorf("expecting empty ID to be serialized into empty string")
}
}
func TestDecodeFromLongerString(t *testing.T) {
var id1 ID
err := id1.DecodeFromString("020f755c3c082000aaa")
if err != nil {
t.Errorf(err.Error())
var id ID
err := id.DecodeFromString("020f755c3c082000aaa")
if err == nil {
t.Errorf("expecting shorter inputs to error")
}
want1 := []byte{48, 50, 48, 102, 55, 53, 53, 99, 51, 99, 48, 56, 50, 48, 48, 48}
byte1 := id1.Encode()
if !bytes.Equal(want1, byte1) {
t.Errorf("encoding error")
if id.String() != "" {
t.Errorf("expecting empty ID to be serialized into empty string")
}
}
// todo
// func TestDecodeFromEmptyString(t *testing.T) {}
func TestDecodeFromEmptyString(t *testing.T) {
var id ID
err := id.DecodeFromString("")
if err == nil {
t.Errorf("expecting empty inputs to error")
}
if !id.Empty() {
t.Errorf("expecting ID to be empty")
}
if id.String() != "" {
t.Errorf("expecting empty ID to be serialized into empty string")
}
}
func TestAllZerosDoesNotMeanEmpty(t *testing.T) {
var id ID
id.DecodeFromString("0000000000000000")
if id.Empty() {
t.Errorf("expecting id to not be empty")
}
}