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#7: Communication with Python interpreter
- Loading branch information
Showing
1 changed file
with
70 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,70 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
|
||
expect "github.com/Netflix/go-expect" | ||
) | ||
|
||
func TestPythonInterpreter(t *testing.T) { | ||
console, err := expect.NewConsole(expect.WithStdout(os.Stdout)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer console.Close() | ||
t.Log("Console created") | ||
|
||
command := exec.Command("python") | ||
command.Stdin = console.Tty() | ||
command.Stdout = console.Tty() | ||
command.Stderr = console.Tty() | ||
|
||
err = command.Start() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Log("Python interpreter started") | ||
time.Sleep(time.Second) | ||
|
||
str, err := console.Expect(expect.String("Python 2", "Python 3"), expect.WithTimeout(100*time.Millisecond)) | ||
if err != nil { | ||
t.Fatal("Python not detected") | ||
} | ||
t.Log("Python interpreter detected: " + str) | ||
|
||
if str == "Python 2" { | ||
console.SendLine("print 1,2,3") | ||
_, err = console.ExpectString("1 2 3") | ||
if err != nil { | ||
t.Fatal("print statement failure") | ||
} | ||
t.Log("print statement works as expected") | ||
_, err = console.ExpectString(">>> ") | ||
if err != nil { | ||
t.Fatal("prompt is not displayed") | ||
} | ||
} else { | ||
console.SendLine("print(1,2,3)") | ||
_, err = console.ExpectString("1 2 3") | ||
if err != nil { | ||
t.Fatal("print function failure") | ||
} | ||
t.Log("print function works as expected") | ||
_, err = console.ExpectString(">>> ") | ||
if err != nil { | ||
t.Fatal("prompt is not displayed") | ||
} | ||
} | ||
|
||
console.SendLine("quit()") | ||
|
||
err = command.Wait() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log("Done") | ||
} |