File: test_option.py

package info (click to toggle)
pygobject 3.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,492 kB
  • ctags: 5,045
  • sloc: ansic: 20,987; python: 15,991; sh: 4,659; makefile: 569; xml: 31
file content (115 lines) | stat: -rw-r--r-- 4,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python

import unittest
import sys

# py3k has StringIO in a different module
try:
    from StringIO import StringIO
    StringIO  # pyflakes
except ImportError:
    from io import StringIO

from gi.repository import GLib


class TestOption(unittest.TestCase):
    EXCEPTION_MESSAGE = "This callback fails"

    def setUp(self):
        self.parser = GLib.option.OptionParser("NAMES...",
                                               description="Option unit test")
        self.parser.add_option("-t", "--test", help="Unit test option",
                               action="store_false", dest="test", default=True)
        self.parser.add_option("--g-fatal-warnings",
                               action="store_true",
                               dest="fatal_warnings",
                               help="dummy"),

    def _create_group(self):
        def option_callback(option, opt, value, parser):
            raise Exception(self.EXCEPTION_MESSAGE)

        group = GLib.option.OptionGroup(
            "unittest", "Unit test options", "Show all unittest options",
            option_list=[
                GLib.option.make_option("-f", "-u", "--file", "--unit-file",
                                        type="filename",
                                        dest="unit_file",
                                        help="Unit test option"),
                GLib.option.make_option("--test-integer",
                                        type="int",
                                        dest="test_integer",
                                        help="Unit integer option"),
                GLib.option.make_option("--callback-failure-test",
                                        action="callback",
                                        callback=option_callback,
                                        dest="test_integer",
                                        help="Unit integer option"),
            ])
        group.add_option("-t", "--test",
                         action="store_false",
                         dest="test",
                         default=True,
                         help="Unit test option")
        self.parser.add_option_group(group)
        return group

    def test_parse_args(self):
        options, args = self.parser.parse_args(
            ["test_option.py"])
        self.assertFalse(args)

        options, args = self.parser.parse_args(
            ["test_option.py", "foo"])
        self.assertEqual(args, [])

        options, args = self.parser.parse_args(
            ["test_option.py", "foo", "bar"])
        self.assertEqual(args, [])

    def test_parse_args_double_dash(self):
        options, args = self.parser.parse_args(
            ["test_option.py", "--", "-xxx"])
        # self.assertEqual(args, ["-xxx"])

    def test_parse_args_group(self):
        group = self._create_group()

        options, args = self.parser.parse_args(
            ["test_option.py", "--test", "-f", "test"])

        self.assertFalse(options.test)
        self.assertEqual(options.unit_file, "test")

        self.assertTrue(group.values.test)
        self.assertFalse(self.parser.values.test)
        self.assertEqual(group.values.unit_file, "test")
        self.assertFalse(args)

    def test_option_value_error(self):
        self._create_group()
        self.assertRaises(GLib.option.OptionValueError, self.parser.parse_args,
                          ["test_option.py", "--test-integer=text"])

    def test_bad_option_error(self):
        self.assertRaises(GLib.option.BadOptionError,
                          self.parser.parse_args,
                          ["test_option.py", "--unknwon-option"])

    def test_option_group_constructor(self):
        self.assertRaises(TypeError, GLib.option.OptionGroup)

    def test_standard_error(self):
        self._create_group()
        sio = StringIO()
        old_stderr = sys.stderr
        sys.stderr = sio
        try:
            self.parser.parse_args(
                ["test_option.py", "--callback-failure-test"])
        finally:
            sys.stderr = old_stderr

        assert (sio.getvalue().split('\n')[-2] ==
                "Exception: " + self.EXCEPTION_MESSAGE)