3 ### Test ui functionality
5 ### (c) 2019 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to mLib.
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.
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.
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,
33 ###--------------------------------------------------------------------------
34 class TestUI (U
.TestCase
):
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")
44 def _capture_stderr(me
):
45 pin
, pout
= OS
.pipe(); OS
.dup2(pout
, 2); OS
.close(pout
)
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)
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)
62 stuff
= OS
.read(pin
, 32)
64 assert SYS
.exc_info()[1].code
== 123
66 raise AssertionError("die didn't exit")
68 ###--------------------------------------------------------------------------
69 class TestMdwOpt (U
.TestCase
):
71 def test_argv_default(me
):
74 SYS
.argv
= ["just", "another", "Python", "hacker"]
76 me
.assertEqual(mo
.argv
, SYS
.argv
)
81 mo
= M
.MdwOpt(argv
= ["example",
90 "--switch", "--frob", "--toggle", "--no-frob",
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
,
99 ("toggle", 4, M
.OPTF_SWITCH
, "flags")],
100 flags
= M
.OPTF_NEGATION
)
101 me
.assertEqual(list(mo
), [("a", None, 0),
105 ("d", None, M
.OPTF_NEGATED
),
106 ("e", None, M
.OPTF_NEGATED
),
107 ("t", None, M
.OPTF_NEGATED
),
109 me
.assertEqual(mo
.argv
[mo
.ind
:], ["one", "two", "three"])
110 me
.assertEqual(mo
.state
.foo
, "beta")
111 me
.assertEqual(mo
.state
.flags
, 5)
113 ###----- That's all, folks --------------------------------------------------
115 if __name__
== "__main__": U
.main()