chiark / gitweb /
@@@ mdwopt buggy wip
[mLib-python] / t / t-ui.py
1 ### -*-python-*-
2 ###
3 ### Test ui functionality
4 ###
5 ### (c) 2019 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to mLib.
11 ###
12 ### mLib/Python is free software: you can redistribute it and/or modify it
13 ### under the terms of the GNU General Public License as published by the
14 ### Free Software Foundation; either version 2 of the License, or (at your
15 ### option) any later version.
16 ###
17 ### mLib/Python is distributed in the hope that it will be useful, but
18 ### WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ### General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python.  If not, write to the Free Software
24 ### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 ### USA.
26
27 import mLib as M
28 import os as OS
29 import sys as SYS
30 import testutils as T
31 import unittest as U
32
33 ###--------------------------------------------------------------------------
34 class TestUI (U.TestCase):
35
36   def test_quis(me):
37     old = M.quis
38     M.ego(OS.path.join("test", "thing")); me.assertEqual(M.quis, "thing")
39     buf = T.StringIO(); M.pquis("a simple $ test", file = buf)
40     me.assertEqual(buf.getvalue(), "a simple thing test")
41     M.ego(OS.path.join("other-thing")); me.assertEqual(M.quis, "other-thing")
42     M.ego(old)
43
44   def _capture_stderr(me):
45     pin, pout = OS.pipe(); OS.dup2(pout, 2); OS.close(pout)
46     return pin
47
48   @T.subprocess
49   def test_report(me):
50     pin = me._capture_stderr()
51     ref = T.bin("%s: all ok really\n" % M.quis)
52     M.moan("all ok really")
53     stuff = OS.read(pin, 32)
54     assert stuff == ref
55
56   @T.subprocess
57   def test_die(me):
58     pin = me._capture_stderr()
59     ref = T.bin("%s: so this is it\n" % M.quis)
60     try: M.die("so this is it", 123)
61     except SystemExit:
62       stuff = OS.read(pin, 32)
63       assert stuff == ref
64       assert SYS.exc_info()[1].code == 123
65     else:
66       raise AssertionError("die didn't exit")
67
68   def test_mdwopt(me):
69     mo = M.MdwOpt()
70     print(list(mo))
71
72 ###----- That's all, folks --------------------------------------------------
73
74 if __name__ == "__main__": U.main()