def test__singlecharmatch__chunkmatch_2(): cmp = SingleCharMatchPattern('*', None) # Explicitly check matching the first character chk = PositionedChunk('*\n', 13, 20, None) sub_chunk = cmp.chunkmatch(chk) assert sub_chunk is not None assert sub_chunk == PositionedChunk('*', 13, 20, 'match')
def test__singlecharmatch__chunkmatch_1(): cmp = SingleCharMatchPattern('*', None) # Check that it only matches once! chk = PositionedChunk('aefal "fuin^ef" uynl*fa**', 13, 20, None) sub_chunk = cmp.chunkmatch(chk) assert sub_chunk is not None assert sub_chunk == PositionedChunk('*', 13 + 20, 20, 'match')
def test__cli__formatters__violations(): # check not just the formatting, but the ordering v = { 'foo': [ RuleViolation( PositionedChunk('blah', 1, 25, 'context'), RuleGhost('A', 'DESC'), []), RuleViolation( PositionedChunk('blah', 2, 21, 'context'), RuleGhost('B', 'DESC'), [])], 'bar': [ RuleViolation( PositionedChunk('blah', 10, 2, 'context'), RuleGhost('C', 'DESC'), [])] } f = format_violations(v) k = sorted(['foo', 'bar']) chk = { 'foo': ["L: 21 | P: 3 | B | DESC", "L: 25 | P: 2 | A | DESC"], 'bar': ["L: 2 | P: 11 | C | DESC"] } chk2 = [] for elem in k: chk2 = chk2 + [format_filename(elem)] + chk[elem] chk2 = '\n'.join(chk2) assert escape_ansi(f) == escape_ansi(chk2)
def test__rules__std__L005(): # L005 is about spaces before commas cs = ChunkString(PositionedChunk('1', 0, 20, 'content'), PositionedChunk(' ', 0, 21, 'whitespace'), PositionedChunk(',', 0, 22, 'comma')) rs = StandardRuleSet() vs = rs.evaluate_chunkstring(cs) assert any([v.rule.code == 'L005' for v in vs])
def test__rules__base__chunkrule(): """ Check functionality with a rule which returns a chunk """ # A rule that returns a chunk test_chunk = PositionedChunk('foo', 1, 20, 'a') TRuleD = BaseRule.rule('TRuleD', "NA", lambda c, m: test_chunk) r = TRuleD() eval_chunk = PositionedChunk('bar', 1, 20, 'a') # Check that the violation refers to the other chunk assert r.evaluate(eval_chunk).chunk == test_chunk
def test__matcherbag__chunkmatch_a(): a = CharMatchPattern('a', 'foo') b = CharMatchPattern('b', 'bar') m = MatcherBag(a, b) chk = PositionedChunk('asdfbjkebkjaekljds', 13, 20, None) matches = m.chunkmatch(chk) assert len(matches) == 2 assert matches == [ (PositionedChunk('asdfbjkebkja', 13, 20, 'match'), 0, a), (PositionedChunk('bjkeb', 13 + 4, 20, 'match'), 4, b)]
def test__rules__base__ruleset_chunkstring(): """ An extension of the above test, but applied to a chunkstring """ rs = TRuleSet() cs = ChunkString(PositionedChunk('foo', 1, 20, 'a'), PositionedChunk('bar', 1, 21, 'b')) vs = rs.evaluate_chunkstring(cs) # We should get two instances of rule A assert len(vs) == 2 assert vs[0].rule.code == 'TRuleA' assert vs[0].chunk.chunk == 'foo' assert vs[1].rule.code == 'TRuleA' assert vs[1].chunk.chunk == 'bar'
def test__recursive__lex_chunk_buffer(): # This test requires recursion rl = RecursiveLexer() # The whitespace on the end of a comment should be it's own chunk pc_list = [ PositionedChunk('SELECT\n', 0, 1, None), PositionedChunk('NOTHING\n', 0, 2, None) ] res, _ = rl.lex_chunk_buffer(pc_list) assert res.context_list() == [ 'content', 'whitespace', 'content', 'whitespace' ] assert res[1].chunk == '\n' assert res[3].chunk == '\n'
def test__recursive__basic_1(): rl = RecursiveLexer() pc = PositionedChunk(' ', 0, 1, None) res, _ = rl.lex(pc) assert isinstance(res, ChunkString) assert len(res) == 1 assert res[0].chunk == ' '
def test__rules__std__L004(): cs = ChunkString(PositionedChunk(' \n', 0, 20, 'whitespace'), PositionedChunk('\t\n', 0, 21, 'whitespace')) # Check individually, there's no errors # First (alone should not raise) rs = StandardRuleSet() vs = rs.evaluate(cs[0]) assert not any([v.rule.code == 'L004' for v in vs]) # Second (alone should not raise) rs = StandardRuleSet() vs = rs.evaluate(cs[1]) assert not any([v.rule.code == 'L004' for v in vs]) # Combined (which should raise an L004) rs = StandardRuleSet() vs = rs.evaluate_chunkstring(cs) assert any([v.rule.code == 'L004' for v in vs])
def test__cli__formatters__violation(): """ NB Position is 1 + start_pos """ c = PositionedChunk('foobarbar', 10, 20, 'context') r = RuleGhost('A', 'DESC') v = RuleViolation(c, r, []) f = format_violation(v) assert escape_ansi(f) == "L: 20 | P: 11 | A | DESC"
def test__recursive__multi_whitespace_a(): rl = RecursiveLexer() pc = PositionedChunk(' SELECT \n', 0, 1, None) res, _ = rl.lex(pc) assert isinstance(res, ChunkString) assert len(res) == 3 assert res[0].context == 'whitespace' assert res[1].chunk == 'SELECT'
def test__recursive__comment_a(): # This test requires recursion rl = RecursiveLexer() # The whitespace on the end of a comment should be it's own chunk pc = PositionedChunk('SELECT -- Testing Comment\n', 0, 1, None) res, _ = rl.lex(pc) assert res.context_list() == [ 'content', 'whitespace', 'comment', 'whitespace' ] assert res[3].chunk == '\n'
def test__recursive__multi_whitespace_b(): # This test requires recursion rl = RecursiveLexer() pc = PositionedChunk(' SELECT foo \n', 0, 1, None) res, _ = rl.lex(pc) assert isinstance(res, ChunkString) assert len(res) == 5 assert res[0].context == 'whitespace' assert res[1].chunk == 'SELECT' assert res[3].chunk == 'foo' assert res[3].start_pos == 13
def test__matcherbag__chunkmatch_c(): """ A more complicated matcher test, explicitly testing sorting, with ambiguous matchers """ a = SingleCharMatchPattern('a', 'bim', priority=2) b = SingleCharMatchPattern('a', 'bar', priority=2) c = SingleCharMatchPattern('a', 'foo', priority=3) m = MatcherBag(a, b, c) chk = PositionedChunk('asd', 11, 2, None) matches = m.chunkmatch(chk) matcher_ordered = [m[2] for m in matches] assert matcher_ordered == [c, b, a]
def test__matcherbag__chunkmatch_b(): """ A more complicated matcher test, explicitly testing sorting """ k = CharMatchPattern('k', 'bim') b = CharMatchPattern('b', 'bar') a = CharMatchPattern('a', 'foo') s = SingleCharMatchPattern('s', 'ess') r = RegexMatchPattern(r'e[a-z][a-df-z]+e[a-z]', 'eee') m = MatcherBag(k, b, a, r, s) chk = PositionedChunk('asdfbjkebkjaekljds', 11, 2, None) matches = m.chunkmatch(chk) assert matches == [ (PositionedChunk('asdfbjkebkja', 11, 2, 'match'), 0, a), (PositionedChunk('s', 11 + 1, 2, 'match'), 1, s), (PositionedChunk('bjkeb', 11 + 4, 2, 'match'), 4, b), (PositionedChunk('kebk', 11 + 6, 2, 'match'), 6, k), (PositionedChunk('ebkjaek', 11 + 7, 2, 'match'), 7, r)]
def test__charmatch__chunkmatch_3(): # Check for an no match scenario cmp = CharMatchPattern('a', None) chk = PositionedChunk('sdflkg;j;d;sflkgjds', 13, 20, None) sub_chunk = cmp.chunkmatch(chk) assert sub_chunk is None
def test__rules__std__L001(): rs = StandardRuleSet() c = PositionedChunk(' \n', 10, 20, 'whitespace') assert any([v.rule.code == 'L001' for v in rs.evaluate(c)])
def test__charmatch__chunkmatch_1(): cmp = CharMatchPattern('"', None) chk = PositionedChunk('aefal "fuin^ef" uynl*fa', 13, 20, None) sub_chunk = cmp.chunkmatch(chk) assert sub_chunk is not None assert sub_chunk == PositionedChunk('"fuin^ef"', 13 + 6, 20, 'match')
def test__charmatch__chunkmatch_2(): cmp = CharMatchPattern('a', 'foo') chk = PositionedChunk('asdfbjkebkjaekljds', 13, 20, None) sub_chunk = cmp.chunkmatch(chk) assert sub_chunk == PositionedChunk('asdfbjkebkja', 13, 20, 'match')
def test__linter__linted_file__apply_corrections_to_fileobj(): chunk = PositionedChunk('foofoofoo', 9, 2, None) fo = StringIO("12345678\n123456789foofoofoo654fish\n1234567") LintedFile.apply_corrections_to_fileobj( fo, [PositionedCorrection(chunk, 'bar')]) assert fo.getvalue() == "12345678\n123456789bar654fish\n1234567"
def test__chunklist__simple_content(): # Raise an exception if we try to create with anything but chunks cs = ChunkString(PositionedChunk('foobarbar', 1, 20, 'a'), PositionedChunk('foobarbar', 1, 21, 'b')) assert list(cs.simple_list()) == [('foobarbar', 'a'), ('foobarbar', 'b')]
def test__chunk__split(): c = PositionedChunk('foobarbar', 10, 20, None) a, b = c.split_at(3) assert a == PositionedChunk('foo', 10, 20, None) assert b == PositionedChunk('barbar', 13, 20, None)
def test__chunk__split_context_error(): c = PositionedChunk('foobarbar', 10, 20, 'context') with pytest.raises(RuntimeError): c.split_at(4)
def test__singlecharmatch__none(): cmp = SingleCharMatchPattern('*', None) chk = PositionedChunk('aefal "fuin^ef" uynlfa', 13, 20, None) sub_chunk = cmp.chunkmatch(chk) assert sub_chunk is None
def test__rules__base__violation_tuple(): r = TRuleA() v = r.evaluate(PositionedChunk('foo', 1, 20, 'a')) t = v.check_tuple() assert t == ('TRuleA', 20, 1)
def test__chunk__subchunk(): c = PositionedChunk('foobarbar', 10, 20, None) r = c.subchunk(3, 6) assert r == PositionedChunk('bar', 13, 20, None)