...

Source file src/edge-infra.dev/pkg/lib/cli/rags/rag_test.go

Documentation: edge-infra.dev/pkg/lib/cli/rags

     1  package rags
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestRag(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	// Test the generic implementation using Bool
    15  
    16  	tcs := []struct {
    17  		f   *Rag
    18  		set string
    19  	}{
    20  		{
    21  			&Rag{Name: "0-def-value", Value: &Bool{}},
    22  			"true",
    23  		},
    24  		{
    25  			&Rag{Name: "short-true", Value: &Bool{Default: true}, Usage: "use me :plead:"},
    26  			"false",
    27  		},
    28  		{
    29  			&Rag{Name: "short-var-true", Value: NewValueDefault(new(bool), true), Usage: "use me :plead:"},
    30  			"true",
    31  		},
    32  		{
    33  			&Rag{Name: "short-var", Value: NewValue(new(bool)), Usage: "use me :plead:"},
    34  			"true",
    35  		},
    36  	}
    37  
    38  	for _, tc := range tcs {
    39  		tc := tc
    40  		t.Run(tc.f.Name, func(t *testing.T) {
    41  			t.Parallel()
    42  
    43  			var (
    44  				a  = assert.New(t)
    45  				rs = New(t.Name(), flag.ContinueOnError)
    46  			)
    47  
    48  			a.NotPanics(func() { rs.Add(tc.f) })
    49  			require.Len(t, rs.rags, 1)
    50  			f := rs.rags[0]
    51  			a.NotNil(f)
    52  
    53  			fval := f.Value
    54  			a.NotNil(fval)
    55  			a.IsType(tc.f.Value.Get(), fval.Get())
    56  			a.Equal(tc.f.Value.Get(), fval.Get())
    57  
    58  			a.NoError(fval.Set(tc.set))
    59  			// Flag value reflects expected string
    60  			a.Equal(tc.set, fval.String())
    61  		})
    62  	}
    63  }
    64  
    65  func TestCategories(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	t.Run("normalization", func(t *testing.T) {
    69  		tcs := map[string]struct {
    70  			input string
    71  			exp   string
    72  		}{
    73  			"unchanged":                   {"category", "category"},
    74  			"lower case":                  {"Category", "category"},
    75  			"upper case flags with colon": {"BOO FLAGS:", "boo"},
    76  			"flags with colon":            {"BOO flags:", "boo"},
    77  			"flags":                       {"BOO flags", "boo"},
    78  			"upper case flags":            {"BOO FLAGS", "boo"},
    79  			"trailing whitespace":         {"BOO FLAGS:   ", "boo"},
    80  			"leading whitespace":          {"    hOo bOO", "hoo boo"},
    81  			"colon":                       {"boo HOO:", "boo hoo"},
    82  		}
    83  
    84  		for name, tc := range tcs {
    85  			tc := tc
    86  			t.Run(name, func(t *testing.T) {
    87  				t.Parallel()
    88  
    89  				assert.Equal(t, tc.exp, normalizeCategory(tc.input))
    90  			})
    91  		}
    92  	})
    93  
    94  	t.Run("sort", func(t *testing.T) {
    95  		t.Parallel()
    96  
    97  		tcs := []struct {
    98  			input []string
    99  			exp   []string
   100  		}{
   101  			{
   102  				[]string{"uncategorized", "bar", "foo"},
   103  				[]string{"uncategorized", "bar", "foo"},
   104  			},
   105  			{
   106  				[]string{"uncategorized", "foo", "bar"},
   107  				[]string{"uncategorized", "bar", "foo"},
   108  			},
   109  			{
   110  				[]string{"foo", "bar", "uncategorized"},
   111  				[]string{"uncategorized", "bar", "foo"},
   112  			},
   113  			{
   114  				[]string{"foo", "uncategorized", "bar"},
   115  				[]string{"uncategorized", "bar", "foo"},
   116  			},
   117  			{
   118  				[]string{"", "bar", "foo"},
   119  				[]string{"", "bar", "foo"},
   120  			},
   121  			{
   122  				[]string{"", "foo", "bar"},
   123  				[]string{"", "bar", "foo"},
   124  			},
   125  			{
   126  				[]string{"foo", "bar", ""},
   127  				[]string{"", "bar", "foo"},
   128  			},
   129  			{
   130  				[]string{"foo", "", "bar"},
   131  				[]string{"", "bar", "foo"},
   132  			},
   133  			{
   134  				[]string{"uncategorized", "bar", "foo", "global"},
   135  				[]string{"uncategorized", "bar", "foo", "global"},
   136  			},
   137  			{
   138  				[]string{"uncategorized", "global", "foo", "bar"},
   139  				[]string{"uncategorized", "bar", "foo", "global"},
   140  			},
   141  			{
   142  				[]string{"global", "foo", "bar", "uncategorized"},
   143  				[]string{"uncategorized", "bar", "foo", "global"},
   144  			},
   145  		}
   146  
   147  		for _, tc := range tcs {
   148  			sortCategories(tc.input)
   149  			assert.Equal(t, tc.exp, tc.input)
   150  		}
   151  	})
   152  }
   153  

View as plain text