Skip to content

Commit

Permalink
Example RedHatOfficial#9: Python interpreter math check as unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tisnik committed Nov 28, 2019
1 parent a0baff3 commit 674cda5
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions testing/gexpect/09_python_math_test.go
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()
}

0 comments on commit 674cda5

Please sign in to comment.