forked from RedHatOfficial/GoCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example RedHatOfficial#9: Python interpreter math check as unit test
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ThomasRooney/gexpect" | ||
) | ||
|
||
func expectOutput(t *testing.T, child *gexpect.ExpectSubprocess, output string) { | ||
err := child.ExpectTimeout(output, time.Second) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func expectPrompt(t *testing.T, child *gexpect.ExpectSubprocess) { | ||
expectOutput(t, child, ">>> ") | ||
} | ||
|
||
func sendCommand(t *testing.T, child *gexpect.ExpectSubprocess, command string) { | ||
err := child.SendLine(command) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestPythonInterpreter(t *testing.T) { | ||
child, err := gexpect.Spawn("python") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log("Python interpreter started") | ||
|
||
strs, err := child.ExpectRegexFind("Python [23]") | ||
if err != nil { | ||
t.Fatal("Python not detected") | ||
} | ||
t.Log("Python interpreter detected: " + strs[0]) | ||
|
||
for i := uint(1); i < 10; i++ { | ||
sendCommand(t, child, fmt.Sprintf("2**%d", i)) | ||
expectOutput(t, child, fmt.Sprintf("%d", 1<<i)) | ||
t.Logf("Math is ok for input %d", i) | ||
} | ||
|
||
expectPrompt(t, child) | ||
sendCommand(t, child, "quit()") | ||
|
||
child.Wait() | ||
} |