1 | import unittest |
||
2 | |||
3 | from pyprint.ConsolePrinter import ConsolePrinter |
||
4 | |||
5 | from coalib.bears.GlobalBear import GlobalBear |
||
6 | from coalib.bears.LocalBear import LocalBear |
||
7 | from coalib.misc.ContextManagers import simulate_console_inputs |
||
8 | from coalib.output.ConsoleInteraction import acquire_settings |
||
9 | from coalib.output.printers.LogPrinter import LogPrinter |
||
10 | from coalib.settings.Section import Section |
||
11 | from coalib.settings.SectionFilling import Setting, fill_section, fill_settings |
||
12 | from tests.TestUtilities import bear_test_module |
||
13 | |||
14 | |||
15 | class GlobalTestBear(GlobalBear): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
16 | |||
17 | def __init__(self): |
||
18 | GlobalBear.__init__(self, {}, Section("irrelevant"), None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
19 | |||
20 | @staticmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
21 | def get_non_optional_settings(): |
||
22 | return {"global name": "global help text", |
||
23 | "key": "this setting does exist"} |
||
24 | |||
25 | |||
26 | class LocalTestBear(LocalBear): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
27 | |||
28 | def __init__(self): |
||
29 | LocalBear.__init__(self, [], "", Section("irrelevant"), None) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
30 | |||
31 | @staticmethod |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
32 | def get_non_optional_settings(): |
||
33 | return {"local name": "local help text", |
||
34 | "global name": "this setting is needed by two bears"} |
||
35 | |||
36 | |||
37 | class SectionFillingTest(unittest.TestCase): |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
38 | |||
39 | def setUp(self): |
||
40 | self.log_printer = LogPrinter(ConsolePrinter()) |
||
41 | self.section = Section("test") |
||
42 | self.section.append(Setting("key", "val")) |
||
43 | |||
44 | def test_fill_settings(self): |
||
45 | sections = {"test": self.section} |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
46 | with simulate_console_inputs() as generator: |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
47 | fill_settings(sections, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
48 | acquire_settings, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
49 | self.log_printer) |
||
50 | self.assertEqual(generator.last_input, -1) |
||
51 | |||
52 | self.section.append(Setting("bears", "SpaceConsistencyTestBear")) |
||
53 | |||
54 | with simulate_console_inputs("True"), bear_test_module(): |
||
55 | local_bears, global_bears = fill_settings(sections, |
||
56 | acquire_settings, |
||
57 | self.log_printer) |
||
58 | self.assertEqual(len(local_bears["test"]), 1) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
59 | self.assertEqual(len(global_bears["test"]), 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
60 | |||
61 | self.assertEqual(bool(self.section["use_spaces"]), True) |
||
62 | self.assertEqual(len(self.section.contents), 3) |
||
63 | |||
64 | def test_fill_section(self): |
||
65 | # Use the same value for both because order isn't predictable (uses |
||
66 | # dict) |
||
67 | with simulate_console_inputs(0, 0): |
||
68 | new_section = fill_section(self.section, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
69 | acquire_settings, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
70 | self.log_printer, |
||
71 | [LocalTestBear, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
72 | GlobalTestBear, |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
73 | "an inappropriate string object here"]) |
||
74 | |||
75 | self.assertEqual(int(new_section["local name"]), 0) |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
76 | self.assertEqual(int(new_section["global name"]), 0) |
||
77 | self.assertEqual(new_section["key"].value, "val") |
||
78 | self.assertEqual(len(new_section.contents), 3) |
||
79 | |||
80 | # Shouldnt change anything the second time |
||
81 | new_section = fill_section(self.section, |
||
82 | acquire_settings, |
||
83 | self.log_printer, |
||
84 | [LocalTestBear, GlobalTestBear]) |
||
85 | |||
86 | self.assertTrue("local name" in new_section) |
||
87 | self.assertTrue("global name" in new_section) |
||
88 | self.assertEqual(new_section["key"].value, "val") |
||
89 | self.assertEqual(len(new_section.contents), 3) |
||
90 |