mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-09 19:06:15 +01:00
GSettings: major refactor. Add enums, range.
This commit is contained in:
parent
b205dc77cb
commit
597290d5c8
@ -374,7 +374,7 @@ libgio_2_0_la_SOURCES = \
|
||||
$(marshal_sources) \
|
||||
$(NULL)
|
||||
|
||||
EXTRA_DIST += gnullapplication.c gdbusapplication.c
|
||||
EXTRA_DIST += gnullapplication.c gdbusapplication.c strinfo.c
|
||||
|
||||
$(libgio_2_0_la_OBJECTS): $(marshal_sources)
|
||||
|
||||
|
@ -1468,6 +1468,8 @@ g_settings_get_double
|
||||
g_settings_set_double
|
||||
g_settings_get_boolean
|
||||
g_settings_set_boolean
|
||||
g_settings_get_enum
|
||||
g_settings_set_enum
|
||||
g_settings_sync
|
||||
#endif
|
||||
#endif
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -377,6 +377,21 @@ g_settings_set_mapping (const GValue *value,
|
||||
return g_variant_new_signature (g_value_get_string (value));
|
||||
}
|
||||
|
||||
else if (G_VALUE_HOLDS_ENUM (value))
|
||||
{
|
||||
GEnumValue *enumval;
|
||||
GEnumClass *eclass;
|
||||
|
||||
eclass = g_type_class_ref (G_VALUE_TYPE (value));
|
||||
enumval = g_enum_get_value (eclass, g_value_get_enum (value));
|
||||
g_type_class_unref (eclass);
|
||||
|
||||
if (enumval)
|
||||
return g_variant_new_string (enumval->value_nick);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
type_string = g_variant_type_dup_string (expected_type);
|
||||
g_critical ("No GSettings bind handler for type \"%s\".", type_string);
|
||||
g_free (type_string);
|
||||
@ -426,8 +441,11 @@ g_settings_get_mapping (GValue *value,
|
||||
g_variant_is_of_type (variant, G_VARIANT_TYPE_OBJECT_PATH) ||
|
||||
g_variant_is_of_type (variant, G_VARIANT_TYPE_SIGNATURE))
|
||||
{
|
||||
g_value_set_string (value, g_variant_get_string (variant, NULL));
|
||||
return TRUE;
|
||||
if (G_VALUE_HOLDS_STRING (value))
|
||||
{
|
||||
g_value_set_string (value, g_variant_get_string (variant, NULL));
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
else if (g_variant_is_of_type (variant, G_VARIANT_TYPE ("ay")))
|
||||
{
|
||||
@ -470,6 +488,8 @@ g_settings_mapping_is_compatible (GType gvalue_type,
|
||||
g_variant_type_equal (variant_type, G_VARIANT_TYPE ("ay")) ||
|
||||
g_variant_type_equal (variant_type, G_VARIANT_TYPE_OBJECT_PATH) ||
|
||||
g_variant_type_equal (variant_type, G_VARIANT_TYPE_SIGNATURE));
|
||||
else if (G_TYPE_IS_ENUM (gvalue_type))
|
||||
ok = g_variant_type_equal (variant_type, G_VARIANT_TYPE_STRING);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
2147
gio/gsettings.c
2147
gio/gsettings.c
File diff suppressed because it is too large
Load Diff
@ -121,7 +121,11 @@ gchar ** g_settings_get_strv (GSettin
|
||||
gboolean g_settings_set_strv (GSettings *settings,
|
||||
const gchar *key,
|
||||
const gchar *const *value);
|
||||
|
||||
gint g_settings_get_enum (GSettings *settings,
|
||||
const gchar *key);
|
||||
gboolean g_settings_set_enum (GSettings *settings,
|
||||
const gchar *key,
|
||||
gint value);
|
||||
GSettings * g_settings_get_child (GSettings *settings,
|
||||
const gchar *name);
|
||||
|
||||
|
@ -732,7 +732,8 @@ g_settings_backend_changed_tree (GSettingsBackend *backend,
|
||||
* g_settings_backend_read:
|
||||
* @backend: a #GSettingsBackend implementation
|
||||
* @key: the key to read
|
||||
* @expected_type: a #GVariantType hint
|
||||
* @expected_type: a #GVariantType
|
||||
* @default_value: if the default value should be returned
|
||||
* @returns: the value that was read, or %NULL
|
||||
*
|
||||
* Reads a key. This call will never block.
|
||||
@ -740,11 +741,13 @@ g_settings_backend_changed_tree (GSettingsBackend *backend,
|
||||
* If the key exists, the value associated with it will be returned.
|
||||
* If the key does not exist, %NULL will be returned.
|
||||
*
|
||||
* If @expected_type is given, it serves as a type hint to the backend.
|
||||
* If you expect a key of a certain type then you should give
|
||||
* @expected_type to increase your chances of getting it. Some backends
|
||||
* may ignore this argument and return values of a different type; it is
|
||||
* mostly used by backends that don't store strong type information.
|
||||
* The returned value will be of the type given in @expected_type. If
|
||||
* the backend stored a value of a different type then %NULL will be
|
||||
* returned.
|
||||
*
|
||||
* If @default_value is %TRUE then this gets the default value from the
|
||||
* backend (ie: the one that the backend would contain if
|
||||
* g_settings_reset() were called).
|
||||
*/
|
||||
GVariant *
|
||||
g_settings_backend_read (GSettingsBackend *backend,
|
||||
@ -752,8 +755,18 @@ g_settings_backend_read (GSettingsBackend *backend,
|
||||
const GVariantType *expected_type,
|
||||
gboolean default_value)
|
||||
{
|
||||
return G_SETTINGS_BACKEND_GET_CLASS (backend)
|
||||
GVariant *value;
|
||||
|
||||
value = G_SETTINGS_BACKEND_GET_CLASS (backend)
|
||||
->read (backend, key, expected_type, default_value);
|
||||
|
||||
if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
|
||||
{
|
||||
g_variant_unref (value);
|
||||
value = NULL;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/*< private >
|
||||
|
@ -162,37 +162,32 @@ g_settings_schema_new (const gchar *name)
|
||||
return schema;
|
||||
}
|
||||
|
||||
GVariant *
|
||||
g_settings_schema_get_value (GSettingsSchema *schema,
|
||||
const gchar *key,
|
||||
GVariant **options)
|
||||
GVariantIter *
|
||||
g_settings_schema_get_value (GSettingsSchema *schema,
|
||||
const gchar *key)
|
||||
{
|
||||
GVariant *variant, *value;
|
||||
GVariantIter *iter;
|
||||
GVariant *value;
|
||||
|
||||
value = gvdb_table_get_value (schema->priv->table, key);
|
||||
|
||||
if G_UNLIKELY (value == NULL)
|
||||
g_error ("schema does not contain a key named '%s'", key);
|
||||
|
||||
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
||||
GVariant *tmp;
|
||||
{
|
||||
GVariant *tmp;
|
||||
|
||||
tmp = gvdb_table_get_value (schema->priv->table, key);
|
||||
|
||||
if (tmp)
|
||||
{
|
||||
variant = g_variant_byteswap (tmp);
|
||||
g_variant_unref (tmp);
|
||||
}
|
||||
else
|
||||
variant = NULL;
|
||||
#else
|
||||
variant = gvdb_table_get_value (schema->priv->table, key);
|
||||
tmp = g_variant_byteswap (value);
|
||||
g_variant_unref (value);
|
||||
value = tmp;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (variant == NULL)
|
||||
return NULL;
|
||||
iter = g_variant_iter_new (value);
|
||||
g_variant_unref (value);
|
||||
|
||||
value = g_variant_get_child_value (variant, 0);
|
||||
if (options != NULL)
|
||||
*options = g_variant_get_child_value (variant, 1);
|
||||
g_variant_unref (variant);
|
||||
|
||||
return value;
|
||||
return iter;
|
||||
}
|
||||
|
||||
const gchar *
|
||||
|
@ -61,9 +61,8 @@ const gchar * g_settings_schema_get_path (GSettin
|
||||
G_GNUC_INTERNAL
|
||||
const gchar * g_settings_schema_get_gettext_domain (GSettingsSchema *schema);
|
||||
G_GNUC_INTERNAL
|
||||
GVariant * g_settings_schema_get_value (GSettingsSchema *schema,
|
||||
const gchar *key,
|
||||
GVariant **options);
|
||||
GVariantIter * g_settings_schema_get_value (GSettingsSchema *schema,
|
||||
const gchar *key);
|
||||
G_GNUC_INTERNAL
|
||||
gboolean g_settings_schema_has_key (GSettingsSchema *schema,
|
||||
const gchar *key);
|
||||
|
308
gio/strinfo.c
Normal file
308
gio/strinfo.c
Normal file
@ -0,0 +1,308 @@
|
||||
/*
|
||||
* Copyright © 2010 Codethink Limited
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the licence, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Ryan Lortie <desrt@desrt.ca>
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
/**
|
||||
* The string info map is an efficient data structure designed to be
|
||||
* used with a small set of items. It is used by GSettings schemas for
|
||||
* three purposes:
|
||||
*
|
||||
* 1) Implement <choices> with a list of valid strings
|
||||
*
|
||||
* 2) Implement <alias> by mapping one string to another
|
||||
*
|
||||
* 3) Implement enumerated types by mapping strings to integer values
|
||||
* (and back).
|
||||
*
|
||||
* The map is made out of an array of uint32s. Each entry in the array
|
||||
* is an integer value, followed by a specially formatted string value:
|
||||
*
|
||||
* The string starts with the byte 0xff or 0xfe, followed by the
|
||||
* content of the string, followed by a nul byte, followed by
|
||||
* additional nul bytes for padding, followed by a 0xff byte.
|
||||
*
|
||||
* Padding is added so that the entire formatted string takes up a
|
||||
* multiple of 4 bytes, and not less than 8 bytes. The requirement
|
||||
* for a string to take up 8 bytes is so that the scanner doesn't lose
|
||||
* synch and mistake a string for an integer value.
|
||||
*
|
||||
* The first byte of the formatted string depends on if the integer is
|
||||
* an enum value (0xff) or an alias (0xfe). If it is an alias then the
|
||||
* number refers to the word offset within the info map at which the
|
||||
* integer corresponding to the "target" value is stored.
|
||||
*
|
||||
* For example, consider the case of the string info map representing an
|
||||
* enumerated type of 'foo' (value 1) and 'bar' (value 2) and 'baz'
|
||||
* (alias for 'bar'). Note that string info maps are always little
|
||||
* endian.
|
||||
*
|
||||
* x01 x00 x00 x00 xff 'f' 'o' 'o' x00 x00 x00 xff x02 x00 x00 x00
|
||||
* xff 'b' 'a' 'r' x00 x00 x00 xff x03 x00 x00 x00 xfe 'b' 'a' 'z'
|
||||
* x00 x00 x00 xff
|
||||
*
|
||||
*
|
||||
* The operations that someone may want to perform with the map:
|
||||
*
|
||||
* - lookup if a string is valid (and not an alias)
|
||||
* - lookup the integer value for a enum 'nick'
|
||||
* - lookup the integer value for the target of an alias
|
||||
* - lookup an alias and convert it to its target string
|
||||
* - lookup the enum nick for a given value
|
||||
*
|
||||
* In order to lookup if a string is valid, it is padded on either side
|
||||
* (as described) and scanned for in the array. For example, you might
|
||||
* look for "foo":
|
||||
*
|
||||
* xff 'f' 'o' 'o' x00 x00 x00 xff
|
||||
*
|
||||
* In order to lookup the integer value for a nick, the string is padded
|
||||
* on either side and scanned for in the array, as above. Instead of
|
||||
* merely succeeding, we look at the integer value to the left of the
|
||||
* match. This is the enum value.
|
||||
*
|
||||
* In order to lookup an alias and convert it to its target enum value,
|
||||
* the string is padded on either side (as described, with 0xfe) and
|
||||
* scanned for. For example, you might look for "baz":
|
||||
*
|
||||
* xfe 'b' 'a' 'z' x00 x00 x00 xff
|
||||
*
|
||||
* The integer immediately preceeding the match then contains the offset
|
||||
* of the integer value of the target. In our example, that's '3'.
|
||||
* This index is dereferenced to find the enum value of '2'.
|
||||
*
|
||||
* To convert the alias to its target string, 5 bytes just need to be
|
||||
* added past the start of the integer value to find the start of the
|
||||
* string.
|
||||
*
|
||||
* To lookup the enum nick for a given value, the value is searched for
|
||||
* in the array. To ensure that the value isn't matching the inside of a
|
||||
* string, we must check that it is either the first item in the array or
|
||||
* immediately preceeded by the byte 0xff. It must also be immediately
|
||||
* followed by the byte 0xff.
|
||||
*
|
||||
* Because strings always take up a minimum of 2 words, because 0xff or
|
||||
* 0xfe never appear inside of a utf-8 string and because no two integer
|
||||
* values ever appear in sequence, the only way we can have the
|
||||
* sequence:
|
||||
*
|
||||
* xff __ __ __ __ xff (or 0xfe)
|
||||
*
|
||||
* is in the event of an integer nested between two strings.
|
||||
*
|
||||
* For implementation simplicity/efficiency, strings may not be more
|
||||
* than 65 characters in length (ie: 17 32bit words after padding).
|
||||
*
|
||||
* In the event that we are doing <choices> (ie: not an enum type) then
|
||||
* the value of each choice is set to zero and ignored.
|
||||
*/
|
||||
|
||||
#define STRINFO_MAX_WORDS 17
|
||||
G_GNUC_UNUSED static guint
|
||||
strinfo_string_to_words (const gchar *string,
|
||||
guint32 *words,
|
||||
gboolean alias)
|
||||
{
|
||||
guint n_words;
|
||||
gsize size;
|
||||
|
||||
size = strlen (string);
|
||||
|
||||
n_words = MAX (2, (size + 6) >> 2);
|
||||
|
||||
if (n_words > STRINFO_MAX_WORDS)
|
||||
return FALSE;
|
||||
|
||||
words[0] = GUINT32_TO_LE (alias ? 0xfe : 0xff);
|
||||
words[n_words - 1] = GUINT32_TO_BE (0xff);
|
||||
memcpy (((gchar *) words) + 1, string, size + 1);
|
||||
|
||||
return n_words;
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static gint
|
||||
strinfo_scan (const guint32 *strinfo,
|
||||
guint length,
|
||||
const guint32 *words,
|
||||
guint n_words)
|
||||
{
|
||||
guint i = 0;
|
||||
|
||||
while (i <= length - n_words)
|
||||
{
|
||||
guint j = 0;
|
||||
|
||||
for (j = 0; j < n_words; j++)
|
||||
if (strinfo[i + j] != words[j])
|
||||
break;
|
||||
|
||||
if (j == n_words)
|
||||
return i; /* match */
|
||||
|
||||
/* skip at least one word, continue */
|
||||
i += j ? j : 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static gint
|
||||
strinfo_find_string (const guint32 *strinfo,
|
||||
guint length,
|
||||
const gchar *string,
|
||||
gboolean alias)
|
||||
{
|
||||
guint32 words[STRINFO_MAX_WORDS];
|
||||
guint n_words;
|
||||
|
||||
if (length == 0)
|
||||
return -1;
|
||||
|
||||
n_words = strinfo_string_to_words (string, words, alias);
|
||||
|
||||
return strinfo_scan (strinfo + 1, length - 1, words, n_words);
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static gint
|
||||
strinfo_find_integer (const guint32 *strinfo,
|
||||
guint length,
|
||||
guint32 value)
|
||||
{
|
||||
guint i;
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
if (strinfo[i] == value)
|
||||
{
|
||||
const guchar *charinfo = (const guchar *) &strinfo[i];
|
||||
|
||||
/* make sure it has 0xff on either side */
|
||||
if ((i == 0 || charinfo[-1] == 0xff) && charinfo[4] == 0xff)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static gboolean
|
||||
strinfo_is_string_valid (const guint32 *strinfo,
|
||||
guint length,
|
||||
const gchar *string)
|
||||
{
|
||||
return strinfo_find_string (strinfo, length, string, FALSE) != -1;
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static gboolean
|
||||
strinfo_enum_from_string (const guint32 *strinfo,
|
||||
guint length,
|
||||
const gchar *string,
|
||||
guint *result)
|
||||
{
|
||||
gint index;
|
||||
|
||||
index = strinfo_find_string (strinfo, length, string, FALSE);
|
||||
|
||||
if (index < 0)
|
||||
return FALSE;
|
||||
|
||||
*result = strinfo[index];
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static const gchar *
|
||||
strinfo_string_from_enum (const guint32 *strinfo,
|
||||
guint length,
|
||||
guint value)
|
||||
{
|
||||
gint index;
|
||||
|
||||
index = strinfo_find_integer (strinfo, length, value);
|
||||
|
||||
if (index < 0)
|
||||
return NULL;
|
||||
|
||||
return 1 + (const gchar *) &strinfo[index + 1];
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static const gchar *
|
||||
strinfo_string_from_alias (const guint32 *strinfo,
|
||||
guint length,
|
||||
const gchar *alias)
|
||||
{
|
||||
gint index;
|
||||
|
||||
index = strinfo_find_string (strinfo, length, alias, TRUE);
|
||||
|
||||
if (index < 0)
|
||||
return NULL;
|
||||
|
||||
return 1 + (const gchar *) &strinfo[GUINT32_TO_LE (strinfo[index]) + 1];
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static void
|
||||
strinfo_builder_append_item (GString *builder,
|
||||
const gchar *string,
|
||||
guint value)
|
||||
{
|
||||
guint32 words[STRINFO_MAX_WORDS];
|
||||
guint n_words;
|
||||
|
||||
value = GUINT32_TO_LE (value);
|
||||
|
||||
n_words = strinfo_string_to_words (string, words, FALSE);
|
||||
g_string_append_len (builder, (void *) &value, sizeof value);
|
||||
g_string_append_len (builder, (void *) words, 4 * n_words);
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static gboolean
|
||||
strinfo_builder_append_alias (GString *builder,
|
||||
const gchar *alias,
|
||||
const gchar *target)
|
||||
{
|
||||
guint32 words[STRINFO_MAX_WORDS];
|
||||
guint n_words;
|
||||
guint value;
|
||||
gint index;
|
||||
|
||||
index = strinfo_find_string ((const guint32 *) builder->str,
|
||||
builder->len / 4, target, FALSE);
|
||||
|
||||
if (index == -1)
|
||||
return FALSE;
|
||||
|
||||
value = GUINT32_TO_LE (index);
|
||||
|
||||
n_words = strinfo_string_to_words (alias, words, TRUE);
|
||||
g_string_append_len (builder, (void *) &value, sizeof value);
|
||||
g_string_append_len (builder, (void *) words, 4 * n_words);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
G_GNUC_UNUSED static gboolean
|
||||
strinfo_builder_contains (GString *builder,
|
||||
const gchar *string)
|
||||
{
|
||||
return strinfo_find_string ((const guint32 *) builder->str,
|
||||
builder->len / 4, string, FALSE) != -1 ||
|
||||
strinfo_find_string ((const guint32 *) builder->str,
|
||||
builder->len / 4, string, FALSE) != -1;
|
||||
}
|
18
gio/tests/enums.xml.template
Normal file
18
gio/tests/enums.xml.template
Normal file
@ -0,0 +1,18 @@
|
||||
/*** BEGIN comment ***/
|
||||
<!-- @comment@ -->
|
||||
/*** END comment ***/
|
||||
/*** BEGIN file-header ***/
|
||||
<schemalist>
|
||||
/*** END file-header ***/
|
||||
/*** BEGIN value-header ***/
|
||||
<@type@ id='org.gtk.test.@EnumName@'>
|
||||
/*** END value-header ***/
|
||||
/*** BEGIN value-production ***/
|
||||
<value nick='@valuenick@' value='@valuenum@'/>
|
||||
/*** END value-production ***/
|
||||
/*** BEGIN value-tail ***/
|
||||
</@type@>
|
||||
/*** END value-tail ***/
|
||||
/*** BEGIN file-tail ***/
|
||||
</schemalist>
|
||||
/*** END file-tail ***/
|
@ -6,6 +6,8 @@
|
||||
#define G_SETTINGS_ENABLE_BACKEND
|
||||
#include <gio/gsettingsbackend.h>
|
||||
|
||||
#include "testenum.h"
|
||||
|
||||
static gboolean backend_set;
|
||||
|
||||
/* These tests rely on the schemas in org.gtk.test.gschema.xml
|
||||
@ -41,7 +43,7 @@ test_basic (void)
|
||||
abort ();
|
||||
}
|
||||
g_test_trap_assert_failed ();
|
||||
g_test_trap_assert_stderr ("*correct_type*");
|
||||
g_test_trap_assert_stderr ("*g_settings_type_check*");
|
||||
}
|
||||
|
||||
g_settings_get (settings, "greeting", "s", &str);
|
||||
@ -120,7 +122,7 @@ test_wrong_type (void)
|
||||
|
||||
settings = g_settings_new ("org.gtk.test");
|
||||
|
||||
g_settings_set (settings, "greetings", "o", "/a/path");
|
||||
g_settings_set (settings, "greeting", "o", "/a/path");
|
||||
}
|
||||
g_test_trap_assert_failed ();
|
||||
g_test_trap_assert_stderr ("*CRITICAL*");
|
||||
@ -1186,9 +1188,137 @@ glib_translations_work (void)
|
||||
return str != orig;
|
||||
}
|
||||
|
||||
#include "../strinfo.c"
|
||||
|
||||
static void
|
||||
test_strinfo (void)
|
||||
{
|
||||
/* "foo" has a value of 1
|
||||
* "bar" has a value of 2
|
||||
* "baz" is an alias for "bar"
|
||||
*/
|
||||
gchar array[] =
|
||||
"\1\0\0\0" "\xff""foo" "\0\0\0\xff" "\2\0\0\0"
|
||||
"\xff" "bar" "\0\0\0\xff" "\3\0\0\0" "\xfe""baz"
|
||||
"\0\0\0\xff";
|
||||
const guint32 *strinfo = (guint32 *) array;
|
||||
guint length = sizeof array / 4;
|
||||
guint result;
|
||||
|
||||
{
|
||||
/* build it and compare */
|
||||
GString *builder;
|
||||
|
||||
builder = g_string_new (NULL);
|
||||
strinfo_builder_append_item (builder, "foo", 1);
|
||||
strinfo_builder_append_item (builder, "bar", 2);
|
||||
g_assert (strinfo_builder_append_alias (builder, "baz", "bar"));
|
||||
g_assert_cmpint (builder->len % 4, ==, 0);
|
||||
g_assert_cmpint (builder->len / 4, ==, length);
|
||||
g_assert (memcmp (builder->str, strinfo, length * 4) == 0);
|
||||
g_string_free (builder, TRUE);
|
||||
}
|
||||
|
||||
g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "foo"),
|
||||
==, NULL);
|
||||
g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "bar"),
|
||||
==, NULL);
|
||||
g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "baz"),
|
||||
==, "bar");
|
||||
g_assert_cmpstr (strinfo_string_from_alias (strinfo, length, "quux"),
|
||||
==, NULL);
|
||||
|
||||
g_assert (strinfo_enum_from_string (strinfo, length, "foo", &result));
|
||||
g_assert_cmpint (result, ==, 1);
|
||||
g_assert (strinfo_enum_from_string (strinfo, length, "bar", &result));
|
||||
g_assert_cmpint (result, ==, 2);
|
||||
g_assert (!strinfo_enum_from_string (strinfo, length, "baz", &result));
|
||||
g_assert (!strinfo_enum_from_string (strinfo, length, "quux", &result));
|
||||
|
||||
g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 0), ==, NULL);
|
||||
g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 1), ==, "foo");
|
||||
g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 2), ==, "bar");
|
||||
g_assert_cmpstr (strinfo_string_from_enum (strinfo, length, 3), ==, NULL);
|
||||
|
||||
g_assert (strinfo_is_string_valid (strinfo, length, "foo"));
|
||||
g_assert (strinfo_is_string_valid (strinfo, length, "bar"));
|
||||
g_assert (!strinfo_is_string_valid (strinfo, length, "baz"));
|
||||
g_assert (!strinfo_is_string_valid (strinfo, length, "quux"));
|
||||
}
|
||||
|
||||
static void
|
||||
test_enums (void)
|
||||
{
|
||||
GSettings *settings, *direct;
|
||||
|
||||
settings = g_settings_new ("org.gtk.test.enums");
|
||||
direct = g_settings_new ("org.gtk.test.enums.direct");
|
||||
|
||||
if (!backend_set)
|
||||
{
|
||||
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
||||
g_settings_get_enum (direct, "test");
|
||||
g_test_trap_assert_failed ();
|
||||
g_test_trap_assert_stderr ("*not associated with an enum*");
|
||||
|
||||
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
||||
g_settings_set_enum (settings, "test", 42);
|
||||
g_test_trap_assert_failed ();
|
||||
g_test_trap_assert_stderr ("*invalid enum value 42*");
|
||||
|
||||
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
||||
g_settings_set_string (settings, "test", "qux");
|
||||
g_test_trap_assert_failed ();
|
||||
g_test_trap_assert_stderr ("*g_settings_range_check*");
|
||||
}
|
||||
|
||||
g_assert_cmpstr (g_settings_get_string (settings, "test"), ==, "bar");
|
||||
g_settings_set_enum (settings, "test", TEST_ENUM_FOO);
|
||||
g_assert_cmpstr (g_settings_get_string (settings, "test"), ==, "foo");
|
||||
g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_FOO);
|
||||
g_settings_set_string (direct, "test", "qux");
|
||||
g_assert_cmpstr (g_settings_get_string (direct, "test"), ==, "qux");
|
||||
g_assert_cmpstr (g_settings_get_string (settings, "test"), ==, "quux");
|
||||
g_assert_cmpint (g_settings_get_enum (settings, "test"), ==, TEST_ENUM_QUUX);
|
||||
}
|
||||
|
||||
static void
|
||||
test_range (void)
|
||||
{
|
||||
GSettings *settings, *direct;
|
||||
|
||||
settings = g_settings_new ("org.gtk.test.range");
|
||||
direct = g_settings_new ("org.gtk.test.range.direct");
|
||||
|
||||
if (!backend_set)
|
||||
{
|
||||
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
||||
g_settings_set_int (settings, "val", 45);
|
||||
g_test_trap_assert_failed ();
|
||||
g_test_trap_assert_stderr ("*g_settings_range_check*");
|
||||
|
||||
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
|
||||
g_settings_set_int (settings, "val", 1);
|
||||
g_test_trap_assert_failed ();
|
||||
g_test_trap_assert_stderr ("*g_settings_range_check*");
|
||||
}
|
||||
|
||||
g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
|
||||
g_settings_set_int (direct, "val", 22);
|
||||
g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 22);
|
||||
g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 22);
|
||||
g_settings_set_int (direct, "val", 45);
|
||||
g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 45);
|
||||
g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
|
||||
g_settings_set_int (direct, "val", 1);
|
||||
g_assert_cmpint (g_settings_get_int (direct, "val"), ==, 1);
|
||||
g_assert_cmpint (g_settings_get_int (settings, "val"), ==, 33);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
gchar *enums;
|
||||
gint result;
|
||||
|
||||
setlocale (LC_ALL, "");
|
||||
@ -1203,9 +1333,18 @@ main (int argc, char *argv[])
|
||||
g_type_init ();
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_remove ("org.gtk.test.enums.xml");
|
||||
g_assert (g_spawn_command_line_sync ("../../gobject/glib-mkenums "
|
||||
"--template enums.xml.template "
|
||||
SRCDIR "/testenum.h",
|
||||
&enums, NULL, &result, NULL));
|
||||
g_assert (result == 0);
|
||||
g_assert (g_file_set_contents ("org.gtk.test.enums.xml", enums, -1, NULL));
|
||||
|
||||
g_remove ("gschemas.compiled");
|
||||
g_assert (g_spawn_command_line_sync ("../glib-compile-schemas --targetdir=. " SRCDIR,
|
||||
NULL, NULL, NULL, NULL));
|
||||
NULL, NULL, &result, NULL));
|
||||
g_assert (result == 0);
|
||||
|
||||
g_test_add_func ("/gsettings/basic", test_basic);
|
||||
|
||||
@ -1243,6 +1382,9 @@ main (int argc, char *argv[])
|
||||
|
||||
g_test_add_func ("/gsettings/keyfile", test_keyfile);
|
||||
g_test_add_func ("/gsettings/child-schema", test_child_schema);
|
||||
g_test_add_func ("/gsettings/strinfo", test_strinfo);
|
||||
g_test_add_func ("/gsettings/enums", test_enums);
|
||||
g_test_add_func ("/gsettings/range", test_range);
|
||||
|
||||
result = g_test_run ();
|
||||
|
||||
|
@ -92,4 +92,32 @@
|
||||
</key>
|
||||
</schema>
|
||||
|
||||
<schema id='org.gtk.test.enums' path='/tests/enums/'>
|
||||
<key name='test' enum='org.gtk.test.TestEnum'>
|
||||
<default>'bar'</default>
|
||||
<aliases>
|
||||
<alias value='qux' target='quux'/>
|
||||
</aliases>
|
||||
</key>
|
||||
</schema>
|
||||
|
||||
<schema id='org.gtk.test.enums.direct' path='/tests/enums/'>
|
||||
<key name='test' type='s'>
|
||||
<default>'bar'</default>
|
||||
</key>
|
||||
</schema>
|
||||
|
||||
<schema id='org.gtk.test.range' path='/tests/range/'>
|
||||
<key name='val' type='i'>
|
||||
<default>33</default>
|
||||
<range min='2' max='44'/>
|
||||
</key>
|
||||
</schema>
|
||||
|
||||
<schema id='org.gtk.test.range.direct' path='/tests/range/'>
|
||||
<key name='val' type='i'>
|
||||
<default>33</default>
|
||||
</key>
|
||||
</schema>
|
||||
|
||||
</schemalist>
|
||||
|
7
gio/tests/testenum.h
Normal file
7
gio/tests/testenum.h
Normal file
@ -0,0 +1,7 @@
|
||||
typedef enum
|
||||
{
|
||||
TEST_ENUM_FOO,
|
||||
TEST_ENUM_BAR,
|
||||
TEST_ENUM_BAZ,
|
||||
TEST_ENUM_QUUX
|
||||
} TestEnum;
|
Loading…
Reference in New Issue
Block a user