summaryrefslogtreecommitdiff
path: root/tests/ecore/test_08_exe.py
diff options
context:
space:
mode:
authorDave Andreoli <dave@gurumeditation.it>2016-08-14 13:02:19 +0200
committerDave Andreoli <dave@gurumeditation.it>2016-08-14 13:02:19 +0200
commit4bd421562d2c2403ef2000ce3d903b991eae7f6e (patch)
tree53b2c2fef2616b6101149554c4d44f7f21bc6f14 /tests/ecore/test_08_exe.py
parentd6fc38ea59e2c1438fa9bd1a579ba11747e3632d (diff)
Improve the ecore test suite
no more print and lots more asserts
Diffstat (limited to 'tests/ecore/test_08_exe.py')
-rw-r--r--tests/ecore/test_08_exe.py132
1 files changed, 88 insertions, 44 deletions
diff --git a/tests/ecore/test_08_exe.py b/tests/ecore/test_08_exe.py
index b5a8635..adde85f 100644
--- a/tests/ecore/test_08_exe.py
+++ b/tests/ecore/test_08_exe.py
@@ -1,86 +1,130 @@
1#!/usr/bin/env python 1#!/usr/bin/env python
2 2
3from efl import ecore 3
4import os
4import unittest 5import unittest
5import logging 6import logging
6 7
8from efl import ecore
9
10
11script_path = os.path.dirname(os.path.realpath(__file__))
12
7 13
8class TestExe(unittest.TestCase): 14class TestExe(unittest.TestCase):
9 def testInit(self): 15 def testInit(self):
16 self.num_add = 0
17 self.num_del = 0
18 self.num_data = 0
19
10 # basic read flags with line support 20 # basic read flags with line support
11 flags = ecore.ECORE_EXE_PIPE_READ | ecore.ECORE_EXE_PIPE_READ_LINE_BUFFERED 21 flags = ecore.ECORE_EXE_PIPE_READ | \
22 ecore.ECORE_EXE_PIPE_READ_LINE_BUFFERED | \
23 ecore.ECORE_EXE_TERM_WITH_PARENT
12 24
13 # simple ls -la output, monitor for add, del and data (stdout) 25 # EXE 1: simple ls -la output, monitor for add, del and data (stdout)
14 def on_add(x, event, a, b, c): 26 def on_add(x, event, a, b, c):
15 print("ecore.Exe added:") 27 self.assertEqual(x, exe1)
16 print(" exe : %s" % x)
17 print(" event: %s" % event)
18 print(" extra: arguments: a=%s, b=%s, c=%s" % (a, b, c))
19 print("")
20
21 self.assertEqual(x, exe)
22 self.assertIsInstance(event, ecore.EventExeAdd) 28 self.assertIsInstance(event, ecore.EventExeAdd)
23 self.assertEqual(a, 1) 29 self.assertEqual(a, 1)
24 self.assertEqual(b, 2) 30 self.assertEqual(b, 2)
25 self.assertEqual(c, 3) 31 self.assertEqual(c, 3)
32 self.num_add += 1
33 return ecore.ECORE_CALLBACK_RENEW
26 34
27 def on_del(x, event): 35 def on_del(x, event):
28 print("ecore.Exe deleted:") 36 self.assertEqual(x, exe1)
29 print(" exe : %s" % x) 37 self.assertIsInstance(event, ecore.EventExeDel)
30 ecore.timer_add(1.0, ecore.main_loop_quit) 38 self.num_del += 1
39 return ecore.ECORE_CALLBACK_RENEW
31 40
32 def on_data(x, event): 41 def on_data(x, event):
33 self.assertEqual(x, exe) 42 self.assertEqual(x, exe1)
34 self.assertIsInstance(event, ecore.EventExeData) 43 self.assertIsInstance(event, ecore.EventExeData)
35 print("ecore.Exe data on stdout:") 44 self.assertTrue(len(event.lines) >= 13) #we have at least 13 files here
36 print(" Exe : %s" % repr(exe)) 45 self.num_data += 1
37 print(" Event : %s" % repr(event)) 46 return ecore.ECORE_CALLBACK_RENEW
38 for l in event.lines:
39 print(" %s" % repr(l))
40 print()
41
42 exe = ecore.Exe("ls -la", flags)
43 exe.on_add_event_add(on_add, 1, c=3, b=2)
44 exe.on_del_event_add(on_del)
45 exe.on_data_event_add(on_data)
46
47 47
48 exe1 = ecore.Exe('ls -l "%s"' % script_path, flags)
49 exe1.on_add_event_add(on_add, 1, c=3, b=2)
50 exe1.on_del_event_add(on_del)
51 exe1.on_data_event_add(on_data)
48 52
49 # use C-api like event handler, will catch all 3 Exe() instances 53 # EXE 2: use C-api like event handler, will catch all 3 Exe() instances
50 def catch_all_exe_add(event): 54 def catch_all_exe_add(event, pos1, pos2, karg1):
51 print("") 55 self.assertIsInstance(event, ecore.EventExeAdd)
52 print(">>> EXE ADDED: %s" % event) 56 self.assertEqual(pos1, "pos1")
53 print("") 57 self.assertEqual(pos2, "pos2")
54 return 1 58 self.assertEqual(karg1, "k")
55 59 self.num_add += 1
56 ecore.on_exe_add_event_add(catch_all_exe_add) 60 return ecore.ECORE_CALLBACK_RENEW
57 ecore.Exe("ls -l /", flags) 61
62 def catch_all_exe_del(event):
63 self.assertIsInstance(event, ecore.EventExeDel)
64 self.num_del += 1
65 return ecore.ECORE_CALLBACK_RENEW
66
67 def catch_all_exe_data(event):
68 self.assertIsInstance(event, ecore.EventExeData)
69 self.num_data += 1
70 return ecore.ECORE_CALLBACK_RENEW
58 71
72 ecore.on_exe_add_event_add(catch_all_exe_add, "pos1", "pos2", karg1="k")
73 ecore.on_exe_del_event_add(catch_all_exe_del)
74 ecore.on_exe_data_event_add(catch_all_exe_data)
75 exe2 = ecore.Exe('uname -a', flags)
59 76
60 77
61 # start cat, send data to it, then read, then send again, then kill it 78 # EXE 3: start "cat", send data to it, then read, send again and kill
62 def on_cat_pipe_add(x, event): 79 def on_cat_pipe_add(x, event):
80 self.assertEqual(x, exe3)
81 self.assertIsInstance(event, ecore.EventExeAdd)
63 x.send(b"123\nabc\nxyz\n") 82 x.send(b"123\nabc\nxyz\n")
83 self.num_add += 1
84 return ecore.ECORE_CALLBACK_RENEW
85
86 def on_cat_pipe_del(x, event):
87 self.assertEqual(x, exe3)
88 self.assertIsInstance(event, ecore.EventExeDel)
89
90 self.num_del += 1
91 return ecore.ECORE_CALLBACK_RENEW
64 92
65 def on_cat_pipe_data(x, event): 93 def on_cat_pipe_data(x, event):
94 self.assertEqual(x, exe3)
95 self.assertIsInstance(event, ecore.EventExeData)
66 self.assertEqual(event.lines, ["123", "abc", "xyz"]) 96 self.assertEqual(event.lines, ["123", "abc", "xyz"])
67 x.on_data_event_del(on_cat_pipe_data) 97 x.on_data_event_del(on_cat_pipe_data)
98 self.num_data += 1
99 return ecore.ECORE_CALLBACK_CANCEL
68 100
69 def on_cat_pipe_data2(x, event): 101 def on_cat_pipe_data2(x, event):
70 print("cat pipe output:") 102 self.assertEqual(x, exe3)
71 print("\n".join(event.lines)) 103 self.assertIsInstance(event, ecore.EventExeData)
72 print("")
73 x.send("some\nmore\nlines!\n") 104 x.send("some\nmore\nlines!\n")
74 if event.lines and event.lines[-1] == "lines!": 105 if event.lines and event.lines[-1] == "lines!":
75 x.on_data_event_del(on_cat_pipe_data2) 106 x.on_data_event_del(on_cat_pipe_data2)
76 x.kill() # otherwise it will stay there forever (will be detached) 107 x.kill()
108 self.num_data += 1
109 return ecore.ECORE_CALLBACK_RENEW
110
111 exe3 = ecore.Exe("cat", flags | ecore.ECORE_EXE_PIPE_WRITE)
112 exe3.on_add_event_add(on_cat_pipe_add)
113 exe3.on_del_event_add(on_cat_pipe_del)
114 exe3.on_data_event_add(on_cat_pipe_data)
115 exe3.on_data_event_add(on_cat_pipe_data2)
77 116
78 cat_pipe = ecore.Exe("cat", flags | ecore.ECORE_EXE_PIPE_WRITE)
79 cat_pipe.on_add_event_add(on_cat_pipe_add)
80 cat_pipe.on_data_event_add(on_cat_pipe_data)
81 cat_pipe.on_data_event_add(on_cat_pipe_data2)
82 117
118 # start everything (assuming 2 secs is enough for ls)
119 t = ecore.timer_add(2, ecore.main_loop_quit)
83 ecore.main_loop_begin() 120 ecore.main_loop_begin()
121 t.delete()
122
123
124 # all the callback has been called?
125 self.assertEqual(self.num_add, 5)
126 self.assertEqual(self.num_del, 5)
127 self.assertEqual(self.num_data, 8)
84 128
85 129
86if __name__ == '__main__': 130if __name__ == '__main__':