-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstracts actions into its own file plus adds a lot of testing
- Loading branch information
Showing
5 changed files
with
990 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package app | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestParseArgsInternal(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
expHelp bool | ||
expVersion bool | ||
expCmds []string | ||
}{ | ||
{ | ||
name: "No args", | ||
args: []string{}, | ||
expHelp: true, | ||
expVersion: false, | ||
expCmds: []string{}, | ||
}, | ||
{ | ||
name: "Only help", | ||
args: []string{"--help"}, | ||
expHelp: true, | ||
expVersion: false, | ||
expCmds: []string{}, | ||
}, | ||
{ | ||
name: "Only version", | ||
args: []string{"--version"}, | ||
expHelp: false, | ||
expVersion: true, | ||
expCmds: []string{}, | ||
}, | ||
{ | ||
name: "Help and version", | ||
args: []string{"--help", "--version"}, | ||
expHelp: true, | ||
expVersion: true, | ||
expCmds: []string{}, | ||
}, | ||
{ | ||
name: "Commands only", | ||
args: []string{"init", "config", "edit"}, | ||
expHelp: false, | ||
expVersion: false, | ||
expCmds: []string{"init", "config", "edit"}, | ||
}, | ||
{ | ||
name: "Commands with help", | ||
args: []string{"init", "config", "--help"}, | ||
expHelp: true, | ||
expVersion: false, | ||
expCmds: []string{"init", "config"}, | ||
}, | ||
{ | ||
name: "Commands with version", | ||
args: []string{"init", "config", "--version"}, | ||
expHelp: false, | ||
expVersion: true, | ||
expCmds: []string{"init", "config"}, | ||
}, | ||
{ | ||
name: "Commands with help and version", | ||
args: []string{"init", "--help", "config", "--version"}, | ||
expHelp: true, | ||
expVersion: true, | ||
expCmds: []string{"init", "config"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
hasHelp, hasVersion, commands, _ := parseArgsInternal(tt.args) | ||
if hasHelp != tt.expHelp { | ||
t.Errorf("expected hasHelp=%v, got %v", tt.expHelp, hasHelp) | ||
} | ||
if hasVersion != tt.expVersion { | ||
t.Errorf("expected hasVersion=%v, got %v", tt.expVersion, hasVersion) | ||
} | ||
if !reflect.DeepEqual(commands, tt.expCmds) { | ||
t.Errorf("expected commands=%v, got %v", tt.expCmds, commands) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCleanArgs(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
expectErr bool | ||
expected []string | ||
}{ | ||
{ | ||
name: "No args", | ||
args: []string{"./program"}, | ||
expectErr: false, | ||
expected: []string{"./program", "help"}, | ||
}, | ||
{ | ||
name: "Help flag", | ||
args: []string{"./program", "--help"}, | ||
expectErr: false, | ||
expected: []string{"./program", "help"}, | ||
}, | ||
{ | ||
name: "Version flag", | ||
args: []string{"./program", "--version"}, | ||
expectErr: false, | ||
expected: []string{"./program", "version"}, | ||
}, | ||
{ | ||
name: "Help and command", | ||
args: []string{"./program", "--help", "init"}, | ||
expectErr: false, | ||
expected: []string{"./program", "help", "init"}, | ||
}, | ||
{ | ||
name: "Commands only", | ||
args: []string{"./program", "init", "config"}, | ||
expectErr: true, | ||
expected: []string{"./program", "init", "config"}, | ||
}, | ||
{ | ||
name: "Help and version", | ||
args: []string{"./program", "--help", "--version"}, | ||
expectErr: false, | ||
expected: []string{"./program", "help"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := cleanArgs(tt.args) | ||
if !reflect.DeepEqual(result, tt.expected) { | ||
t.Errorf("expected %v, got %v", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.