From 5857bc79f77f1f35c219fa138619b4a65780bc4a Mon Sep 17 00:00:00 2001
From: Per Cederqvist <ceder@lysator.liu.se>
Date: Sat, 12 Mar 2005 22:48:36 +0000
Subject: [PATCH] Import test.base instead of importing unittest.  Removed
 top-level code -- use testdriver.py to run the tests instead. (TestSpawn):
 Inherit from test.base.TestCase. (TestSpawn.test_spawn_sleep_1): Use
 assertTimeDiff instead of 	assertAlmostEqual.
 (TestSpawn.test_spawn_sleep_1_compat): Ditto. (TestSpawn.test_hello_world):
 New test. (TestSpawn.test_stderr): New test. (TestSpawn.test_stderr_2): New
 test.

---
 test/test_spawn.py | 87 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 77 insertions(+), 10 deletions(-)

diff --git a/test/test_spawn.py b/test/test_spawn.py
index c1ba4e9..30bc9d1 100755
--- a/test/test_spawn.py
+++ b/test/test_spawn.py
@@ -1,12 +1,11 @@
-#!/usr/bin/env python
-import unittest
-
 import time
 
 import pcl_expect
 import pcl_expect.spawn
 
-class TestSpawn(unittest.TestCase):
+import test.base
+
+class TestSpawn(test.base.TestCase):
     def test_spawn_sleep_1(self):
         t0 = time.time()
         sleeper = pcl_expect.spawn.Spawn(['sleep', '1'])
@@ -21,8 +20,7 @@ class TestSpawn(unittest.TestCase):
         pid, status = sleeper.close()
         self.assertEqual(status, 0)
         t1 = time.time()
-        self.assertAlmostEqual(t0 + 1.0, t1, 1,
-                               'sleep 1 slept too long or too short')
+        self.assertTimeDiff(t0, t1, 1.0)
 
     def test_spawn_sleep_1_compat(self):
         t0 = time.time()
@@ -38,9 +36,78 @@ class TestSpawn(unittest.TestCase):
         pid, status = sleeper.close()
         self.assertEqual(status, 0)
         t1 = time.time()
-        self.assertAlmostEqual(t0 + 1.0, t1, 1,
-                               'sleep 1 slept too long or too short')
+        self.assertTimeDiff(t0, t1, 1.0)
+
+
+    def test_hello_world(self):
+        hw = pcl_expect.spawn.Spawn(['test/hello.sh'])
+        x = pcl_expect.Controller()
+        while x.loop():
+            if hw.re(x, "hello, world"):
+                break
+        x = pcl_expect.Controller()
+        while hw.eof(x):
+            break
+        pid, status = hw.close()
+        self.assertEqual(status, 0)
 
+    def test_stderr(self):
+        tool = pcl_expect.spawn.Spawn("test/tool.sh")
+        x = pcl_expect.Controller()
+        while x.loop():
+            if tool.re(x, "prompt: "):
+                break
+        tool.send("echo hello, world\n")
+        x = pcl_expect.Controller()
+        while x.loop():
+            if tool.re(x, "hello, world\r?\n"):
+                break
+        tool.send("stderr hello, world\n")
+        x = pcl_expect.Controller()
+        while x.loop():
+            if tool.re(x, "hello, world\r?\n"):
+                break
+        tool.send("bye\n")
+        found = 0
+        x = pcl_expect.Controller()
+        while x.loop():
+            if tool.eof(x):
+                break
+            elif tool.re(x, "Exiting\r?\n"):
+                found += 1
+        self.assertEqual(found, 1)
+        pid, status = tool.close()
+        self.assertEqual(status, 0)
 
-if __name__ == '__main__':
-    unittest.main()
+    def test_stderr_2(self):
+        (tool, stderr) = pcl_expect.spawn.spawn2("test/tool.sh")
+        x = pcl_expect.Controller()
+        while x.loop():
+            if tool.re(x, "prompt: "):
+                break
+        tool.send("echo hello, world\n")
+        x = pcl_expect.Controller()
+        while x.loop():
+            if tool.re(x, "hello, world\r?\n"):
+                break
+        tool.send("stderr hello, world\n")
+        x = pcl_expect.Controller()
+        while x.loop():
+            if stderr.re(x, "hello, world\r?\n"):
+                break
+        tool.send("bye\n")
+        found = 0
+        x = pcl_expect.Controller()
+        while x.loop():
+            if tool.eof(x):
+                break
+            elif tool.re(x, "Exiting\r?\n"):
+                found += 1
+        self.assertEqual(found, 1)
+        x = pcl_expect.Controller()
+        while x.loop():
+            if stderr.eof(x):
+                break
+        pid, status = tool.close()
+        self.assertEqual(status, 0)
+        stderr.close()
-- 
GitLab