comparison src/testdir/test_lua.vim @ 25503:9947b7e4b319 v8.2.3288

patch 8.2.3288: cannot easily access namespace dictionaries from Lua Commit: https://github.com/vim/vim/commit/9dc4bef897a37a610a28d69cee6d30749db61296 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Wed Aug 4 21:12:52 2021 +0200 patch 8.2.3288: cannot easily access namespace dictionaries from Lua Problem: Cannot easily access namespace dictionaries from Lua. Solution: Add vim.g, vim.b, etc. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/8693, from NeoVim)
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 Aug 2021 21:15:04 +0200
parents 241d26b17192
children 7ed54019fbb8
comparison
equal deleted inserted replaced
25502:4c2191b35282 25503:9947b7e4b319
914 vim.command('let s ..= "E"') 914 vim.command('let s ..= "E"')
915 eof 915 eof
916 call assert_equal('ABCDE', s) 916 call assert_equal('ABCDE', s)
917 endfunc 917 endfunc
918 918
919 " Test for adding, accessing and removing global variables using the vim.g
920 " Lua table
921 func Test_lua_global_var_table()
922 " Access global variables with different types of values
923 let g:Var1 = 10
924 let g:Var2 = 'Hello'
925 let g:Var3 = ['a', 'b']
926 let g:Var4 = #{x: 'edit', y: 'run'}
927 let g:Var5 = function('min')
928 call assert_equal(10, luaeval('vim.g.Var1'))
929 call assert_equal('Hello', luaeval('vim.g.Var2'))
930 call assert_equal(['a', 'b'], luaeval('vim.g.Var3'))
931 call assert_equal(#{x: 'edit', y: 'run'}, luaeval('vim.g.Var4'))
932 call assert_equal(2, luaeval('vim.g.Var5')([5, 9, 2]))
933
934 " Access list of dictionaries and dictionary of lists
935 let g:Var1 = [#{a: 10}, #{b: 20}]
936 let g:Var2 = #{p: [5, 6], q: [1.1, 2.2]}
937 call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
938 call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
939
940 " Create new global variables with different types of values
941 unlet g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
942 lua << trim END
943 vim.g.Var1 = 34
944 vim.g.Var2 = 'World'
945 vim.g.Var3 = vim.list({'#', '$'})
946 vim.g.Var4 = vim.dict({model='honda', year=2020})
947 vim.g.Var5 = vim.funcref('max')
948 END
949 call assert_equal(34, g:Var1)
950 call assert_equal('World', g:Var2)
951 call assert_equal(['#', '$'], g:Var3)
952 call assert_equal(#{model: 'honda', year: 2020}, g:Var4)
953 call assert_equal(10, g:Var5([5, 10, 9]))
954
955 " Create list of dictionaries and dictionary of lists
956 unlet g:Var1 g:Var2
957 lua << trim END
958 vim.g.Var1 = vim.list({vim.dict({a=10}), vim.dict({b=20})})
959 vim.g.Var2 = vim.dict({p=vim.list({5, 6}), q=vim.list({1.1, 2.2})})
960 END
961 call assert_equal([#{a: 10}, #{b: 20}], luaeval('vim.g.Var1'))
962 call assert_equal(#{p: [5, 6], q: [1.1, 2.2]}, luaeval('vim.g.Var2'))
963
964 " Modify a global variable with a list value or a dictionary value
965 let g:Var1 = [10, 20]
966 let g:Var2 = #{one: 'mercury', two: 'mars'}
967 lua << trim END
968 vim.g.Var1[2] = Nil
969 vim.g.Var1[3] = 15
970 vim.g.Var2['two'] = Nil
971 vim.g.Var2['three'] = 'earth'
972 END
973 call assert_equal([10, 15], g:Var1)
974 call assert_equal(#{one: 'mercury', three: 'earth'}, g:Var2)
975
976 " Remove global variables with different types of values
977 let g:Var1 = 10
978 let g:Var2 = 'Hello'
979 let g:Var3 = ['a', 'b']
980 let g:Var4 = #{x: 'edit', y: 'run'}
981 let g:Var5 = function('min')
982 lua << trim END
983 vim.g.Var1 = Nil
984 vim.g.Var2 = Nil
985 vim.g.Var3 = Nil
986 vim.g.Var4 = Nil
987 vim.g.Var5 = Nil
988 END
989 call assert_false(exists('g:Var1'))
990 call assert_false(exists('g:Var2'))
991 call assert_false(exists('g:Var3'))
992 call assert_false(exists('g:Var4'))
993 call assert_false(exists('g:Var5'))
994
995 " Try to modify and remove a locked global variable
996 let g:Var1 = 10
997 lockvar g:Var1
998 call assert_fails('lua vim.g.Var1 = 20', 'variable is locked')
999 call assert_fails('lua vim.g.Var1 = Nil', 'variable is locked')
1000 unlockvar g:Var1
1001 let g:Var2 = [7, 14]
1002 lockvar 0 g:Var2
1003 lua vim.g.Var2[2] = Nil
1004 lua vim.g.Var2[3] = 21
1005 call assert_fails('lua vim.g.Var2 = Nil', 'variable is locked')
1006 call assert_equal([7, 21], g:Var2)
1007 lockvar 1 g:Var2
1008 call assert_fails('lua vim.g.Var2[2] = Nil', 'list is locked')
1009 call assert_fails('lua vim.g.Var2[3] = 21', 'list is locked')
1010 unlockvar g:Var2
1011
1012 " Attempt to access a non-existing global variable
1013 call assert_equal(v:null, luaeval('vim.g.NonExistingVar'))
1014 lua vim.g.NonExisting = Nil
1015
1016 unlet! g:Var1 g:Var2 g:Var3 g:Var4 g:Var5
1017 endfunc
1018
1019 " Test for accessing and modifying predefined vim variables using the vim.v
1020 " Lua table
1021 func Test_lua_predefined_var_table()
1022 call assert_equal(v:progpath, luaeval('vim.v.progpath'))
1023 let v:errmsg = 'SomeError'
1024 call assert_equal('SomeError', luaeval('vim.v.errmsg'))
1025 lua vim.v.errmsg = 'OtherError'
1026 call assert_equal('OtherError', v:errmsg)
1027 call assert_fails('lua vim.v.errmsg = Nil', 'variable is fixed')
1028 let v:oldfiles = ['one', 'two']
1029 call assert_equal(['one', 'two'], luaeval('vim.v.oldfiles'))
1030 lua vim.v.oldfiles = vim.list({})
1031 call assert_equal([], v:oldfiles)
1032 call assert_equal(v:null, luaeval('vim.v.null'))
1033 call assert_fails('lua vim.v.argv[1] = Nil', 'list is locked')
1034 call assert_fails('lua vim.v.newvar = 1', 'Dictionary is locked')
1035 endfunc
1036
1037 " Test for adding, accessing and modifying window-local variables using the
1038 " vim.w Lua table
1039 func Test_lua_window_var_table()
1040 " Access window variables with different types of values
1041 new
1042 let w:wvar1 = 10
1043 let w:wvar2 = 'edit'
1044 let w:wvar3 = 3.14
1045 let w:wvar4 = 0zdeadbeef
1046 let w:wvar5 = ['a', 'b']
1047 let w:wvar6 = #{action: 'run'}
1048 call assert_equal(10, luaeval('vim.w.wvar1'))
1049 call assert_equal('edit', luaeval('vim.w.wvar2'))
1050 call assert_equal(3.14, luaeval('vim.w.wvar3'))
1051 call assert_equal(0zdeadbeef, luaeval('vim.w.wvar4'))
1052 call assert_equal(['a', 'b'], luaeval('vim.w.wvar5'))
1053 call assert_equal(#{action: 'run'}, luaeval('vim.w.wvar6'))
1054 call assert_equal(v:null, luaeval('vim.w.NonExisting'))
1055
1056 " modify a window variable
1057 lua vim.w.wvar2 = 'paste'
1058 call assert_equal('paste', w:wvar2)
1059
1060 " change the type stored in a variable
1061 let w:wvar2 = [1, 2]
1062 lua vim.w.wvar2 = vim.dict({a=10, b=20})
1063 call assert_equal(#{a: 10, b: 20}, w:wvar2)
1064
1065 " create a new window variable
1066 lua vim.w.wvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1067 call assert_equal(#{a: [1, 2], b: 20}, w:wvar7)
1068
1069 " delete a window variable
1070 lua vim.w.wvar2 = Nil
1071 call assert_false(exists('w:wvar2'))
1072
1073 new
1074 call assert_equal(v:null, luaeval('vim.w.wvar1'))
1075 call assert_equal(v:null, luaeval('vim.w.wvar2'))
1076 %bw!
1077 endfunc
1078
1079 " Test for adding, accessing and modifying buffer-local variables using the
1080 " vim.b Lua table
1081 func Test_lua_buffer_var_table()
1082 " Access buffer variables with different types of values
1083 let b:bvar1 = 10
1084 let b:bvar2 = 'edit'
1085 let b:bvar3 = 3.14
1086 let b:bvar4 = 0zdeadbeef
1087 let b:bvar5 = ['a', 'b']
1088 let b:bvar6 = #{action: 'run'}
1089 call assert_equal(10, luaeval('vim.b.bvar1'))
1090 call assert_equal('edit', luaeval('vim.b.bvar2'))
1091 call assert_equal(3.14, luaeval('vim.b.bvar3'))
1092 call assert_equal(0zdeadbeef, luaeval('vim.b.bvar4'))
1093 call assert_equal(['a', 'b'], luaeval('vim.b.bvar5'))
1094 call assert_equal(#{action: 'run'}, luaeval('vim.b.bvar6'))
1095 call assert_equal(v:null, luaeval('vim.b.NonExisting'))
1096
1097 " modify a buffer variable
1098 lua vim.b.bvar2 = 'paste'
1099 call assert_equal('paste', b:bvar2)
1100
1101 " change the type stored in a variable
1102 let b:bvar2 = [1, 2]
1103 lua vim.b.bvar2 = vim.dict({a=10, b=20})
1104 call assert_equal(#{a: 10, b: 20}, b:bvar2)
1105
1106 " create a new buffer variable
1107 lua vim.b.bvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1108 call assert_equal(#{a: [1, 2], b: 20}, b:bvar7)
1109
1110 " delete a buffer variable
1111 lua vim.b.bvar2 = Nil
1112 call assert_false(exists('b:bvar2'))
1113
1114 new
1115 call assert_equal(v:null, luaeval('vim.b.bvar1'))
1116 call assert_equal(v:null, luaeval('vim.b.bvar2'))
1117 %bw!
1118 endfunc
1119
1120 " Test for adding, accessing and modifying tabpage-local variables using the
1121 " vim.t Lua table
1122 func Test_lua_tabpage_var_table()
1123 " Access tabpage variables with different types of values
1124 let t:tvar1 = 10
1125 let t:tvar2 = 'edit'
1126 let t:tvar3 = 3.14
1127 let t:tvar4 = 0zdeadbeef
1128 let t:tvar5 = ['a', 'b']
1129 let t:tvar6 = #{action: 'run'}
1130 call assert_equal(10, luaeval('vim.t.tvar1'))
1131 call assert_equal('edit', luaeval('vim.t.tvar2'))
1132 call assert_equal(3.14, luaeval('vim.t.tvar3'))
1133 call assert_equal(0zdeadbeef, luaeval('vim.t.tvar4'))
1134 call assert_equal(['a', 'b'], luaeval('vim.t.tvar5'))
1135 call assert_equal(#{action: 'run'}, luaeval('vim.t.tvar6'))
1136 call assert_equal(v:null, luaeval('vim.t.NonExisting'))
1137
1138 " modify a tabpage variable
1139 lua vim.t.tvar2 = 'paste'
1140 call assert_equal('paste', t:tvar2)
1141
1142 " change the type stored in a variable
1143 let t:tvar2 = [1, 2]
1144 lua vim.t.tvar2 = vim.dict({a=10, b=20})
1145 call assert_equal(#{a: 10, b: 20}, t:tvar2)
1146
1147 " create a new tabpage variable
1148 lua vim.t.tvar7 = vim.dict({a=vim.list({1, 2}), b=20})
1149 call assert_equal(#{a: [1, 2], b: 20}, t:tvar7)
1150
1151 " delete a tabpage variable
1152 lua vim.t.tvar2 = Nil
1153 call assert_false(exists('t:tvar2'))
1154
1155 tabnew
1156 call assert_equal(v:null, luaeval('vim.t.tvar1'))
1157 call assert_equal(v:null, luaeval('vim.t.tvar2'))
1158 %bw!
1159 endfunc
1160
919 " vim: shiftwidth=2 sts=2 expandtab 1161 " vim: shiftwidth=2 sts=2 expandtab