Skip to content

Commit

Permalink
ConsoleInteractorTest: Use simulate_console_inputs
Browse files Browse the repository at this point in the history
This makes the test cleaner and more secure as we're not fiddling with
overriding the builtins directly.
  • Loading branch information
sils committed May 25, 2015
1 parent f3df586 commit 4563cf0
Showing 1 changed file with 55 additions and 77 deletions.
132 changes: 55 additions & 77 deletions coalib/tests/output/ConsoleInteractorTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest
import sys
import os
import builtins

sys.path.insert(0, ".")
from coalib.results.result_actions.ResultAction import ResultAction
Expand All @@ -13,6 +12,7 @@
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.output.printers.NullPrinter import NullPrinter
from coalib.misc.i18n import _
from coalib.misc.ContextManagers import simulate_console_inputs
from coalib.output.ConsoleInteractor import ConsoleInteractor
from coalib.output.printers.ConsolePrinter import ConsolePrinter
from coalib.results.result_actions.ApplyPatchAction import ApplyPatchAction
Expand All @@ -26,8 +26,6 @@ def apply(self, result, original_file_dict, file_diff_dict, param):

class ConsoleInteractorTest(unittest.TestCase):
def setUp(self):
self._input = builtins.__dict__["input"]
builtins.__dict__["input"] = lambda x: x
self.log_printer = ConsolePrinter()
self.uut = ConsoleInteractor(self.log_printer)

Expand All @@ -42,44 +40,34 @@ def setUp(self):
else:
self.FileNotFoundError = FileNotFoundError

def tearDown(self):
builtins.__dict__["input"] = self._input

def test_require_settings(self):
self.assertRaises(TypeError, self.uut.acquire_settings, 0)
self.assertEqual(self.uut.acquire_settings({0: 0}), {})

self.assertEqual(self.uut.acquire_settings({"setting": ["help text",
"SomeBear"]}),
{"setting": self.uut.STR_GET_VAL_FOR_SETTING.format(
"setting",
"help text",
"SomeBear")})

self.assertEqual(self.uut.acquire_settings({"setting":
["help text",
"SomeBear",
"AnotherBear"]}),
{"setting": self.uut.STR_GET_VAL_FOR_SETTING.format(
"setting",
"help text",
"SomeBear" + _(" and ") + "AnotherBear")})

self.assertEqual(self.uut.acquire_settings({"setting":
["help text",
"SomeBear",
"AnotherBear",
"YetAnotherBear"]}),
{"setting": self.uut.STR_GET_VAL_FOR_SETTING.format(
"setting",
"help text",
"SomeBear, AnotherBear" + _(" and ") +
"YetAnotherBear")})
with simulate_console_inputs(0, 1, 2) as generator:
self.assertEqual(
self.uut.acquire_settings(
{"setting": ["help text", "SomeBear"]}),
{"setting": 0})

def test_print_result(self):
builtins.__dict__["input"] = lambda x: 0
self.assertEqual(
self.uut.acquire_settings(
{"setting": ["help text", "SomeBear", "AnotherBear"]}),
{"setting": 1})

self.assertEqual(
self.uut.acquire_settings(
{"setting": ["help text",
"SomeBear",
"AnotherBear",
"YetAnotherBear"]}),
{"setting": 2})

self.assertEqual(generator.last_input, 2)

self.uut.print_result(PatchResult("origin", "msg", {}), {})
def test_print_result(self):
with simulate_console_inputs(0):
self.uut.print_result(PatchResult("origin", "msg", {}), {})

(testfile, testfile_path) = tempfile.mkstemp()
os.close(testfile)
Expand All @@ -91,50 +79,50 @@ def test_print_result(self):
diff.delete_line(2)
diff.change_line(3, "3\n", "3_changed\n")

builtins.__dict__["input"] = lambda x: 1
with self.assertRaises(ValueError):
with simulate_console_inputs(1), self.assertRaises(ValueError):
self.uut.print_result(PatchResult("origin",
"msg",
{testfile_path: diff}),
file_dict)

# To assure user can rechose if he didn't chose wisely
input_generator = InputGenerator(["INVALID", -1, 1, 3])
builtins.__dict__["input"] = input_generator.generate_input
# To load current_section in ConsoleInteractor object
self.uut.begin_section(Section(""))
self.uut.print_result(PatchResult("origin",
"msg",
{testfile_path: diff}), file_dict)
self.assertEqual(input_generator.current_input, 2)
self.uut.finalize(file_dict)
with open(testfile_path) as f:
self.assertEqual(f.readlines(), ["1\n", "3_changed\n"])

os.remove(testfile_path)
os.remove(testfile_path + ".orig")

name, section = self.uut._get_action_info(TestAction().get_metadata())
self.assertEqual(str(section), " {param : 3}")
self.assertEqual(name, "TestAction")
with simulate_console_inputs("INVALID", -1, 1, 3) as input_generator:
# To load current_section in ConsoleInteractor object
self.uut.begin_section(Section(""))
self.uut.print_result(PatchResult("origin",
"msg",
{testfile_path: diff}),
file_dict)
self.assertEqual(input_generator.last_input, 2)
self.uut.finalize(file_dict)

with open(testfile_path) as f:
self.assertEqual(f.readlines(), ["1\n", "3_changed\n"])

os.remove(testfile_path)
os.remove(testfile_path + ".orig")

name, section = self.uut._get_action_info(
TestAction().get_metadata())
self.assertEqual(input_generator.last_input, 3)
self.assertEqual(str(section), " {param : 3}")
self.assertEqual(name, "TestAction")

# Check if the user is asked for the parameter only the first time.
# Use OpenEditorAction that needs this parameter (editor command).
input_generator = InputGenerator([1, "test_editor", 0, 1, 0])
# Choose open editor action
builtins.__dict__["input"] = input_generator.generate_input
PatchResult.get_actions = lambda self: [OpenEditorAction()]
with simulate_console_inputs(1, "test_editor", 0, 1, 0) as generator:
PatchResult.get_actions = lambda self: [OpenEditorAction()]

patch_result = PatchResult("origin", "msg", {testfile_path: diff})
patch_result.file = "f_b"
patch_result = PatchResult("origin", "msg", {testfile_path: diff})
patch_result.file = "f_b"

self.uut.print_result(patch_result, file_dict)
# choose action, choose editor, choose no action (-1 -> 2)
self.assertEqual(input_generator.current_input, 2)
self.uut.print_result(patch_result, file_dict)
# choose action, choose editor, choose no action (-1 -> 2)
self.assertEqual(generator.last_input, 2)

# It shoudn't ask for parameter again
self.uut.print_result(patch_result, file_dict)
self.assertEqual(input_generator.current_input, 4)
# It shoudn't ask for parameter again
self.uut.print_result(patch_result, file_dict)
self.assertEqual(generator.last_input, 4)

def test_static_functions(self):
q = queue.Queue()
Expand Down Expand Up @@ -268,15 +256,5 @@ def get_str_from_queue(q):
return result


class InputGenerator:
def __init__(self, inputs):
self.current_input = -1
self.inputs = inputs

def generate_input(self, x):
self.current_input += 1
return self.inputs[self.current_input]


if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 4563cf0

Please sign in to comment.