summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfstree/btree_err_test.go
blob: 381bd560c20a51fccb075edf81b5d178341ab6fe (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
// Copyright (C) 2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package btrfstree_test

import (
	"errors"
	"fmt"
	iofs "io/fs"
	"testing"

	"github.com/stretchr/testify/assert"

	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfstree"
)

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

	errItem := fmt.Errorf("my item: %w", btrfstree.ErrNoItem)
	errTree := fmt.Errorf("my tree: %w", btrfstree.ErrNoTree)

	// 1. errItem
	// 2. errTree
	// 3. btrfstree.ErrNoItem
	// 4. btrfstree.ErrNoTree
	// 5. iofs.ErrNotExist

	// 1
	assert.Equal(t, errors.Is(errItem, errItem), true)
	assert.Equal(t, errors.Is(errItem, errTree), false)
	assert.Equal(t, errors.Is(errItem, btrfstree.ErrNoItem), true)
	assert.Equal(t, errors.Is(errItem, btrfstree.ErrNoTree), false)
	assert.Equal(t, errors.Is(errItem, iofs.ErrNotExist), true)

	// 2
	assert.Equal(t, errors.Is(errTree, errItem), false)
	assert.Equal(t, errors.Is(errTree, errTree), true)
	assert.Equal(t, errors.Is(errTree, btrfstree.ErrNoItem), false)
	assert.Equal(t, errors.Is(errTree, btrfstree.ErrNoTree), true)
	assert.Equal(t, errors.Is(errTree, iofs.ErrNotExist), true)

	// 3
	assert.Equal(t, errors.Is(btrfstree.ErrNoItem, errItem), false)
	assert.Equal(t, errors.Is(btrfstree.ErrNoItem, errTree), false)
	assert.Equal(t, errors.Is(btrfstree.ErrNoItem, btrfstree.ErrNoItem), true)
	assert.Equal(t, errors.Is(btrfstree.ErrNoItem, btrfstree.ErrNoTree), false)
	assert.Equal(t, errors.Is(btrfstree.ErrNoItem, iofs.ErrNotExist), true)

	// 4
	assert.Equal(t, errors.Is(btrfstree.ErrNoTree, errItem), false)
	assert.Equal(t, errors.Is(btrfstree.ErrNoTree, errTree), false)
	assert.Equal(t, errors.Is(btrfstree.ErrNoTree, btrfstree.ErrNoItem), false)
	assert.Equal(t, errors.Is(btrfstree.ErrNoTree, btrfstree.ErrNoTree), true)
	assert.Equal(t, errors.Is(btrfstree.ErrNoTree, iofs.ErrNotExist), true)

	// 5
	assert.Equal(t, errors.Is(iofs.ErrNotExist, errItem), false)
	assert.Equal(t, errors.Is(iofs.ErrNotExist, errTree), false)
	assert.Equal(t, errors.Is(iofs.ErrNotExist, btrfstree.ErrNoItem), false)
	assert.Equal(t, errors.Is(iofs.ErrNotExist, btrfstree.ErrNoTree), false)
	assert.Equal(t, errors.Is(iofs.ErrNotExist, iofs.ErrNotExist), true)
}