Use parser2 in querier tests

This commit is contained in:
Dan Buch 2022-05-27 08:22:07 -04:00
parent 95453bf197
commit cc29386cee
Signed by: meatballhat
GPG Key ID: A12F782281063434
3 changed files with 18 additions and 16 deletions

View File

@ -55,6 +55,8 @@ func (p *parser2) parseArgs() (*ParseTree, error) {
tracef("parseArgs() parsing %q as program command; cfg=%+#v", p.lit, p.cfg.Prog)
prog := p.parseCommand(&p.cfg.Prog)
tracef("parseArgs() top level node is %T", prog)
nodes := []Node{prog}
if v := p.parsePassthrough(); v != nil {
tracef("parseArgs() appending passthrough argument %v", v)

View File

@ -1,7 +1,7 @@
package argh
type Querier interface {
Program() (Command, bool)
Program() (*Command, bool)
AST() []Node
}
@ -13,12 +13,19 @@ type defaultQuerier struct {
nodes []Node
}
func (dq *defaultQuerier) Program() (Command, bool) {
func (dq *defaultQuerier) Program() (*Command, bool) {
if len(dq.nodes) == 0 {
return Command{}, false
tracef("Program nodes are empty")
return nil, false
}
tracef("Program node[0] is %T", dq.nodes[0])
v, ok := dq.nodes[0].(*Command)
if ok && v.Name == "" {
return v, false
}
v, ok := dq.nodes[0].(Command)
return v, ok
}

View File

@ -12,41 +12,34 @@ func TestQuerier_Program(t *testing.T) {
name string
args []string
cfg *argh.ParserConfig
exp argh.Command
exp string
expOK bool
}{
{
name: "typical",
args: []string{"pizzas", "ahoy", "--treatsa", "fun"},
exp: argh.Command{Name: "pizzas"},
exp: "pizzas",
expOK: true,
},
{
name: "minimal",
args: []string{"pizzas"},
exp: argh.Command{Name: "pizzas"},
exp: "pizzas",
expOK: true,
},
{
name: "invalid",
args: []string{},
exp: argh.Command{},
expOK: false,
},
{
name: "invalid flag only",
args: []string{"--oh-no"},
exp: argh.Command{},
expOK: false,
},
} {
t.Run(tc.name, func(ct *testing.T) {
pt, err := argh.ParseArgs(tc.args, tc.cfg)
pt, err := argh.ParseArgs2(tc.args, tc.cfg)
require.Nil(ct, err)
prog, ok := argh.NewQuerier(pt.Nodes).Program()
require.Equal(ct, tc.exp, prog)
require.Equal(ct, tc.expOK, ok)
require.Equal(ct, tc.exp, prog.Name)
})
}
}