126 lines
2.9 KiB
Go
Executable File
126 lines
2.9 KiB
Go
Executable File
package x_test
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"git.sdf.org/jchenry/x"
|
|
)
|
|
|
|
func TestCheck(t *testing.T) {
|
|
t.Run("single failing check", func(t *testing.T) {
|
|
testErr := errors.New("test error")
|
|
err := x.Check(false, testErr).Join()
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !errors.Is(err, testErr) {
|
|
t.Fatalf("expected error to contain %v, got %v", testErr, err)
|
|
}
|
|
})
|
|
|
|
t.Run("single passing check", func(t *testing.T) {
|
|
err := x.Check(true, errors.New("should not appear")).Join()
|
|
if err != nil {
|
|
t.Fatalf("expected nil, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("multiple checks", func(t *testing.T) {
|
|
err1 := errors.New("error 1")
|
|
err2 := errors.New("error 2")
|
|
err := x.Check(false, err1).Check(false, err2).Join()
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
if !errors.Is(err, err1) {
|
|
t.Errorf("expected error to contain %v", err1)
|
|
}
|
|
if !errors.Is(err, err2) {
|
|
t.Errorf("expected error to contain %v", err2)
|
|
}
|
|
})
|
|
|
|
t.Run("First method", func(t *testing.T) {
|
|
err1 := errors.New("first error")
|
|
err2 := errors.New("second error")
|
|
err := x.Check(false, err1).Check(false, err2).First()
|
|
if err != err1 {
|
|
t.Fatalf("expected first error %v, got %v", err1, err)
|
|
}
|
|
})
|
|
|
|
t.Run("All method", func(t *testing.T) {
|
|
err1 := errors.New("error 1")
|
|
err2 := errors.New("error 2")
|
|
errs := x.Check(false, err1).Check(false, err2).All()
|
|
if len(errs) != 2 {
|
|
t.Fatalf("expected 2 errors, got %d", len(errs))
|
|
}
|
|
if errs[0] != err1 {
|
|
t.Errorf("expected first error %v, got %v", err1, errs[0])
|
|
}
|
|
if errs[1] != err2 {
|
|
t.Errorf("expected second error %v, got %v", err2, errs[1])
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestAssert(t *testing.T) {
|
|
t.Run("passing assertion", func(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Fatalf("expected no panic, got %v", r)
|
|
}
|
|
}()
|
|
x.Assert(true, "should not panic")
|
|
})
|
|
|
|
t.Run("failing assertion", func(t *testing.T) {
|
|
defer func() {
|
|
if r := recover(); r == nil {
|
|
t.Fatal("expected panic, got none")
|
|
}
|
|
}()
|
|
x.Assert(false, "test panic")
|
|
})
|
|
}
|
|
|
|
func TestNewError(t *testing.T) {
|
|
t.Run("with debug enabled", func(t *testing.T) {
|
|
original := errors.New("original error")
|
|
err := x.NewError(original, true)
|
|
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
|
|
errStr := err.Error()
|
|
if errStr == "original error" {
|
|
t.Error("expected error string to include file and line info")
|
|
}
|
|
|
|
if !errors.Is(err, original) {
|
|
t.Error("expected error to unwrap to original")
|
|
}
|
|
})
|
|
|
|
t.Run("with debug disabled", func(t *testing.T) {
|
|
original := errors.New("original error")
|
|
err := x.NewError(original, false)
|
|
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
|
|
errStr := err.Error()
|
|
if errStr != "original error" {
|
|
t.Errorf("expected error string to be 'original error', got %q", errStr)
|
|
}
|
|
|
|
if !errors.Is(err, original) {
|
|
t.Error("expected error to unwrap to original")
|
|
}
|
|
})
|
|
}
|