|
| 1 | +package double |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/krostar/test" |
| 8 | + "github.com/krostar/test/check" |
| 9 | + |
| 10 | + "github.com/krostar/cli" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_Fake(t *testing.T) { |
| 14 | + t.Run("default only implements Execute", func(t *testing.T) { |
| 15 | + f := NewFake() |
| 16 | + |
| 17 | + test.Assert(check.Not(check.Panics(t, func() { |
| 18 | + test.Assert(t, f.Execute(t.Context(), []string{}, []string{}) == nil) |
| 19 | + }, nil))) |
| 20 | + |
| 21 | + _, okContext := f.(cli.CommandContext) |
| 22 | + _, okDescription := f.(cli.CommandDescription) |
| 23 | + _, okExamples := f.(cli.CommandExamples) |
| 24 | + _, okFlags := f.(cli.CommandFlags) |
| 25 | + _, okHook := f.(cli.CommandHook) |
| 26 | + _, okPersistentFlags := f.(cli.CommandPersistentFlags) |
| 27 | + _, okPersistentHook := f.(cli.CommandPersistentHook) |
| 28 | + _, okUsage := f.(cli.CommandUsage) |
| 29 | + |
| 30 | + test.Assert(t, !okContext && !okDescription && !okExamples && !okFlags && !okHook && !okPersistentFlags && !okPersistentHook && !okUsage) |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("FakeWithContext", func(t *testing.T) { |
| 34 | + ctx := t.Context() |
| 35 | + |
| 36 | + f := NewFake(FakeWithContext(func(context.Context) context.Context { return ctx })) |
| 37 | + |
| 38 | + fContext, okContext := f.(cli.CommandContext) |
| 39 | + test.Assert(t, okContext && fContext.Context(t.Context()) == ctx) |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("FakeWithDescription", func(t *testing.T) { |
| 43 | + description := "hello world" |
| 44 | + |
| 45 | + f := NewFake(FakeWithDescription(func() string { return description })) |
| 46 | + |
| 47 | + fDescription, okDescription := f.(cli.CommandDescription) |
| 48 | + test.Assert(t, okDescription && fDescription.Description() == description) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("FakeWithExamples", func(t *testing.T) { |
| 52 | + examples := []string{"hello", "world"} |
| 53 | + |
| 54 | + f := NewFake(FakeWithExamples(func() []string { return examples })) |
| 55 | + |
| 56 | + fExamples, okExamples := f.(cli.CommandExamples) |
| 57 | + test.Assert(t, okExamples) |
| 58 | + test.Assert(check.Compare(t, fExamples.Examples(), examples)) |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("FakeWithFlags", func(t *testing.T) { |
| 62 | + flags := []cli.Flag{cli.NewBuiltinFlag("long", "s", new(int), "description")} |
| 63 | + |
| 64 | + f := NewFake(FakeWithFlags(func() []cli.Flag { return flags })) |
| 65 | + |
| 66 | + fFlags, okFlags := f.(cli.CommandFlags) |
| 67 | + test.Assert(t, okFlags && len(fFlags.Flags()) == len(flags)) |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("FakeWithHook", func(t *testing.T) { |
| 71 | + hook := &cli.Hook{BeforeCommandExecution: func(context.Context) error { return nil }} |
| 72 | + |
| 73 | + f := NewFake(FakeWithHook(func() *cli.Hook { return hook })) |
| 74 | + |
| 75 | + fHook, okHook := f.(cli.CommandHook) |
| 76 | + test.Assert(t, okHook && fHook.Hook() == hook) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("FakeWithPersistentFlags", func(t *testing.T) { |
| 80 | + flags := []cli.Flag{cli.NewBuiltinFlag("long", "s", new(int), "description")} |
| 81 | + |
| 82 | + f := NewFake(FakeWithPersistentFlags(func() []cli.Flag { return flags })) |
| 83 | + |
| 84 | + fPersistentFlags, okPersistentFlags := f.(cli.CommandPersistentFlags) |
| 85 | + test.Assert(t, okPersistentFlags && len(fPersistentFlags.PersistentFlags()) == len(flags)) |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("FakeWithPersistentHook", func(t *testing.T) { |
| 89 | + hook := &cli.PersistentHook{BeforeCommandExecution: func(context.Context) error { return nil }} |
| 90 | + |
| 91 | + f := NewFake(FakeWithPersistentHook(func() *cli.PersistentHook { return hook })) |
| 92 | + |
| 93 | + fPersistentHook, okPersistentHook := f.(cli.CommandPersistentHook) |
| 94 | + test.Assert(t, okPersistentHook && fPersistentHook.PersistentHook() == hook) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("FakeWithUsage", func(t *testing.T) { |
| 98 | + usage := "hello world" |
| 99 | + |
| 100 | + f := NewFake(FakeWithUsage(func() string { return usage })) |
| 101 | + |
| 102 | + fUsage, okUsage := f.(cli.CommandUsage) |
| 103 | + test.Assert(t, okUsage && fUsage.Usage() == usage) |
| 104 | + }) |
| 105 | +} |
0 commit comments