Add testcases for <range> and <choices>
[platform/upstream/glib.git] / gio / tests / gschema-compile.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <locale.h>
4 #include <libintl.h>
5 #include <gio.h>
6 #include <gstdio.h>
7
8 typedef struct {
9   const gchar *name;
10   const gchar *opt;
11   const gchar *err;
12 } SchemaTest;
13
14 static void
15 test_schema (gpointer data)
16 {
17   SchemaTest *test = (SchemaTest *) data;
18
19   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
20     {
21       gchar *filename = g_strconcat (test->name, ".gschema.xml", NULL);
22       gchar *path = g_build_filename (SRCDIR, "schema-tests", filename, NULL);
23       gchar *argv[] = {
24         "../glib-compile-schemas",
25         "--dry-run",
26         "--schema-files", path,
27         (gchar *)test->opt,
28         NULL
29       };
30       gchar *envp[] = { NULL };
31       execve (argv[0], argv, envp);
32       g_free (filename);
33       g_free (path);
34     }
35   if (test->err)
36     {
37       g_test_trap_assert_failed ();
38       g_test_trap_assert_stderr (test->err);
39     }
40   else
41     g_test_trap_assert_passed();
42 }
43
44 static const SchemaTest tests[] = {
45   { "no-default",                   NULL, "*<default> is required in <key>*"                    },
46   { "missing-quotes",               NULL, "*unknown keyword*"                                   },
47   { "incomplete-list",              NULL, "*to follow array element*"                           },
48   { "wrong-category",               NULL, "*attribute 'l10n' invalid*"                          },
49   { "bad-type",                     NULL, "*invalid GVariant type string*"                      },
50   { "overflow",                     NULL, "*out of range*"                                      },
51   { "range-wrong-type",             NULL, "*<range> not allowed for keys of type*"              },
52   { "range-missing-min",            NULL, "*element 'range' requires attribute 'min'*"          },
53   { "range-missing-max",            NULL, "*element 'range' requires attribute 'max'*"          },
54   { "default-out-of-range",         NULL, "*<default> is not contained in the specified range*" },
55   { "choices-wrong-type",           NULL, "*<choices> not allowed for keys of type*"            },
56   { "choice-missing-value",         NULL, "*element 'choice' requires attribute 'value'*"       },
57   { "default-not-in-choices",       NULL, "*<default> contains string not in <choices>*"        },
58   { "array-default-not-in-choices", NULL, "*<default> contains string not in <choices>*"        },
59   { "bad-key",                      NULL, "*invalid name*"                                      },
60   { "bad-key",                      "--allow-any-name", NULL                                    },
61   { "bad-key2",                     NULL, "*invalid name*"                                      },
62   { "bad-key2",                     "--allow-any-name", NULL                                    },
63   { "bad-key3",                     NULL, "*invalid name*"                                      },
64   { "bad-key3",                     "--allow-any-name", NULL                                    },
65   { "bad-key4",                     NULL, "*invalid name*"                                      },
66   { "bad-key4",                     "--allow-any-name", NULL                                    },
67   { "empty-key",                    NULL, "*empty names*"                                       },
68   { "empty-key",                    "--allow-any-name", "*empty names*"                         }
69 };
70
71 int
72 main (int argc, char *argv[])
73 {
74   guint i;
75
76   setlocale (LC_ALL, "");
77
78   g_type_init ();
79   g_test_init (&argc, &argv, NULL);
80
81   for (i = 0; i < G_N_ELEMENTS (tests); ++i)
82     {
83       gchar *name = g_strdup_printf ("/gschema/%s%s", tests[i].name, tests[i].opt ? "/opt" : "");
84       g_test_add_data_func (name, &tests[i], (gpointer) test_schema);
85       g_free (name);
86     }
87
88   return g_test_run ();
89 }