-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum_bitflags_test.go
More file actions
73 lines (58 loc) · 1.48 KB
/
Copy pathenum_bitflags_test.go
File metadata and controls
73 lines (58 loc) · 1.48 KB
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
package enum
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type eTestBitflag struct {
BitflagEnumImpl[uint16, TestBitflag, eTestBitflag]
}
var ETestBitFlag = eTestBitflag{}
func (e eTestBitflag) Foo() TestBitflag {
return e.FromRawValue(1)
}
func (e eTestBitflag) Bar() TestBitflag {
return e.FromRawValue(1 << 1)
}
func (e eTestBitflag) Baz() TestBitflag {
return e.FromRawValue(1 << 2)
}
type TestBitflag struct {
BitflagImpl[uint16, TestBitflag, eTestBitflag]
}
func (t TestBitflag) String() string {
return ETestBitFlag.String(t)
}
func TestBitflagImpl(t *testing.T) {
a := assert.New(t)
foo := ETestBitFlag.Foo()
bar := ETestBitFlag.Bar()
baz := ETestBitFlag.Baz()
fooBaz := foo.Add(baz)
a.Equal(uint16(0b101), fooBaz.Value()) // validate basic operations
a.True(fooBaz.Contains(foo))
a.True(fooBaz.Contains(baz))
a.False(fooBaz.Contains(bar))
a.False(fooBaz.Add(bar).Remove(baz).Contains(baz))
a.True(fooBaz.Add(bar).Remove(baz).Contains(bar))
parsed, err := ETestBitFlag.Parse(ETestBitFlag.String(fooBaz), true)
a.NoError(err)
a.Equal(fooBaz.Value(), parsed.Value())
a.True(strings.Contains(fooBaz.String(), ","))
a.True(strings.Contains(ETestBitFlag.String(fooBaz), ","))
a.Equal(foo, foo)
a.NotEqual(foo, bar)
a.True(foo == foo)
a.False(foo == bar)
switch foo.Add(baz) {
case baz:
a.Fail("triggered baz")
case foo:
a.Fail("triggered foo")
case bar:
a.Fail("triggered bar")
case foo.Add(baz):
default:
a.Fail("defaulted")
}
}