diff options
| author | Daniele Sluijters <[email protected]> | 2022-10-05 17:59:23 +0200 |
|---|---|---|
| committer | Daniele Sluijters <[email protected]> | 2022-10-10 17:25:14 +0200 |
| commit | 4fc9666f79d7a9a84be9963e34b5f2479cd340b6 (patch) | |
| tree | 3c922731582aa6a69241607838bcb52d783457ce /parse_test.go | |
| parent | 11f9b624a9e5ee803b3810d2329fb027662c69f4 (diff) | |
Implement MustParse on Parse
This moves most of the body of the MustParse function into a MustParse
method on a Parser. The MustParse function is now implemented by calling
the MustParse function on the Parser it implicitly creates.
Closes: #194
Diffstat (limited to 'parse_test.go')
| -rw-r--r-- | parse_test.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/parse_test.go b/parse_test.go index 4ea6bc4..7e84def 100644 --- a/parse_test.go +++ b/parse_test.go @@ -860,6 +860,54 @@ func TestEnvironmentVariableInSubcommandIgnored(t *testing.T) { assert.Equal(t, "", args.Sub.Foo) } +func TestParserMustParseEmptyArgs(t *testing.T) { + // this mirrors TestEmptyArgs + p, err := NewParser(Config{}, &struct{}{}) + require.NoError(t, err) + assert.NotNil(t, p) + p.MustParse(nil) +} + +func TestParserMustParse(t *testing.T) { + tests := []struct { + name string + args versioned + cmdLine []string + code int + output string + }{ + {name: "help", args: struct{}{}, cmdLine: []string{"--help"}, code: 0, output: "display this help and exit"}, + {name: "version", args: versioned{}, cmdLine: []string{"--version"}, code: 0, output: "example 3.2.1"}, + {name: "invalid", args: struct{}{}, cmdLine: []string{"invalid"}, code: -1, output: ""}, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + originalExit := osExit + originalStdout := stdout + defer func() { + osExit = originalExit + stdout = originalStdout + }() + + var exitCode *int + osExit = func(code int) { exitCode = &code } + var b bytes.Buffer + stdout = &b + + p, err := NewParser(Config{}, &tt.args) + require.NoError(t, err) + assert.NotNil(t, p) + + p.MustParse(tt.cmdLine) + assert.NotNil(t, exitCode) + assert.Equal(t, tt.code, *exitCode) + assert.Contains(t, b.String(), tt.output) + }) + } +} + type textUnmarshaler struct { val int } |
