github.com/koko1123/flow-go-1@v0.29.6/network/channels/channel_test.go (about)

     1  package channels_test
     2  
     3  import (
     4  	"regexp"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/koko1123/flow-go-1/network/channels"
    10  )
    11  
    12  // TestChannelList_Contain tests that the channel list Contain method returns true if the channel
    13  // is in the list.
    14  func TestChannelList_Contain(t *testing.T) {
    15  	list := channels.ChannelList{"a", "b", "c"}
    16  	require.True(t, list.Contains("a"))
    17  	require.True(t, list.Contains("b"))
    18  	require.True(t, list.Contains("c"))
    19  	require.Equal(t, 3, len(list))
    20  
    21  	require.False(t, list.Contains("d"))
    22  }
    23  
    24  // TestChannelList_ExcludeChannels tests that the channel list ExcludeChannels method returns a new channel list
    25  // with the excluded channels removed.
    26  func TestChannelList_ExcludeChannels(t *testing.T) {
    27  	list := channels.ChannelList{"a", "b", "c"}
    28  	require.ElementsMatch(t, channels.ChannelList{"a", "b"}, list.ExcludeChannels(channels.ChannelList{"c"}))
    29  	require.ElementsMatch(t, channels.ChannelList{"a", "c"}, list.ExcludeChannels(channels.ChannelList{"b"}))
    30  	require.ElementsMatch(t, channels.ChannelList{"b", "c"}, list.ExcludeChannels(channels.ChannelList{"a"}))
    31  	require.ElementsMatch(t, channels.ChannelList{"a", "b", "c"}, list.ExcludeChannels(channels.ChannelList{"d"}))
    32  	require.Empty(t, list.ExcludeChannels(channels.ChannelList{"a", "b", "c"}))
    33  }
    34  
    35  // TestChannelList_ExcludePattern tests that the channel list ExcludePattern method returns a new channel list
    36  // with the filtered channels.
    37  func TestChannelList_ExcludePattern(t *testing.T) {
    38  	list := channels.ChannelList{"test-a", "test-b", "c", "d"}
    39  	require.ElementsMatch(t, channels.ChannelList{"c", "d"}, list.ExcludePattern(regexp.MustCompile("^(test).*")))
    40  	require.ElementsMatch(t, channels.ChannelList{"test-a", "test-b"}, list.ExcludePattern(regexp.MustCompile("^[cd].*")))
    41  }