tests: don't test for specific device labels
[pygobject.git] / tests / test_option.py
blob251eb3adc8d1bb42e3018abae948edc419581810
1 #!/usr/bin/env python
3 from __future__ import absolute_import
5 import unittest
7 # py3k has StringIO in a different module
8 try:
9 from StringIO import StringIO
10 StringIO # pyflakes
11 except ImportError:
12 from io import StringIO
14 from gi.repository import GLib
16 from .helper import capture_exceptions
19 class TestOption(unittest.TestCase):
21 def setUp(self):
22 self.parser = GLib.option.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",
27 action="store_true",
28 dest="fatal_warnings",
29 help="dummy"),
31 def _create_group(self):
32 def option_callback(option, opt, value, parser):
33 raise Exception("foo")
35 group = GLib.option.OptionGroup(
36 "unittest", "Unit test options", "Show all unittest options",
37 option_list=[
38 GLib.option.make_option("-f", "-u", "--file", "--unit-file",
39 type="filename",
40 dest="unit_file",
41 help="Unit test option"),
42 GLib.option.make_option("--test-integer",
43 type="int",
44 dest="test_integer",
45 help="Unit integer option"),
46 GLib.option.make_option("--callback-failure-test",
47 action="callback",
48 callback=option_callback,
49 dest="test_integer",
50 help="Unit integer option"),
52 group.add_option("-t", "--test",
53 action="store_false",
54 dest="test",
55 default=True,
56 help="Unit test option")
57 self.parser.add_option_group(group)
58 return group
60 def test_integer(self):
61 self._create_group()
62 options, args = self.parser.parse_args(
63 ["--test-integer", "42", "bla"])
64 assert options.test_integer == 42
65 assert args == ["bla"]
67 def test_file(self):
68 self._create_group()
70 options, args = self.parser.parse_args(
71 ["--file", "fn", "bla"])
72 assert options.unit_file == "fn"
73 assert args == ["bla"]
75 def test_mixed(self):
76 self._create_group()
78 options, args = self.parser.parse_args(
79 ["--file", "fn", "--test-integer", "12", "--test",
80 "--g-fatal-warnings", "nope"])
82 assert options.unit_file == "fn"
83 assert options.test_integer == 12
84 assert options.test is False
85 assert options.fatal_warnings is True
86 assert args == ["nope"]
88 def test_parse_args(self):
89 options, args = self.parser.parse_args([])
90 self.assertFalse(args)
92 options, args = self.parser.parse_args(["foo"])
93 self.assertEqual(args, ["foo"])
95 options, args = self.parser.parse_args(["foo", "bar"])
96 self.assertEqual(args, ["foo", "bar"])
98 def test_parse_args_double_dash(self):
99 options, args = self.parser.parse_args(["--", "-xxx"])
100 self.assertEqual(args, ["--", "-xxx"])
102 def test_parse_args_group(self):
103 group = self._create_group()
105 options, args = self.parser.parse_args(
106 ["--test", "-f", "test"])
108 self.assertFalse(options.test)
109 self.assertEqual(options.unit_file, "test")
111 self.assertTrue(group.values.test)
112 self.assertFalse(self.parser.values.test)
113 self.assertEqual(group.values.unit_file, "test")
114 self.assertFalse(args)
116 def test_option_value_error(self):
117 self._create_group()
118 self.assertRaises(GLib.option.OptionValueError, self.parser.parse_args,
119 ["--test-integer=text"])
121 def test_bad_option_error(self):
122 self.assertRaises(GLib.option.BadOptionError,
123 self.parser.parse_args,
124 ["--unknwon-option"])
126 def test_option_group_constructor(self):
127 self.assertRaises(TypeError, GLib.option.OptionGroup)
129 def test_standard_error(self):
130 self._create_group()
132 with capture_exceptions() as exc:
133 self.parser.parse_args(["--callback-failure-test"])
135 assert len(exc) == 1
136 assert exc[0].value.args[0] == "foo"