Skip to content

Commit

Permalink
Add functionality to find examples
Browse files Browse the repository at this point in the history
In order for an app to find examples it needs to search in all levels
for a match.

For the moment there are only two levels, the app and the commands, so
substring check is sufficent for this purpose.
  • Loading branch information
matthewdunsdon committed Jul 3, 2017
1 parent dbda1b9 commit 85672c1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
19 changes: 19 additions & 0 deletions levels.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package egcmd

import (
"strings"
)

// A Level provides a context for examples to be found,
// whether this is at the top-level of your app or at a command.
type Level struct {
Expand Down Expand Up @@ -45,6 +49,21 @@ func (a *App) Command(name string) (command *Command) {
return
}

// Find returns the examples that belong to a particular level
func (a *App) Find(search string) (examples []*Example) {
if search == a.name {
examples = a.examples
} else if strings.HasPrefix(search, a.name) {
command := search[len(a.name)+1:]

if value, ok := a.commands[command]; ok {
examples = value.examples
}
}

return
}

// Command provides a container for top-level and command examples to be instantiated
type Command struct {
Level
Expand Down
76 changes: 76 additions & 0 deletions levels_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package egcmd

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -142,3 +143,78 @@ func TestAppCommand(t *testing.T) {
})
}
}

func TestAppFind(t *testing.T) {
appExamples := []*Example{
{"", "Action: default", ""},
{"simple", "Action: simple", ""},
{"complex sub-action", "Action: complex sub-action", ""},
}
simpleExamples := []*Example{
{"", "Default for simple", ""},
{"--debug", "Simple with debug", ""},
}
complexExamples := []*Example{
{"--json", "Default for complex with json arg", ""},
{"sub-action", "complex action with sub-action", ""},
}
subActionExamples := []*Example{
{"", "Subaction", "APP_KEY=1234"},
}

app := App{
Level: Level{"root", appExamples},
commands: map[string]*Command{
"simple": {Level: Level{"simple", simpleExamples}},
"complex": {Level: Level{"complex", complexExamples}},
"complex sub-action": {Level: Level{"complex sub-action", subActionExamples}},
},
}

testCases := []struct {
testName string
search string
want []*Example
}{
{
"Root",
"root",
appExamples,
},
{
"SimpleCommand",
"root simple",
simpleExamples,
},
{
"ComplexCommand",
"root complex",
complexExamples,
},
{
"ComplexSubCommand",
"root complex sub-action",
subActionExamples,
},
{
"CommandNotFound",
"root no-match",
nil,
},
{
"InvalidSearchs",
"invalid",
nil,
},
}

for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
got := app.Find(tc.search)

if !reflect.DeepEqual(tc.want, got) {
t.Errorf("Expected examples to be %#v, got %#v", tc.want, got)
}
})
}
}

0 comments on commit 85672c1

Please sign in to comment.