chiark / gitweb /
@@@ remove debugging print
[mLib-python] / t / t-ui.py
CommitLineData
81f68b64
MW
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
27import mLib as M
28import os as OS
29import sys as SYS
30import testutils as T
31import unittest as U
32
33###--------------------------------------------------------------------------
34class 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
d33275b6
MW
68###--------------------------------------------------------------------------
69class TestMdwOpt (U.TestCase):
70
71 def test_argv_default(me):
72 old_argv = SYS.argv
73 try:
74 SYS.argv = ["just", "another", "Python", "hacker"]
75 mo = M.MdwOpt()
76 me.assertEqual(mo.argv, SYS.argv)
77 finally:
78 SYS.argv = old_argv
79
80 def test_basic(me):
81 mo = M.MdwOpt(argv = ["example",
82 "one",
83 "-abcarg",
84 "--opt",
85 "+de",
86 "two",
87 "--no-thing",
88 "-f", "alpha",
89 "--foo", "beta",
90 "--switch", "--frob", "--toggle", "--no-frob",
91 "three"],
92 shortopt = "abc:d+e+f:",
93 longopt = [("opt", "o"),
94 ("thing", "t", M.OPTF_NEGATE),
95 ("foo", None, M.OPTF_ARGREQ, "foo"),
96 ("switch", 1, M.OPTF_SWITCH, "flags"),
97 ("frob", 2, M.OPTF_SWITCH | M.OPTF_NEGATE,
98 "flags"),
99 ("toggle", 4, M.OPTF_SWITCH, "flags")],
100 flags = M.OPTF_NEGATION)
101 me.assertEqual(list(mo), [("a", None, 0),
102 ("b", None, 0),
103 ("c", "arg", 0),
104 ("o", None, 0),
105 ("d", None, M.OPTF_NEGATED),
106 ("e", None, M.OPTF_NEGATED),
107 ("t", None, M.OPTF_NEGATED),
108 ("f", "alpha", 0)])
109 me.assertEqual(mo.argv[mo.ind:], ["one", "two", "three"])
110 me.assertEqual(mo.state.foo, "beta")
111 me.assertEqual(mo.state.flags, 5)
fcc8f4ca 112
81f68b64
MW
113###----- That's all, folks --------------------------------------------------
114
115if __name__ == "__main__": U.main()