Exemplo n.º 1
0
    def test_deprecation(self):
        import warnings
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument('-n', '--normal', action='store')
        argparser.add_argument(
            '-d', '--deprecated', action='store', deprecation=True)
        argparser.add_argument(
            '--bye', action=StoreDelimitedListBase, deprecation='bye')

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            # test that they store stuff
            args = argparser.parse_args(['-n', 'hello'])
            self.assertEqual(args.normal, 'hello')
            args = argparser.parse_args(['-d', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--deprecated', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--bye', 'hello,goodbye'])
            self.assertEqual(args.bye, ['hello', 'goodbye'])

        # and the warnings are triggered
        self.assertEqual(
            "option '-d' is deprecated", str(w[0].message))
        self.assertEqual(
            "option '--deprecated' is deprecated", str(w[1].message))
        self.assertEqual(
            "option '--bye' is deprecated: bye", str(w[2].message))

        stream = StringIO()
        argparser.print_help(file=stream)
        # deprecated options are not visible on help
        self.assertNotIn("--deprecated", stream.getvalue())
        self.assertNotIn("--bye", stream.getvalue())
        self.assertIn("--normal", stream.getvalue())
Exemplo n.º 2
0
 def test_dump(self):
     stream = StringIO()
     json_dump({'a': 'b', 'c': 'd'}, stream)
     self.assertEqual(
         stream.getvalue(),
         '{\n    "a": "b",\n    "c": "d"\n}'
     )
Exemplo n.º 3
0
 def test_sorted_standard(self):
     parser = argparse.ArgumentParser(formatter_class=SortedHelpFormatter,
                                      add_help=False)
     parser.add_argument('-z', '--zebra', help='make zebras')
     parser.add_argument('-a', '--alpaca', help='make alpacas')
     parser.add_argument('-s', '--sheep', help='make sheep')
     parser.add_argument('-g', '--goat', help='make goats')
     stream = StringIO()
     parser.print_help(file=stream)
     options = [
         line.split()[0] for line in stream.getvalue().splitlines()
         if '--' in line
     ]
     self.assertEqual(options, ['-a', '-g', '-s', '-z'])
Exemplo n.º 4
0
    def test_deprecation(self):
        import warnings
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument('-n', '--normal', action='store')
        argparser.add_argument('-d',
                               '--deprecated',
                               action='store',
                               deprecation=True)
        argparser.add_argument('--bye',
                               action=StoreDelimitedListBase,
                               deprecation='bye')

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            # test that they store stuff
            args = argparser.parse_args(['-n', 'hello'])
            self.assertEqual(args.normal, 'hello')
            args = argparser.parse_args(['-d', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--deprecated', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--bye', 'hello,goodbye'])
            self.assertEqual(args.bye, ['hello', 'goodbye'])

        # and the warnings are triggered
        self.assertEqual("option '-d' is deprecated", str(w[0].message))
        self.assertEqual("option '--deprecated' is deprecated",
                         str(w[1].message))
        self.assertEqual("option '--bye' is deprecated: bye",
                         str(w[2].message))

        stream = StringIO()
        argparser.print_help(file=stream)
        # deprecated options are not visible on help
        self.assertNotIn("--deprecated", stream.getvalue())
        self.assertNotIn("--bye", stream.getvalue())
        self.assertIn("--normal", stream.getvalue())
Exemplo n.º 5
0
 def test_sorted_standard(self):
     parser = argparse.ArgumentParser(
         formatter_class=SortedHelpFormatter, add_help=False)
     parser.add_argument('-z', '--zebra', help='make zebras')
     parser.add_argument('-a', '--alpaca', help='make alpacas')
     parser.add_argument('-s', '--sheep', help='make sheep')
     parser.add_argument('-g', '--goat', help='make goats')
     stream = StringIO()
     parser.print_help(file=stream)
     options = [
         line.split()[0]
         for line in stream.getvalue().splitlines() if
         '--' in line
     ]
     self.assertEqual(options, ['-a', '-g', '-s', '-z'])
Exemplo n.º 6
0
 def test_sorted_case_insensitivity(self):
     parser = argparse.ArgumentParser(formatter_class=SortedHelpFormatter,
                                      add_help=False)
     parser.add_argument('-z', '--zebra', help='make zebras')
     parser.add_argument('-a', '--alpaca', help='make alpacas')
     parser.add_argument('-A', '--anteater', help='make anteater')
     parser.add_argument('-S', '--SNAKE', help='make snake')
     parser.add_argument('-s', '--sheep', help='make sheep')
     parser.add_argument('-g', '--goat', help='make goats')
     stream = StringIO()
     parser.print_help(file=stream)
     options = [
         line.split()[0] for line in stream.getvalue().splitlines()
         if '--' in line
     ]
     # the case is unspecified due to in-place sorting
     self.assertEqual(options, ['-a', '-A', '-g', '-S', '-s', '-z'])
Exemplo n.º 7
0
 def test_sorted_case_insensitivity(self):
     parser = argparse.ArgumentParser(
         formatter_class=SortedHelpFormatter, add_help=False)
     parser.add_argument('-z', '--zebra', help='make zebras')
     parser.add_argument('-a', '--alpaca', help='make alpacas')
     parser.add_argument('-A', '--anteater', help='make anteater')
     parser.add_argument('-S', '--SNAKE', help='make snake')
     parser.add_argument('-s', '--sheep', help='make sheep')
     parser.add_argument('-g', '--goat', help='make goats')
     stream = StringIO()
     parser.print_help(file=stream)
     options = [
         line.split()[0]
         for line in stream.getvalue().splitlines() if
         '--' in line
     ]
     # the case is unspecified due to in-place sorting
     self.assertEqual(options, ['-a', '-A', '-g', '-S', '-s', '-z'])