rules: add include statements to rules files
[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 "test-config.h"
26
27 #include "test.h"
28 #include "xkbcomp/xkbcomp-priv.h"
29 #include "xkbcomp/rules.h"
30
31 struct test_data {
32     /* Rules file */
33     const char *rules;
34
35     /* Input */
36     const char *model;
37     const char *layout;
38     const char *variant;
39     const char *options;
40
41     /* Expected output */
42     const char *keycodes;
43     const char *types;
44     const char *compat;
45     const char *symbols;
46
47     /* Or set this if xkb_components_from_rules() should fail. */
48     bool should_fail;
49 };
50
51 static bool
52 test_rules(struct xkb_context *ctx, struct test_data *data)
53 {
54     bool passed;
55     const struct xkb_rule_names rmlvo = {
56         data->rules, data->model, data->layout, data->variant, data->options
57     };
58     struct xkb_component_names kccgst;
59
60     fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
61             data->model, data->layout, data->variant, data->options);
62
63     if (data->should_fail)
64         fprintf(stderr, "Expecting: FAILURE\n");
65     else
66         fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
67                 data->keycodes, data->types, data->compat, data->symbols);
68
69     if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
70         fprintf(stderr, "Received : FAILURE\n");
71         return data->should_fail;
72     }
73
74     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
75             kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
76
77     passed = streq(kccgst.keycodes, data->keycodes) &&
78              streq(kccgst.types, data->types) &&
79              streq(kccgst.compat, data->compat) &&
80              streq(kccgst.symbols, data->symbols);
81
82     free(kccgst.keycodes);
83     free(kccgst.types);
84     free(kccgst.compat);
85     free(kccgst.symbols);
86
87     return passed;
88 }
89
90 int
91 main(int argc, char *argv[])
92 {
93     struct xkb_context *ctx;
94
95     setenv("XKB_CONFIG_ROOT", TEST_XKB_CONFIG_ROOT, 1);
96
97     ctx = test_get_context(0);
98     assert(ctx);
99
100     struct test_data test1 = {
101         .rules = "inc-src-simple",
102
103         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
104
105         .keycodes = "my_keycodes", .types = "default_types",
106         .compat = "default_compat", .symbols = "my_symbols",
107     };
108     assert(test_rules(ctx, &test1));
109
110     struct test_data test2 = {
111         .rules = "inc-src-nested",
112
113         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
114
115         .keycodes = "my_keycodes", .types = "default_types",
116         .compat = "default_compat", .symbols = "my_symbols",
117     };
118     assert(test_rules(ctx, &test2));
119
120     struct test_data test3 = {
121         .rules = "inc-src-looped",
122
123         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
124
125         .should_fail = true,
126     };
127     assert(test_rules(ctx, &test3));
128
129     struct test_data test4 = {
130         .rules = "inc-src-before-after",
131
132         .model = "before_model", .layout = "my_layout", .variant = "", .options = "",
133
134         .keycodes = "my_keycodes", .types = "default_types",
135         .compat = "default_compat", .symbols = "default_symbols",
136     };
137     assert(test_rules(ctx, &test4));
138
139     struct test_data test5 = {
140         .rules = "inc-src-options",
141
142         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
143         .options = "option11,my_option,colon:opt,option111",
144
145         .keycodes = "my_keycodes", .types = "default_types",
146         .compat = "default_compat+substring+group(bla)|some:compat",
147         .symbols = "my_symbols+extra_variant+altwin(menu)",
148     };
149     assert(test_rules(ctx, &test5));
150
151     struct test_data test6 = {
152         .rules = "inc-src-loop-twice",
153
154         .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
155
156         .keycodes = "my_keycodes", .types = "default_types",
157         .compat = "default_compat", .symbols = "my_symbols",
158     };
159     assert(test_rules(ctx, &test6));
160
161     xkb_context_unref(ctx);
162     return 0;
163 }