Skip to content

Commit

Permalink
Restructured the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrik Lindahl committed Apr 9, 2024
1 parent 56acf31 commit fcc3947
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

func TestError(t *testing.T) {
func TestIsAsError(t *testing.T) {
var e Error
tcs := []struct {
err error
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestError(t *testing.T) {
}
}

func TestAllErrorMessages(t *testing.T) {
func TestParseErrors(t *testing.T) {
tcs := []struct {
function string
uuidStr string
Expand Down Expand Up @@ -119,8 +119,9 @@ func TestAllErrorMessages(t *testing.T) {
}
})
}
}

// Unmarshal binary
func TestUnmarshalBinaryError(t *testing.T) {
id := UUID{}
b := make([]byte, 33)
expectedErr := "uuid: UUID must be exactly 16 bytes long, got 33 bytes"
Expand All @@ -132,68 +133,74 @@ func TestAllErrorMessages(t *testing.T) {
if err.Error() != expectedErr {
t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr)
}
}

// no hw address error
netInterfaces = func() ([]net.Interface, error) {
return nil, nil
}
defer func() {
netInterfaces = net.Interfaces
}()
_, err = defaultHWAddrFunc()
func TestScanError(t *testing.T) {
id := UUID{}
err := id.Scan(123)
if err == nil {
t.Error("expected an error")
return
}
expectedErr = "uuid: no HW address found"
expectedErr := "uuid: cannot convert int to UUID"
if err.Error() != expectedErr {
t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr)
}
}

// scan error
err = id.Scan(123)
func TestUUIDVersionErrors(t *testing.T) {
// UUId V1 Version
id := FromStringOrNil("e86160d3-beff-443c-b9b5-1f8197ccb12e")
_, err := TimestampFromV1(id)
if err == nil {
t.Error("expected an error")
return
}
expectedErr = "uuid: cannot convert int to UUID"
expectedErr := "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 1"
if err.Error() != expectedErr {
t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr)
}

// UUId V1 Version
// UUId V2 Version
id = FromStringOrNil("e86160d3-beff-443c-b9b5-1f8197ccb12e")
_, err = TimestampFromV1(id)
_, err = TimestampFromV6(id)
if err == nil {
t.Error("expected an error")
return
}
expectedErr = "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 1"
expectedErr = "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 6"
if err.Error() != expectedErr {
t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr)
}

// UUId V2 Version
// UUId V7 Version
id = FromStringOrNil("e86160d3-beff-443c-b9b5-1f8197ccb12e")
_, err = TimestampFromV6(id)
_, err = TimestampFromV7(id)
if err == nil {
t.Error("expected an error")
return
}
expectedErr = "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 6"
expectedErr = "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 7"
if err.Error() != expectedErr {
t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr)
}
}

// UUId V7 Version
id = FromStringOrNil("e86160d3-beff-443c-b9b5-1f8197ccb12e")
_, err = TimestampFromV7(id)
// This test cannot be run in parallel with other tests since it modifies the
// global state
func TestErrNoHwAddressFound(t *testing.T) {
netInterfaces = func() ([]net.Interface, error) {
return nil, nil
}
defer func() {
netInterfaces = net.Interfaces
}()
_, err := defaultHWAddrFunc()
if err == nil {
t.Error("expected an error")
return
}
// There is a "bug" in the error message, this should probably be fixed (6 -> 7)
expectedErr = "uuid: e86160d3-beff-443c-b9b5-1f8197ccb12e is version 4, not version 6"
expectedErr := "uuid: no HW address found"
if err.Error() != expectedErr {
t.Errorf("unexpected error '%s' != '%s'", err.Error(), expectedErr)
}
Expand Down

0 comments on commit fcc3947

Please sign in to comment.