generated from platypusguy/jacobin-swift
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathHello3_test.go
134 lines (118 loc) · 3.74 KB
/
Hello3_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Jacobin VM - A Java virtual machine
* Copyright (c) 2022 by the Jacobin authors. All rights reserved.
* Licensed under Mozilla Public License 2.0 (MPL 2.0)
*/
package wholeClassTests
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"testing"
)
// Test for Hello3 class, which has loop and function calls 3 levels deep. Source code:
//
// class Hello3 {
//
// public static void main( String[] args) {
// int x;
// for( int i = 0; i < 10; i++) {
// x = addTwo(i, i-1);
// System.out.println( x );
// }
// }
//
// static int addTwo(int j, int k) {
// int m = multTwo(j, k);
// return m+1;
// }
//
// static int multTwo(int m, int n){
// return m*n;
// }
//
// }
//
// To run your class, enter its name in _TESTCLASS, any args in their respective variables and then run the tests.
// This test harness expects that environmental variable JACOBIN_EXE gives the full name and path of the executable
// we're running the tests on. The folder which contains the test class should be specified in the environmental
// variable JACOBIN_TESTDATA (without a terminating slash).
func initVarsHello3() error {
if testing.Short() { // don't run if running quick tests only. (Used primarily so GitHub doesn't run and bork)
return fmt.Errorf("test not run due to -short")
}
_JACOBIN = os.Getenv("JACOBIN_EXE") // returns "" if JACOBIN_EXE has not been specified.
_JVM_ARGS = ""
_TESTCLASS = "Hello3.class" // the class to test
_APP_ARGS = ""
if _JACOBIN == "" {
return fmt.Errorf("test failure due to missing Jacobin executable. Please specify it in JACOBIN_EXE")
} else if _, err := os.Stat(_JACOBIN); err != nil {
return fmt.Errorf("missing Jacobin executable, which was specified as %s", _JACOBIN)
}
if _TESTCLASS != "" {
testClass := os.Getenv("JACOBIN_TESTDATA") + string(os.PathSeparator) + _TESTCLASS
if _, err := os.Stat(testClass); err != nil {
return fmt.Errorf("missing class to test, which was specified as %s", testClass)
} else {
_TESTCLASS = testClass
}
}
return nil
}
func TestRunHello3(t *testing.T) {
if testing.Short() { // don't run if running quick tests only. (Used primarily so GitHub doesn't run and bork)
t.Skip()
}
initErr := initVarsHello3()
if initErr != nil {
t.Fatalf("Test failure due to: %s", initErr.Error())
}
var cmd *exec.Cmd
if testing.Short() { // don't run if running quick tests only. (Used primarily so GitHub doesn't run and bork)
t.Skip()
}
// test that executable exists
if _, err := os.Stat(_JACOBIN); err != nil {
t.Errorf("Missing Jacobin executable, which was specified as %s", _JACOBIN)
}
// run the various combinations of args. This is necessary b/c the empty string is viewed as
// an actual specified option on the command line.
if len(_JVM_ARGS) > 0 {
if len(_APP_ARGS) > 0 {
cmd = exec.Command(_JACOBIN, _JVM_ARGS, _TESTCLASS, _APP_ARGS)
} else {
cmd = exec.Command(_JACOBIN, _JVM_ARGS, _TESTCLASS)
}
} else {
if len(_APP_ARGS) > 0 {
cmd = exec.Command(_JACOBIN, _TESTCLASS, _APP_ARGS)
} else {
cmd = exec.Command(_JACOBIN, _TESTCLASS)
}
}
// get the stdout and stderr contents from the file execution
stderr, err := cmd.StderrPipe()
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
// run the command
if err = cmd.Start(); err != nil {
t.Errorf("Got error running Jacobin: %s", err.Error())
}
// Here begin the actual tests on the output to stderr and stdout
slurp, _ := io.ReadAll(stderr)
slurpErr := string(slurp)
if len(slurp) != 0 {
t.Errorf("Got unexpected output to stderr: %s", slurpErr)
}
slurp, _ = io.ReadAll(stdout)
slurpOut := string(slurp)
if !strings.Contains(slurpOut, "57") && !strings.Contains(string(slurp), "73") {
t.Errorf("Did not get expected output to stdout. Got: %s", slurpOut)
}
}