Skip to content

Commit 32c3f10

Browse files
Add the examples for the testrunner.RunCode() function
1 parent 60a55d7 commit 32c3f10

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ $ dep ensure -vendor-only
2929

3030
## Example
3131

32+
`coderunner.RunCode`:
33+
3234
```go
3335
package main
3436

@@ -76,6 +78,104 @@ func main() {
7678
}
7779
```
7880

81+
`testrunner.RunCode` (success):
82+
83+
```go
84+
package main
85+
86+
import (
87+
"fmt"
88+
"log"
89+
"os"
90+
"path/filepath"
91+
92+
coderunner "github.com/thewizardplusplus/go-code-runner"
93+
testrunner "github.com/thewizardplusplus/go-code-runner/test-runner"
94+
)
95+
96+
func main() {
97+
const code = `
98+
package main
99+
100+
func main() {
101+
var x, y int
102+
fmt.Scan(&x, &y)
103+
104+
fmt.Println(x + y)
105+
}
106+
`
107+
108+
pathToCode, err := coderunner.SaveTemporaryCode(code)
109+
if err != nil {
110+
log.Fatal(err)
111+
}
112+
defer os.RemoveAll(filepath.Dir(pathToCode)) // nolint: errcheck
113+
114+
pathToExecutable, err := coderunner.CompileCode(pathToCode)
115+
if err != nil {
116+
log.Fatal(err)
117+
}
118+
119+
err = testrunner.RunCode(pathToExecutable, []testrunner.TestCase{
120+
{Input: "5 12", ExpectedOutput: "17\n"},
121+
{Input: "23 42", ExpectedOutput: "65\n"},
122+
})
123+
fmt.Printf("%v\n", err)
124+
125+
// Output:
126+
// <nil>
127+
}
128+
```
129+
130+
`testrunner.RunCode` (error):
131+
132+
```go
133+
package main
134+
135+
import (
136+
"fmt"
137+
"log"
138+
"os"
139+
"path/filepath"
140+
141+
coderunner "github.com/thewizardplusplus/go-code-runner"
142+
testrunner "github.com/thewizardplusplus/go-code-runner/test-runner"
143+
)
144+
145+
func main() {
146+
const code = `
147+
package main
148+
149+
func main() {
150+
var x, y int
151+
fmt.Scan(&x, &y)
152+
153+
fmt.Println(x + y)
154+
}
155+
`
156+
157+
pathToCode, err := coderunner.SaveTemporaryCode(code)
158+
if err != nil {
159+
log.Fatal(err)
160+
}
161+
defer os.RemoveAll(filepath.Dir(pathToCode)) // nolint: errcheck
162+
163+
pathToExecutable, err := coderunner.CompileCode(pathToCode)
164+
if err != nil {
165+
log.Fatal(err)
166+
}
167+
168+
err = testrunner.RunCode(pathToExecutable, []testrunner.TestCase{
169+
{Input: "5 12", ExpectedOutput: "17\n"},
170+
{Input: "23 42", ExpectedOutput: "100\n"},
171+
})
172+
fmt.Printf("%v\n", err)
173+
174+
// Output:
175+
// unexpected output (input - "23 42"): expected - "100\n", actual - "65\n"
176+
}
177+
```
178+
79179
## License
80180

81181
The MIT License (MIT)

test-runner/examples_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package testrunner_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
9+
coderunner "github.com/thewizardplusplus/go-code-runner"
10+
testrunner "github.com/thewizardplusplus/go-code-runner/test-runner"
11+
)
12+
13+
func ExampleRunCode_success() {
14+
const code = `
15+
package main
16+
17+
func main() {
18+
var x, y int
19+
fmt.Scan(&x, &y)
20+
21+
fmt.Println(x + y)
22+
}
23+
`
24+
25+
pathToCode, err := coderunner.SaveTemporaryCode(code)
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
defer os.RemoveAll(filepath.Dir(pathToCode)) // nolint: errcheck
30+
31+
pathToExecutable, err := coderunner.CompileCode(pathToCode)
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
err = testrunner.RunCode(pathToExecutable, []testrunner.TestCase{
37+
{Input: "5 12", ExpectedOutput: "17\n"},
38+
{Input: "23 42", ExpectedOutput: "65\n"},
39+
})
40+
fmt.Printf("%v\n", err)
41+
42+
// Output:
43+
// <nil>
44+
}
45+
46+
func ExampleRunCode_error() {
47+
const code = `
48+
package main
49+
50+
func main() {
51+
var x, y int
52+
fmt.Scan(&x, &y)
53+
54+
fmt.Println(x + y)
55+
}
56+
`
57+
58+
pathToCode, err := coderunner.SaveTemporaryCode(code)
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
defer os.RemoveAll(filepath.Dir(pathToCode)) // nolint: errcheck
63+
64+
pathToExecutable, err := coderunner.CompileCode(pathToCode)
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
69+
err = testrunner.RunCode(pathToExecutable, []testrunner.TestCase{
70+
{Input: "5 12", ExpectedOutput: "17\n"},
71+
{Input: "23 42", ExpectedOutput: "100\n"},
72+
})
73+
fmt.Printf("%v\n", err)
74+
75+
// Output:
76+
// unexpected output (input - "23 42"): expected - "100\n", actual - "65\n"
77+
}

0 commit comments

Comments
 (0)