|
| 1 | +// (c) 2023 Jacek Olszak |
| 2 | +// This code is licensed under MIT license (see LICENSE for details) |
| 3 | + |
| 4 | +//go:build !js |
| 5 | + |
| 6 | +package help_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/elgopher/pi/devtools/internal/help" |
| 14 | + "github.com/elgopher/pi/devtools/internal/test" |
| 15 | +) |
| 16 | + |
| 17 | +func TestPrintHelp(t *testing.T) { |
| 18 | + t.Run("should return error when trying to print help for not imported packages", func(t *testing.T) { |
| 19 | + topics := []string{ |
| 20 | + "io", "io.Writer", |
| 21 | + } |
| 22 | + for _, topic := range topics { |
| 23 | + t.Run(topic, func(t *testing.T) { |
| 24 | + // when |
| 25 | + err := help.PrintHelp(topic) |
| 26 | + // then |
| 27 | + assert.ErrorIs(t, err, help.NotFound) |
| 28 | + }) |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("should return error when trying to print help for non-existent symbol", func(t *testing.T) { |
| 33 | + err := help.PrintHelp("pi.NonExistent") |
| 34 | + assert.ErrorIs(t, err, help.NotFound) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("should print help for", func(t *testing.T) { |
| 38 | + tests := map[string]struct { |
| 39 | + topic string |
| 40 | + expected string |
| 41 | + }{ |
| 42 | + "package": { |
| 43 | + topic: "pi", |
| 44 | + expected: `Package pi`, |
| 45 | + }, |
| 46 | + "function": { |
| 47 | + topic: "pi.Spr", |
| 48 | + expected: `func Spr(n, x, y int)`, |
| 49 | + }, |
| 50 | + "struct": { |
| 51 | + topic: "pi.PixMap", |
| 52 | + expected: `type PixMap struct {`, |
| 53 | + }, |
| 54 | + } |
| 55 | + for testName, testCase := range tests { |
| 56 | + t.Run(testName, func(t *testing.T) { |
| 57 | + swapper := test.SwapStdout(t) |
| 58 | + // when |
| 59 | + err := help.PrintHelp(testCase.topic) |
| 60 | + // then |
| 61 | + swapper.BringStdoutBack() |
| 62 | + assert.NoError(t, err) |
| 63 | + output := swapper.ReadOutput(t) |
| 64 | + assert.Contains(t, output, testCase.expected) |
| 65 | + }) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("should show help for image.Image from github.com/elgopher/pi package, not from stdlib", func(t *testing.T) { |
| 70 | + topics := []string{ |
| 71 | + "image", "image.Image", |
| 72 | + } |
| 73 | + for _, topic := range topics { |
| 74 | + t.Run(topic, func(t *testing.T) { |
| 75 | + swapper := test.SwapStdout(t) |
| 76 | + // when |
| 77 | + err := help.PrintHelp("image.Image") |
| 78 | + // then |
| 79 | + swapper.BringStdoutBack() |
| 80 | + assert.NoError(t, err) |
| 81 | + output := swapper.ReadOutput(t) |
| 82 | + assert.Contains(t, output, `// import "github.com/elgopher/pi/image"`) |
| 83 | + }) |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("should show detailed help for pi.Button", func(t *testing.T) { |
| 88 | + tests := map[string]string{ |
| 89 | + "pi.Button": "Keyboard mappings", |
| 90 | + "pi.MouseButton": "MouseRight MouseButton = 2", |
| 91 | + "key.Button": "func (b Button) String() string", |
| 92 | + } |
| 93 | + for topic, expected := range tests { |
| 94 | + t.Run(topic, func(t *testing.T) { |
| 95 | + swapper := test.SwapStdout(t) |
| 96 | + // when |
| 97 | + err := help.PrintHelp(topic) |
| 98 | + // then |
| 99 | + swapper.BringStdoutBack() |
| 100 | + assert.NoError(t, err) |
| 101 | + output := swapper.ReadOutput(t) |
| 102 | + assert.Contains(t, output, expected) |
| 103 | + }) |
| 104 | + } |
| 105 | + }) |
| 106 | +} |
0 commit comments