| 2440 | } |
| 2441 | |
| 2442 | func (tc *calledAsTestcase) test(t *testing.T) { |
| 2443 | defer func(ov bool) { EnablePrefixMatching = ov }(EnablePrefixMatching) |
| 2444 | EnablePrefixMatching = tc.epm |
| 2445 | |
| 2446 | var called *Command |
| 2447 | run := func(c *Command, _ []string) { t.Logf("called: %q", c.Name()); called = c } |
| 2448 | |
| 2449 | parent := &Command{Use: "parent", Run: run} |
| 2450 | child1 := &Command{Use: "child1", Run: run, Aliases: []string{"this"}} |
| 2451 | child2 := &Command{Use: "child2", Run: run, Aliases: []string{"that"}} |
| 2452 | |
| 2453 | parent.AddCommand(child1) |
| 2454 | parent.AddCommand(child2) |
| 2455 | parent.SetArgs(tc.args) |
| 2456 | |
| 2457 | output := new(bytes.Buffer) |
| 2458 | parent.SetOut(output) |
| 2459 | parent.SetErr(output) |
| 2460 | |
| 2461 | _ = parent.Execute() |
| 2462 | |
| 2463 | if called == nil { |
| 2464 | if tc.call != "" { |
| 2465 | t.Errorf("missing expected call to command: %s", tc.call) |
| 2466 | } |
| 2467 | return |
| 2468 | } |
| 2469 | |
| 2470 | if called.Name() != tc.call { |
| 2471 | t.Errorf("called command == %q; Wanted %q", called.Name(), tc.call) |
| 2472 | } else if got := called.CalledAs(); got != tc.want { |
| 2473 | t.Errorf("%s.CalledAs() == %q; Wanted: %q", tc.call, got, tc.want) |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | func TestCalledAs(t *testing.T) { |
| 2478 | tests := map[string]calledAsTestcase{ |