robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe =t . describe 3local itp =t . gen_itp ( t . it ) 4local t_eval =require ( 'test.unit.eval.testutil' ) 5local api_t =require ( 'test.unit.api.testutil' ) 6 7local cimport =t . cimport 8local NULL =t . NULL 9local eq =t . eq 10 11local lua2typvalt =t_eval . lua2typvalt 12local typvalt2lua =t_eval . typvalt2lua 13local typvalt =t_eval . typvalt 14 15local nil_value =api_t . nil_value 16local list_type =api_t . list_type 17local int_type =api_t . int_type 18local type_key =api_t . type_key 19local obj2lua =api_t . obj2lua 20local func_type =api_t . func_type 21 22local api =cimport ( './src/nvim/api/private/helpers.h' , './src/nvim/api/private/converter.h' ) 23 24describe ( 'vim_to_object' , function () 25local vim_to_object =function ( l ) 26return obj2lua ( api . vim_to_object ( lua2typvalt ( l ), nil , false )) 27end 28 29local different_output_test =function ( name , input , output ) 30itp ( name , function () 31eq ( output , vim_to_object ( input )) 32end ) 33end 34 35local simple_test =function ( name , l ) 36different_output_test ( name , l , l ) 37end 38 39simple_test ( 'converts true' , true ) 40simple_test ( 'converts false' , false ) 41simple_test ( 'converts nil' , nil_value ) 42simple_test ( 'converts 1' , 1 ) 43simple_test ( 'converts -1.5' , - 1.5 ) 44simple_test ( 'converts empty string' , '' ) 45simple_test ( 'converts non-empty string' , 'foobar' ) 46simple_test ( 'converts integer 10' , { [ type_key ] =int_type , value =10 } ) 47simple_test ( 'converts empty dict' , {} ) 48simple_test ( 'converts dict with scalar values' , { test =10 , test2 =true , test3 ='test' } ) 49simple_test ( 'converts dict with containers inside' , { test ={} , test2 ={ 1 , 2 } } ) 50simple_test ( 'converts empty list' , { [ type_key ] =list_type } ) 51simple_test ( 'converts list with scalar values' , { 1 , 2 , 'test' , 'foo' } ) 52simple_test ( 53'converts list with containers inside' , 54{ {} , { test ={} , test3 ={ test4 =true } } } 55) 56 57local dct ={} 58dct . dct =dct 59different_output_test ( 'outputs nil for nested dictionaries (1 level)' , dct , { dct =nil_value } ) 60 61local lst ={} 62lst [ 1 ] =lst 63different_output_test ( 'outputs nil for nested lists (1 level)' , lst , { nil_value } ) 64 65local dct2 ={ test =true , dict =nil_value } 66dct2 . dct ={ dct2 } 67different_output_test ( 68'outputs nil for nested dictionaries (2 level, in list)' , 69dct2 , 70{ dct ={ nil_value } , test =true , dict =nil_value } 71) 72 73local dct3 ={ test =true , dict =nil_value } 74dct3 . dct ={ dctin =dct3 } 75different_output_test ( 76'outputs nil for nested dictionaries (2 level, in dict)' , 77dct3 , 78{ dct ={ dctin =nil_value } , test =true , dict =nil_value } 79) 80 81local lst2 ={} 82lst2 [ 1 ] ={ lst2 } 83different_output_test ( 'outputs nil for nested lists (2 level, in list)' , lst2 , { { nil_value } } ) 84 85local lst3 ={ nil , true , false , 'ttest' } 86lst3 [ 1 ] ={ lst =lst3 } 87different_output_test ( 88'outputs nil for nested lists (2 level, in dict)' , 89lst3 , 90{ { lst =nil_value } , true , false , 'ttest' } 91) 92 93itp ( 'outputs empty list for NULL list' , function () 94local tt =typvalt ( 'VAR_LIST' , { v_list =NULL } ) 95eq ( nil , tt . vval . v_list ) 96eq ( { [ type_key ] =list_type } , obj2lua ( api . vim_to_object ( tt , nil , false ))) 97end ) 98 99itp ( 'outputs empty dict for NULL dict' , function () 100local tt =typvalt ( 'VAR_DICT' , { v_dict =NULL } ) 101eq ( nil , tt . vval . v_dict ) 102eq ( {} , obj2lua ( api . vim_to_object ( tt , nil , false ))) 103end ) 104 105itp ( 'regression: partials in a list' , function () 106local llist ={ 107{ 108[ type_key ] =func_type , 109value ='printf' , 110args ={ '%s' } , 111dict ={ v =1 } , 112} , 113{} , 114} 115local list =lua2typvalt ( llist ) 116eq ( llist , typvalt2lua ( list )) 117eq ( { nil_value , {} } , obj2lua ( api . vim_to_object ( list , nil , false ))) 118end ) 119end )