mirror of
https://gitlab.freedesktop.org/pipewire/wireplumber.git
synced 2026-05-05 00:48:01 +02:00
wplua: table_to_properties: use luaL_tolstring to do string conversions
The advantage is that luaL_tolstring behaves the same as the native lua tostring() function, while lua_tostring() does not. In particular, boolean values are converted properly to "true" and "false" now, while they were not converted with lua_tostring() Add a unit test too
This commit is contained in:
parent
7336c271a2
commit
7c1fc1c567
2 changed files with 42 additions and 4 deletions
|
|
@ -20,10 +20,8 @@ wplua_table_to_properties (lua_State *L, int idx)
|
|||
lua_pushnil(L);
|
||||
while (lua_next (L, table) != 0) {
|
||||
/* copy key & value to convert them to string */
|
||||
lua_pushvalue (L, -2);
|
||||
key = lua_tostring (L, -1);
|
||||
lua_pushvalue (L, -2);
|
||||
value = lua_tostring (L, -1);
|
||||
key = luaL_tolstring (L, -2, NULL);
|
||||
value = luaL_tolstring (L, -2, NULL);
|
||||
wp_properties_set (p, key, value);
|
||||
lua_pop (L, 3);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -609,6 +609,44 @@ test_wplua_convert_gvariant_array ()
|
|||
|
||||
wplua_free (L);
|
||||
}
|
||||
static void
|
||||
test_wplua_convert_wp_properties ()
|
||||
{
|
||||
g_autoptr (GError) error = NULL;
|
||||
lua_State *L = wplua_new ();
|
||||
|
||||
const gchar code[] =
|
||||
"props = { "
|
||||
" ['test-int'] = 42, "
|
||||
" ['test-double'] = 3.14, "
|
||||
" ['test-string'] = 'foobar', "
|
||||
" ['test-boolean'] = false, "
|
||||
"}\n";
|
||||
wplua_load_buffer (L, code, sizeof (code) - 1, 0, 0, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
lua_getglobal (L, "props");
|
||||
g_autoptr (WpProperties) fromlua = wplua_table_to_properties (L, -1);
|
||||
|
||||
g_assert_cmpstr (wp_properties_get (fromlua, "test-int"), ==, "42");
|
||||
g_assert_cmpstr (wp_properties_get (fromlua, "test-double"), ==, "3.14");
|
||||
g_assert_cmpstr (wp_properties_get (fromlua, "test-string"), ==, "foobar");
|
||||
g_assert_cmpstr (wp_properties_get (fromlua, "test-boolean"), ==, "false");
|
||||
|
||||
lua_pop(L, 1);
|
||||
wplua_properties_to_table (L, fromlua);
|
||||
lua_setglobal (L, "fromc");
|
||||
|
||||
const gchar code2[] =
|
||||
"assert (fromc['test-string'] == 'foobar')\n"
|
||||
"assert (fromc['test-int'] == '42')\n"
|
||||
"assert (fromc['test-double'] == '3.14')\n"
|
||||
"assert (fromc['test-boolean'] == 'false')\n";
|
||||
wplua_load_buffer (L, code2, sizeof (code2) - 1, 0, 0, &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
wplua_free (L);
|
||||
}
|
||||
|
||||
static void
|
||||
test_wplua_script_arguments ()
|
||||
|
|
@ -660,6 +698,8 @@ main (gint argc, gchar *argv[])
|
|||
g_test_add_func ("/wplua/convert/asv", test_wplua_convert_asv);
|
||||
g_test_add_func ("/wplua/convert/gvariant_array",
|
||||
test_wplua_convert_gvariant_array);
|
||||
g_test_add_func ("/wplua/convert/wp_properties",
|
||||
test_wplua_convert_wp_properties);
|
||||
g_test_add_func ("/wplua/script_arguments", test_wplua_script_arguments);
|
||||
|
||||
return g_test_run ();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue