summaryrefslogtreecommitdiffstats
path: root/plugins/externaltools/tests/testlinkparsing.py
blob: 3b8a78e6a771db3c1a52ce69f78cb510e54c9e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
#
#    Copyright (C) 2009-2010  Per Arneng <per.arneng@anyplanet.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import unittest
from linkparsing import LinkParser


class TestLinkParser(unittest.TestCase):

    def setUp(self):
        self.p = LinkParser()

    def assert_link_count(self, links, expected_count):
        self.assertEqual(len(links), expected_count, 'incorrect nr of links')

    def assert_link(self, actual, path, line_nr, col_nr=0):
        self.assertEqual(actual.path, path, "incorrect path")
        self.assertEqual(actual.line_nr, line_nr, "incorrect line nr")
        self.assertEqual(actual.col_nr, col_nr, "incorrect col nr")

    def assert_link_text(self, text, link, link_text):
        self.assertEqual(text[link.start:link.end], link_text,
           "the expected link text does not match the text within the string")

    def test_parse_gcc_simple_test_with_real_output(self):
        gcc_output = """
test.c: In function 'f':
test.c:5:6: warning: passing argument 1 of 'f' makes integer from pointer without a cast
test.c:3:7: note: expected 'int' but argument is of type 'char *'
test.c: In function 'main':
test.c:11:10: warning: initialization makes pointer from integer without a cast
test.c:12:11: warning: initialization makes integer from pointer without a cast
test.c:13:12: error: too few arguments to function 'f'
test.c:14:13: error: expected ';' before 'return'
"""
        links = self.p.parse(gcc_output)
        self.assert_link_count(links, 6)
        lnk = links[2]
        self.assert_link(lnk, "test.c", 11, 10)
        self.assert_link_text(gcc_output, lnk, "test.c:11:10")

    def test_parse_gcc_one_line(self):
        line = "/tmp/myfile.c:1212:12: error: ..."
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "/tmp/myfile.c", 1212, 12)
        self.assert_link_text(line, lnk, "/tmp/myfile.c:1212:12")

    def test_parse_gcc_empty_string(self):
        links = self.p.parse("")
        self.assert_link_count(links, 0)

    def test_parse_gcc_no_files_in_text(self):
        links = self.p.parse("no file links in this string")
        self.assert_link_count(links, 0)

    def test_parse_gcc_none_as_argument(self):
        self.assertRaises(ValueError, self.p.parse, None)

    def test_parse_grep_one_line(self):
        line = "libnautilus-private/nautilus-canvas-container.h:45:#define NAUTILUS_CANVAS_ICON_DATA(pointer)"
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "libnautilus-private/nautilus-canvas-container.h", 45)
        self.assert_link_text(line, lnk, "libnautilus-private/nautilus-canvas-container.h:45")

    def test_parse_python_simple_test_with_real_output(self):
        output = """
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    err()
  File "test.py", line 7, in err
    real_err()
  File "test.py", line 4, in real_err
    int('xxx')
ValueError: invalid literal for int() with base 10: 'xxx'
"""
        links = self.p.parse(output)
        self.assert_link_count(links, 3)
        lnk = links[2]
        self.assert_link(lnk, "test.py", 4)
        self.assert_link_text(output, lnk, '"test.py", line 4')

    def test_parse_python_one_line(self):
        line = "  File \"test.py\", line 1\n    def a()"
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "test.py", 1)
        self.assert_link_text(line, lnk, '"test.py", line 1')

    def test_parse_bash_one_line(self):
        line = "test.sh: line 5: gerp: command not found"
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "test.sh", 5)
        self.assert_link_text(line, lnk, 'test.sh: line 5')

    def test_parse_javac_one_line(self):
        line = "/tmp/Test.java:10: incompatible types"
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "/tmp/Test.java", 10)
        self.assert_link_text(line, lnk, '/tmp/Test.java:10')

    def test_parse_valac_simple_test_with_real_output(self):
        output = """
Test.vala:14.13-14.21: error: Assignment: Cannot convert from `string' to `int'
        int a = "xxx";
            ^^^^^^^^^
"""
        links = self.p.parse(output)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "Test.vala", 14)
        self.assert_link_text(output, lnk, 'Test.vala:14.13-14.21')

    def test_parse_ruby_simple_test_with_real_output(self):
        output = """
test.rb:5: undefined method `fake_method' for main:Object (NoMethodError)
	from test.rb:3:in `each'
	from test.rb:3
"""
        links = self.p.parse(output)
        self.assert_link_count(links, 3)
        lnk = links[0]
        self.assert_link(lnk, "test.rb", 5)
        self.assert_link_text(output, lnk, 'test.rb:5')
        lnk = links[1]
        self.assert_link(lnk, "test.rb", 3)
        self.assert_link_text(output, lnk, 'test.rb:3')

    def test_parse_scalac_one_line(self):
        line = "Test.scala:7: error: not found: value fakeMethod"
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "Test.scala", 7)
        self.assert_link_text(line, lnk, 'Test.scala:7')

    def test_parse_sbt_one_line(self):
        line = "[error] /home/hank/foo/Test.scala:7: not found: value fakeMethod"
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "/home/hank/foo/Test.scala", 7)
        self.assert_link_text(line, lnk, '/home/hank/foo/Test.scala:7')

    def test_parse_go_6g_one_line(self):
        line = "test.go:9: undefined: FakeMethod"
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "test.go", 9)
        self.assert_link_text(line, lnk, 'test.go:9')

    def test_parse_perl_one_line(self):
        line = 'syntax error at test.pl line 889, near "$fake_var'
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "test.pl", 889)
        self.assert_link_text(line, lnk, 'test.pl line 889')

    def test_parse_mcs_one_line(self):
        line = 'Test.cs(12,7): error CS0103: The name `fakeMethod'
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "Test.cs", 12)
        self.assert_link_text(line, lnk, 'Test.cs(12,7)')

    def test_parse_pas_one_line(self):
        line = 'hello.pas(11,1) Fatal: Syntax error, ":" expected but "BEGIN"'
        links = self.p.parse(line)
        self.assert_link_count(links, 1)
        lnk = links[0]
        self.assert_link(lnk, "hello.pas", 11)
        self.assert_link_text(line, lnk, 'hello.pas(11,1)')

if __name__ == '__main__':
    unittest.main()

# ex:ts=4:et: