diff options
author | DongHun Kwak <dh0128.kwak@samsung.com> | 2017-07-12 08:35:07 +0900 |
---|---|---|
committer | DongHun Kwak <dh0128.kwak@samsung.com> | 2017-07-12 08:35:08 +0900 |
commit | df82aa0a770d4b3e52e96eb5c60b528ddcdb2343 (patch) | |
tree | bbf6d1c2270fe7057773ff1c2cf578ff40f704da /tests/test_option.py | |
parent | 095413828bd9b33e57b352f9ca80473172611202 (diff) | |
download | pygobject2-df82aa0a770d4b3e52e96eb5c60b528ddcdb2343.tar.gz pygobject2-df82aa0a770d4b3e52e96eb5c60b528ddcdb2343.tar.bz2 pygobject2-df82aa0a770d4b3e52e96eb5c60b528ddcdb2343.zip |
Imported Upstream version 2.26.0upstream/2.26.0
Change-Id: I616aeca320e366d54dd6e188c260b64639305c49
Signed-off-by: DongHun Kwak <dh0128.kwak@samsung.com>
Diffstat (limited to 'tests/test_option.py')
-rw-r--r-- | tests/test_option.py | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/tests/test_option.py b/tests/test_option.py new file mode 100644 index 0000000..a6ecc98 --- /dev/null +++ b/tests/test_option.py @@ -0,0 +1,120 @@ +#!/usr/bin/env python + +import unittest +import sys + +# py3k has StringIO in a different module +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +from glib.option import OptionParser, OptionGroup, OptionValueError, \ + make_option, BadOptionError + +from compathelper import _bytes + +class TestOption(unittest.TestCase): + EXCEPTION_MESSAGE = "This callback fails" + + def setUp(self): + self.parser = 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 = OptionGroup( + "unittest", "Unit test options", "Show all unittest options", + option_list = [ + make_option("-f", "-u", "--file", "--unit-file", + type="filename", + dest="unit_file", + help="Unit test option"), + make_option("--test-integer", + type="int", + dest="test_integer", + help="Unit integer 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 testParseArgs(self): + options, args = self.parser.parse_args( + ["test_option.py"]) + self.failIf(args) + + options, args = self.parser.parse_args( + ["test_option.py", "foo"]) + self.assertEquals(args, ["foo"]) + + options, args = self.parser.parse_args( + ["test_option.py", "foo", "bar"]) + self.assertEquals(args, ["foo", "bar"]) + + def testParseArgsDoubleDash(self): + options, args = self.parser.parse_args( + ["test_option.py", "--", "-xxx"]) + #self.assertEquals(args, ["-xxx"]) + + def testParseArgs(self): + options, args = self.parser.parse_args() + + def testParseArgsGroup(self): + group = self._create_group() + + options, args = self.parser.parse_args( + ["test_option.py", "--test", "-f", "test"]) + + self.failIf(options.test) + self.assertEqual(options.unit_file, "test") + + self.failUnless(group.values.test) + self.failIf(self.parser.values.test) + self.assertEqual(group.values.unit_file, "test") + self.failIf(args) + + def testOptionValueError(self): + self._create_group() + self.assertRaises(OptionValueError, self.parser.parse_args, + ["test_option.py", "--test-integer=text"]) + + def testBadOptionError(self): + self.assertRaises(BadOptionError, + self.parser.parse_args, + ["test_option.py", "--unknwon-option"]) + + def testOptionGroupConstructor(self): + self.assertRaises(TypeError, OptionGroup) + + def testStandardError(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) + |