Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:04
created

tests/parsing/CliParsingTest.py (11 issues)

1
import argparse
2
import unittest
3
4
from coalib.parsing.CliParsing import parse_cli, check_conflicts
5
6
7
class CliParserTest(unittest.TestCase):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unittest does not seem to be defined.
Loading history...
8
9
    def setUp(self):
10
        self.test_arg_parser = argparse.ArgumentParser()
11
        self.test_arg_parser.add_argument('-t', nargs='+', dest='test')
12
        self.test_arg_parser.add_argument('-S',
13
                                          '--settings',
14
                                          nargs='+',
15
                                          dest='settings')
16
17
    @staticmethod
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable staticmethod does not seem to be defined.
Loading history...
18
    def dict_from_sections(parsed_sections):
19
        parsed_dict = {}
20
        for section_name, section in parsed_sections.items():
21
            parsed_dict[section_name] = (
22
                set([(key,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable key does not seem to be defined.
Loading history...
23
                      str(value)) for key, value in section.contents.items()]))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable value does not seem to be defined.
Loading history...
24
        return parsed_dict
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable parsed_dict does not seem to be defined.
Loading history...
25
26
    def test_parse_cli(self):
27
        # regular parse
28
        parsed_sections = parse_cli(
29
            ['-t', 'ignored1', 'ignored2',
30
             '-t', 'taken',
31
             '-S', 'section1.key1,section2.key2=value1,value2',
32
             'section2.key2=only_this_value',
33
             'SECTION2.key2a=k2a',
34
             'invalid.=shouldnt_be_shown',
35
             '.=not_either',
36
             '.key=only_in_default',
37
             'default_key1,default_key2=single_value',
38
             'default_key3=first_value,second_value'],
39
            arg_parser=self.test_arg_parser)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
40
        expected_dict = {
41
            'default': {
42
                ("test", "taken"),
43
                ("key", "only_in_default"),
44
                ("default_key1", "single_value"),
45
                ("default_key2", "single_value"),
46
                ("default_key3", "first_value,second_value")},
47
            'section1': {
48
                ("key1", "value1,value2")},
49
            'section2': {
50
                ("key2", "only_this_value"),
51
                ("key2a", "k2a")}}
52
        self.assertEqual(parsed_sections["default"].name, "Default")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable parsed_sections does not seem to be defined.
Loading history...
53
        self.assertEqual(self.dict_from_sections(parsed_sections),
54
                         expected_dict)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable expected_dict does not seem to be defined.
Loading history...
55
56
    def test_check_conflicts(self):
57
        sections = parse_cli(arg_list=["--save", "--no-config"])
58
        with self.assertRaises(SystemExit) as cm:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SystemExit does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable cm does not seem to be defined.
Loading history...
59
            check_conflicts(sections)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable sections does not seem to be defined.
Loading history...
60
            self.assertEqual(cm.exception.code, 2)
61
62
        sections = parse_cli(arg_list=["--no-config", "-S", "val=42"])
63
        self.assertTrue(check_conflicts(sections))
64