File: action_set_api.tests.cpp

package info (click to toggle)
keyman 18.0.246-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,316 kB
  • sloc: python: 52,784; cpp: 21,289; sh: 7,633; ansic: 4,823; xml: 3,617; perl: 959; makefile: 139; javascript: 138
file content (392 lines) | stat: -rw-r--r-- 11,694 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
  Copyright:    © 2018 SIL International.
  Description:  Tests for the context API family of functions.
  Create Date:  23 Oct 2023
  Authors:      Marc Durdin
  History:      23 Oct 2023 - MCD - Initial implementation.
*/
#include <string>

#include "keyman_core.h"

#include "path.hpp"
#include "action.hpp"

#include <test_assert.h>
#include "../emscripten_filesystem.h"
#include "../load_kmx_file.hpp"

const km_core_action_item alert_action_item();
const km_core_action_item bksp_action_item(uint8_t type, uintptr_t value);
const km_core_action_item caps_action_item(uint8_t capsLock);
const km_core_action_item char_action_item(km_core_usv chr);
const km_core_action_item emit_keystroke_action_item();
const km_core_action_item persist_opt_action_item(km_core_option_item const *option);
const km_core_action_item end_action_item();
const km_core_action_item invalidate_context_action_item();
const km_core_action_item marker_action_item(uint32_t marker);

//-------------------------------------------------------------------------------------

km_core_option_item test_env_opts[] =
{
  KM_CORE_OPTIONS_END
};

km_core_keyboard * test_kb = nullptr;
km_core_state * test_state = nullptr;
km_core_context_item * citems = nullptr;
km_core_usv test_empty_output[] = {0};
std::string arg_path;

void teardown() {
  if(citems) {
    km_core_context_items_dispose(citems);
    citems = nullptr;
  }
  if(test_state) {
    km_core_state_dispose(test_state);
    test_state = nullptr;
  }
  if(test_kb) {
    km_core_keyboard_dispose(test_kb);
    test_kb = nullptr;
  }
}

void setup(const char *keyboard, const km_core_cu* context) {
  teardown();

  km::core::path path = km::core::path::join(arg_path, keyboard);

  auto blob = km::tests::load_kmx_file(path.native().c_str());
  try_status(km_core_keyboard_load_from_blob(path.stem().c_str(), blob.data(), blob.size(), &test_kb));
  try_status(km_core_state_create(test_kb, test_env_opts, &test_state));
  try_status(context_items_from_utf16(context, &citems));
  try_status(km_core_context_set(km_core_state_context(test_state), citems));
  try_status(km_core_context_set(km_core_state_app_context(test_state), citems));
}

void run_test(km_core_action_item const * action_items, const km_core_actions &actions) {
  setup("k_000___null_keyboard.kmx", u"");
  test_state->set_actions(actions);
  auto set_actions = test_state->actions();

  int n = 0;
  for(auto act = set_actions.begin(); act != set_actions.end(); act++, n++) {
    test_assert(act->type == action_items[n].type);
    // TODO: all other fields
    switch(act->type) {
      case KM_CORE_IT_ALERT:
        break;
      case KM_CORE_IT_BACK:
        test_assert(act->backspace.expected_type == action_items[n].backspace.expected_type);
        test_assert(act->backspace.expected_value == action_items[n].backspace.expected_value);
        break;
      case KM_CORE_IT_CAPSLOCK:
        test_assert(act->capsLock == action_items[n].capsLock);
        break;
      case KM_CORE_IT_CHAR:
        test_assert(act->character == action_items[n].character);
        break;
      case KM_CORE_IT_EMIT_KEYSTROKE:
        break;
      case KM_CORE_IT_END:
        break;
      case KM_CORE_IT_INVALIDATE_CONTEXT:
        break;
      case KM_CORE_IT_MARKER:
        test_assert(act->marker == action_items[n].marker);
        break;
      case KM_CORE_IT_PERSIST_OPT:
        test_assert(act->option->scope == action_items[n].option->scope);
        test_assert(std::u16string(act->option->key) == action_items[n].option->key);
        test_assert(std::u16string(act->option->value) == action_items[n].option->value);
        break;
      default:
        // Invalid action type
        test_assert(false);
    }
  }
}

//-------------------------------------------------------------------------------------
// Set Action tests
//-------------------------------------------------------------------------------------

void test_two_backspaces() {
  puts("test_two_backspaces");
  const km_core_action_item action_items[] = {
    bksp_action_item(KM_CORE_BT_CHAR, 0),
    bksp_action_item(KM_CORE_BT_CHAR, 0),
    end_action_item()
  };

  const km_core_actions actions = {
    2, // unsigned int code_points_to_delete;
    test_empty_output, // km_core_usv* output;
    test_env_opts, // km_core_option_item* persist_options;
    KM_CORE_FALSE, // km_core_bool do_alert;
    KM_CORE_FALSE, // km_core_bool emit_keystroke;
    KM_CORE_CAPS_UNCHANGED, // new_caps_lock_state;
    nullptr // km_core_usv* deleted_context;
  };

  run_test(action_items, actions);
}

void test_character() {
  puts("test_character");
  const km_core_action_item action_items[] = {
    char_action_item(u'A'),
    end_action_item()
  };

  const km_core_actions actions = {
    0, // unsigned int code_points_to_delete;
    (km_core_usv*) U"A", // km_core_usv* output;
    test_env_opts, // km_core_option_item* persist_options;
    KM_CORE_FALSE, // km_core_bool do_alert;
    KM_CORE_FALSE, // km_core_bool emit_keystroke;
    KM_CORE_CAPS_UNCHANGED, // new_caps_lock_state;
    nullptr // km_core_usv* deleted_context;
  };

  run_test(action_items, actions);
}

//-------------------------------------------------------------------------------------

void test_alert() {
  puts("test_alert");
  const km_core_action_item action_items[] = {
    alert_action_item(),
    end_action_item()
  };

  const km_core_actions actions = {
    0, // unsigned int code_points_to_delete;
    test_empty_output, // km_core_usv* output;
    test_env_opts, // km_core_option_item* persist_options;
    KM_CORE_TRUE, // km_core_bool do_alert;
    KM_CORE_FALSE, // km_core_bool emit_keystroke;
    KM_CORE_CAPS_UNCHANGED, // new_caps_lock_state;
    nullptr // km_core_usv* deleted_context;
  };

  run_test(action_items, actions);
}

//-------------------------------------------------------------------------------------

void test_emit_keystroke() {
  puts("test_emit_keystroke");
  const km_core_action_item action_items[] = {
    emit_keystroke_action_item(),
    end_action_item()
  };

  const km_core_actions actions = {
    0, // unsigned int code_points_to_delete;
    test_empty_output, // km_core_usv* output;
    test_env_opts, // km_core_option_item* persist_options;
    KM_CORE_FALSE, // km_core_bool do_alert;
    KM_CORE_TRUE, // km_core_bool emit_keystroke;
    KM_CORE_CAPS_UNCHANGED, // new_caps_lock_state;
    nullptr // km_core_usv* deleted_context;
  };

  run_test(action_items, actions);
}

//-------------------------------------------------------------------------------------

void test_invalidate_context() {
  puts("test_invalidate_context");
  // note, this generates a no-op, DELETE?
  const km_core_action_item action_items[] = {
    // invalidate_context_action_item(),
    end_action_item()
  };

  const km_core_actions actions = {
    0, // unsigned int code_points_to_delete;
    test_empty_output, // km_core_usv* output;
    test_env_opts, // km_core_option_item* persist_options;
    KM_CORE_FALSE, // km_core_bool do_alert;
    KM_CORE_FALSE, // km_core_bool emit_keystroke;
    KM_CORE_CAPS_UNCHANGED, // new_caps_lock_state;
    nullptr // km_core_usv* deleted_context;
  };

  run_test(action_items, actions);
}

//-------------------------------------------------------------------------------------

void test_persist_opt() {
  puts("test_persist_opt");
  const km_core_option_item option = {
    u"key",
    u"value",
    KM_CORE_OPT_KEYBOARD
  };

  /*TODO: const*/ km_core_option_item options[] = {{
    u"key",
    u"value",
    KM_CORE_OPT_KEYBOARD
  },
    KM_CORE_OPTIONS_END
  };

  const km_core_action_item action_items[] = {
    persist_opt_action_item(&option),
    end_action_item()
  };

  const km_core_actions actions = {
    0, // unsigned int code_points_to_delete;
    test_empty_output, // km_core_usv* output;
    options, // km_core_option_item* persist_options;
    KM_CORE_FALSE, // km_core_bool do_alert;
    KM_CORE_FALSE, // km_core_bool emit_keystroke;
    KM_CORE_CAPS_UNCHANGED, // new_caps_lock_state;
    nullptr // km_core_usv* deleted_context;
  };

  run_test(action_items, actions);
}

//-------------------------------------------------------------------------------------

void test_caps_lock() {
  puts("test_caps_lock");

  const km_core_action_item action_items[] = {
    caps_action_item(KM_CORE_CAPS_ON),
    // invalidate_context_action_item(),
    end_action_item()
  };

  const km_core_actions actions = {
    0, // unsigned int code_points_to_delete;
    test_empty_output, // km_core_usv* output;
    test_env_opts, // km_core_option_item* persist_options;
    KM_CORE_FALSE, // km_core_bool do_alert;
    KM_CORE_FALSE, // km_core_bool emit_keystroke;
    KM_CORE_CAPS_ON, // new_caps_lock_state;
    nullptr // km_core_usv* deleted_context;
  };

  run_test(action_items, actions);
}

//-------------------------------------------------------------------------------------
// Launcher
//-------------------------------------------------------------------------------------

constexpr const auto help_str = "\
action_set_api [--color] <SOURCE_PATH>\n\
\n\
  --color         Force color output\n\
  SOURCE_PATH     Path where action_set_api.cpp is found; kmx files are\n\
                  located relative to this path.\n";

int error_args() {
  std::cerr << "action_set_api: Invalid arguments." << std::endl;
  std::cout << help_str;
  return 1;
}

int main(int argc, char *argv []) {

  if(argc < 2) {
    return error_args();
  }

  auto arg_color = std::string(argv[1]) == "--color";
  if(arg_color && argc < 3) {
    return error_args();
  }
  console_color::enabled = console_color::isaterminal() || arg_color;

#ifdef __EMSCRIPTEN__
  arg_path = get_wasm_file_path(argv[arg_color ? 2 : 1]);
#else
  arg_path = argv[arg_color ? 2 : 1];
#endif

  // actions
  test_two_backspaces();
  test_character();
  test_alert();
  test_emit_keystroke();
  test_invalidate_context();
  test_persist_opt();
  test_caps_lock();
}

//-------------------------------------------------------------------------------------
// Helper functions
//-------------------------------------------------------------------------------------

const km_core_action_item alert_action_item() {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_ALERT;
  return res;
}

const km_core_action_item bksp_action_item(uint8_t type, uintptr_t value) {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_BACK;
  res.backspace.expected_type = type;
  res.backspace.expected_value = value;
  return res;
}

const km_core_action_item caps_action_item(uint8_t capsLock) {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_CAPSLOCK;
  res.capsLock = capsLock;
  return res;
}

const km_core_action_item char_action_item(km_core_usv chr) {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_CHAR;
  res.character = chr;
  return res;
}

const km_core_action_item emit_keystroke_action_item() {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_EMIT_KEYSTROKE;
  return res;
}

const km_core_action_item persist_opt_action_item(km_core_option_item const *option) {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_PERSIST_OPT;
  res.option = option;
  return res;
}

const km_core_action_item end_action_item() {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_END;
  return res;
}

const km_core_action_item invalidate_context_action_item() {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_INVALIDATE_CONTEXT;
  return res;
}

const km_core_action_item marker_action_item(uint32_t marker) {
  km_core_action_item res = {0};
  res.type = KM_CORE_IT_MARKER;
  res.character = marker;
  return res;
}