Skip to content

Commit

Permalink
Add vim tests for hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
bstaletic committed Jun 12, 2024
1 parent 866128c commit 57f737b
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
172 changes: 172 additions & 0 deletions test/hierarchies.test.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
function! Test_Call_Hierarchy()
call youcompleteme#test#setup#OpenFile( '/test/testdata/cpp/hierarchies.cc', {} )
call cursor( [ 1, 5 ] )

" Check that f's callers are present
function! Check1( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 4 )
call assert_match( '^+Function: f.*:1', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ +Function: g.*:4', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ +Function: g.*:4', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call assert_match( '^ +Function: h.*:9', getbufline( hierarchy_buf_nr, 4 )[ 0 ] )
call FeedAndCheckAgain( "\<Down>\<Tab>", funcref( 'Check2' ) )
endfunction

" Check that g's callers are present
function! Check2( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 5 )
call assert_match( '^+Function: f.*:1', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call assert_match( '^ +Function: h.*:8', getbufline( hierarchy_buf_nr, 4 )[ 0 ] )
call assert_match( '^ +Function: h.*:9', getbufline( hierarchy_buf_nr, 5 )[ 0 ] )
call FeedAndCheckAgain( "\<Down>\<Down>\<Tab>", funcref( 'Check3' ) )
endfunction

" Check that 1st h's callers are present
function! Check3( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 5 )
call assert_match( '^+Function: f.*:1', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call assert_match( '^ -Function: h.*:8', getbufline( hierarchy_buf_nr, 4 )[ 0 ] )
call assert_match( '^ +Function: h.*:9', getbufline( hierarchy_buf_nr, 5 )[ 0 ] )
call FeedAndCheckAgain( "\<Down>\<Tab>", funcref( 'Check4' ) )
endfunction

" Check that 2nd h's callers are present
function! Check4( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 5 )
call assert_match( '^+Function: f.*:1', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call assert_match( '^ -Function: h.*:8', getbufline( hierarchy_buf_nr, 4 )[ 0 ] )
call assert_match( '^ -Function: h.*:9', getbufline( hierarchy_buf_nr, 5 )[ 0 ] )
call FeedAndCheckAgain( "\<Up>\<Up>\<Up>\<Up>\<S-Tab>", funcref( 'Check5' ) )
endfunction

" Try to access callees of f
function! Check5( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 5 )
call assert_match( '^-Function: f.*:1', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ -Function: g.*:4', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call assert_match( '^ -Function: h.*:8', getbufline( hierarchy_buf_nr, 4 )[ 0 ] )
call assert_match( '^ -Function: h.*:9', getbufline( hierarchy_buf_nr, 5 )[ 0 ] )
call FeedAndCheckAgain( "\<Down>\<Down>\<Down>\<Down>\<S-Tab>", funcref( 'Check6' ) )
endfunction

" Re-root at h
function! Check6( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 1 )
call assert_match( '^+Function: h', getbufline( hierarchy_buf_nr, 1 ) )
call FeedAndCheckAgain( "\<S-Tab>\<Tab>", funcref( 'Check7' ) )
endfunction

" Expansion after re-rooting works.
" NOTE: Clangd does not support outgoing calls, hence, we are stuck at just h.
function! Check7( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 1 )
call assert_match( '^-Function: h', getbufline( hierarchy_buf_nr, 1 ) )
call FeedAndCheckAgain( "\<C-c>", funcref( 'Check8' ) )
endfunction

" Make sure it is closed
function! Check8( id )
call assert_equal( len( popup_list() ), 0 )
endfunction

call youcompleteme#hierarchy#StartRequest( 'call' )
call WaitForAssert( { -> assert_equal( len( popup_list() ), 1 ) } )
let popup_ids = popup_list()
let hierarchy_win_id = popup_ids[ 0 ]
let hierarchy_buf_nr = winbufnr( hierarchy_win_id )
" Check that `+Function f` is at the start of the only line in the popup
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 1 )
call assert_match( '^+Function: f', getbufline( hierarchy_buf_nr, 1 ) )
call FeedAndCheckMain( "\<Tab>", funcref( 'Check1' ) )
endfunction

function! Test_Type_Hierarchy()
call youcompleteme#test#setup#OpenFile( '/test/testdata/cpp/hierarchies.cc', {} )
call cursor( [ 13, 8 ] )

" Check that B1's subtypes are present
function! Check1( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 2 )
call assert_match( '^+Struct: B1.*:13', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ +Struct: D1.*:16', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call FeedAndCheckAgain( "\<Down>\<Tab>", funcref( 'Check2' ) )
endfunction

" Try to access D1's subtypes
function! Check2( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 2 )
call assert_match( '^+Struct: B1.*:13', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ -Struct: D1.*:16', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call FeedAndCheckAgain( "\<Up>\<S-Tab>", funcref( 'Check3' ) )
endfunction

" Check that 1st h's callers are present
function! Check3( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 3 )
call assert_match( '^ +Struct: B0.*:12', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^-Struct: B1.*:13', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ -Struct: D1.*:16', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call FeedAndCheckAgain( "\<Up>\<S-Tab>", funcref( 'Check4' ) )
endfunction

" Check that 2nd h's callers are present
function! Check4( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 3 )
call assert_match( '^ -Struct: B0.*:12', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^-Struct: B1.*:13', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ -Struct: D1.*:16', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call FeedAndCheckAgain( "\<Tab>", funcref( 'Check5' ) )
endfunction

" Re-root at B0: supertypes->subtypes
function! Check5( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 4 )
call assert_match( '^+Struct: B0.*:12', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ +Struct: B1.*:13', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ +Struct: D0.*:15', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call assert_match( '^ +Struct: D1.*:16', getbufline( hierarchy_buf_nr, 4 )[ 0 ] )
call FeedAndCheckAgain( "\<Down>\<Down>\<Down>\<S-Tab>", funcref( 'Check6' ) )
endfunction

" Re-root at D1: subtypes->supertypes
function! Check6( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 3 )
call assert_match( '^ +Struct: B0.*:12', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ +Struct: B1.*:13', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^+Struct: D1.*:16', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call FeedAndCheckAgain( "\<Tab>\<Up>\<S-Tab>", funcref( 'Check7' ) )
endfunction

" Expansion after re-rooting works.
function! Check7( id )
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 4 )
call assert_match( '^ +Struct: B0.*:12', getbufline( hierarchy_buf_nr, 1 )[ 0 ] )
call assert_match( '^ +Struct: B0.*:12', getbufline( hierarchy_buf_nr, 2 )[ 0 ] )
call assert_match( '^ -Struct: B1.*:13', getbufline( hierarchy_buf_nr, 3 )[ 0 ] )
call assert_match( '^-Struct: D1.*:16', getbufline( hierarchy_buf_nr, 4 )[ 0 ] )
call FeedAndCheckAgain( "\<C-c>", funcref( 'Check7' ) )
endfunction

" Make sure it is closed
function! Check8( id )
call assert_equal( len( popup_list() ), 0 )
endfunction

call youcompleteme#hierarchy#StartRequest( 'type' )
call WaitForAssert( { -> assert_equal( len( popup_list() ), 1 ) } )
let popup_ids = popup_list()
let hierarchy_win_id = popup_ids[ 0 ]
let hierarchy_buf_nr = winbufnr( hierarchy_win_id )
" Check that `+Function f` is at the start of the only line in the popup
py3 import vim
call assert_equal( py3eval( 'len( vim.buffers[ ' . string( hierarchy_buf_nr ) . ' ]' ), 1 )
call assert_match( '^+Struct: B1', getbufline( hierarchy_buf_nr, 1 ) )
call FeedAndCheckMain( "\<Tab>", funcref( 'Check1' ) )
endfunction
16 changes: 16 additions & 0 deletions test/testdata/cpp/hierarchies.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
int f();

int g() {
return f() + f();
}

int h() {
int x = g();
return f() + x;
}

struct B0 {};
struct B1 : B0 {};

struct D0 : B0 {};
struct D1 : B0, B1 {};

0 comments on commit 57f737b

Please sign in to comment.