~ubuntu-branches/ubuntu/wily/bandit/wily-proposed

« back to all changes in this revision

Viewing changes to tests/test_node_visitor.py

  • Committer: Package Import Robot
  • Author(s): Dave Walker (Daviey)
  • Date: 2015-07-22 09:01:39 UTC
  • Revision ID: package-import@ubuntu.com-20150722090139-fl0nluy0x8m9ctx4
Tags: upstream-0.12.0
ImportĀ upstreamĀ versionĀ 0.12.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding:utf-8 -*-
 
2
#
 
3
# Copyright 2014 Hewlett-Packard Development Company, L.P.
 
4
#
 
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
 
8
#
 
9
#      http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
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
 
15
# under the License.
 
16
 
 
17
import os
 
18
import ast
 
19
 
 
20
import unittest
 
21
from bandit.core import node_visitor
 
22
 
 
23
 
 
24
class StatementBufferTests(unittest.TestCase):
 
25
 
 
26
    def setUp(self):
 
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)
 
31
 
 
32
    def tearDown(self):
 
33
        pass
 
34
 
 
35
    def test_load_buffer(self):
 
36
        # Check buffer contains 10 statements
 
37
        self.assertEqual(10, len(self.buf._buffer))
 
38
 
 
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))
 
47
 
 
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))
 
56
 
 
57
    def test_get_next_count(self):
 
58
        # Check get_next returns exactly 10 statements
 
59
        count = 0
 
60
        stmt = self.buf.get_next()
 
61
        while stmt is not None:
 
62
            count = count + 1
 
63
            stmt = self.buf.get_next()
 
64
 
 
65
        self.assertEqual(10, count)
 
66
 
 
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)
 
73
 
 
74
    def test_linenumber_range(self):
 
75
        # Check linenumber_range returns corrent number of lines
 
76
        count = 9
 
77
        while count > 0:
 
78
            stmt = self.buf.get_next()
 
79
            count = count - 1
 
80
 
 
81
        # line 9 should be three lines long
 
82
        self.assertEqual(3, len(stmt['linerange']))
 
83
 
 
84
        # the range should be the correct line numbers
 
85
        self.assertEqual([11, 12, 13], list(stmt['linerange']))