wireplumber/tests/wp/state.c
George Kiagiadakis 38f7483793 state: remove support for groups and propagate save errors
There is no real use for groups in our API. Just use the name of
the file as the default group and be done with it...
Storing multiple groups with this API is problematic because it
forces flushing the file to disk multiple times, one for each group,
and it's just more performant if we use a prefix in the keys
to implement some form of logical separation.

This commit also makes the GKeyFile a temporary object. As we
always load the file from the file system in _load()
and we always replace its contents with a new dictionary in _save(),
there is no point in keeping the keyfile's internal data structures
stored in memory.

Save errors are now also propagated to adhere to the programming
practices of GObject
2021-06-04 18:36:19 +03:00

149 lines
4 KiB
C

/* WirePlumber
*
* Copyright © 2020 Collabora Ltd.
* @author Julian Bouzas <julian.bouzas@collabora.com>
*
* SPDX-License-Identifier: MIT
*/
#include <wp/wp.h>
static void
test_state_basic (void)
{
g_autoptr (GError) error = NULL;
g_autoptr (WpState) state = wp_state_new ("basic");
g_assert_nonnull (state);
g_assert_cmpstr (wp_state_get_name (state), ==, "basic");
g_assert_true (g_str_has_suffix (wp_state_get_location (state), "basic"));
/* Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "key1", "value1");
wp_properties_set (props, "key2", "value2");
wp_properties_set (props, "key3", "value3");
g_assert_true (wp_state_save (state, props, &error));
g_assert_no_error (error);
}
/* Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "key1"), ==, "value1");
g_assert_cmpstr (wp_properties_get (props, "key2"), ==, "value2");
g_assert_cmpstr (wp_properties_get (props, "key2"), ==, "value2");
g_assert_null (wp_properties_get (props, "invalid"));
}
/* Re-Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "new-key", "new-value");
g_assert_true (wp_state_save (state, props, &error));
g_assert_no_error (error);
}
/* Re-Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "new-key"), ==, "new-value");
g_assert_null (wp_properties_get (props, "key1"));
g_assert_null (wp_properties_get (props, "key2"));
g_assert_null (wp_properties_get (props, "key3"));
}
wp_state_clear (state);
/* Load empty */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_null (wp_properties_get (props, "new-key"));
g_assert_null (wp_properties_get (props, "key1"));
g_assert_null (wp_properties_get (props, "key2"));
g_assert_null (wp_properties_get (props, "key3"));
}
wp_state_clear (state);
}
static void
test_state_empty (void)
{
g_autoptr (GError) error = NULL;
g_autoptr (WpState) state = wp_state_new ("empty");
g_assert_nonnull (state);
/* Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "key", "value");
g_assert_true (wp_state_save (state, props, &error));
g_assert_no_error (error);
}
/* Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "key"), ==, "value");
}
/* Save empty */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
g_assert_true (wp_state_save (state, props, &error));
g_assert_no_error (error);
}
/* Load empty */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_null (wp_properties_get (props, "key"));
}
wp_state_clear (state);
}
static void
test_state_spaces (void)
{
g_autoptr (GError) error = NULL;
g_autoptr (WpState) state = wp_state_new ("spaces");
g_assert_nonnull (state);
/* Save */
{
g_autoptr (WpProperties) props = wp_properties_new_empty ();
wp_properties_set (props, "key", "value with spaces");
g_assert_true (wp_state_save (state, props, &error));
g_assert_no_error (error);
}
/* Load */
{
g_autoptr (WpProperties) props = wp_state_load (state);
g_assert_nonnull (props);
g_assert_cmpstr (wp_properties_get (props, "key"), ==, "value with spaces");
}
wp_state_clear (state);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_log_set_writer_func (wp_log_writer_default, NULL, NULL);
g_test_add_func ("/wp/state/basic", test_state_basic);
g_test_add_func ("/wp/state/empty", test_state_empty);
g_test_add_func ("/wp/state/spaces", test_state_spaces);
return g_test_run ();
}