Add tests for funcs in cobra.go (#2094)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Denis · committed by GitHub · parent cbcf75e · Browse files · 1 file·+182 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+182 added−0 removed
- other:+182 added−0 removed
Provenance
- Commit
0dec88e- Tree
e1982bf- Author
Denis <43725617+korovindenis@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
cbcf75e
1 file changed+182 added−0 removed
modified:cobra_test.go+182 added
38 unmodified lines39 if got := c.UsageString(); got != expected {40 t.Errorf("Expected UsageString: %v\nGot: %v", expected, got)41 }42 }43+44+func TestLevenshteinDistance(t *testing.T) {45+ tests := []struct {46+ name string47+ s string48+ t string49+ ignoreCase bool50+ expected int51+ }{52+ {53+ name: "Equal strings (case-sensitive)",54+ s: "hello",55+ t: "hello",56+ ignoreCase: false,57+ expected: 0,58+ },59+ {60+ name: "Equal strings (case-insensitive)",61+ s: "Hello",62+ t: "hello",63+ ignoreCase: true,64+ expected: 0,65+ },66+ {67+ name: "Different strings (case-sensitive)",68+ s: "kitten",69+ t: "sitting",70+ ignoreCase: false,71+ expected: 3,72+ },73+ {74+ name: "Different strings (case-insensitive)",75+ s: "Kitten",76+ t: "Sitting",77+ ignoreCase: true,78+ expected: 3,79+ },80+ {81+ name: "Empty strings",82+ s: "",83+ t: "",84+ ignoreCase: false,85+ expected: 0,86+ },87+ {88+ name: "One empty string",89+ s: "abc",90+ t: "",91+ ignoreCase: false,92+ expected: 3,93+ },94+ {95+ name: "Both empty strings",96+ s: "",97+ t: "",98+ ignoreCase: true,99+ expected: 0,100+ },101+ }102+103+ for _, tt := range tests {104+ t.Run(tt.name, func(t *testing.T) {105+ // Act106+ got := ld(tt.s, tt.t, tt.ignoreCase)107+108+ // Assert109+ if got != tt.expected {110+ t.Errorf("Expected ld: %v\nGot: %v", tt.expected, got)111+ }112+ })113+ }114+}115+116+func TestStringInSlice(t *testing.T) {117+ tests := []struct {118+ name string119+ a string120+ list []string121+ expected bool122+ }{123+ {124+ name: "String in slice (case-sensitive)",125+ a: "apple",126+ list: []string{"orange", "banana", "apple", "grape"},127+ expected: true,128+ },129+ {130+ name: "String not in slice (case-sensitive)",131+ a: "pear",132+ list: []string{"orange", "banana", "apple", "grape"},133+ expected: false,134+ },135+ {136+ name: "String in slice (case-insensitive)",137+ a: "APPLE",138+ list: []string{"orange", "banana", "apple", "grape"},139+ expected: false,140+ },141+ {142+ name: "Empty slice",143+ a: "apple",144+ list: []string{},145+ expected: false,146+ },147+ {148+ name: "Empty string",149+ a: "",150+ list: []string{"orange", "banana", "apple", "grape"},151+ expected: false,152+ },153+ {154+ name: "Empty strings match",155+ a: "",156+ list: []string{"orange", ""},157+ expected: true,158+ },159+ {160+ name: "Empty string in empty slice",161+ a: "",162+ list: []string{},163+ expected: false,164+ },165+ }166+167+ for _, tt := range tests {168+ t.Run(tt.name, func(t *testing.T) {169+ // Act170+ got := stringInSlice(tt.a, tt.list)171+172+ // Assert173+ if got != tt.expected {174+ t.Errorf("Expected stringInSlice: %v\nGot: %v", tt.expected, got)175+ }176+ })177+ }178+}179+180+func TestRpad(t *testing.T) {181+ tests := []struct {182+ name string183+ inputString string184+ padding int185+ expected string186+ }{187+ {188+ name: "Padding required",189+ inputString: "Hello",190+ padding: 10,191+ expected: "Hello ",192+ },193+ {194+ name: "No padding required",195+ inputString: "World",196+ padding: 5,197+ expected: "World",198+ },199+ {200+ name: "Empty string",201+ inputString: "",202+ padding: 8,203+ expected: " ",204+ },205+ {206+ name: "Zero padding",207+ inputString: "cobra",208+ padding: 0,209+ expected: "cobra",210+ },211+ }212+213+ for _, tt := range tests {214+ t.Run(tt.name, func(t *testing.T) {215+ // Act216+ got := rpad(tt.inputString, tt.padding)217+218+ // Assert219+ if got != tt.expected {220+ t.Errorf("Expected rpad: %v\nGot: %v", tt.expected, got)221+ }222+ })223+ }224+}