github.com/emitter-io/go/v2@v2.1.0/subtrie_test.go (about)

     1  package emitter
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestRoute(t *testing.T) {
    10  	query, _ := newRoute("a/", nil)
    11  	assert.Len(t, query, 1)
    12  }
    13  
    14  func TestTrieMatch(t *testing.T) {
    15  	m := NewTrie()
    16  	testPopulateWithStrings(m, []string{
    17  		"a/",
    18  		"a/b/c/",
    19  		"a/+/c/",
    20  		"a/b/c/d/",
    21  		"a/+/c/+/",
    22  		"x/",
    23  		"x/y/",
    24  		"x/+/z",
    25  	})
    26  
    27  	// Tests to run
    28  	tests := []struct {
    29  		topic string
    30  		n     int
    31  	}{
    32  		{topic: "a/", n: 1},
    33  		{topic: "a/1/", n: 1},
    34  		{topic: "a/2/", n: 1},
    35  		{topic: "a/1/2/", n: 1},
    36  		{topic: "a/1/2/3/", n: 1},
    37  		{topic: "a/x/y/c/", n: 1},
    38  		{topic: "a/x/c/", n: 2},
    39  		{topic: "a/b/c/", n: 3},
    40  		{topic: "a/b/c/d/", n: 5},
    41  		{topic: "a/b/c/e/", n: 4},
    42  		{topic: "x/y/c/e/", n: 2},
    43  	}
    44  
    45  	for _, tc := range tests {
    46  		result := m.Lookup(tc.topic)
    47  		assert.Equal(t, tc.n, len(result))
    48  	}
    49  }
    50  
    51  // Populates the trie with a set of strings
    52  func testPopulateWithStrings(m *trie, values []string) {
    53  	for _, s := range values {
    54  		m.AddHandler(s, func(_ *Client, _ Message) {
    55  			println("dummy handler")
    56  		})
    57  	}
    58  }