def test_select_values(): default = [4., 12.] inputs = ['1 4', '1., 10', '-2', ' '] expecteds = [[1., 4.], [1., 10.], [-2.], default] for inp, exp in zip(inputs, expecteds): with replace_stdin(io.StringIO(inp)): found = InputTools.select_values("friends", ["dog ", "frog"], default) assert isinstance(found[0], type(exp[0])) tst.assert_allclose(found, exp) inputs = ['1 4', '1.3, 10.2', '-2'] expecteds = [[1., 4.], [1, 10], [-2]] converters = [float, int, int] for inp, exp, conv in zip(inputs, expecteds, converters): with replace_stdin(io.StringIO(inp)): found = InputTools.select_values("animals", ["dog ", "frog"], default, conv) tst.assert_allclose(found, exp)
def test_PreSelections(): # manually clear the last sugestions InputTools.last_selections = InputTools.PreSelections() messages1 = ["Blarfle", "good dog", "bork bork", "clip clop"] constant_lenghts = [-1, 5, 9, -1] functions = [ InputTools.get_file_name, lambda msg, clen: InputTools.list_complete(msg, [], clen), lambda msg, clen: InputTools.select_values(msg, ['bog', 'fog'], [2, 3], consistant_length=clen), lambda msg, clen: InputTools.select_value( msg, 1, consistant_length=clen) ] inputs = ["fog.meow", "boop", '3, 4', '5'] expected = ["fog.meow", "boop", [3., 4.], 5.] for i, func in enumerate(functions): with replace_stdin(io.StringIO(inputs[i])): func(messages1[i], constant_lenghts[i]) # this should have filled the last_selections for i, message in enumerate(messages1): # there may be stuff printed besides the message assert message in InputTools.last_selections.questions[i] assert constant_lenghts[ i] == InputTools.last_selections.consistant_length[i] try: assert expected[i] == InputTools.last_selections.answers[i] except ValueError: tst.assert_allclose(expected[i], InputTools.last_selections.answers[i]) # write and read with TempTestDir("tst") as dir_name: file_name = os.path.join(dir_name, "last_sel.dat") InputTools.last_selections.write(file_name) InputTools.pre_selections = InputTools.PreSelections(file_name) messages2 = ["Blarfle", "good cat", "bork bork!"] for i, msg in enumerate(messages2): found = functions[i](msg, constant_lenghts[i]) try: assert expected[i] == found except ValueError: tst.assert_allclose(expected[i], found)