6 # py3k has StringIO in a different module
8 from StringIO import StringIO
10 from io import StringIO
12 # FIXME: we need a way to import the options module from a public module
13 from gi._glib.option import OptionParser, OptionGroup, OptionValueError, \
14 make_option, BadOptionError
16 from compathelper import _bytes
18 class TestOption(unittest.TestCase):
19 EXCEPTION_MESSAGE = "This callback fails"
22 self.parser = OptionParser("NAMES...",
23 description="Option unit test")
24 self.parser.add_option("-t", "--test", help="Unit test option",
25 action="store_false", dest="test", default=True)
26 self.parser.add_option("--g-fatal-warnings",
28 dest="fatal_warnings",
31 def _create_group(self):
32 def option_callback(option, opt, value, parser):
33 raise Exception(self.EXCEPTION_MESSAGE)
36 "unittest", "Unit test options", "Show all unittest options",
38 make_option("-f", "-u", "--file", "--unit-file",
41 help="Unit test option"),
42 make_option("--test-integer",
45 help="Unit integer option"),
46 make_option("--callback-failure-test",
48 callback=option_callback,
50 help="Unit integer option"),
52 group.add_option("-t", "--test",
56 help="Unit test option")
57 self.parser.add_option_group(group)
60 def testParseArgs(self):
61 options, args = self.parser.parse_args(
65 options, args = self.parser.parse_args(
66 ["test_option.py", "foo"])
67 self.assertEquals(args, ["foo"])
69 options, args = self.parser.parse_args(
70 ["test_option.py", "foo", "bar"])
71 self.assertEquals(args, ["foo", "bar"])
73 def testParseArgsDoubleDash(self):
74 options, args = self.parser.parse_args(
75 ["test_option.py", "--", "-xxx"])
76 #self.assertEquals(args, ["-xxx"])
78 def testParseArgs(self):
79 options, args = self.parser.parse_args()
81 def testParseArgsGroup(self):
82 group = self._create_group()
84 options, args = self.parser.parse_args(
85 ["test_option.py", "--test", "-f", "test"])
87 self.failIf(options.test)
88 self.assertEqual(options.unit_file, "test")
90 self.failUnless(group.values.test)
91 self.failIf(self.parser.values.test)
92 self.assertEqual(group.values.unit_file, "test")
95 def testOptionValueError(self):
97 self.assertRaises(OptionValueError, self.parser.parse_args,
98 ["test_option.py", "--test-integer=text"])
100 def testBadOptionError(self):
101 self.assertRaises(BadOptionError,
102 self.parser.parse_args,
103 ["test_option.py", "--unknwon-option"])
105 def testOptionGroupConstructor(self):
106 self.assertRaises(TypeError, OptionGroup)
108 def testStandardError(self):
111 old_stderr = sys.stderr
114 self.parser.parse_args(
115 ["test_option.py", "--callback-failure-test"])
117 sys.stderr = old_stderr
119 assert (sio.getvalue().split('\n')[-2] ==
120 "Exception: " + self.EXCEPTION_MESSAGE)