aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/uevent/uevent_test.go
blob: 9af3dcdd8a8ade124e417439aa77f6544b4843ee (plain)
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
81
82
83
package uevent_test

import (
	"encoding"
	"fmt"
	"reflect"
	"testing"

	"hakurei.app/internal/uevent"
)

// adeT sets up a parallel subtest for a textual appender/decoder/encoder.
func adeT[V any, S interface {
	encoding.TextAppender
	encoding.TextMarshaler
	encoding.TextUnmarshaler
	fmt.Stringer

	*V
}](t *testing.T, name string, v V, want string, wantErr, wantErrE error) {
	t.Helper()
	t.Run(name, func(t *testing.T) {
		t.Parallel()
		t.Helper()

		t.Run("decode", func(t *testing.T) {
			t.Parallel()
			t.Helper()

			var got V
			if err := S(&got).UnmarshalText([]byte(want)); !reflect.DeepEqual(err, wantErr) {
				t.Fatalf("UnmarshalText: error = %v, want %v", err, wantErr)
			}
			if wantErr != nil {
				return
			}

			if !reflect.DeepEqual(&got, &v) {
				t.Errorf("UnmarshalText: %#v, want %#v", got, v)
			}
		})

		t.Run("encode", func(t *testing.T) {
			t.Parallel()
			t.Helper()

			if got, err := S(&v).MarshalText(); !reflect.DeepEqual(err, wantErrE) {
				t.Fatalf("MarshalText: error = %v, want %v", err, wantErrE)
			} else if err == nil && string(got) != want {
				t.Errorf("MarshalText: %q, want %q", string(got), want)
			}
			if wantErrE != nil {
				return
			}

			if got := S(&v).String(); got != want {
				t.Errorf("String: %q, want %q", got, want)
			}
		})
	})
}

func TestErrors(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name string
		err  error
		want string
	}{
		{"UnsupportedActionError", uevent.UnsupportedActionError("explode"),
			`unsupported kobject_action "explode"`},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			if got := tc.err.Error(); got != tc.want {
				t.Errorf("Error: %q, want %q", got, tc.want)
			}
		})
	}
}