rules-file-includes.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright © 2012 Ran Benita <ran234@gmail.com>
  3. * Copyright © 2019 Red Hat, Inc.
  4. * SPDX-License-Identifier: MIT
  5. */
  6. #include "config.h"
  7. #include "test-config.h"
  8. #include "xkbcommon/xkbcommon.h"
  9. #include "test.h"
  10. #include "utils.h"
  11. #include "xkbcomp/rules.h"
  12. struct test_data {
  13. /* Rules file */
  14. const char *rules;
  15. /* Input */
  16. const char *model;
  17. const char *layout;
  18. const char *variant;
  19. const char *options;
  20. /* Expected output */
  21. const char *keycodes;
  22. const char *types;
  23. const char *compat;
  24. const char *symbols;
  25. const char *geometry;
  26. /* Or set this if xkb_components_from_rules() should fail. */
  27. bool should_fail;
  28. };
  29. static bool
  30. test_rules(struct xkb_context *ctx, struct test_data *data)
  31. {
  32. fprintf(stderr, "\n\nChecking : %s\t%s\t%s\t%s\t%s\n", data->rules,
  33. data->model, data->layout, data->variant, data->options);
  34. if (data->should_fail)
  35. fprintf(stderr, "Expecting: FAILURE\n");
  36. else
  37. fprintf(stderr, "Expecting: %s\t%s\t%s\t%s\n",
  38. data->keycodes, data->types, data->compat, data->symbols);
  39. const struct xkb_rule_names rmlvo = {
  40. data->rules, data->model, data->layout, data->variant, data->options
  41. };
  42. struct xkb_component_names kccgst;
  43. if (xkb_components_names_from_rules(ctx, &rmlvo, NULL, &kccgst)) {
  44. fprintf(stderr, "Received : %s\t%s\t%s\t%s\n",
  45. kccgst.keycodes, kccgst.types, kccgst.compatibility,
  46. kccgst.symbols);
  47. } else {
  48. fprintf(stderr, "Received : FAILURE\n");
  49. return data->should_fail;
  50. }
  51. const bool passed = streq_not_null(kccgst.keycodes, data->keycodes) &&
  52. streq_not_null(kccgst.types, data->types) &&
  53. streq_not_null(kccgst.compatibility, data->compat) &&
  54. streq_not_null(kccgst.symbols, data->symbols) &&
  55. streq_null(kccgst.geometry, data->geometry);
  56. free(kccgst.keycodes);
  57. free(kccgst.types);
  58. free(kccgst.compatibility);
  59. free(kccgst.symbols);
  60. free(kccgst.geometry);
  61. return passed;
  62. }
  63. int
  64. main(int argc, char *argv[])
  65. {
  66. struct xkb_context *ctx;
  67. test_init();
  68. setenv("XKB_CONFIG_ROOT", TEST_XKB_CONFIG_ROOT, 1);
  69. ctx = test_get_context(0);
  70. assert(ctx);
  71. struct test_data test1 = {
  72. .rules = "inc-src-simple",
  73. .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
  74. .keycodes = "my_keycodes", .types = "default_types",
  75. .compat = "default_compat", .symbols = "my_symbols",
  76. };
  77. assert(test_rules(ctx, &test1));
  78. struct test_data test2 = {
  79. .rules = "inc-src-nested",
  80. .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
  81. .keycodes = "my_keycodes", .types = "default_types",
  82. .compat = "default_compat", .symbols = "my_symbols",
  83. };
  84. assert(test_rules(ctx, &test2));
  85. struct test_data test3 = {
  86. .rules = "inc-src-looped",
  87. .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
  88. .should_fail = true,
  89. };
  90. assert(test_rules(ctx, &test3));
  91. struct test_data test4 = {
  92. .rules = "inc-src-before-after",
  93. .model = "before_model", .layout = "my_layout", .variant = "", .options = "",
  94. .keycodes = "my_keycodes", .types = "default_types",
  95. .compat = "default_compat", .symbols = "default_symbols",
  96. };
  97. assert(test_rules(ctx, &test4));
  98. struct test_data test5 = {
  99. .rules = "inc-src-options",
  100. .model = "my_model", .layout = "my_layout", .variant = "my_variant",
  101. .options = "option11,my_option,colon:opt,option111",
  102. .keycodes = "my_keycodes", .types = "default_types",
  103. .compat = "default_compat+substring+group(bla)|some:compat",
  104. .symbols = "my_symbols+extra_variant+altwin(menu)",
  105. };
  106. assert(test_rules(ctx, &test5));
  107. struct test_data test6 = {
  108. .rules = "inc-src-loop-twice",
  109. .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
  110. .keycodes = "my_keycodes", .types = "default_types",
  111. .compat = "default_compat", .symbols = "my_symbols",
  112. };
  113. assert(test_rules(ctx, &test6));
  114. struct test_data test7 = {
  115. .rules = "inc-no-newline",
  116. .should_fail = true,
  117. };
  118. assert(test_rules(ctx, &test7));
  119. struct test_data test8 = {
  120. .rules = "inc-src-relative-path",
  121. .model = "my_model", .layout = "my_layout", .variant = "", .options = "",
  122. .keycodes = "my_keycodes", .types = "default_types",
  123. .compat = "default_compat", .symbols = "my_symbols",
  124. };
  125. assert(test_rules(ctx, &test8));
  126. xkb_context_unref(ctx);
  127. return 0;
  128. }