Search Apps Documentation Source Content File Folder Download Copy Actions Download

permission_set_test.gno

2.01 Kb · 124 lines
  1package boards
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/nt/uassert/v0"
  7)
  8
  9func TestNewPermissionSet(t *testing.T) {
 10	cases := []struct {
 11		name  string
 12		perms []Permission
 13		check []Permission
 14		want  bool
 15	}{
 16		{
 17			name:  "empty",
 18			check: []Permission{0},
 19			want:  false,
 20		},
 21		{
 22			name:  "single permission",
 23			perms: []Permission{0},
 24			check: []Permission{0},
 25			want:  true,
 26		},
 27		{
 28			name:  "multiple permissions",
 29			perms: []Permission{1, 3, 5},
 30			check: []Permission{1, 3, 5},
 31			want:  true,
 32		},
 33		{
 34			name:  "high permission value",
 35			perms: []Permission{100},
 36			check: []Permission{100},
 37			want:  true,
 38		},
 39		{
 40			name:  "missing permission",
 41			perms: []Permission{100},
 42			check: []Permission{0},
 43			want:  false,
 44		},
 45		{
 46			name:  "multiple missing permissions",
 47			perms: []Permission{1, 3, 5},
 48			check: []Permission{0, 2, 4},
 49			want:  false,
 50		},
 51	}
 52
 53	for _, tc := range cases {
 54		t.Run(tc.name, func(t *testing.T) {
 55			s := NewPermissionSet(tc.perms...)
 56
 57			for _, p := range tc.check {
 58				uassert.Equal(t, tc.want, s.Has(p))
 59			}
 60		})
 61	}
 62}
 63
 64func TestPermissionSetHas(t *testing.T) {
 65	cases := []struct {
 66		name  string
 67		set   PermissionSet
 68		check Permission
 69		want  bool
 70	}{
 71		{
 72			name:  "out of range",
 73			set:   NewPermissionSet(0),
 74			check: 100,
 75			want:  false,
 76		},
 77		{
 78			name:  "nil set",
 79			check: 0,
 80			want:  false,
 81		},
 82		{
 83			name:  "permission present",
 84			set:   NewPermissionSet(5),
 85			check: 5,
 86			want:  true,
 87		},
 88	}
 89
 90	for _, tc := range cases {
 91		t.Run(tc.name, func(t *testing.T) {
 92			uassert.Equal(t, tc.want, tc.set.Has(tc.check))
 93		})
 94	}
 95}
 96
 97func TestPermissionSetIsEmpty(t *testing.T) {
 98	cases := []struct {
 99		name string
100		set  PermissionSet
101		want bool
102	}{
103		{
104			name: "nil set",
105			want: true,
106		},
107		{
108			name: "non-empty set",
109			set:  NewPermissionSet(0),
110			want: false,
111		},
112		{
113			name: "empty allocated set",
114			set:  make(PermissionSet, 1),
115			want: true,
116		},
117	}
118
119	for _, tc := range cases {
120		t.Run(tc.name, func(t *testing.T) {
121			uassert.Equal(t, tc.want, tc.set.IsEmpty())
122		})
123	}
124}