Example #1
0
    def test_match_string_pattern(self):
        MAT = SourceString('Nekroze')
        alphas = 'abcdefghijklmnopqrstuvwxyz'

        assert MAT.match_string_pattern(alphas.upper()) == 'N'
        assert MAT.match_string_pattern(alphas.upper(), alphas) == 'Nekroze'
        assert MAT.match_string_pattern(alphas) == ''
Example #2
0
    def test_get_length(self):
        SRC = SourceString('hello world')

        assert SRC.get_length(5) == 'hello'
        assert SRC.get_length(11) == 'hello world'
        assert SRC.get_length(12) == ''
        assert SRC.get_length(12, True) == 'hello world'
Example #3
0
    def test_specials(self):
        MAT = SourceString('hello.world')
        MAT2 = SourceString('-1234')

        assert MAT.match_function_pattern(*pat.identifier) == 'hello'
        assert MAT.match_function_pattern(*pat.qualified) == 'hello.world'
        assert MAT2.match_function_pattern(*pat.integer) == '-1234'
Example #4
0
    def test_eat_string_multiline_chunk(self):
        SRC = SourceString('hello\nworld')

        SRC.eat_string('hello\nworld')
        assert SRC.row == 2
        assert SRC.col == 5
        assert SRC.get_char() == ''
Example #5
0
    def test_alphas(self):
        MAT = SourceString('hello world')
        MAT2 = SourceString('HEllo world')

        assert MAT.match_function_pattern(pat.alphal) == 'hello'
        assert MAT2.match_function_pattern(pat.alphau) == 'HE'
        assert MAT.match_function_pattern(pat.alpha) == 'hello'
Example #6
0
 def test_match_string_pattern_imp(self, string):
     MAT = SourceString(string)
     alphas = 'abcdefghijklmnopqrstuvwxyz'
     expected = ''
     for char in string:
         if char not in alphas:
             break
         expected += char
     assert MAT.match_string_pattern(alphas) == expected
Example #7
0
 def test_match_function_pattern_imp(self, string):
     MAT = SourceString(string)
     alphas = 'abcdefghijklmnopqrstuvwxyz'
     expected = ''
     for char in string:
         if not char.islower():
             break
         expected += char
     assert MAT.match_function_pattern(str.islower) == expected
Example #8
0
    def test_get_all_lines(self):
        base = 'hello\nworld\nthis\nis\na\ntest'
        SRC = SourceString(base)

        lines = [str(x) for x in SRC.get_all_lines()]
        expected = ['1   |hello\n', '2   |world\n', '3   |this\n', '4   |is\n',
            '5   |a\n', '6   |test']
        assert lines == expected
        lines = ''.join([repr(x) for x in SRC.get_all_lines()])
        assert lines == base
Example #9
0
    def test_count_indents_length_last_line(self):
        MAT = SourceString('Test100\n  test\n  more')

        assert MAT.count_indents_length_last_line(2) == (0, 0)
        MAT.eat_length(8)
        assert MAT.count_indents_length_last_line(2) == (0, 0)
        MAT.eat_length(7)
        assert MAT.count_indents_length_last_line(2) == (1, 2)
Example #10
0
    def test_numbers(self):
        MAT = SourceString()
        MAT.set_string('1234.5')
        MAT2 = SourceString()
        MAT2.set_string('-1234.5')

        assert MAT.match_string_pattern(pat.number) == '1234'
        assert MAT2.match_string_pattern(pat.number) == ''
Example #11
0
    def test_get_char(self):
        SRC = SourceString('hello world')

        assert SRC.get_char() == 'h'
        SRC.eat_length(10)
        assert SRC.get_char() == 'd'
        SRC.eat_length(1)
        assert SRC.eos
        assert SRC.get_char() == ''
Example #12
0
    def test_spew_length_multiline_peices(self):
        SRC = SourceString('hello\nworld')

        SRC.eat_length(11)
        assert SRC.row == 2
        assert SRC.col == 5
        assert SRC.get_char() == ''
        assert SRC.eos

        SRC.spew_length(5)
        assert SRC.row == 2
        assert SRC.col == 0
        assert SRC.get_char() == 'w'
        assert not SRC.eos

        SRC.spew_length(6)
        assert SRC.row == 1
        assert SRC.col == 0
        assert SRC.get_char() == 'h'
Example #13
0
    def test_match_string(self):
        MAT = SourceString('hello world\ntesting stuff')

        assert MAT.match_string('hello', 1) == True
        assert MAT.match_string('hello') == True
        assert MAT.match_string('hel', 1) == False
        assert MAT.match_string('hel') == True
        assert MAT.match_string('hello world', 1) == False
        assert MAT.match_string('hello world') == True
Example #14
0
    def test_eat_line(self):
        SRC = SourceString('hello\nworld')

        SRC.eat_line()
        assert SRC.row == 2
        assert SRC.col == 0
        assert SRC.get_char() == 'w'

        SRC.eat_line()
        assert SRC.row == 2
        assert SRC.col == 5
        assert SRC.get_char() == ''
Example #15
0
    def test_has_space(self):
        SRC = SourceString('hello world')

        assert SRC.has_space() == True
        assert SRC.has_space(11) == True
        assert SRC.has_space(12) == False
        SRC.eat_length(11)

        assert SRC.has_space() == False
Example #16
0
    def test_match_function_pattern(self):
        MAT = SourceString('Test100')

        assert MAT.match_function_pattern(str.isalpha) == 'Test'
        assert MAT.match_function_pattern(str.isalpha, str.isalnum) == 'Test100'
        assert MAT.match_function_pattern(str.isdigit) == ''

        assert MAT.match_function_pattern(lambda c: c == 'T' or c in 'te') == 'Te'
        lam = (lambda c: c == 'T', lambda c: c == 'e')
        assert MAT.match_function_pattern(*lam) == 'Te'
Example #17
0
    def test_get_lines(self):
        SRC = SourceString('hello\nworld\nthis\nis\na\ntest')

        lines = [str(x) for x in SRC.get_lines(1, 2)]
        assert lines == ['1   |hello\n', '2   |world\n']
        lines = ''.join([repr(x) for x in SRC.get_lines(0, 2)])
        assert lines == 'hello\nworld\n'

        lines = [str(x) for x in SRC.get_lines(5, 6)]
        assert lines == ['5   |a\n', '6   |test']
        lines = ''.join([repr(x) for x in SRC.get_lines(5, 6)])
        assert lines == 'a\ntest'

        assert SRC.get_lines(10, 20) == None
Example #18
0
    def test_get_string(self):
        SRC = SourceString('hello world')

        assert SRC.get_string() == 'hello'
        SRC.eat_length(5)
        assert SRC.get_string() == ''
        SRC.eat_length(1)
        assert SRC.get_string() == 'world'
        SRC.eat_length(5)
        assert SRC.get_string() == ''
        SRC.eat_length(5)
        assert SRC.eos
Example #19
0
    def test_get_line(self):
        SRC = SourceString('hello\nworld\nthis\nis\na\ntest')

        assert repr(SRC.get_line(0)) == 'hello\n'
        assert repr(SRC.get_line(2)) == 'this\n'
        assert SRC.get_line(20) == None
Example #20
0
    def test_get_current_line(self):
        SRC = SourceString('hello\nworld\nthis\nis\na\ntest')

        assert repr(SRC.get_current_line()) == 'hello\n'
        SRC.eat_string('hello\n')
        assert repr(SRC.get_current_line()) == 'world\n'
Example #21
0
    def test_eat_string_multiline_peices(self):
        SRC = SourceString('hello\nworld')

        SRC.eat_string(SRC.get_length(5))
        assert SRC.row == 1
        assert SRC.col == 5

        assert SRC.get_char() == '\n'
        SRC.eat_string(SRC.get_char())
        assert SRC.row == 2
        assert SRC.col == 0

        SRC.eat_string(SRC.get_length(5))
        assert SRC.row == 2
        assert SRC.col == 5
        assert SRC.get_char() == ''
Example #22
0
    def test_get_surrounding_lines(self):
        SRC = SourceString('hello\nworld\nthis\nis\na\ntest')

        lines = [str(x) for x in SRC.get_surrounding_lines()]
        assert lines == ['1   |hello\n', '2   |world\n']
        lines = ''.join([repr(x) for x in SRC.get_surrounding_lines()])
        assert lines == 'hello\nworld\n'

        SRC.eat_string('hello\nworld\n')

        lines = [str(x) for x in SRC.get_surrounding_lines()]
        assert lines == ['2   |world\n', '3   |this\n', '4   |is\n']
        lines = ''.join([repr(x) for x in SRC.get_surrounding_lines()])
        assert lines == 'world\nthis\nis\n'

        lines = [str(x) for x in SRC.get_surrounding_lines(1, 0)]
        assert lines == ['2   |world\n', '3   |this\n']
        lines = ''.join([repr(x) for x in SRC.get_surrounding_lines(1, 0)])
        assert lines == 'world\nthis\n'

        SRC.eat_string('this\nis\na\n')
        lines = [str(x) for x in SRC.get_surrounding_lines()]
        assert lines == ['5   |a\n', '6   |test']
        lines = ''.join([repr(x) for x in SRC.get_surrounding_lines()])
        assert lines == 'a\ntest'
Example #23
0
    def test_eat_string(self):
        SRC = SourceString('hello world')

        SRC.eat_string('hello world')
        assert SRC.eos
Example #24
0
    def test_skip_whitespace(self):
        MAT = SourceString('  \tTest100')
        MAT2 = SourceString('  \nTest100')

        MAT.skip_whitespace()
        assert MAT.get_char() == 'T'

        MAT2.skip_whitespace()
        assert MAT2.get_char() == '\n'
        MAT2.skip_whitespace(1)
        assert MAT2.get_char() == 'T'
Example #25
0
    def test_spew_length(self):
        SRC = SourceString('hello world')

        SRC.eat_string('hello world')
        assert SRC.eos
        SRC.spew_length(1)
        assert SRC.get_char() == 'd'
        SRC.spew_length(10)
        assert SRC.get_char() == 'h'
        SRC.spew_length(3)
        assert SRC.get_char() == 'h'
Example #26
0
    def test_match_any_string(self):
        MAT = SourceString('import partpy')
        strings = ['def', 'imp', 'import ', 'import']

        assert MAT.match_any_string(strings) == 'imp'
        assert MAT.match_any_string(strings, 1) == 'import'
Example #27
0
    def test_count_indents_length(self):
        MAT = SourceString('  \tTest100')

        assert MAT.count_indents_length(2, 1) == (2, 3)
        assert MAT.count_indents_length(2) == (1, 2)
Example #28
0
    def test_count_indents(self):
        MAT = SourceString('  \tTest100')

        assert MAT.count_indents(2, 1) == 2
        assert MAT.count_indents(2) == 1
Example #29
0
    def test_eol_distance_last(self):
        SRC = SourceString('hello world')

        assert SRC.eol_distance_last() == 0
        SRC.eat_string('hello')
        assert SRC.eol_distance_last() == 5
Example #30
0
    def test_match_any_char(self):
        MAT = SourceString('import partpy')
        alphas = 'abcdefghijklmnopqrstuvwxyz'

        assert MAT.match_any_char(alphas) == 'i'
        assert MAT.match_any_char(alphas.replace('i', '')) == ''