Keysyms: Update using latest xorgproto
[platform/upstream/libxkbcommon.git] / test / rules-file-includes.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  * Copyright © 2019 Red Hat, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "config.h"
26 #include "test-config.h"
27
28 #include "test.h"
29 #include "xkbcomp/xkbcomp-priv.h"
30 #include "xkbcomp/rules.h"
31
32 struct test_data {
33     /* Rules file */
34     const char *rules;
35
36     /* Input */
37     const char *model;
38     const char *layout;
39     const char *variant;
40     const char *options;
41
42     /* Expected output */
43     const char *keycodes;
44     const char *types;
45     const char *compat;
46     const char *symbols;
47
48     /* Or set this if xkb_components_from_rules() should fail. */
49     bool should_fail;
50 };
51
52 static bool
53 test_rules(struct xkb_context *ctx, struct test_data *data)
54 {
55     bool passed;
56     const struct xkb_rule_names rmlvo = {
57         data->rules, data->model, data->layout, data->variant, data->options
58     };
59     struct xkb_component_names kccgst;
60
61     fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
62             data->model, data->layout, data->variant, data->options);
63
64     if (data->should_fail)
65         fprintf(stderr, "Expecting: FAILURE\n");
66     else
67         fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
68                 data->keycodes, data->types, data->compat, data->symbols);
69
70     if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
71         fprintf(stderr, "Received : FAILURE\n");
72         return data->should_fail;
73     }
74
75     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
76             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
77
78     passed = streq(kccgst.keycodes, data->keycodes) &&
79              streq(kccgst.types, data->types) &&
80              streq(kccgst.compat, data->compat) &&
81              streq(kccgst.symbols, data->symbols);
82
83     free(kccgst.keycodes);
84     free(kccgst.types);
85     free(kccgst.compat);
86     free(kccgst.symbols);
87
88     return passed;
89 }
90
91 int
92 main(int argc, char *argv[])
93 {
94     struct xkb_context *ctx;
95
96     setenv("XKB_CONFIG_ROOT", TEST_XKB_CONFIG_ROOT, 1);
97
98     ctx = test_get_context(0);
99     assert(ctx);
100
101     struct test_data test1 = {
102         .rules = "inc-src-simple",
103
104         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
105
106         .keycodes = "my_keycodes", .types = "default_types",
107         .compat = "default_compat", .symbols = "my_symbols",
108     };
109     assert(test_rules(ctx, &test1));
110
111     struct test_data test2 = {
112         .rules = "inc-src-nested",
113
114         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
115
116         .keycodes = "my_keycodes", .types = "default_types",
117         .compat = "default_compat", .symbols = "my_symbols",
118     };
119     assert(test_rules(ctx, &test2));
120
121     struct test_data test3 = {
122         .rules = "inc-src-looped",
123
124         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
125
126         .should_fail = true,
127     };
128     assert(test_rules(ctx, &test3));
129
130     struct test_data test4 = {
131         .rules = "inc-src-before-after",
132
133         .model = "before_model", .layout = "my_layout", .variant = "", .options = "",
134
135         .keycodes = "my_keycodes", .types = "default_types",
136         .compat = "default_compat", .symbols = "default_symbols",
137     };
138     assert(test_rules(ctx, &test4));
139
140     struct test_data test5 = {
141         .rules = "inc-src-options",
142
143         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
144         .options = "option11,my_option,colon:opt,option111",
145
146         .keycodes = "my_keycodes", .types = "default_types",
147         .compat = "default_compat+substring+group(bla)|some:compat",
148         .symbols = "my_symbols+extra_variant+altwin(menu)",
149     };
150     assert(test_rules(ctx, &test5));
151
152     struct test_data test6 = {
153         .rules = "inc-src-loop-twice",
154
155         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
156
157         .keycodes = "my_keycodes", .types = "default_types",
158         .compat = "default_compat", .symbols = "my_symbols",
159     };
160     assert(test_rules(ctx, &test6));
161
162     xkb_context_unref(ctx);
163     return 0;
164 }