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