3
# Copyright 2014 Hewlett-Packard Development Company, L.P.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
21
from bandit.core import node_visitor
24
class StatementBufferTests(unittest.TestCase):
27
super(StatementBufferTests, self).setUp()
28
self.test_file = open("./examples/jinja2_templating.py")
29
self.buf = node_visitor.StatementBuffer()
30
self.buf.load_buffer(self.test_file)
35
def test_load_buffer(self):
36
# Check buffer contains 10 statements
37
self.assertEqual(10, len(self.buf._buffer))
39
def test_get_next(self):
40
# Check get_next returns an AST statement
41
stmt = self.buf.get_next()
42
self.assertTrue(isinstance(stmt['node'], ast.AST))
43
# Check get_next returned the first statement
44
self.assertEqual(1, stmt['linerange'][0])
45
# Check buffer has been reduced by one
46
self.assertEqual(9, len(self.buf._buffer))
48
def test_get_next_lookahead(self):
49
# Check get_next(pop=False) returns an AST statement
50
stmt = self.buf.get_next(pop=False)
51
self.assertTrue(isinstance(stmt['node'], ast.AST))
52
# Check get_next(pop=False) returned the first statement
53
self.assertEqual(1, stmt['linerange'][0])
54
# Check buffer remains the same length
55
self.assertEqual(10, len(self.buf._buffer))
57
def test_get_next_count(self):
58
# Check get_next returns exactly 10 statements
60
stmt = self.buf.get_next()
61
while stmt is not None:
63
stmt = self.buf.get_next()
65
self.assertEqual(10, count)
67
def test_get_next_empty(self):
68
# Check get_next on an empty buffer returns None
69
# self.test_file has already been read, so is empty file handle
70
self.buf.load_buffer(self.test_file)
71
stmt = self.buf.get_next()
72
self.assertEqual(None, stmt)
74
def test_linenumber_range(self):
75
# Check linenumber_range returns corrent number of lines
78
stmt = self.buf.get_next()
81
# line 9 should be three lines long
82
self.assertEqual(3, len(stmt['linerange']))
84
# the range should be the correct line numbers
85
self.assertEqual([11, 12, 13], list(stmt['linerange']))