예제 #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')
예제 #2
0
    def test_password(self):
        input_str = "\nfoo\nfooFFFF\nffffffffoooooobbbb\nFOOBAR!\nfoobar!\nFooBar!\nFooBar1!\nFooBar1!!\nfbr^"
        any_password_val = PasswordValidator()

        with redirect_stdin(StringIO(input_str)):
            result = get_input(validators=any_password_val)
            print(result)
            assert (result == 'foo')

        print(any_password_val)  # for code coverage

        with redirect_stdin(StringIO(input_str)):
            result = get_input(validators=[any_password_val], prompt='type in any password', required=False, hidden=True)
            print(result)
            assert (result is None)

        stronger_password_val = PasswordValidator(allowed='fobarFOB1!^', disallowed='[]', min_len=5, max_len=15, min_lower=4, min_upper=2, min_digits=1, min_puncts=2)

        with redirect_stdin(StringIO(input_str)):
            result = get_input(validators=[stronger_password_val],
                               prompt='type in a password (length=5-15, with at least 2 lower, 2 upper, 1 digit, and 2 puncts)', hidden=True)
            print(result)
            assert (result == 'FooBar1!!')

        disallowed_chars = 'aeiou!*&%2468'
        disallowed_chars_password_val = PasswordValidator(disallowed=disallowed_chars)

        with redirect_stdin(StringIO(input_str)):
            result = get_input(validators=[disallowed_chars_password_val], prompt='type in a password (type in a password(no vowels, even digits or !, *, \ %)')
            print(result)
            assert (result == 'fbr^')
예제 #3
0
    def test_choices_3(self):
        input_str_blank = "\n\n"

        input_str_y = " a\ny"

        input_str_n = "a\n  no"

        strip_cleaner = StripCleaner()

        with redirect_stdin(StringIO(input_str_y)):
            result = get_input(cleaners=strip_cleaner,
                               convertor=YesNoConvertor(),
                               default='Y')
            assert (result == 'yes')

        with redirect_stdin(StringIO(input_str_blank)):
            result = get_input(cleaners=strip_cleaner,
                               convertor=YesNoConvertor(),
                               default='Y')
            assert (result == 'yes')

        with redirect_stdin(StringIO(input_str_n)):
            result = get_input(cleaners=strip_cleaner,
                               convertor=YesNoConvertor(),
                               default='Y')
            assert (result == 'no')
예제 #4
0
    def test_get_input_int(self):
        input_str = u"""
            10
            5
            -1
            1
    
            """

        irv = RangeValidator(min_val=1, max_val=10)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='enter an integer (1<=x<=10)',
                               convertor=IntConvertor(),
                               validators=irv)
            print(result)
            assert (result == 10)

            result = get_input(prompt='enter an integer (1<=x<=10)',
                               convertor=IntConvertor(),
                               validators=irv)
            print(result)
            assert (result == 5)

            result = get_input(prompt='enter an integer (1<=x<=10)',
                               convertor=IntConvertor(),
                               validators=irv)
            print(result)
            assert (result == 1)

        print(self.int_convertor)  # for code coverage
예제 #5
0
    def test_capitalization_cleaner(self):
        input_str = 'foo Bar bLaT'
        sc = CapitalizationCleaner(style='lower')
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=sc)
            assert (result == 'foo bar blat')

        print(sc)

        sc = CapitalizationCleaner(style='upper')
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=sc)
            assert (result == 'FOO BAR BLAT')

        sc = CapitalizationCleaner(style='first_word')
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=sc)
            assert (result == 'Foo bar blat')

        sc = CapitalizationCleaner(style='all_words')
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=sc)
            assert (result == 'Foo Bar Blat')

        sc = CapitalizationCleaner(style='last_word')
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=sc)
            assert (result == 'foo bar Blat')

        with pytest.raises(ValueError):
            sc = CapitalizationCleaner(style=6)

        with pytest.raises(ValueError):
            sc = CapitalizationCleaner(style='bad_style')
예제 #6
0
    def test_choice_cleaner(self):
        input_str = 'bar\nf'
        color_choices = ['foo']
        cc = ChoiceCleaner(color_choices)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=cc)
            assert(result == 'bar')
            result = get_input(cleaners=cc)
            assert (result == 'foo')

        print(cc)
예제 #7
0
    def test_simple_str(self):
        input_str = 'foo\n\n'

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='Enter any string')
            assert (result == 'foo')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='Enter any string', required=True)
            assert (result == 'foo')

            result = get_input(prompt='Enter any string', required=False)
            assert (result is None)
예제 #8
0
 def test_subset_choice_cleaner(self):
     # test choice cleaner if one of the choices is the subset of another
     input_str = 'foo\nf\nfoob'
     color_choices = ['foo', 'foobar']
     cc = ChoiceCleaner(color_choices)
     with redirect_stdin(StringIO(input_str)):
         result = get_input(cleaners=cc)
         assert(result == 'foo')
         result = get_input(cleaners=cc)
         assert (result == 'f')
         result = get_input(cleaners=cc)
         assert (result == 'foobar')
     print(cc)
예제 #9
0
    def test_remove_cleaner(self):
        input_str = 'foo is bar'
        rc = RemoveCleaner(patterns=['is', u'bar'])
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=rc)
            assert(result == 'foo  ')

        print(rc)

        rc = RemoveCleaner(patterns=['is', 10])
        with pytest.raises(TypeError):
            with redirect_stdin(StringIO(input_str)):
                result = get_input(cleaners=rc)
예제 #10
0
    def test_case_sesitive_choice_cleaner(self):
        input_str = 'b\nbl\nBL\nf'
        color_choices = ['foo', 'bar', 'BLAT']
        cc = ChoiceCleaner(color_choices, case_sensitive=True)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=cc)
            assert (result == 'bar')
            result = get_input(cleaners=cc)
            assert (result == 'bl')
            result = get_input(cleaners=cc)
            assert (result == 'BLAT')
            result = get_input(cleaners=cc)
            assert (result == 'foo')

        print(cc)
예제 #11
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)
예제 #12
0
    def test_regex_cleaner(self):
        input_str = 'foo and bar'
        rc = RegexCleaner(pattern=r'\sAND\s', repl=' & ', flags=re.IGNORECASE)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=rc)
            assert(result == 'foo & bar')

        print(rc)
예제 #13
0
    def test_replace_cleaner(self):
        input_str = 'foo and bar and blat'
        rc = ReplaceCleaner(old='and', new='&')
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=rc)
            assert(result == 'foo & bar & blat')

        print(rc)

        input_str = 'foo and bar and blat'
        rc = ReplaceCleaner(old='and', new='&', count=1)
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=rc)
            assert (result == 'foo & bar and blat')

        with pytest.raises(TypeError):
            rc = ReplaceCleaner(old='and', new='&', bad_option='foo')
예제 #14
0
    def test_use_default_value(self):
        input_str = u"""

            """

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='Enter an integer',
                               convertor=IntConvertor(),
                               default=5)
            assert (result == 5)
예제 #15
0
    def test_ignore_bad_conversion(self):
        input_str = u"""
            foo
            101
            """

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt='Enter an integer',
                               convertor=IntConvertor())
            assert (result == 101)
예제 #16
0
    def test_choices(self):
        input_str = "\nfoo\nffffffffoooooobbbb\nFOOBAR!\nfoobar!\nFooBar!\nfoobar\nFooBar1!\nFooBar1!!\nfbr^"
        cv = ChoiceValidator(choices=['foobar', 'bar', 'blat'])

        with redirect_stdin(StringIO(input_str)):
            result = get_input(validators=cv)
            print(result)
            assert (result == 'foobar')

        print(cv)  # for code coverage
예제 #17
0
    def test_subset_choice(self):
        # make sure works if one value is a subset of another and case insenstive
        input_str = 'date'

        type_choices = ['Boolean', 'Date', 'DateTime']
        ft_cleaner = ChoiceCleaner(type_choices, case_sensitive=False)

        with redirect_stdin(StringIO(input_str)):
            result = get_input(prompt="Type", cleaners=ft_cleaner)
            assert (result == 'Date')
예제 #18
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])
예제 #19
0
    def test_exactly_val(self):
        # get zero - silly but makes more sense with the in any or not in validators
        input_str = u"""
            1
            0
            """

        with redirect_stdin(StringIO(input_str)):
            result = get_input(convertor=self.int_convertor,
                               validators=[self.exactly_0_validator],
                               prompt='Enter 0')
            assert (result == 0)
예제 #20
0
    def test_not_in(self):
        # get a non-zero integer
        input_str = u"""
            0
            -101
            """

        with redirect_stdin(StringIO(input_str)):
            result = get_input(convertor=self.int_convertor,
                               validators=[self.not_0_validator],
                               prompt='Enter a non-zero integer')
            assert (result == -101)
예제 #21
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
예제 #22
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)
예제 #23
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
예제 #24
0
    def test_get_input_list_3(self):
        input_str = u"""
            foo, bar
            -1,2,3
            """

        lc = ListConvertor(delimiter=None,
                           elem_get_input=GetInput(convertor=IntConvertor()))
        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=StripCleaner(), convertor=lc)
            print(result)
            assert (result == [-1, 2, 3])
예제 #25
0
    def test_get_0_to_10(self):
        input_str = u"""
            -1
            11
            0
            """

        with redirect_stdin(StringIO(input_str)):
            result = get_input(convertor=self.int_convertor,
                               validators=[self.zero_to_ten_validator],
                               prompt='Enter an integer between 0 and 10')
            assert (result == 0)
예제 #26
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)
예제 #27
0
    def test_get_pos_int(self):
        input_str = u"""
            -1
            0
            10
            """

        with redirect_stdin(StringIO(input_str)):
            result = get_input(convertor=IntConvertor(),
                               validators=self.pos_int_validator,
                               prompt='Enter a positive integer')
            assert (result == 10)
예제 #28
0
    def test_choices(self):
        input_str_blank = """
 
            """

        input_str = """licorice
            booger
            lemon 
            """

        colors = ['red', 'green', 'blue']
        good_flavors = ['cherry', 'lime', 'lemon', 'orange']
        bad_flavors = 'licorice'
        choices_validator = ChoiceValidator(choices=colors)
        good_flavor_validator = ChoiceValidator(choices=good_flavors)
        bad_flavor_validator = ChoiceValidator(choices=bad_flavors)
        not_in_choices_validator = NoneOfValidator(
            validators=[bad_flavor_validator])
        strip_cleaner = StripCleaner()
        lower_cleaner = CapitalizationCleaner()
        strip_and_lower_cleaners = [strip_cleaner, lower_cleaner]

        with redirect_stdin(StringIO(input_str_blank)):
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=not_in_choices_validator,
                               default='cherry')
            assert (result == 'cherry')

        with redirect_stdin(StringIO(input_str)):
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=not_in_choices_validator,
                               default='cherry')
            assert (result == 'booger')

        with redirect_stdin(StringIO(input_str)):
            validators = [good_flavor_validator, not_in_choices_validator]
            result = get_input(cleaners=strip_and_lower_cleaners,
                               validators=validators,
                               default='cherry')
            assert (result == 'lemon')
예제 #29
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)
예제 #30
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