aboutsummaryrefslogtreecommitdiffstats
path: root/errors_test.go
blob: fb739a0cf5d01f7c1342b563916cef284fe1b995 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package envelope

import (
	"errors"
	"reflect"
	"testing"

	"git.sr.ht/~eugenetriguba/checkmate/assert"
	"git.sr.ht/~eugenetriguba/checkmate/check"
)

// GIVEN a BaseError
// WHEN we call Error on it
// THEN it should return the error message if there's no underlying error
// or a formatted message including the underlying error when one exists
func TestBaseError(t *testing.T) {
	err := BaseError{
		Message: "test error",
		Err:     nil,
	}
	check.Equal(t, err.Error(), err.Message)

	err.Err = errors.New("underlying error")
	check.Equal(t, err.Error(), "test error: underlying error")

}

// GIVEN a NotPointerToStructError
// WHEN we create it with a non-pointer type
// THEN it should:
//   - Return an error message indicating the value must be a pointer to a struct
//   - Be identifiable via errors.As
//   - Contain the actual type that was provided
func TestNotPointerToStructError(t *testing.T) {
	var s string
	err := newNotPointerToStructError(reflect.TypeOf(s))
	assert.Equal(t, "value must be a pointer to a struct", err.Error())

	var notPtrErr *NotPointerToStructError
	assert.NotNil(t, notPtrErr)
	assert.True(t, errors.As(err, &notPtrErr))
	assert.Equal(t, reflect.TypeOf(s), notPtrErr.ActualType)
}

// GIVEN an UnsupportedTypeError
// WHEN we create it with a field name and type
// THEN it should:
//   - Return an error message indicating the field type is unsupported
//   - Be identifiable via errors.As
//   - Contain the field name and type that caused the error
func TestUnsupportedTypeError(t *testing.T) {
	err := newUnsupportedTypeError("TestField", reflect.TypeOf(complex128(0)))
	assert.Equal(t, "unsupported field type for field TestField", err.Error())

	var typeErr *UnsupportedTypeError
	assert.True(t, errors.As(err, &typeErr))
	assert.Equal(t, "TestField", typeErr.FieldName)
	assert.Equal(t, reflect.TypeOf(complex128(0)), typeErr.FieldType)
}

// GIVEN a ParseError created with field information and an underlying error
// WHEN we examine its properties and behavior
// THEN it should:
//   - Contain the correct error message with field and environment variable context
//   - Wrap the original error properly
//   - Provide access to its field name, environment variable name, value, and target type
func TestParseError(t *testing.T) {
	originalErr := errors.New("test error")
	err := newParseError("TestField", "TEST_ENV", "invalid", reflect.TypeOf(int(0)), originalErr)
	assert.ErrorContains(t, err, "error parsing value 'invalid' to 'int' for field 'TestField' from environment variable 'TEST_ENV'")

	var parseErr *ParseError
	assert.True(t, errors.As(err, &parseErr))
	assert.Equal(t, "TestField", parseErr.FieldName)
	assert.Equal(t, "TEST_ENV", parseErr.EnvName)
	assert.Equal(t, "invalid", parseErr.Value)
	assert.Equal(t, reflect.TypeOf(int(0)), parseErr.TargetType)

	assert.True(t, errors.Is(err, originalErr))
}