|
1 | 1 | package gofr |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + "gofr.dev/pkg/gofr/config" |
| 10 | + "gofr.dev/pkg/gofr/container" |
| 11 | + "gofr.dev/pkg/gofr/testutil" |
7 | 12 | ) |
8 | 13 |
|
9 | | -func Test_ErrCommandNotFound(t *testing.T) { |
10 | | - err := ErrCommandNotFound{} |
| 14 | +func Test_Run_SuccessCallRegisteredArgument(t *testing.T) { |
| 15 | + os.Args = []string{"", "log"} |
| 16 | + |
| 17 | + c := cmd{} |
11 | 18 |
|
12 | | - result := err.Error() |
| 19 | + c.addRoute("log", func(c *Context) (interface{}, error) { |
| 20 | + c.Logger.Info("handler called") |
| 21 | + |
| 22 | + return nil, nil |
| 23 | + }) |
13 | 24 |
|
14 | | - assert.Equal(t, "No Command Found!", result, "Test Failed \nMatch Error String") |
| 25 | + logs := testutil.StdoutOutputForFunc(func() { |
| 26 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 27 | + }) |
| 28 | + |
| 29 | + assert.Contains(t, logs, "handler called") |
15 | 30 | } |
16 | 31 |
|
17 | | -func Test_addRouteNotNilHandler(t *testing.T) { |
18 | | - testHandler := func(c *Context) (interface{}, error) { |
| 32 | +func Test_Run_SuccessSkipEmptySpaceAndMatchCommandWithSpace(t *testing.T) { |
| 33 | + os.Args = []string{"", "", " ", "log"} |
| 34 | + |
| 35 | + c := cmd{} |
| 36 | + |
| 37 | + c.addRoute("log", func(c *Context) (interface{}, error) { |
| 38 | + c.Logger.Info("handler called") |
| 39 | + |
19 | 40 | return nil, nil |
20 | | - } |
| 41 | + }) |
| 42 | + |
| 43 | + logs := testutil.StdoutOutputForFunc(func() { |
| 44 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 45 | + }) |
| 46 | + |
| 47 | + assert.Contains(t, logs, "handler called") |
| 48 | +} |
| 49 | + |
| 50 | +func Test_Run_SuccessCommandWithMultipleParameters(t *testing.T) { |
| 51 | + os.Args = []string{"", "log", "-param=value", "-b", "-c"} |
| 52 | + |
| 53 | + c := cmd{} |
21 | 54 |
|
| 55 | + c.addRoute("log", func(c *Context) (interface{}, error) { |
| 56 | + assert.Equal(t, c.Request.Param("param"), "value") |
| 57 | + assert.Equal(t, c.Request.Param("b"), "true") |
| 58 | + |
| 59 | + c.Logger.Info("handler called") |
| 60 | + |
| 61 | + return nil, nil |
| 62 | + }) |
| 63 | + |
| 64 | + logs := testutil.StdoutOutputForFunc(func() { |
| 65 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 66 | + }) |
| 67 | + |
| 68 | + assert.Contains(t, logs, "handler called") |
| 69 | +} |
| 70 | + |
| 71 | +func Test_Run_SuccessRouteWithSpecialCharacters(t *testing.T) { |
22 | 72 | testCases := []struct { |
23 | | - desc string |
24 | | - pattern string |
25 | | - handler func(c *Context) (interface{}, error) |
| 73 | + desc string |
| 74 | + args []string |
26 | 75 | }{ |
27 | | - {"valid pattern and handler", "test", testHandler}, |
28 | | - {"empty pattern and valid handler", " ", testHandler}, |
| 76 | + {"special character !", []string{"", "command-with-special-characters!"}}, |
| 77 | + {"special character @", []string{"", "command-with-special-characters@"}}, |
| 78 | + {"special character #", []string{"", "command-with-special-characters#"}}, |
| 79 | + {"special character %", []string{"", "command-with-special-characters%"}}, |
| 80 | + {"special character &", []string{"", "command-with-special-characters&"}}, |
| 81 | + {"special character *", []string{"", "command-with-special-characters*"}}, |
29 | 82 | } |
30 | 83 |
|
31 | 84 | for i, tc := range testCases { |
32 | | - cmd := &cmd{} |
| 85 | + os.Args = tc.args |
| 86 | + c := cmd{} |
33 | 87 |
|
34 | | - cmd.addRoute(tc.pattern, tc.handler) |
| 88 | + c.addRoute(tc.args[1], func(c *Context) (interface{}, error) { |
| 89 | + c.Logger.Info("handler called") |
35 | 90 |
|
36 | | - assert.Equal(t, tc.pattern, cmd.routes[0].pattern, "TEST[%d], Failed.\n%s", i, tc.desc) |
37 | | - assert.NotNil(t, cmd.routes[0].handler, "TEST[%d], Failed.\n%s", i, tc.desc) |
| 91 | + return nil, nil |
| 92 | + }) |
| 93 | + |
| 94 | + logs := testutil.StdoutOutputForFunc(func() { |
| 95 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 96 | + }) |
| 97 | + |
| 98 | + assert.Contains(t, logs, "handler called", "TEST[%d] Failed.\n %s", i, tc.desc) |
| 99 | + assert.NotContains(t, logs, "No Command Found!", "TEST[%d] Failed.\n %s", i, tc.desc) |
38 | 100 | } |
39 | 101 | } |
40 | 102 |
|
41 | | -func Test_addRouteNilHanlder(t *testing.T) { |
| 103 | +func Test_Run_ErrorRouteWithSpecialCharacters(t *testing.T) { |
42 | 104 | testCases := []struct { |
43 | | - desc string |
44 | | - pattern string |
45 | | - handler func(c *Context) (interface{}, error) |
| 105 | + desc string |
| 106 | + args []string |
46 | 107 | }{ |
47 | | - {"valid pattern and nil handler", "test", nil}, |
48 | | - {"empty pattern and nil handler", " ", nil}, |
| 108 | + {"special character $", []string{"", "command-with-special-characters$"}}, |
| 109 | + {"special character ^", []string{"", "command-with-special-characters^"}}, |
49 | 110 | } |
50 | 111 |
|
51 | 112 | for i, tc := range testCases { |
52 | | - cmd := &cmd{} |
| 113 | + os.Args = tc.args |
| 114 | + c := cmd{} |
| 115 | + |
| 116 | + c.addRoute(tc.args[1], func(c *Context) (interface{}, error) { |
| 117 | + c.Logger.Info("handler called") |
53 | 118 |
|
54 | | - cmd.addRoute(tc.pattern, tc.handler) |
| 119 | + return nil, nil |
| 120 | + }) |
55 | 121 |
|
56 | | - assert.Equal(t, tc.pattern, cmd.routes[0].pattern, "TEST[%d], Failed.\n%s", i, tc.desc) |
57 | | - assert.Nil(t, cmd.routes[0].handler, "TEST[%d], Failed.\n%s", i, tc.desc) |
| 122 | + logs := testutil.StderrOutputForFunc(func() { |
| 123 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 124 | + }) |
| 125 | + |
| 126 | + assert.NotContains(t, logs, "handler called", "TEST[%d] Failed.\n %s", i, tc.desc) |
| 127 | + assert.Contains(t, logs, "No Command Found!", "TEST[%d] Failed.\n %s", i, tc.desc) |
58 | 128 | } |
59 | 129 | } |
60 | 130 |
|
61 | | -func Test_handlerReturnsNilIfRouteHandlerIsNotNil(t *testing.T) { |
62 | | - cmd := &cmd{ |
63 | | - routes: []route{ |
64 | | - {pattern: "pattern", handler: func(c *Context) (interface{}, error) { |
65 | | - return nil, nil |
66 | | - }}, |
67 | | - }, |
68 | | - } |
69 | | - path := "pattern" |
| 131 | +func Test_Run_ErrorParamNotReadWithoutHyphen(t *testing.T) { |
| 132 | + os.Args = []string{"", "log", "hello=world"} |
| 133 | + |
| 134 | + c := cmd{} |
70 | 135 |
|
71 | | - result := cmd.handler(path) |
| 136 | + c.addRoute("log", func(c *Context) (interface{}, error) { |
| 137 | + assert.Equal(t, c.Request.Param("hello"), "") |
| 138 | + c.Logger.Info("handler called") |
72 | 139 |
|
73 | | - assert.NotNil(t, result, "TEST, Failed.\n not nil handler") |
| 140 | + return nil, nil |
| 141 | + }) |
| 142 | + |
| 143 | + logs := testutil.StdoutOutputForFunc(func() { |
| 144 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 145 | + }) |
| 146 | + |
| 147 | + assert.Contains(t, logs, "handler called") |
74 | 148 | } |
75 | 149 |
|
76 | | -func Test_handlerReturnsNil(t *testing.T) { |
77 | | - nonMatchingPattern := &cmd{ |
78 | | - routes: []route{ |
79 | | - {pattern: "pattern1", handler: func(c *Context) (interface{}, error) { |
80 | | - return nil, nil |
81 | | - }}, |
82 | | - {pattern: "pattern2", handler: func(c *Context) (interface{}, error) { |
83 | | - return nil, nil |
84 | | - }}, |
85 | | - }, |
86 | | - } |
| 150 | +func Test_Run_ErrorNotARegisteredCommand(t *testing.T) { |
| 151 | + os.Args = []string{"", "log"} |
87 | 152 |
|
88 | | - emptyRouteSlice := &cmd{routes: []route{}} |
| 153 | + c := cmd{} |
89 | 154 |
|
90 | | - nilHandler := &cmd{ |
91 | | - routes: []route{ |
92 | | - {pattern: "pattern", handler: nil}, |
93 | | - }, |
94 | | - } |
| 155 | + logs := testutil.StderrOutputForFunc(func() { |
| 156 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 157 | + }) |
95 | 158 |
|
96 | | - testCases := []struct { |
97 | | - desc string |
98 | | - cmd cmd |
99 | | - pattern string |
100 | | - }{ |
101 | | - {"pattern not matching with routes", *nonMatchingPattern, "non-matching-pattern"}, |
102 | | - {"routes slice in empty", *emptyRouteSlice, "pattern"}, |
103 | | - {"handler in nil", *nilHandler, "pattern"}, |
104 | | - } |
| 159 | + assert.Contains(t, logs, "No Command Found!") |
| 160 | +} |
105 | 161 |
|
106 | | - for i, tc := range testCases { |
107 | | - r := tc.cmd.handler(tc.pattern) |
| 162 | +func Test_Run_ErrorWhenOnlyParamAreGiven(t *testing.T) { |
| 163 | + os.Args = []string{"", "-route"} |
108 | 164 |
|
109 | | - assert.Nil(t, r, "TEST[%d], Failed.\n%s", i, tc.desc) |
110 | | - } |
| 165 | + c := cmd{} |
| 166 | + |
| 167 | + c.addRoute("-route", func(c *Context) (interface{}, error) { |
| 168 | + c.Logger.Info("handler called of route -route") |
| 169 | + |
| 170 | + return nil, nil |
| 171 | + }) |
| 172 | + |
| 173 | + logs := testutil.StderrOutputForFunc(func() { |
| 174 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 175 | + }) |
| 176 | + |
| 177 | + assert.Contains(t, logs, "No Command Found!") |
| 178 | + assert.NotContains(t, logs, "handler called of route -route") |
| 179 | +} |
| 180 | + |
| 181 | +func Test_Run_ErrorRouteRegisteredButNilHandler(t *testing.T) { |
| 182 | + os.Args = []string{"", "route"} |
| 183 | + |
| 184 | + c := cmd{} |
| 185 | + |
| 186 | + c.addRoute("route", nil) |
| 187 | + |
| 188 | + logs := testutil.StderrOutputForFunc(func() { |
| 189 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 190 | + }) |
| 191 | + |
| 192 | + assert.Contains(t, logs, "No Command Found!") |
| 193 | +} |
| 194 | + |
| 195 | +func Test_Run_ErrorNoArgumentGiven(t *testing.T) { |
| 196 | + os.Args = []string{""} |
| 197 | + |
| 198 | + c := cmd{} |
| 199 | + |
| 200 | + logs := testutil.StderrOutputForFunc(func() { |
| 201 | + c.Run(container.NewContainer(config.NewEnvFile(""))) |
| 202 | + }) |
| 203 | + |
| 204 | + assert.Contains(t, logs, "No Command Found!") |
111 | 205 | } |
0 commit comments