package rags import ( "flag" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestRag(t *testing.T) { t.Parallel() // Test the generic implementation using Bool tcs := []struct { f *Rag set string }{ { &Rag{Name: "0-def-value", Value: &Bool{}}, "true", }, { &Rag{Name: "short-true", Value: &Bool{Default: true}, Usage: "use me :plead:"}, "false", }, { &Rag{Name: "short-var-true", Value: NewValueDefault(new(bool), true), Usage: "use me :plead:"}, "true", }, { &Rag{Name: "short-var", Value: NewValue(new(bool)), Usage: "use me :plead:"}, "true", }, } for _, tc := range tcs { tc := tc t.Run(tc.f.Name, func(t *testing.T) { t.Parallel() var ( a = assert.New(t) rs = New(t.Name(), flag.ContinueOnError) ) a.NotPanics(func() { rs.Add(tc.f) }) require.Len(t, rs.rags, 1) f := rs.rags[0] a.NotNil(f) fval := f.Value a.NotNil(fval) a.IsType(tc.f.Value.Get(), fval.Get()) a.Equal(tc.f.Value.Get(), fval.Get()) a.NoError(fval.Set(tc.set)) // Flag value reflects expected string a.Equal(tc.set, fval.String()) }) } } func TestCategories(t *testing.T) { t.Parallel() t.Run("normalization", func(t *testing.T) { tcs := map[string]struct { input string exp string }{ "unchanged": {"category", "category"}, "lower case": {"Category", "category"}, "upper case flags with colon": {"BOO FLAGS:", "boo"}, "flags with colon": {"BOO flags:", "boo"}, "flags": {"BOO flags", "boo"}, "upper case flags": {"BOO FLAGS", "boo"}, "trailing whitespace": {"BOO FLAGS: ", "boo"}, "leading whitespace": {" hOo bOO", "hoo boo"}, "colon": {"boo HOO:", "boo hoo"}, } for name, tc := range tcs { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() assert.Equal(t, tc.exp, normalizeCategory(tc.input)) }) } }) t.Run("sort", func(t *testing.T) { t.Parallel() tcs := []struct { input []string exp []string }{ { []string{"uncategorized", "bar", "foo"}, []string{"uncategorized", "bar", "foo"}, }, { []string{"uncategorized", "foo", "bar"}, []string{"uncategorized", "bar", "foo"}, }, { []string{"foo", "bar", "uncategorized"}, []string{"uncategorized", "bar", "foo"}, }, { []string{"foo", "uncategorized", "bar"}, []string{"uncategorized", "bar", "foo"}, }, { []string{"", "bar", "foo"}, []string{"", "bar", "foo"}, }, { []string{"", "foo", "bar"}, []string{"", "bar", "foo"}, }, { []string{"foo", "bar", ""}, []string{"", "bar", "foo"}, }, { []string{"foo", "", "bar"}, []string{"", "bar", "foo"}, }, { []string{"uncategorized", "bar", "foo", "global"}, []string{"uncategorized", "bar", "foo", "global"}, }, { []string{"uncategorized", "global", "foo", "bar"}, []string{"uncategorized", "bar", "foo", "global"}, }, { []string{"global", "foo", "bar", "uncategorized"}, []string{"uncategorized", "bar", "foo", "global"}, }, } for _, tc := range tcs { sortCategories(tc.input) assert.Equal(t, tc.exp, tc.input) } }) }