github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/test_units_func_test.go (about) 1 package lang_test 2 3 /* 4 This test library relates to using the Go testing framework to test murex's 5 framework for unit testing shell scripts. 6 7 The naming convention here is basically the inverse of Go's test naming 8 convention. ie Go source files will be named "test_unit.go" (because 9 calling it unit_test.go would mean it's a Go test rather than murex test) 10 and the code is named UnitTestPlans (etc) rather than TestUnitPlans (etc) 11 because the latter might suggest they would be used by `go test`. This 12 naming convention is a little counterintuitive but it at least avoids 13 naming conflicts with `go test`. 14 */ 15 16 import ( 17 "testing" 18 19 _ "github.com/lmorg/murex/builtins" 20 "github.com/lmorg/murex/lang" 21 "github.com/lmorg/murex/lang/types" 22 "github.com/lmorg/murex/utils" 23 ) 24 25 func TestRunTestVarScoping(t *testing.T) { 26 plans := []testUTPs{ 27 { 28 Function: "foobar", 29 TestBlock: `out $foo`, 30 Passed: true, 31 UTP: lang.UnitTestPlan{ 32 PreBlock: "global foo=bar", 33 PostBlock: "!global foo", 34 StdoutMatch: "bar\n", 35 }, 36 }, 37 { 38 Function: "foobar", 39 TestBlock: `out $foo`, 40 Passed: false, 41 UTP: lang.UnitTestPlan{ 42 PreBlock: "set foo=bar", 43 PostBlock: "!set foo", 44 StdoutMatch: "bar\n", 45 }, 46 }, 47 } 48 49 testRunTest(t, plans) 50 } 51 52 func TestRunTestParameters(t *testing.T) { 53 plans := []testUTPs{ 54 { 55 Function: "foobar", 56 TestBlock: "out $ARGS", 57 Passed: true, 58 UTP: lang.UnitTestPlan{ 59 Parameters: []string{"a", "b", "c"}, 60 StdoutMatch: `["foobar","a","b","c"]` + utils.NewLineString, 61 }, 62 }, 63 { 64 Function: "foobar", 65 TestBlock: "out $ARGS", 66 Passed: true, 67 UTP: lang.UnitTestPlan{ 68 Parameters: []string{"1", "2", "3"}, 69 StdoutMatch: `["foobar","1","2","3"]` + utils.NewLineString, 70 }, 71 }, 72 { 73 Function: "foobar", 74 TestBlock: "out $ARGS", 75 Passed: true, 76 UTP: lang.UnitTestPlan{ 77 Parameters: []string{"foo bar"}, 78 StdoutMatch: `["foobar","foo bar"]` + utils.NewLineString, 79 }, 80 }, 81 } 82 83 testRunTest(t, plans) 84 } 85 86 func TestRunTestDataTypes(t *testing.T) { 87 plans := []testUTPs{ 88 { 89 Function: "foobar", 90 TestBlock: "tout json {}", 91 Passed: true, 92 UTP: lang.UnitTestPlan{ 93 StdoutMatch: `{}`, 94 StdoutType: types.Json, 95 }, 96 }, 97 { 98 Function: "foobar", 99 TestBlock: "tout <err> json {}", 100 Passed: true, 101 UTP: lang.UnitTestPlan{ 102 StderrMatch: `{}`, 103 StderrType: types.Json, 104 }, 105 }, 106 { 107 Function: "foobar", 108 TestBlock: "tout notjson {}", 109 Passed: false, 110 UTP: lang.UnitTestPlan{ 111 StdoutMatch: `{}`, 112 StdoutType: types.Json, 113 }, 114 }, 115 { 116 Function: "foobar", 117 TestBlock: "tout <err> notjson {}", 118 Passed: false, 119 UTP: lang.UnitTestPlan{ 120 StderrMatch: `{}`, 121 StderrType: types.Json, 122 }, 123 }, 124 } 125 126 testRunTest(t, plans) 127 } 128 129 func TestRunTestStdin(t *testing.T) { 130 plans := []testUTPs{ 131 { 132 Function: "foobar", 133 TestBlock: `-> set foo; $foo`, 134 Passed: true, 135 UTP: lang.UnitTestPlan{ 136 Stdin: "bar", 137 StdoutMatch: "bar", 138 }, 139 }, 140 { 141 Function: "foobar", 142 TestBlock: `-> set foo; $foo`, 143 Passed: true, 144 UTP: lang.UnitTestPlan{ 145 Stdin: "bar", 146 StdoutMatch: "bar", 147 StdoutType: types.Generic, 148 }, 149 }, 150 { 151 Function: "foobar", 152 TestBlock: `-> set foo; $foo`, 153 Passed: true, 154 UTP: lang.UnitTestPlan{ 155 Stdin: "[1,2,3]", 156 StdinType: types.Json, 157 StdoutMatch: "[1,2,3]", 158 StdoutType: types.Json, 159 }, 160 }, 161 { 162 Function: "foobar", 163 TestBlock: `-> set foo; out $foo`, 164 Passed: true, 165 UTP: lang.UnitTestPlan{ 166 Stdin: "bar", 167 StdinType: types.Generic, 168 StdoutMatch: "bar\n", 169 StdoutType: types.String, 170 }, 171 }, 172 } 173 174 testRunTest(t, plans) 175 } 176 177 func TestRunTestExitNumber(t *testing.T) { 178 plans := []testUTPs{ 179 { 180 Function: "foobar", 181 TestBlock: `err`, 182 Passed: false, 183 UTP: lang.UnitTestPlan{ 184 StderrMatch: "\n", 185 ExitNum: 0, 186 }, 187 }, 188 { 189 Function: "foobar", 190 TestBlock: `err`, 191 Passed: true, 192 UTP: lang.UnitTestPlan{ 193 StderrMatch: "\n", 194 ExitNum: 1, 195 }, 196 }, 197 { 198 Function: "foobar", 199 TestBlock: `err`, 200 Passed: false, 201 UTP: lang.UnitTestPlan{ 202 StderrMatch: "\n", 203 ExitNum: 2, 204 }, 205 }, 206 } 207 208 testRunTest(t, plans) 209 } 210 211 func TestRunTestRegexStdout(t *testing.T) { 212 plans := []testUTPs{ 213 { 214 Function: "foobar", 215 TestBlock: `out foo`, 216 Passed: true, 217 UTP: lang.UnitTestPlan{ 218 StdoutRegex: "(foo|bar)", 219 }, 220 }, 221 { 222 Function: "foobar", 223 TestBlock: `out foo`, 224 Passed: false, 225 UTP: lang.UnitTestPlan{ 226 StdoutRegex: "(FOO|BAR)", 227 }, 228 }, 229 { 230 Function: "foobar", 231 TestBlock: `out foobar`, 232 Passed: true, 233 UTP: lang.UnitTestPlan{ 234 StdoutRegex: "foobar", 235 }, 236 }, 237 { 238 Function: "foobar", 239 TestBlock: `out foobar`, 240 Passed: true, 241 UTP: lang.UnitTestPlan{ 242 StdoutRegex: "^foobar\n$", 243 }, 244 }, 245 { 246 Function: "foobar", 247 TestBlock: `out foobar`, 248 Passed: false, 249 UTP: lang.UnitTestPlan{ 250 StdoutRegex: "^ob$", 251 }, 252 }, 253 { 254 Function: "foobar", 255 TestBlock: `out foobar`, 256 Passed: true, 257 UTP: lang.UnitTestPlan{ 258 StdoutRegex: "ob", 259 }, 260 }, 261 } 262 263 testRunTest(t, plans) 264 } 265 266 func TestRunTestRegexStderr(t *testing.T) { 267 plans := []testUTPs{ 268 { 269 Function: "foobar", 270 TestBlock: `err foo`, 271 Passed: true, 272 UTP: lang.UnitTestPlan{ 273 StderrRegex: "(foo|bar)", 274 ExitNum: 1, 275 }, 276 }, 277 { 278 Function: "foobar", 279 TestBlock: `err foo`, 280 Passed: false, 281 UTP: lang.UnitTestPlan{ 282 StderrRegex: "(FOO|BAR)", 283 ExitNum: 1, 284 }, 285 }, 286 { 287 Function: "foobar", 288 TestBlock: `err foobar`, 289 Passed: true, 290 UTP: lang.UnitTestPlan{ 291 StderrRegex: "foobar", 292 ExitNum: 1, 293 }, 294 }, 295 { 296 Function: "foobar", 297 TestBlock: `err foobar`, 298 Passed: true, 299 UTP: lang.UnitTestPlan{ 300 StderrRegex: "^foobar\n$", 301 ExitNum: 1, 302 }, 303 }, 304 { 305 Function: "foobar", 306 TestBlock: `err foobar`, 307 Passed: false, 308 UTP: lang.UnitTestPlan{ 309 StderrRegex: "^ob$", 310 ExitNum: 1, 311 }, 312 }, 313 { 314 Function: "foobar", 315 TestBlock: `err foobar`, 316 Passed: true, 317 UTP: lang.UnitTestPlan{ 318 StderrRegex: "ob", 319 ExitNum: 1, 320 }, 321 }, 322 } 323 324 testRunTest(t, plans) 325 }