Skip to content

Commit

Permalink
updated modifier tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jan 22, 2023
1 parent 8173346 commit bad4778
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
15 changes: 12 additions & 3 deletions example/cmd/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ func init() {

carapace.Gen(modifierCmd).FlagCompletion(carapace.ActionMap{
"batch": carapace.Batch(
carapace.ActionValues("A", "B"),
carapace.ActionValues("C", "D"),
carapace.ActionValues("E", "F"),
carapace.ActionValuesDescribed(
"A", "description of A",
"B", "description of first B",
),
carapace.ActionValuesDescribed(
"B", "description of second B",
"C", "description of first C",
),
carapace.ActionValuesDescribed(
"C", "description of second C",
"D", "description of D",
),
).ToA(),
"timeout": carapace.ActionMultiParts(":", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
Expand Down
40 changes: 39 additions & 1 deletion example/cmd/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ import (

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/sandbox"
"github.com/rsteube/carapace/pkg/style"
)

func TestModifier(t *testing.T) {
func TestBatch(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Run("modifier", "--batch", "").
Expect(carapace.ActionValuesDescribed(
"A", "description of A",
"B", "description of second B",
"C", "description of second C",
"D", "description of D",
).
Usage("Batch()"))
})
}

func TestTimeout(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Run("modifier", "--timeout", "1s:").
Expect(carapace.ActionValues("within timeout").
Expand All @@ -20,3 +34,27 @@ func TestModifier(t *testing.T) {
Usage("Timeout()"))
})
}

func TestUsage(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Run("modifier", "--usage", "").
Expect(carapace.ActionValues().
Usage("explicit flag usage"))
})
}

func TestChdir(t *testing.T) {
sandbox.Action(t, func() carapace.Action {
return carapace.ActionFiles().Chdir("subdir")
})(func(s *sandbox.Sandbox) {
s.Files("subdir/file1.txt", "")

s.Run("").Expect(
carapace.ActionValues("file1.txt").
StyleF(func(s string, sc style.Context) string {
return style.ForPath("subdir/file1.txt", sc)
}).
NoSpace('/').
Tag("files"))
})
}
15 changes: 13 additions & 2 deletions pkg/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (r run) Expect(expected carapace.Action) {
})
}

// Command executes the command generated by given function for sandbox tests.
// Command executes the command generated by given function.
func Command(t *testing.T, cmdF func() *cobra.Command) (f func(func(s *Sandbox))) {
return func(f func(s *Sandbox)) {
s := newSandbox(t, cmdF)
Expand All @@ -178,7 +178,7 @@ func Command(t *testing.T, cmdF func() *cobra.Command) (f func(func(s *Sandbox))
}
}

// Run invokes `go run` on given package for sandbox tests.
// Run invokes `go run` on given package.
func Package(t *testing.T, pkg string) (f func(func(s *Sandbox))) {
return Command(t, func() *cobra.Command {
cmd := &cobra.Command{DisableFlagParsing: true}
Expand All @@ -202,3 +202,14 @@ func Package(t *testing.T, pkg string) (f func(func(s *Sandbox))) {
return cmd
})
}

// Action executes a a command with the action return by given function as first positional argument.
func Action(t *testing.T, actionF func() carapace.Action) (f func(func(s *Sandbox))) {
return Command(t, func() *cobra.Command {
cmd := &cobra.Command{DisableFlagParsing: true}
cmd.CompletionOptions.DisableDefaultCmd = true

carapace.Gen(cmd).PositionalCompletion(actionF())
return cmd
})
}

0 comments on commit bad4778

Please sign in to comment.