def test_reparse_arguments(): path = os.path.join(kInputsDir, 'parse_arguments.c') tu = Program.from_source(path, ['-DDECL_ONE=hello', '-DDECL_TWO=hi']) tu.reparse() spellings = [c.spelling for c in tu.cursor.get_children()] assert spellings[-2] == 'hello' assert spellings[-1] == 'hi'
def get_tu(source, lang='c', all_warnings=False): """Obtain a translation unit from source and language. By default, the translation unit is created from source file "t.<ext>" where <ext> is the default file extension for the specified language. By default it is C, so "t.c" is the default file name. Supported languages are {c, cpp, objc}. all_warnings is a convenience argument to enable all compiler warnings. """ name = 't.c' args = [] if lang == 'cpp': name = 't.cpp' args.append('-std=c++11') elif lang == 'objc': name = 't.m' elif lang != 'c': raise Exception('Unknown language: %s' % lang) if all_warnings: args += ['-Wall', '-Wextra'] return Program.from_source(name, args, unsaved_files=[(name, source)])
def test_code_complete(): files = [('fake.c', """ /// Aaa. int test1; /// Bbb. void test2(void); void f() { } """)] tu = Program.from_source( 'fake.c', ['-std=c99'], unsaved_files=files, options=Program.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION) cr = tu.codeComplete('fake.c', 9, 1, unsaved_files=files, include_brief_comments=True) expected = [ "{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.", "{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.", "{'return', TypedText} || Priority: 40 || Availability: Available || Brief comment: None" ] check_completion_results(cr, expected)
def test_unsaved_files(): tu = Program.from_source('fake.c', ['-I./'], unsaved_files = [ ('fake.c', """ #include "fake.h" int x; int SOME_DEFINE; """), ('./fake.h', """ #define SOME_DEFINE y """) ]) spellings = [c.spelling for c in tu.cursor.get_children()] assert spellings[-2] == 'x' assert spellings[-1] == 'y'
def test_includes(): def eq(expected, actual): if not actual.is_input_file: return normpaths_equal(expected[0], actual.source.name) and \ normpaths_equal(expected[1], actual.include.name) else: return normpaths_equal(expected[1], actual.include.name) src = os.path.join(kInputsDir, 'include.cpp') h1 = os.path.join(kInputsDir, "header1.h") h2 = os.path.join(kInputsDir, "header2.h") h3 = os.path.join(kInputsDir, "header3.h") inc = [(src, h1), (h1, h3), (src, h2), (h2, h3)] tu = Program.from_source(src) for i in zip(inc, tu.get_includes()): assert eq(i[0], i[1])
def test_load(): """Ensure Programs can be constructed from saved files.""" tu = get_tu('int foo();') assert len(tu.diagnostics) == 0 path = save_tu(tu) assert os.path.exists(path) assert os.path.getsize(path) > 0 tu2 = Program.from_ast_file(filename=path) assert len(tu2.diagnostics) == 0 foo = get_cursor(tu2, 'foo') assert foo is not None # Just in case there is an open file descriptor somewhere. del tu2 os.unlink(path)
def test_code_complete_availability(): files = [('fake.cpp', """ class P { protected: int member; }; class Q : public P { public: using P::member; }; void f(P x, Q y) { x.; // member is inaccessible y.; // member is accessible } """)] tu = Program.from_source('fake.cpp', ['-std=c++98'], unsaved_files=files) cr = tu.codeComplete('fake.cpp', 12, 5, unsaved_files=files) expected = [ "{'const', TypedText} || Priority: 40 || Availability: Available || Brief comment: None", "{'volatile', TypedText} || Priority: 40 || Availability: Available || Brief comment: None", "{'operator', TypedText} || Priority: 40 || Availability: Available || Brief comment: None", "{'P', TypedText} | {'::', Text} || Priority: 75 || Availability: Available || Brief comment: None", "{'Q', TypedText} | {'::', Text} || Priority: 75 || Availability: Available || Brief comment: None" ] check_completion_results(cr, expected) cr = tu.codeComplete('fake.cpp', 13, 5, unsaved_files=files) expected = [ "{'P', TypedText} | {'::', Text} || Priority: 75 || Availability: Available || Brief comment: None", "{'P &', ResultType} | {'operator=', TypedText} | {'(', LeftParen} | {'const P &', Placeholder} | {')', RightParen} || Priority: 34 || Availability: Available || Brief comment: None", "{'int', ResultType} | {'member', TypedText} || Priority: 35 || Availability: NotAccessible || Brief comment: None", "{'void', ResultType} | {'~P', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 34 || Availability: Available || Brief comment: None" ] check_completion_results(cr, expected)
def test_code_complete(): files = [('fake.c', """ /// Aaa. int test1; /// Bbb. void test2(void); void f() { } """)] tu = Program.from_source('fake.c', ['-std=c99'], unsaved_files=files, options=Program.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION) cr = tu.codeComplete('fake.c', 9, 1, unsaved_files=files, include_brief_comments=True) expected = [ "{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.", "{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.", "{'return', TypedText} || Priority: 40 || Availability: Available || Brief comment: None" ] check_completion_results(cr, expected)
def test_unsaved_files_2(): import StringIO tu = Program.from_source('fake.c', unsaved_files = [ ('fake.c', StringIO.StringIO('int x;'))]) spellings = [c.spelling for c in tu.cursor.get_children()] assert spellings[-1] == 'x'
def test_spelling(): path = os.path.join(kInputsDir, 'hello.cpp') tu = Program.from_source(path) assert tu.spelling == path