wire: add HasFlag method

This commit is contained in:
Calvin Kim 2023-11-07 10:57:45 +09:00
parent 1012f1e4ba
commit a09e7b224a
2 changed files with 26 additions and 1 deletions

View File

@ -126,6 +126,11 @@ var orderedSFStrings = []ServiceFlag{
SFNodeNetworkLimited,
}
// HasFlag returns a bool indicating if the service has the given flag.
func (f ServiceFlag) HasFlag(s ServiceFlag) bool {
return f&s == s
}
// String returns the ServiceFlag in human-readable form.
func (f ServiceFlag) String() string {
// No flags are set.

View File

@ -4,7 +4,11 @@
package wire
import "testing"
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestServiceFlagStringer tests the stringized output for service flag types.
func TestServiceFlagStringer(t *testing.T) {
@ -59,3 +63,19 @@ func TestBitcoinNetStringer(t *testing.T) {
}
}
}
func TestHasFlag(t *testing.T) {
tests := []struct {
in ServiceFlag
check ServiceFlag
want bool
}{
{0, SFNodeNetwork, false},
{SFNodeNetwork | SFNodeNetworkLimited | SFNodeWitness, SFNodeBloom, false},
{SFNodeNetwork | SFNodeNetworkLimited | SFNodeWitness, SFNodeNetworkLimited, true},
}
for _, test := range tests {
require.Equal(t, test.want, test.in.HasFlag(test.check))
}
}