Example #1
0
    def test_length(self):
        input_str = u"""
            1
            foo
            foobar
            foob
            fb
            """

        lv = LengthValidator()
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), validators=lv)
            print(result)
            assert (result == '1')

        print(lv)   # for code coverage

        lv = LengthValidator(min_len=2)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), validators=lv)
            print(result)
            assert (result == 'foo')

        lv = LengthValidator(max_len=2)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), validators=lv)
            print(result)
            assert (result == '1')

        lv = LengthValidator(min_len=4, max_len=5)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), validators=lv)
            print(result)
            assert (result == 'foob')
Example #2
0
    def test_strip_cleaner(self):
        input_str = '  \t foo  \nf'
        sc = StripCleaner(lstrip=True, rstrip=True)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=sc)
            assert (result == 'foo')

        print(sc)
Example #3
0
    def test_none_of(self):
        input_str = u"""
            -1
            6
            16
            2
            """

        nov = NoneOfValidator(validators=[RangeValidator(0,5), RangeValidator(10,15)])
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=nov)
            print(result)
            assert (result == -1)

        print(nov)   # for code coverage

        nov = NoneOfValidator(validators=RangeValidator(-2,5))
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=nov)
            print(result)
            assert (result == 6)
Example #4
0
    def test_list(self):
        input_str = u"""
            1
            3,4,5,6,7
            2,3,4
            """

        lc = ListConvertor(elem_get_input=GetInput(convertor=IntConvertor()))
        lv = ListValidator(len_validators=RangeValidator(min_val=2, max_val=7))
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=lc, validators=lv)
            print(result)
            assert (result == [3,4,5,6,7])

        print(lv)   # for code coverage

        lv = ListValidator(len_validators=RangeValidator(min_val=2), elem_validators=RangeValidator(max_val=6))
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=lc, validators=lv)
            print(result)
            assert (result == [2,3,4])
Example #5
0
    def test_get_input_list(self):
        input_str = u"""
            foo, bar, blat
            """

        lc = ListConvertor()
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=lc)
            print(result)
            assert (result == ['foo', 'bar', 'blat'])

        print(lc)  # for code coverage
Example #6
0
    def test_get_input_yes_no(self):
        input_str = u"""
            foo
            No
            """

        ync = YesNoConvertor()
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=ync)
            print(result)
            assert (result == 'no')
        print(ync)
Example #7
0
    def test_get_input_date(self):
        input_str = u"""
            9/4/2017
            """

        dc = DateConvertor()
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=dc)
            print(result)
            assert (str(result) == '2017-09-04 00:00:00')

        print(dc)  # for code coverage
Example #8
0
    def test_equal(self):
        input_str = u"""
            1
            3
            """

        ev = EqualToValidator(3)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=ev)
            print(result)
            assert (result == 3)

        print(ev)   # for code coverage
Example #9
0
    def test_get_boolean_false(self):
        input_str = u"""
            10

            f
            """

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='enter a boolean (True/False)',
                               cleaners=StripCleaner(),
                               convertor=self.bool_convertor)
            print(result)
            assert (result == False)
Example #10
0
def on_button():
    value = entry1.get()
    valid, processed_value = process_value(value,
                                           cleaners=StripCleaner(),
                                           convertor=IntConvertor(),
                                           validators=RangeValidator(
                                               min_val=1, max_val=10),
                                           error_callback=tk_msg_box_error)

    if valid:
        messagebox.showinfo("Integer is...",
                            "Integer is good: {}".format(processed_value))
        top.quit()
Example #11
0
    def test_any_of(self):
        input_str = u"""
            -1
            6
            16
            2
            """

        av = AnyOfValidator(validators=[RangeValidator(0,5), RangeValidator(10,15)])
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=av)
            print(result)
            assert (result == 2)

        print(av)   # for code coverage

        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=None)
            print(result)
            assert (result == -1)

        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=RangeValidator(5,10))
            print(result)
            assert (result == 6)

        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=16)
            print(result)
            assert (result == 16)

        av = AnyOfValidator(validators=EqualToValidator(16))
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=av)
            print(result)
            assert (result == 16)

        av = AnyOfValidator(validators=16)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=av)
            print(result)
            assert (result == 16)

        av = AnyOfValidator(validators=None)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=IntConvertor(), validators=av)
            print(result)
            assert (result == -1)
Example #12
0
This example shows the RemoveCleaner to strip out unwanted characters and the RegexCleaner to check
the phone number has 10 digits and doesn't start with '0' or '1'.

A more sophisticated phone validator comes with validus (see: [https://shopnilsazal.github.io/validus/usage.html?highlight=phone#validus.isphone])

Len Wanger, 2017
"""

from cooked_input import get_input, RegexValidator
from cooked_input import StripCleaner, RemoveCleaner, RegexCleaner

if __name__ == '__main__':
    phone_validator = [
        RegexValidator(pattern=r'^[2-9]\d{9}$',
                       regex_desc='a 10 digit phone number')
    ]
    remove_cleaners = [
        StripCleaner(),
        RemoveCleaner(patterns=list('()-, ')),
        RegexCleaner(pattern=r'^[01]', repl=r'')
    ]

    print(
        'Get a phone number from the user. You can format it anyway you want (spaces, dashes, parenthesis, and leading 0 or 1 stripped out)'
    )
    result = get_input(cleaners=remove_cleaners,
                       validators=phone_validator,
                       prompt='Please enter a phone number')
    print(result)