Ejemplo n.º 1
0
    def testParseNumericOption(self):
        """Tests the _ParseNumericOption function."""
        test_helper = test_lib.TestHelper()

        expected_integer = 123
        options = cli_test_lib.TestOptions()
        options.test = expected_integer

        integer = test_helper._ParseNumericOption(options, u'test')
        self.assertEqual(integer, expected_integer)

        options = cli_test_lib.TestOptions()

        integer = test_helper._ParseNumericOption(options, u'test')
        self.assertIsNone(integer)

        integer = test_helper._ParseNumericOption(
            options, u'test', default_value=expected_integer)
        self.assertEqual(integer, expected_integer)

        options = cli_test_lib.TestOptions()
        options.test = b'abc'

        with self.assertRaises(errors.BadConfigOption):
            test_helper._ParseNumericOption(options, u'test')
Ejemplo n.º 2
0
    def testParseStringOption(self):
        """Tests the _ParseStringOption function."""
        encoding = sys.stdin.encoding

        # Note that sys.stdin.encoding can be None.
        if not encoding:
            encoding = locale.getpreferredencoding()

        test_helper = test_lib.TestHelper()

        expected_string = u'Test Unicode string'
        options = cli_test_lib.TestOptions()
        options.test = expected_string

        string = test_helper._ParseStringOption(options, u'test')
        self.assertEqual(string, expected_string)

        options = cli_test_lib.TestOptions()

        string = test_helper._ParseStringOption(options, u'test')
        self.assertIsNone(string)

        string = test_helper._ParseStringOption(options,
                                                u'test',
                                                default_value=expected_string)
        self.assertEqual(string, expected_string)

        options = cli_test_lib.TestOptions()
        options.test = expected_string.encode(encoding)

        string = test_helper._ParseStringOption(options, u'test')
        self.assertEqual(string, expected_string)

        if encoding and encoding == u'UTF-8':
            options = cli_test_lib.TestOptions()
            options.test = (
                b'\xad\xfd\xab\x73\x99\xc7\xb4\x78\xd0\x8c\x8a\xee\x6d\x6a\xcb\x90'
            )

            with self.assertRaises(errors.BadConfigOption):
                test_helper._ParseStringOption(options, u'test')