Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
d48b43da
Commit
d48b43da
authored
Jan 15, 2021
by
Adin Schmahmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
testing: refactor to extract testOptionHelper for reuse
parent
cde56b1d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
20 deletions
+20
-20
cli/parse_test.go
cli/parse_test.go
+20
-20
No files found.
cli/parse_test.go
View file @
d48b43da
...
...
@@ -77,6 +77,24 @@ func TestSameWords(t *testing.T) {
test
(
f
,
f
,
true
)
}
func
testOptionHelper
(
t
*
testing
.
T
,
cmd
*
cmds
.
Command
,
args
string
,
expectedOpts
kvs
,
expectedWords
words
,
expectErr
bool
)
{
req
:=
&
cmds
.
Request
{}
err
:=
parse
(
req
,
strings
.
Split
(
args
,
" "
),
cmd
)
if
err
==
nil
{
err
=
req
.
FillDefaults
()
}
if
expectErr
{
if
err
==
nil
{
t
.
Errorf
(
"Command line '%v' parsing should have failed"
,
args
)
}
}
else
if
err
!=
nil
{
t
.
Errorf
(
"Command line '%v' failed to parse: %v"
,
args
,
err
)
}
else
if
!
sameWords
(
req
.
Arguments
,
expectedWords
)
||
!
sameKVs
(
kvs
(
req
.
Options
),
expectedOpts
)
{
t
.
Errorf
(
"Command line '%v':
\n
parsed as %v %v
\n
instead of %v %v"
,
args
,
req
.
Options
,
req
.
Arguments
,
expectedOpts
,
expectedWords
)
}
}
func
TestOptionParsing
(
t
*
testing
.
T
)
{
cmd
:=
&
cmds
.
Command
{
Options
:
[]
cmds
.
Option
{
...
...
@@ -96,30 +114,12 @@ func TestOptionParsing(t *testing.T) {
},
}
testHelper
:=
func
(
args
string
,
expectedOpts
kvs
,
expectedWords
words
,
expectErr
bool
)
{
req
:=
&
cmds
.
Request
{}
err
:=
parse
(
req
,
strings
.
Split
(
args
,
" "
),
cmd
)
if
err
==
nil
{
err
=
req
.
FillDefaults
()
}
if
expectErr
{
if
err
==
nil
{
t
.
Errorf
(
"Command line '%v' parsing should have failed"
,
args
)
}
}
else
if
err
!=
nil
{
t
.
Errorf
(
"Command line '%v' failed to parse: %v"
,
args
,
err
)
}
else
if
!
sameWords
(
req
.
Arguments
,
expectedWords
)
||
!
sameKVs
(
kvs
(
req
.
Options
),
expectedOpts
)
{
t
.
Errorf
(
"Command line '%v':
\n
parsed as %v %v
\n
instead of %v %v"
,
args
,
req
.
Options
,
req
.
Arguments
,
expectedOpts
,
expectedWords
)
}
}
testFail
:=
func
(
args
string
)
{
testHelper
(
args
,
kvs
{},
words
{},
true
)
test
Option
Helper
(
t
,
cmd
,
args
,
kvs
{},
words
{},
true
)
}
test
:=
func
(
args
string
,
expectedOpts
kvs
,
expectedWords
words
)
{
testHelper
(
args
,
expectedOpts
,
expectedWords
,
false
)
test
Option
Helper
(
t
,
cmd
,
args
,
expectedOpts
,
expectedWords
,
false
)
}
test
(
"test -"
,
kvs
{},
words
{
"-"
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment