RunMurexTests runs through all the test cases for MurexTest where STDOUT/ERR are literal strings
(tests []MurexTest, t *testing.T)
| 27 | |
| 28 | // RunMurexTests runs through all the test cases for MurexTest where STDOUT/ERR are literal strings |
| 29 | func RunMurexTests(tests []MurexTest, t *testing.T) { |
| 30 | t.Helper() |
| 31 | count.Tests(t, len(tests)) |
| 32 | |
| 33 | defaults.Config(config.InitConf, false) |
| 34 | lang.InitEnv() |
| 35 | |
| 36 | for i := range tests { |
| 37 | hasError := false |
| 38 | |
| 39 | fork := lang.ShellProcess.Fork(lang.F_FUNCTION | lang.F_NEW_MODULE | lang.F_NO_STDIN | lang.F_CREATE_STDOUT | lang.F_CREATE_STDERR) |
| 40 | go func(testNum int) { |
| 41 | time.Sleep(_TIMEOUT_SECONDS * time.Second) |
| 42 | if !fork.Process.HasCancelled() { |
| 43 | panic(fmt.Sprintf("timeout in %s() test %d", t.Name(), testNum)) |
| 44 | } |
| 45 | }(i) |
| 46 | |
| 47 | fork.Name.Set("RunMurexTests()") |
| 48 | fork.FileRef = &ref.File{Source: &ref.Source{Module: fmt.Sprintf("murex/%s-%d", t.Name(), i)}} |
| 49 | exitNum, err := fork.Execute([]rune(tests[i].Block)) |
| 50 | if err != nil { |
| 51 | t.Errorf("Cannot execute script on test %d", i) |
| 52 | t.Log(" ", err) |
| 53 | continue |
| 54 | } |
| 55 | |
| 56 | bErr, err := fork.Stderr.ReadAll() |
| 57 | if err != nil { |
| 58 | t.Errorf("Cannot ReadAll() from Stderr on test %d", i) |
| 59 | t.Log(" ", err) |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | if string(bErr) != tests[i].Stderr { |
| 64 | hasError = true |
| 65 | } |
| 66 | |
| 67 | bOut, err := fork.Stdout.ReadAll() |
| 68 | if err != nil { |
| 69 | t.Errorf("Cannot ReadAll() from Stdout on test %d", i) |
| 70 | t.Log(" ", err) |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | if string(bOut) != tests[i].Stdout { |
| 75 | hasError = true |
| 76 | } |
| 77 | |
| 78 | if exitNum != tests[i].ExitNum { |
| 79 | hasError = true |
| 80 | } |
| 81 | |
| 82 | if hasError { |
| 83 | t.Errorf("Code block doesn't return expected values in test %d", i) |
| 84 | t.Log(" Code block: ", tests[i].Block) |
| 85 | |
| 86 | t.Log(" Expected Stdout: ", strings.Replace(tests[i].Stdout, "\n", `\n`, -1)) |