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): |
16
|
|
|
|
17
|
|
|
def __init__(self): |
18
|
|
|
GlobalBear.__init__(self, {}, Section("irrelevant"), None) |
19
|
|
|
|
20
|
|
|
@staticmethod |
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): |
27
|
|
|
|
28
|
|
|
def __init__(self): |
29
|
|
|
LocalBear.__init__(self, [], "", Section("irrelevant"), None) |
30
|
|
|
|
31
|
|
|
@staticmethod |
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): |
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} |
46
|
|
|
with simulate_console_inputs() as generator: |
47
|
|
|
fill_settings(sections, |
48
|
|
|
acquire_settings, |
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) |
59
|
|
|
self.assertEqual(len(global_bears["test"]), 0) |
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, |
69
|
|
|
acquire_settings, |
70
|
|
|
self.log_printer, |
71
|
|
|
[LocalTestBear, |
72
|
|
|
GlobalTestBear]) |
73
|
|
|
|
74
|
|
|
self.assertEqual(int(new_section["local name"]), 0) |
75
|
|
|
self.assertEqual(int(new_section["global name"]), 0) |
76
|
|
|
self.assertEqual(new_section["key"].value, "val") |
77
|
|
|
self.assertEqual(len(new_section.contents), 3) |
78
|
|
|
|
79
|
|
|
# Shouldnt change anything the second time |
80
|
|
|
new_section = fill_section(self.section, |
81
|
|
|
acquire_settings, |
82
|
|
|
self.log_printer, |
83
|
|
|
[LocalTestBear, GlobalTestBear]) |
84
|
|
|
|
85
|
|
|
self.assertTrue("local name" in new_section) |
86
|
|
|
self.assertTrue("global name" in new_section) |
87
|
|
|
self.assertEqual(new_section["key"].value, "val") |
88
|
|
|
self.assertEqual(len(new_section.contents), 3) |
89
|
|
|
|
90
|
|
|
def test_dependency_resolving(self): |
91
|
|
|
sections = {"test": self.section} |
92
|
|
|
self.section['bears'] = "DependentBear" |
93
|
|
|
with simulate_console_inputs("True"), bear_test_module(): |
94
|
|
|
fill_settings(sections, acquire_settings, self.log_printer) |
95
|
|
|
|
96
|
|
|
self.assertEqual(bool(self.section["use_spaces"]), True) |
97
|
|
|
|