errors_test.go

52 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
package platform

import (
	"errors"
	"testing"
)

func TestErrorsAreDistinct(t *testing.T) {
	errs := []error{ErrNotFound, ErrTimeout, ErrUnsupportedRegion, ErrUnsupportedSize}

	for i := 0; i < len(errs); i++ {
		for j := i + 1; j < len(errs); j++ {
			if errors.Is(errs[i], errs[j]) {
				t.Errorf("expected %v and %v to be distinct errors", errs[i], errs[j])
			}
		}
	}
}

func TestErrorMessages(t *testing.T) {
	tests := []struct {
		err  error
		want string
	}{
		{ErrNotFound, "resource not found"},
		{ErrTimeout, "operation timed out"},
		{ErrUnsupportedRegion, "unsupported region"},
		{ErrUnsupportedSize, "unsupported size"},
	}

	for _, tt := range tests {
		if tt.err.Error() != tt.want {
			t.Errorf("error message = %q, want %q", tt.err.Error(), tt.want)
		}
	}
}

func TestErrorsMatchWithIs(t *testing.T) {
	// Verify errors.Is works for identity comparison
	if !errors.Is(ErrNotFound, ErrNotFound) {
		t.Error("ErrNotFound should match itself with errors.Is")
	}
	if !errors.Is(ErrTimeout, ErrTimeout) {
		t.Error("ErrTimeout should match itself with errors.Is")
	}
	if !errors.Is(ErrUnsupportedRegion, ErrUnsupportedRegion) {
		t.Error("ErrUnsupportedRegion should match itself with errors.Is")
	}
	if !errors.Is(ErrUnsupportedSize, ErrUnsupportedSize) {
		t.Error("ErrUnsupportedSize should match itself with errors.Is")
	}
}