rules: remove support for keymap rule
[platform/upstream/libxkbcommon.git] / test / rules-file.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "xkbcommon/xkbcommon.h"
30 #include "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 inline bool
53 streq(const char *s1, const char *s2)
54 {
55     if (s1 == NULL || s2 == NULL)
56         return s1 == s2;
57     return strcmp(s1, s2) == 0;
58 }
59
60 static bool
61 test_rules(struct xkb_context *ctx, struct test_data *data)
62 {
63     bool passed;
64     const struct xkb_rule_names rmlvo = {
65         data->rules, data->model, data->layout, data->variant, data->options
66     };
67     struct xkb_component_names *kccgst;
68
69     fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
70             data->model, data->layout, data->variant, data->options);
71
72     if (data->should_fail)
73         fprintf(stderr, "Expecting: NULL\n");
74     else
75         fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
76                 data->keycodes, data->types, data->compat, data->symbols);
77
78     kccgst = xkb_components_from_rules(ctx, &rmlvo);
79     if (!kccgst) {
80         fprintf(stderr, "Received: NULL\n");
81         return data->should_fail;
82     }
83
84     fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
85             kccgst->keycodes, kccgst->types, kccgst->compat, kccgst->symbols);
86
87     passed = streq(kccgst->keycodes, data->keycodes) &&
88              streq(kccgst->types, data->types) &&
89              streq(kccgst->compat, data->compat) &&
90              streq(kccgst->symbols, data->symbols);
91
92     free(kccgst->keycodes);
93     free(kccgst->types);
94     free(kccgst->compat);
95     free(kccgst->symbols);
96     free(kccgst);
97
98     return passed;
99 }
100
101 int
102 main(void)
103 {
104     struct xkb_context *ctx;
105     const char *srcdir = getenv("srcdir");
106     char *path;
107
108     ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
109     assert(ctx);
110
111     assert(asprintf(&path, "%s/test/data", srcdir ? srcdir : ".") > 0);
112     assert(xkb_context_include_path_append(ctx, path));
113     free(path);
114
115     struct test_data test1 = {
116         .rules = "simple",
117
118         .model = "my_model", .layout = "my_layout", .variant = "my_variant",
119         .options = "my_option",
120
121         .keycodes = "my_keycodes", .types = "my_types",
122         .compat = "my_compat+some:compat",
123         .symbols = "my_symbols+extra_variant",
124     };
125     assert(test_rules(ctx, &test1));
126
127     struct test_data test2 = {
128         .rules = "simple",
129
130         .model = "", .layout = "", .variant = "", .options = "",
131
132         .keycodes = "default_keycodes", .types = "default_types",
133         .compat = "default_compat", .symbols = "default_symbols",
134     };
135     assert(test_rules(ctx, &test2));
136
137     struct test_data test3 = {
138         .rules = "groups",
139
140         .model = "pc104", .layout = "foo", .variant = "", .options = "",
141
142         .keycodes = "something(pc104)", .types = "default_types",
143         .compat = "default_compat", .symbols = "default_symbols",
144     };
145     assert(test_rules(ctx, &test3));
146
147     struct test_data test4 = {
148         .rules = "groups",
149
150         .model = "foo", .layout = "ar", .variant = "bar", .options = "",
151
152         .keycodes = "default_keycodes", .types = "default_types",
153         .compat = "default_compat", .symbols = "my_symbols+(bar)",
154     };
155     assert(test_rules(ctx, &test4));
156
157     struct test_data test5 = {
158         .rules = "simple",
159
160         .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
161         .options = "my_option",
162
163         .should_fail = true
164     };
165     assert(test_rules(ctx, &test5));
166
167     struct test_data test6 = {
168         .rules = "index",
169
170         .model = "", .layout = "br,al,cn,az", .variant = "",
171         .options = "some:opt",
172
173         .keycodes = "default_keycodes", .types = "default_types",
174         .compat = "default_compat",
175         .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
176     };
177     assert(test_rules(ctx, &test6));
178
179     xkb_context_unref(ctx);
180     return 0;
181 }