go test -v
=== RUN TestParse
=== RUN TestParse/["-name"_"foo"]
=== RUN TestParse/["--name"_"foo"]
=== RUN TestParse/["-name=foo"]
=== RUN TestParse/["--name=foo"]
--- PASS: TestParse (0.00s)
--- PASS: TestParse/["-name"_"foo"] (0.00s)
--- PASS: TestParse/["--name"_"foo"] (0.00s)
--- PASS: TestParse/["-name=foo"] (0.00s)
--- PASS: TestParse/["--name=foo"] (0.00s)
=== RUN TestParseDefault
--- PASS: TestParseDefault (0.00s)
=== RUN TestHelp
=== RUN TestHelp/["-h"]
=== RUN TestHelp/["--help"]
--- PASS: TestHelp (0.00s)
--- PASS: TestHelp/["-h"] (0.00s)
--- PASS: TestHelp/["--help"] (0.00s)
PASS
ok m/01parse 0.007s
Last active
April 21, 2020 17:26
-
-
Save podhmo/20ce32bef4924ffb16301d54f4eb3c2b to your computer and use it in GitHub Desktop.
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
SHELL := $(shell which bash) | |
test: | |
go test -v | |
readme: OUTPUT ?= README.md | |
readme: | |
( echo '```'; $(MAKE) test; echo '```' ) |& tee README.md |
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
package parse | |
import ( | |
"flag" | |
"strings" | |
) | |
type Option struct { | |
Name string // required | |
} | |
func New(opt *Option) *flag.FlagSet { | |
cmd := flag.NewFlagSet("app", flag.ContinueOnError) | |
cmd.StringVar(&opt.Name, "name", "", "<name>") | |
cmd.StringVar(&opt.Name, "n", "", "<name> (shorthand)") | |
var b strings.Builder | |
cmd.SetOutput(&b) | |
return cmd | |
} |
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
package parse | |
import ( | |
"flag" | |
"fmt" | |
"strings" | |
"testing" | |
) | |
func TestParse(t *testing.T) { | |
cases := [][]string{ | |
{"-name", "foo"}, | |
{"--name", "foo"}, | |
{"-name=foo"}, | |
{"--name=foo"}, | |
{"-n", "foo"}, | |
{"--n", "foo"}, | |
{"-n=foo"}, | |
{"--n=foo"}, | |
} | |
for _, args := range cases { | |
args := args | |
t.Run(fmt.Sprintf("%q", args), func(t *testing.T) { | |
opt := &Option{} | |
cmd := New(opt) | |
err := cmd.Parse(args) | |
if err != nil { | |
t.Fatalf("expect nil, but error %+v", err) | |
} | |
if opt.Name != "foo" { | |
t.Errorf("expected %q, but %q", "foo", opt.Name) | |
} | |
}) | |
} | |
} | |
func TestParseDefault(t *testing.T) { | |
args := []string{} | |
opt := &Option{} | |
cmd := New(opt) | |
err := cmd.Parse(args) | |
if err != nil { | |
t.Fatalf("expect nil, but error %+v", err) | |
} | |
if opt.Name != "" { | |
t.Errorf("expected %q, but %q", "", opt.Name) | |
} | |
} | |
func TestHelp(t *testing.T) { | |
cases := [][]string{ | |
{"-h"}, | |
{"--help"}, | |
} | |
for _, args := range cases { | |
args := args | |
t.Run(fmt.Sprintf("%q", args), func(t *testing.T) { | |
opt := &Option{} | |
cmd := New(opt) | |
err := cmd.Parse(args) | |
if err == nil { | |
t.Fatal("expect error, but nil") | |
} | |
if err != flag.ErrHelp { | |
t.Errorf("must be %s", err) | |
} | |
output := cmd.Output().(*strings.Builder).String() | |
expected := `Usage of app: | |
-n string | |
<name> (shorthand) | |
-name string | |
<name> | |
` | |
if expected != output { | |
t.Errorf( | |
"expected %q, but %q", | |
strings.TrimSpace(output), | |
strings.TrimSpace(expected), | |
) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment