File: state_context_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 (564 lines) | stat: -rw-r--r-- 25,032 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// Tests for the state_context_* API methods

#include "keyman_core.h"
#include <string>

#include "context.hpp"
#include "path.hpp"
#include "mock/mock_processor.hpp"

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

//-------------------------------------------------------------------------------------
// Context tests
//-------------------------------------------------------------------------------------

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;
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, bool setup_app_context = true) {
  teardown();

  km::core::path path = km::core::path::join(arg_path, keyboard);
  auto blob = km::tests::load_kmx_file(path.native().c_str());
  const auto mock_extension = ".mock";
  if (strlen(keyboard) > strlen(mock_extension) && strcmp(keyboard + strlen(keyboard) - strlen(mock_extension), mock_extension) == 0) {
    km::core::abstract_processor* kp = new km::core::mock_processor(keyboard);
    test_kb = static_cast<km_core_keyboard*>(kp);
  } else {
    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));
  if (setup_app_context) {
    try_status(km_core_context_set(km_core_state_app_context(test_state), citems));
  }
}

bool
is_identical_context(km_core_cu const *cached_context) {
  size_t buf_size;
  try_status(km_core_context_get(km_core_state_context(test_state), &citems));
  try_status(context_items_to_utf16(citems, nullptr, &buf_size));
  km_core_cu *new_cached_context = new km_core_cu[buf_size];
  try_status(context_items_to_utf16(citems, new_cached_context, &buf_size));
  bool result = std::u16string(cached_context) == new_cached_context;
  delete[] new_cached_context;
  return result;
}

#define assert_equal_msg(actual, expected, msg) { \
  if ((actual) != (expected)) { \
    std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
             << "Test failed at " << __FILE__ << ":" << __LINE__ << ":" \
             << console_color::reset() \
             << std::endl \
             << (msg) << std::endl \
             << "expected: " << (expected) << std::endl \
             << "actual:   " << (actual) << std::endl; \
    std::exit(EXIT_FAILURE); \
  } \
}

#define assert_identical_context_with_markers(context, citems)                                     \
  {                                                                                                \
    km_core_context_item *citems_new;                                                              \
    try_status(km_core_context_get((context), &citems_new));                                       \
    for (int i = 0; (citems)[i].type || citems_new[i].type; i++) {                                 \
      assert_equal_msg(citems_new[i].type, (citems)[i].type, "Unexpected type:");                  \
      if ((citems)[i].type == KM_CORE_CT_CHAR) {                                                   \
        assert_equal_msg(citems_new[i].character, (citems)[i].character, "Unexpected character:"); \
      } else {                                                                                     \
        assert_equal_msg(citems_new[i].marker, (citems)[i].marker, "Unexpected marker:");          \
      }                                                                                            \
    }                                                                                              \
    km_core_context_items_dispose(citems_new);                                                     \
  }

// citems contains markers, but we will skip over them
#define assert_identical_context_without_markers(context, citems)                                  \
  {                                                                                                \
    km_core_context_item *citems_new;                                                              \
    try_status(km_core_context_get((context), &citems_new));                                       \
    for (int i = 0, i_new = 0; (citems)[i].type || citems_new[i_new].type; i++) {                  \
      if ((citems)[i].type == KM_CORE_CT_CHAR) {                                                   \
        assert_equal_msg(citems_new[i_new].type, (citems)[i].type, "Unexpected type:");            \
        assert_equal_msg(citems_new[i_new].character, (citems)[i].character, "Unexpected character:"); \
        i_new++;                                                                                   \
      } else if((citems)[i].type == KM_CORE_CT_END ) {                                             \
        assert_equal_msg(citems_new[i_new].type, (citems)[i].type, "Unexpected type:");            \
      }                                                                                            \
    }                                                                                              \
    km_core_context_items_dispose(citems_new);                                                     \
  }

#define assert_equal_status(actual, expected) { \
  if ((actual) != (expected)) { \
    std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
             << "Test failed at " << __FILE__ << ":" << __LINE__ << ":" \
             << console_color::reset() \
             << std::endl \
             << "expected: " << #expected << std::endl \
             << "actual:   " << (actual) << std::endl; \
    std::exit(EXIT_FAILURE); \
  } \
}


void
test_context_set_if_needed__identical_context() {
  km_core_cu const *cached_context  = u"This is a test";
  km_core_cu const *new_app_context = u"This is a test";
  setup("k_000___null_keyboard.kmx", cached_context, false);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UNCHANGED);
  test_assert(is_identical_context(cached_context));
  teardown();
}

void
test_context_set_if_needed__different_context() {
  km_core_cu const *cached_context  = u"This isn't a test";
  km_core_cu const *new_app_context = u"This is a    test";
  setup("k_000___null_keyboard.kmx", cached_context);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  test_assert(!is_identical_context(cached_context));
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__cached_context_cleared() {
  km_core_cu const *cached_context  = u"";
  km_core_cu const *new_app_context = u"This is a test";
  setup("k_000___null_keyboard.kmx", cached_context);
  km_core_state_context_clear(test_state);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  test_assert(!is_identical_context(cached_context));
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__application_context_empty() {
  km_core_cu const *cached_context  = u"This is a test";
  km_core_cu const *new_app_context = u"";
  setup("k_000___null_keyboard.kmx", cached_context);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  test_assert(!is_identical_context(cached_context));
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__app_context_is_longer() {
  km_core_cu const *cached_context  = u"This is a test";
  km_core_cu const *new_app_context = u"Longer This is a test";
  setup("k_000___null_keyboard.kmx", cached_context);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  test_assert(!is_identical_context(cached_context));
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__app_context_is_shorter() {
  km_core_cu const *cached_context  = u"This is a test";
  km_core_cu const *new_app_context = u"is a test";
  setup("k_000___null_keyboard.kmx", cached_context);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  test_assert(!is_identical_context(cached_context));
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__identical_context_and_markers() {
  km_core_cu const *cached_context  = u"123";
  km_core_cu const *new_app_context = u"123";
  setup("k_000___null_keyboard.kmx", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_MARKER, {0}, {5}}, {KM_CORE_CT_CHAR, {0}, {'1'}}, {KM_CORE_CT_MARKER, {0}, {1}},
      {KM_CORE_CT_CHAR, {0}, {'2'}}, {KM_CORE_CT_MARKER, {0}, {2}}, {KM_CORE_CT_CHAR, {0}, {'3'}},
      {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}}, KM_CORE_CONTEXT_ITEM_END};

  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UNCHANGED);

  assert_identical_context_with_markers(km_core_state_context(test_state), citems);
  assert_identical_context_without_markers(km_core_state_app_context(test_state), citems);

  teardown();
}

void
test_context_set_if_needed__cached_context_shorter_and_markers() {
  km_core_cu const *cached_context  = u"123";
  km_core_cu const *new_app_context = u"0123";
  setup("k_000___null_keyboard.kmx", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_MARKER, {0}, {5}}, {KM_CORE_CT_CHAR, {0}, {'1'}}, {KM_CORE_CT_MARKER, {0}, {1}},
      {KM_CORE_CT_CHAR, {0}, {'2'}}, {KM_CORE_CT_MARKER, {0}, {2}}, {KM_CORE_CT_CHAR, {0}, {'3'}},
      {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}}, KM_CORE_CONTEXT_ITEM_END};
  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);

  km_core_context_item const expected_citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'0'}},
      {KM_CORE_CT_MARKER, {0}, {5}}, {KM_CORE_CT_CHAR, {0}, {'1'}}, {KM_CORE_CT_MARKER, {0}, {1}},
      {KM_CORE_CT_CHAR, {0}, {'2'}}, {KM_CORE_CT_MARKER, {0}, {2}}, {KM_CORE_CT_CHAR, {0}, {'3'}},
      {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}}, KM_CORE_CONTEXT_ITEM_END};
  assert_identical_context_with_markers(km_core_state_context(test_state), expected_citems);
  assert_identical_context_without_markers(km_core_state_app_context(test_state), expected_citems);
  test_assert(is_identical_context(new_app_context));

  teardown();
}

void
test_context_set_if_needed__cached_context_shorter_and_markers_nfu() {
  km_core_cu const *cached_context  = u"bce\u0323\u0302";
  km_core_cu const *new_app_context = u"abcệ";
  setup("/a/dummy/keyboard.mock", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_MARKER, {0}, {1}}, {KM_CORE_CT_CHAR, {0}, {'b'}}, {KM_CORE_CT_MARKER, {0}, {2}},
      {KM_CORE_CT_CHAR, {0}, {'c'}}, {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}},
      {KM_CORE_CT_CHAR, {0}, {'e'}}, {KM_CORE_CT_CHAR, {0}, {u'\u0323'}},
      {KM_CORE_CT_CHAR, {0}, {u'\u0302'}}, KM_CORE_CONTEXT_ITEM_END};
  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);

  km_core_context_item const expected_citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_MARKER, {0}, {1}}, {KM_CORE_CT_CHAR, {0}, {'b'}}, {KM_CORE_CT_MARKER, {0}, {2}},
      {KM_CORE_CT_CHAR, {0}, {'c'}}, {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}},
      {KM_CORE_CT_CHAR, {0}, {'e'}}, {KM_CORE_CT_CHAR, {0}, {u'\u0323'}},
      {KM_CORE_CT_CHAR, {0}, {u'\u0302'}}, KM_CORE_CONTEXT_ITEM_END};
  assert_identical_context_with_markers(km_core_state_context(test_state), expected_citems);
  km_core_context_item const expected_app_citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_CHAR, {0}, {'b'}},
      {KM_CORE_CT_CHAR, {0}, {'c'}},
      {KM_CORE_CT_CHAR, {0}, {u'ệ'}},
      KM_CORE_CONTEXT_ITEM_END};
  assert_identical_context_without_markers(km_core_state_app_context(test_state), expected_app_citems);

  teardown();
}

void
test_context_set_if_needed__cached_context_longer_and_markers() {
  km_core_cu const *cached_context  = u"0123";
  km_core_cu const *new_app_context = u"123";
  setup("k_000___null_keyboard.kmx", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'0'}},
      {KM_CORE_CT_MARKER, {0}, {5}}, {KM_CORE_CT_CHAR, {0}, {'1'}}, {KM_CORE_CT_MARKER, {0}, {1}},
      {KM_CORE_CT_CHAR, {0}, {'2'}}, {KM_CORE_CT_MARKER, {0}, {2}}, {KM_CORE_CT_CHAR, {0}, {'3'}},
      {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}}, KM_CORE_CONTEXT_ITEM_END};
  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);

  km_core_context_item const expected_citems[] = {
      {KM_CORE_CT_MARKER, {0}, {5}}, {KM_CORE_CT_CHAR, {0}, {'1'}}, {KM_CORE_CT_MARKER, {0}, {1}},
      {KM_CORE_CT_CHAR, {0}, {'2'}}, {KM_CORE_CT_MARKER, {0}, {2}}, {KM_CORE_CT_CHAR, {0}, {'3'}},
      {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}}, KM_CORE_CONTEXT_ITEM_END};
  assert_identical_context_with_markers(km_core_state_context(test_state), expected_citems);
  assert_identical_context_without_markers(km_core_state_app_context(test_state), expected_citems);

  teardown();
}

void
test_context_set_if_needed__cached_context_longer_and_markers_nfu() {
  km_core_cu const *cached_context  = u"abce\u0323\u0302";
  km_core_cu const *new_app_context = u"bcệ";
  setup("/a/dummy/keyboard.mock", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_MARKER, {0}, {1}}, {KM_CORE_CT_CHAR, {0}, {'b'}}, {KM_CORE_CT_MARKER, {0}, {2}},
      {KM_CORE_CT_CHAR, {0}, {'c'}}, {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}},
      {KM_CORE_CT_CHAR, {0}, {'e'}}, {KM_CORE_CT_CHAR, {0}, {u'\u0323'}},
      {KM_CORE_CT_CHAR, {0}, {u'\u0302'}}, KM_CORE_CONTEXT_ITEM_END};
  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);

  km_core_context_item const expected_citems[] = {
      {KM_CORE_CT_MARKER, {0}, {1}}, {KM_CORE_CT_CHAR, {0}, {'b'}}, {KM_CORE_CT_MARKER, {0}, {2}},
      {KM_CORE_CT_CHAR, {0}, {'c'}}, {KM_CORE_CT_MARKER, {0}, {3}}, {KM_CORE_CT_MARKER, {0}, {4}},
      {KM_CORE_CT_CHAR, {0}, {'e'}}, {KM_CORE_CT_CHAR, {0}, {u'\u0323'}},
      {KM_CORE_CT_CHAR, {0}, {u'\u0302'}}, KM_CORE_CONTEXT_ITEM_END};
  assert_identical_context_with_markers(km_core_state_context(test_state), expected_citems);

  km_core_context_item const expected_app_citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'b'}},
      {KM_CORE_CT_CHAR, {0}, {'c'}},
      {KM_CORE_CT_CHAR, {0}, {u'ệ'}},
      KM_CORE_CONTEXT_ITEM_END};
  assert_identical_context_without_markers(km_core_state_app_context(test_state), expected_app_citems);

  teardown();
}

void
test_context_set_if_needed__surrogate_pairs_unchanged() {
  km_core_cu const *cached_context  = u"a\U00010100";
  km_core_cu const *new_app_context = u"a\U00010100";
  setup("k_000___null_keyboard.kmx", cached_context);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UNCHANGED);
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__surrogate_pairs_app_context_longer() {
  km_core_cu const *cached_context  = u"a\U00010100";
  km_core_cu const *new_app_context = u"xa\U00010100";
  setup("k_000___null_keyboard.kmx", cached_context);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__surrogate_pairs_cached_context_longer() {
  km_core_cu const *cached_context  = u"xa\U00010100";
  km_core_cu const *new_app_context = u"a\U00010100";
  setup("k_000___null_keyboard.kmx", cached_context);
  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  test_assert(is_identical_context(new_app_context));
  teardown();
}

void
test_context_set_if_needed__surrogate_pairs_unchanged_and_markers() {
  km_core_cu const *cached_context  = u"a\U00010100";
  km_core_cu const *new_app_context = u"a\U00010100";
  setup("k_000___null_keyboard.kmx", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_MARKER, {0}, {5}},
      {KM_CORE_CT_CHAR, {0}, {0x10100}},
      KM_CORE_CONTEXT_ITEM_END};
  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UNCHANGED);
  assert_identical_context_with_markers(km_core_state_context(test_state), citems);
  assert_identical_context_without_markers(km_core_state_app_context(test_state), citems);

  teardown();
}

void
test_context_set_if_needed__surrogate_pairs_app_context_longer_and_markers() {
  km_core_cu const *cached_context  = u"a\U00010100";
  km_core_cu const *new_app_context = u"\U00010200a\U00010100";
  setup("k_000___null_keyboard.kmx", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_MARKER, {0}, {5}},
      {KM_CORE_CT_CHAR, {0}, {0x10100}},
      KM_CORE_CONTEXT_ITEM_END};
  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  km_core_context_item const expected_citems[] = {
      {KM_CORE_CT_CHAR, {0}, {0x10200}},
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_MARKER, {0}, {5}},
      {KM_CORE_CT_CHAR, {0}, {0x10100}},
      KM_CORE_CONTEXT_ITEM_END};

  assert_identical_context_with_markers(km_core_state_context(test_state), expected_citems);
  assert_identical_context_without_markers(km_core_state_app_context(test_state), expected_citems);

  teardown();
}

void
test_context_set_if_needed__surrogate_pairs_cached_context_longer_and_markers() {
  km_core_cu const *cached_context  = u"\U00010200a\U00010100";
  km_core_cu const *new_app_context = u"a\U00010100";
  setup("k_000___null_keyboard.kmx", cached_context);

  km_core_context_item const citems[] = {
      {KM_CORE_CT_CHAR, {0}, {0x10200}},
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_MARKER, {0}, {5}},
      {KM_CORE_CT_CHAR, {0}, {0x10100}},
      KM_CORE_CONTEXT_ITEM_END};
  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  assert_equal_status(km_core_state_context_set_if_needed(test_state, new_app_context), KM_CORE_CONTEXT_STATUS_UPDATED);
  km_core_context_item const expected_citems[] = {
      {KM_CORE_CT_CHAR, {0}, {'a'}},
      {KM_CORE_CT_MARKER, {0}, {5}},
      {KM_CORE_CT_CHAR, {0}, {0x10100}},
      KM_CORE_CONTEXT_ITEM_END};

  assert_identical_context_with_markers(km_core_state_context(test_state), expected_citems);
  assert_identical_context_without_markers(km_core_state_app_context(test_state), expected_citems);

  teardown();
}

void
test_context_set_if_needed() {
  // Scenarios from #10100:
  // 1. cached context identical to app context
  test_context_set_if_needed__identical_context();
  // 1a. cached context has markers and is identical to app context
  test_context_set_if_needed__identical_context_and_markers();
  // 2. cached context same length as app context but content is different
  test_context_set_if_needed__different_context();
  // 3. cached context is shorter than app context, but content is same as far as it goes
  test_context_set_if_needed__app_context_is_longer();
  // 3a. cached context has markers and is shorter than app context,
  // but content is same as far as it goes
  test_context_set_if_needed__cached_context_shorter_and_markers();
  test_context_set_if_needed__cached_context_shorter_and_markers_nfu();
  // 4. cached context is shorter than app context, but content is different
  test_context_set_if_needed__cached_context_cleared();
  // 5. cached context is longer than app context, but content is same as far as it goes
  test_context_set_if_needed__app_context_is_shorter();
  // 5a. cached context has markers and is longer than app context, but
  // content is same as far as it goes
  test_context_set_if_needed__cached_context_longer_and_markers();
  test_context_set_if_needed__cached_context_longer_and_markers_nfu();
  // 6. cached context is longer than app context, but content is different.
  test_context_set_if_needed__application_context_empty();
  // 7. surrogate pairs in context
  test_context_set_if_needed__surrogate_pairs_unchanged();
  test_context_set_if_needed__surrogate_pairs_unchanged_and_markers();
  test_context_set_if_needed__surrogate_pairs_app_context_longer();
  test_context_set_if_needed__surrogate_pairs_cached_context_longer();
  test_context_set_if_needed__surrogate_pairs_app_context_longer_and_markers();
  test_context_set_if_needed__surrogate_pairs_cached_context_longer_and_markers();
}

void
test_context_clear() {
  km_core_cu const *cached_context = u"This is a test";
  setup("k_000___null_keyboard.kmx", cached_context);
  try_status(km_core_state_context_clear(test_state));
  test_assert(!is_identical_context(cached_context));
  test_assert(is_identical_context(u""));
  teardown();
}

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

void test_context_debug_empty() {
  km_core_cu const *cached_context =      u"";
  setup("k_000___null_keyboard.kmx", cached_context);
  auto str = km_core_state_context_debug(test_state, KM_CORE_DEBUG_CONTEXT_CACHED);
  // std::cout << str << std::endl;
  test_assert(std::u16string(str) == u"|| (len: 0) [ ]");
  km_core_cu_dispose(str);
}

void test_context_debug_various() {
  km_core_cu const *cached_context =      u"123\U0001F923";
  setup("k_000___null_keyboard.kmx", cached_context);

  km_core_context_item const citems[] = {
    { KM_CORE_CT_MARKER, {0}, { 5 } },
    { KM_CORE_CT_CHAR, {0}, { '1' } },
    { KM_CORE_CT_MARKER, {0}, { 1 } },
    { KM_CORE_CT_CHAR, {0}, { '2' } },
    { KM_CORE_CT_MARKER, {0}, { 2 } },
    { KM_CORE_CT_CHAR, {0}, { '3' } },
    { KM_CORE_CT_MARKER, {0}, { 3 } },
    { KM_CORE_CT_MARKER, {0}, { 4 } },
    { KM_CORE_CT_CHAR, {0}, { 0x1F923 /* 🤣 */ } },
    KM_CORE_CONTEXT_ITEM_END
  };

  try_status(km_core_context_set(km_core_state_context(test_state), citems));

  auto str = km_core_state_context_debug(test_state, KM_CORE_DEBUG_CONTEXT_CACHED);
  // std::cout << str << std::endl;
  test_assert(std::u16string(str) == u"|123🤣| (len: 9) [ M(5) U+0031 M(1) U+0032 M(2) U+0033 M(3) M(4) U+1f923 ]");
  km_core_cu_dispose(str);
}

void test_context_debug() {
  test_context_debug_empty();
  test_context_debug_various();
}

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

constexpr const auto help_str =
    "\
state_context [--color] <SOURCE_PATH>\n\
\n\
  --color         Force color output\n\
  SOURCE_PATH     Path of kmx folder that contains the kmx files\n";

int
error_args() {
  std::cerr << "debug_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

  test_context_set_if_needed();
  test_context_clear();
  test_context_debug();
}