aboutsummaryrefslogtreecommitdiffhomepage
path: root/system/op_internal_test.go
blob: 4d43309eed4d37f9af788a74c93cee2b0bf86d80 (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
package system

import "testing"

type tcOp struct {
	et   Enablement
	path string
}

// test an instance of the Op interface
func (ptc tcOp) test(t *testing.T, gotOps []Op, wantOps []Op, fn string) {
	if len(gotOps) != len(wantOps) {
		t.Errorf("%s: inserted %v Ops, want %v", fn,
			len(gotOps), len(wantOps))
		return
	}

	t.Run("path", func(t *testing.T) {
		if len(gotOps) > 0 {
			if got := gotOps[0].Path(); got != ptc.path {
				t.Errorf("Path() = %q, want %q",
					got, ptc.path)
				return
			}
		}
	})

	for i := range gotOps {
		o := gotOps[i]

		t.Run("is", func(t *testing.T) {
			if !o.Is(o) {
				t.Errorf("Is returned false on self")
				return
			}
			if !o.Is(wantOps[i]) {
				t.Errorf("%s: inserted %#v, want %#v",
					fn,
					o, wantOps[i])
				return
			}
		})

		t.Run("criteria", func(t *testing.T) {
			testCases := []struct {
				name string
				ec   *Criteria
				want bool
			}{
				{"nil", nil, ptc.et != User},
				{"self", newCriteria(ptc.et), true},
				{"all", newCriteria(EWayland | EX11 | EDBus | EPulse | User | Process), true},
				{"enablements", newCriteria(EWayland | EX11 | EDBus | EPulse), ptc.et != User && ptc.et != Process},
			}

			for _, tc := range testCases {
				t.Run(tc.name, func(t *testing.T) {
					if got := tc.ec.hasType(o); got != tc.want {
						t.Errorf("hasType: got %v, want %v",
							got, tc.want)
					}
				})
			}
		})
	}
}

func newCriteria(e Enablement) *Criteria { return (*Criteria)(&e) }