|
1
|
|
|
import os |
|
2
|
|
|
import sys |
|
3
|
|
|
import unittest |
|
4
|
|
|
from queue import Queue |
|
5
|
|
|
|
|
6
|
|
|
sys.path.insert(0, ".") |
|
7
|
|
|
|
|
8
|
|
|
from bears.tests.LocalBearTestHelper import LocalBearTestHelper |
|
9
|
|
|
from bears.c_languages.codeclone_detection.ClangASTPrintBear import ClangASTPrintBear |
|
|
|
|
|
|
10
|
|
|
from coalib.bearlib.parsing.clang.cindex import Index, LibclangError |
|
11
|
|
|
from coalib.settings.Section import Section |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class ClangASTPrintBearTest(LocalBearTestHelper): |
|
15
|
|
|
def setUp(self): |
|
16
|
|
|
self.testfile = os.path.abspath(os.path.join( |
|
17
|
|
|
os.path.dirname(__file__), |
|
18
|
|
|
"sample.c")) |
|
19
|
|
|
self.queue = Queue() |
|
20
|
|
|
self.uut = ClangASTPrintBear(Section("name"), self.queue) |
|
21
|
|
|
|
|
22
|
|
|
def test_run(self): |
|
23
|
|
|
self.uut.run(self.testfile, []) |
|
24
|
|
|
with self.assertRaises(AssertionError): |
|
25
|
|
|
self.uut.run("notexistant", []) |
|
26
|
|
|
|
|
27
|
|
|
def test_ast(self): |
|
28
|
|
|
expected_ast = ( |
|
29
|
|
|
""" |
|
30
|
|
|
|-stdio.h CursorKind.INCLUSION_DIRECTIVE Lines 2-2 (# include < stdio . h > #) |
|
31
|
|
|
|-not_existant.c CursorKind.INCLUSION_DIRECTIVE Lines 3-3 (# include """ |
|
32
|
|
|
""""not_existant.c" // Empty function) |
|
33
|
|
|
|-test() CursorKind.FUNCTION_DECL Lines 6-6 (int test ( void ) ;) |
|
34
|
|
|
|-g CursorKind.VAR_DECL Lines 9-9 (int g ;) |
|
35
|
|
|
`-main(int, char *) CursorKind.FUNCTION_DECL Lines 12-30 (int main ( """ |
|
36
|
|
|
"""int t , char * args ) { // Usage in a call smile ( t , g ) ; // Simpl""" |
|
37
|
|
|
"""e stupid assignment t = g ; // Local declaration int * asd ; // Simpl""" |
|
38
|
|
|
"""e more stupid reassignment, this time using other syntax elems t = """ |
|
39
|
|
|
"""args [ g ] ; // Declaration in for loop for ( int i ; i < 5 ; i ++ """ |
|
40
|
|
|
""") { // Checking out constants printf ( "i is %d" , i ) ; } }) |
|
41
|
|
|
""") |
|
42
|
|
|
|
|
43
|
|
|
self.uut.run(self.testfile, []) |
|
44
|
|
|
|
|
45
|
|
|
ast = "\n" |
|
46
|
|
|
# Only check beginning of AST |
|
47
|
|
|
for i in range(expected_ast.count("\n")-1): |
|
48
|
|
|
ast += self.queue.get(timeout=0).message + "\n" |
|
49
|
|
|
|
|
50
|
|
|
self.assertEqual(ast, expected_ast) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def skip_test(): |
|
54
|
|
|
try: |
|
55
|
|
|
Index.create() |
|
56
|
|
|
return False |
|
57
|
|
|
except LibclangError as error: |
|
58
|
|
|
return str(error) |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if __name__ == '__main__': |
|
62
|
|
|
unittest.main(verbosity=2) |
|
63
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.