1 /*
2 * Copyright © 2012 Ran Benita <[email protected]>
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 "config.h"
25
26 #include "test.h"
27 #include "xkbcomp/xkbcomp-priv.h"
28 #include "xkbcomp/rules.h"
29
30 struct test_data {
31 /* Rules file */
32 const char *rules;
33
34 /* Input */
35 const char *model;
36 const char *layout;
37 const char *variant;
38 const char *options;
39
40 /* Expected output */
41 const char *keycodes;
42 const char *types;
43 const char *compat;
44 const char *symbols;
45
46 /* Or set this if xkb_components_from_rules() should fail. */
47 bool should_fail;
48 };
49
50 static bool
test_rules(struct xkb_context * ctx,struct test_data * data)51 test_rules(struct xkb_context *ctx, struct test_data *data)
52 {
53 bool passed;
54 const struct xkb_rule_names rmlvo = {
55 data->rules, data->model, data->layout, data->variant, data->options
56 };
57 struct xkb_component_names kccgst;
58
59 fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
60 data->model, data->layout, data->variant, data->options);
61
62 if (data->should_fail)
63 fprintf(stderr, "Expecting: FAILURE\n");
64 else
65 fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
66 data->keycodes, data->types, data->compat, data->symbols);
67
68 if (!xkb_components_from_rules(ctx, &rmlvo, &kccgst)) {
69 fprintf(stderr, "Received : FAILURE\n");
70 return data->should_fail;
71 }
72
73 fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
74 kccgst.keycodes, kccgst.types, kccgst.compat, kccgst.symbols);
75
76 passed = streq(kccgst.keycodes, data->keycodes) &&
77 streq(kccgst.types, data->types) &&
78 streq(kccgst.compat, data->compat) &&
79 streq(kccgst.symbols, data->symbols);
80
81 free(kccgst.keycodes);
82 free(kccgst.types);
83 free(kccgst.compat);
84 free(kccgst.symbols);
85
86 return passed;
87 }
88
89 int
main(int argc,char * argv[])90 main(int argc, char *argv[])
91 {
92 struct xkb_context *ctx;
93
94 ctx = test_get_context(0);
95 assert(ctx);
96
97 struct test_data test1 = {
98 .rules = "simple",
99
100 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
101 .options = "my_option",
102
103 .keycodes = "my_keycodes", .types = "my_types",
104 .compat = "my_compat|some:compat",
105 .symbols = "my_symbols+extra_variant",
106 };
107 assert(test_rules(ctx, &test1));
108
109 struct test_data test2 = {
110 .rules = "simple",
111
112 .model = "", .layout = "", .variant = "", .options = "",
113
114 .keycodes = "default_keycodes", .types = "default_types",
115 .compat = "default_compat", .symbols = "default_symbols",
116 };
117 assert(test_rules(ctx, &test2));
118
119 struct test_data test3 = {
120 .rules = "groups",
121
122 .model = "pc104", .layout = "foo", .variant = "", .options = "",
123
124 .keycodes = "something(pc104)", .types = "default_types",
125 .compat = "default_compat", .symbols = "default_symbols",
126 };
127 assert(test_rules(ctx, &test3));
128
129 struct test_data test4 = {
130 .rules = "groups",
131
132 .model = "foo", .layout = "ar", .variant = "bar", .options = "",
133
134 .keycodes = "default_keycodes", .types = "default_types",
135 .compat = "default_compat", .symbols = "my_symbols+(bar)",
136 };
137 assert(test_rules(ctx, &test4));
138
139 struct test_data test5 = {
140 .rules = "simple",
141
142 .model = NULL, .layout = "my_layout,second_layout", .variant = "my_variant",
143 .options = "my_option",
144
145 .should_fail = true
146 };
147 assert(test_rules(ctx, &test5));
148
149 struct test_data test6 = {
150 .rules = "index",
151
152 .model = "", .layout = "br,al,cn,az", .variant = "",
153 .options = "some:opt",
154
155 .keycodes = "default_keycodes", .types = "default_types",
156 .compat = "default_compat",
157 .symbols = "default_symbols+extra:1+extra:2+extra:3+extra:4",
158 };
159 assert(test_rules(ctx, &test6));
160
161 struct test_data test7 = {
162 .rules = "multiple-options",
163
164 .model = "my_model", .layout = "my_layout", .variant = "my_variant",
165 .options = "option3,option1,colon:opt,option11",
166
167 .keycodes = "my_keycodes", .types = "my_types",
168 .compat = "my_compat+some:compat+group(bla)",
169 .symbols = "my_symbols+extra_variant+compose(foo)+keypad(bar)+altwin(menu)",
170 };
171 assert(test_rules(ctx, &test7));
172
173 xkb_context_unref(ctx);
174 return 0;
175 }
176