def test_default_value_argument_prompts_until_correct_type_given(monkeypatch, capsys): inputs = ['invalid', '2'] monkeypatch.setattr('builtins.input', lambda _: inputs.pop(0)) a = DefaultValueArgument('name', PrimitiveType(int), '3') assert a.get_value({}) == 2 captured = capsys.readouterr() assert escape_ansi(captured.out) == 'String "invalid" cannot be converted to int\n'
def test_create_unsupported_primitive_type(cls): with pytest.raises(TypeError): PrimitiveType(cls)
def test_value_representation(cls, value_in, value_out): t = PrimitiveType(cls) assert t.repr_value(value_in) == value_out
def test_cannot_cast_float_like_strings_to_int(): t = PrimitiveType(int) with pytest.raises(ValueError): assert t.cast('3.14')
def test_cannot_cast_strings_to_int(): t = PrimitiveType(int) with pytest.raises(ValueError): assert t.cast('string')
def test_no_default_value_argument_prompts_for_value_until_given(monkeypatch): inputs = ['', '', '2'] monkeypatch.setattr('builtins.input', lambda _: inputs.pop(0)) a = NoDefaultValueArgument('name', PrimitiveType(int)) assert a.get_value({}) == 2
def test_default_value_argument_cast_value(): a = DefaultValueArgument('name', PrimitiveType(int), 'default') assert a.get_value({'name': 3.14}) == 3
def test_default_value_argument_with_type_to_string(): a = DefaultValueArgument('name', PrimitiveType(int), 2) assert str(a) == 'name [default: 2, type: int]'
def test_no_default_value_argument_with_type_to_string(): a = NoDefaultValueArgument('name', PrimitiveType(int)) assert str(a) == 'name [type: int]'
def test_default_value_argument_mistmatched_type_of_default_value(): a = DefaultValueArgument('name', PrimitiveType(int), 'string') assert str(a) == 'name [default: string, type: int]'
def test_default_value_argument_passed_arguement_rewrites_default(): a = DefaultValueArgument('name', PrimitiveType(str), 'default') assert a.get_value({'name': 'value'}, use_default=True) == 'value'
def test_default_value_argument_force_usage_of_default(): a = DefaultValueArgument('name', PrimitiveType(str), 'default') assert a.get_value({}, use_default=True) == 'default'
def test_default_value_argument_empty_input_returns_default(monkeypatch): monkeypatch.setattr('builtins.input', lambda _: '') a = DefaultValueArgument('name', PrimitiveType(str), 'default') assert a.get_value({}) == 'default'
def test_cast_values(cls, value_in, value_out): t = PrimitiveType(cls) assert t.cast(value_in) == value_out
def test_no_default_value_argument_prompts_for_value(monkeypatch): monkeypatch.setattr('builtins.input', lambda _: 'string') a = NoDefaultValueArgument('name', PrimitiveType(str)) assert a.get_value({}) == 'string'
def test_none_is_cast_to_none(cls): t = PrimitiveType(cls) assert t.cast(None) is None
def test_cast_value_if_possible(): r = Result('3.14', PrimitiveType(float)) assert r.value == 3.14