File: ldml_test_source.hpp

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 (223 lines) | stat: -rw-r--r-- 6,008 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
#ifndef __KMX_TEST_SOURCE_HPP__
#define __KMX_TEST_SOURCE_HPP__

#include "path.hpp"

#include <map>
#include <set>
#include <memory>

#include "kmx/kmx_plus.h"

#include <test_assert.h>
#include <test_color.h>

namespace km {
namespace tests {

struct key_event {
  km_core_virtual_key vk;
  uint16_t modifier_state;
  public:
    key_event() : vk(0), modifier_state(0) {
    }
    key_event(km_core_virtual_key k, uint16_t m) :vk(k), modifier_state(m) {
    }
    std::string dump() const;
  int compare(const key_event &other) const {
    if (vk < other.vk) return -1;
    if (vk > other.vk) return 1;
    if (modifier_state < other.modifier_state) return -1;
    if (modifier_state > other.modifier_state) return 1;
    return 0;
  }

  bool operator<(const key_event &other) const {
    return compare(other) < 0;
  }
  bool operator>(const key_event &other) const {
    return compare(other) > 0;
  }
  bool operator==(const key_event &other) const {
    return compare(other) == 0;
  }
  /** true if unset (null) key */
  bool empty() const {
    return vk == 0 && modifier_state == 0;
  }

};

enum ldml_action_type {
  /**
   * Done. no more actions
  */
  LDML_ACTION_DONE,
  /**
   * Skip this test
  */
  LDML_ACTION_SKIP,
  /**
   * key_event - a vkey
  */
  LDML_ACTION_KEY_EVENT,
  /**
   * string - emit this
  */
  LDML_ACTION_EMIT_STRING,
  /**
   * expected text
  */
  LDML_ACTION_CHECK_EXPECTED,
  /**
   * string - keylist to check
   */
  LDML_ACTION_CHECK_KEYLIST,
  // TODO-LDML: gestures, etc? Depends on touch.

  /**
   * fail test.  'string' has message.
  */
  LDML_ACTION_FAIL,
};

struct ldml_action {
  ldml_action_type type;
  key_event k;
  std::u16string string;

  /** mark failure as specified type */
  void formatType(const char *file, int line, ldml_action_type type, const std::u16string &msg);
  /** mark failure as specified type. msg2 is concatenated */
  void formatType(const char *file, int line, ldml_action_type type, const std::u16string &msg, const std::u16string &msg2);
  /** mark failure as specified type. msg2 is concatenated */
  void formatType(const char *file, int line, ldml_action_type type, const std::u16string &msg, long msg2);
  /** mark failure as specified type.  msg2 is concatenated */
  void formatType(const char *file, int line, ldml_action_type type, const std::u16string &msg, const std::string &msg2);

  /** @returns true if caller should stop processing events */
  bool done() const;
};

/**
 * pure virtual representing a test source, or a specific subtest
 */
class LdmlTestSource {
public:
  LdmlTestSource();
  virtual ~LdmlTestSource();
  virtual void next_action(ldml_action &fillin) = 0;
  virtual int caps_lock_state();
  virtual void toggle_caps_lock_state();
  virtual void set_caps_lock_on(bool caps_lock_on);
  virtual km_core_status get_expected_load_status();
  virtual const std::u16string &get_context() = 0;
  virtual bool get_expected_beep() const;
  /**
   * fillin a list
   * @param fillin key list to be filled in with all vkeys in the hardware map.
   * @param modifier modifier to load key list for
   * @returns false on load fail, true on OK
   */
  bool get_vkey_table(std::set<key_event> &fillin) const;

  // helper functions
  static key_event char_to_event(char ch);
  static uint16_t get_modifier(std::string const &m);
  static std::u16string parse_source_string(std::string const &s);
  static std::u16string parse_u8_source_string(std::string const &s);

  // sets the normalization switch.
  // why is this here? to prevent an additional pass of parsing the KMX+ file.
  void set_normalization_disabled(bool is_disabled) {
    normalization_disabled = is_disabled;
    setup = true;
  }

  bool get_normalization_disabled() const {
    test_assert(setup); // make sure set_ was called first
    return normalization_disabled;
  }

private:
  bool normalization_disabled = false;
  bool setup = false;

private:
  bool _caps_lock_on = false;
protected:
  /** populate rawdata and kmxplus */
  int load_kmx_plus(const km::core::path &compiled);
  // copy of the kbd data, for lookups
  std::vector<uint8_t> rawdata;
  std::unique_ptr<km::core::kmx::kmx_plus> kmxplus;
};

typedef std::map<std::string, std::unique_ptr<km::tests::LdmlTestSource>> JsonTestMap;

class LdmlJsonTestSourceFactory {
  public:
    LdmlJsonTestSourceFactory();
    /**
     * @param compiled the KMX - for lookup
     * @param path the json
    */
    int load(const km::core::path &compiled, const km::core::path &path);

    static km::core::path kmx_to_test_json(const km::core::path& kmx);

    const JsonTestMap& get_tests() const;
  private:
    JsonTestMap test_map;
};


class LdmlEmbeddedTestSource : public LdmlTestSource {
public:
  LdmlEmbeddedTestSource();
  virtual ~LdmlEmbeddedTestSource();

  /**
   * Load the test_source from comments in the .xml source
   */
  int load_source(const km::core::path &path, const km::core::path &compiled);

  virtual km_core_status get_expected_load_status();
  virtual const std::u16string &get_context();
  virtual bool get_expected_beep() const;

  virtual void next_action(ldml_action &fillin);

private:
  key_event next_key();

  std::deque<std::string> keys;
  std::deque<std::u16string> expected;
  std::u16string context = u"";
  bool expected_beep = false;
  bool expected_error = false;
  bool is_done = false;
  /** did we check the keylist yet? */
  bool check_keylist = true;
  /** set the expected keys in the keylist */
  void set_keylist(std::string const& s) {
    expected_keylist = parse_source_string(s);
  }

  std::u16string expected_keylist;

  /** returns false on fail and updates the message */
  bool handle_check_keylist(std::string &message) const;

// utility
  static key_event vkey_to_event(std::string const &vk_event);
  static bool is_token(const std::string token, std::string &line);

public:
  static key_event parse_next_key(std::string &keys);
};

}  // namespace tests
}  // namespace km

#endif // __LDML_TEST_SOURCE_HPP__