Skip to content
Snippets Groups Projects
community_test.go 1.51 KiB
Newer Older
  • Learn to ignore specific revisions
  • package packet
    
    import (
    	"testing"
    
    	"github.com/stretchr/testify/assert"
    )
    
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    func TestCommunityStringFromUint32(t *testing.T) {
    
    	tests := []struct {
    		name     string
    		value    uint32
    		expected string
    	}{
    		{
    			name:     "both elements",
    			value:    131080,
    			expected: "(2,8)",
    		},
    		{
    			name:     "right element only",
    			value:    250,
    			expected: "(0,250)",
    		},
    		{
    			name:     "left element only",
    			value:    131072,
    			expected: "(2,0)",
    		},
    	}
    
    	for _, test := range tests {
    		t.Run(test.name, func(te *testing.T) {
    
    Daniel Czerwonk's avatar
    Daniel Czerwonk committed
    			assert.Equal(te, test.expected, CommunityStringForUint32(test.value))
    
    		})
    	}
    }
    
    func TestParseCommunityString(t *testing.T) {
    	tests := []struct {
    		name     string
    		value    string
    		expected uint32
    		wantFail bool
    	}{
    		{
    			name:     "both elements",
    			expected: 131080,
    			value:    "(2,8)",
    		},
    		{
    			name:     "right element only",
    			expected: 250,
    			value:    "(0,250)",
    		},
    		{
    			name:     "left element only",
    			expected: 131072,
    			value:    "(2,0)",
    		},
    		{
    			name:     "too big",
    			value:    "(131072,256)",
    			wantFail: true,
    		},
    		{
    			name:     "empty string",
    			value:    "",
    			wantFail: true,
    		},
    		{
    			name:     "random string",
    			value:    "foo-bar",
    			wantFail: true,
    		},
    	}
    
    	for _, test := range tests {
    		t.Run(test.name, func(te *testing.T) {
    			c, err := ParseCommunityString(test.value)
    
    			if test.wantFail {
    				if err == nil {
    					te.Fatal("test was expected to fail, but did not")
    				}
    
    				return
    			}
    
    			assert.Equal(te, test.expected, c)
    		})
    	}
    }