Permalink
Browse files

SettingsFilling: Flexible boolean input

Allows users to enter strings similar to those defined
in `coalib.misc.Constants.TRUE_STRINGS` and
`coalib.misc.Constants.FALSE_STRINGS` for boolean settings.

Closes #152
  • Loading branch information...
satwikkansal committed Aug 5, 2017
1 parent cd7a7dd commit 485b28b847689e18277ad928dea90bac4e5ea186
Showing with 55 additions and 1 deletion.
  1. +14 −1 coala_quickstart/generation/SettingsFilling.py
  2. +41 −0 tests/generation/SettingsFillingTest.py
@@ -1,6 +1,8 @@
from termcolor import colored
from coalib.settings.Setting import Setting
from coalib.misc.Constants import TRUE_STRINGS, FALSE_STRINGS
from coala_quickstart.generation.InfoMapping import INFO_SETTING_MAPS
from coala_utils.string_processing.Core import join_names
@@ -214,8 +216,19 @@ def require_setting(setting_name, setting_info, section):
if setting_info["type"]:
try:
user_input = setting_info["type"](user_input)
if setting_info["type"] is bool:
processed_input = user_input.strip().strip("!").lower()
if processed_input in TRUE_STRINGS:
user_input = "True"
elif processed_input in FALSE_STRINGS:
user_input = "False"
else:
raise ValueError
else:
setting_info["type"](user_input)
break
except ValueError:
print(colored(
STR_REPORT_INVALID_VALUE_TYPE.format(setting_info["type"]),
@@ -184,6 +184,47 @@ def test_fill_section_invalid_type(self):
self.assertEqual(new_section['key'].value, 'val')
self.assertEqual(len(new_section.contents), 3)
def test_fill_section_boolean_setting(self):
self.section = Section('test')
sections = {'test': self.section}
self.section.append(Setting('bears', 'SpaceConsistencyTestBear'))
with simulate_console_inputs(" hell yeah!!! ") as generator, \
bear_test_module():
local_bears, global_bears = fill_settings(
sections, acquire_settings, self.log_printer,
fill_section_method=fill_section,
extracted_info={})
self.assertEqual(generator.last_input, 0)
self.assertEqual(bool(self.section['use_spaces']), True)
self.section = Section('test')
sections = {'test': self.section}
self.section.append(Setting('bears', 'SpaceConsistencyTestBear'))
with simulate_console_inputs("not in a million years") as generator, \
bear_test_module():
local_bears, global_bears = fill_settings(
sections, acquire_settings, self.log_printer,
fill_section_method=fill_section,
extracted_info={})
self.assertEqual(generator.last_input, 0)
self.assertEqual(bool(self.section['use_spaces']), False)
self.section = Section('test')
sections = {'test': self.section}
self.section.append(Setting('bears', 'SpaceConsistencyTestBear'))
with simulate_console_inputs("don't know", "nah") as generator, \
bear_test_module():
local_bears, global_bears = fill_settings(
sections, acquire_settings, self.log_printer,
fill_section_method=fill_section,
extracted_info={})
self.assertEqual(generator.last_input, 1)
self.assertEqual(bool(self.section['use_spaces']), False)
def test_dependency_resolving(self):
sections = {'test': self.section}
self.section['bears'] = 'DependentBear'

0 comments on commit 485b28b

Please sign in to comment.