From 674cda53a64555ca434121684527340948b586f5 Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Thu, 28 Nov 2019 11:14:20 +0100 Subject: [PATCH] Example #9: Python interpreter math check as unit test --- testing/gexpect/09_python_math_test.go | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 testing/gexpect/09_python_math_test.go diff --git a/testing/gexpect/09_python_math_test.go b/testing/gexpect/09_python_math_test.go new file mode 100644 index 0000000..93921a8 --- /dev/null +++ b/testing/gexpect/09_python_math_test.go @@ -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<