openMSX
Keys_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2#include "SDLKey.hh"
3
4using namespace openmsx;
5
6static bool equal(const SDLKey& x, const SDLKey& y)
7{
8 if (x.sym.sym != y.sym.sym) return false;
9 if (x.sym.scancode != y.sym.scancode) return false;
10 if (x.sym.mod != y.sym.mod) return false;
11 if (x.sym.unused != y.sym.unused) return false;
12 if (x.down != y.down) return false;
13 return true;
14}
15
16static void test(const std::string& name1, SDLKey code1, bool canonical)
17{
18 std::string name2 = code1.toString();
19 if (canonical) {
20 CHECK(name2 == name1);
21 }
22
23 std::optional<SDLKey> code2 = SDLKey::fromString(name1);
24 CHECK(code2);
25 CHECK(equal(*code2, code1));
26
27 std::string name3 = code2->toString();
28 CHECK(name3 == name2);
29
30 std::optional<SDLKey> code3 = SDLKey::fromString(name2);
31 CHECK(code3);
32 CHECK(equal(*code3, *code2));
33}
34
35TEST_CASE("Keys")
36{
37 test("P", SDLKey::createDown(SDLK_p), true);
38 test("p", SDLKey::createDown(SDLK_p), false);
39 test("1", SDLKey::createDown(SDLK_1), true);
40 test("z", SDLKey::createDown(SDLK_z), false);
41 test("AT", SDLKey::createDown(SDLK_AT), false);
42 test("@", SDLKey::createDown(SDLK_AT), true);
43 test("Hash", SDLKey::createDown(SDLK_HASH), false);
44 test("slash", SDLKey::createDown(SDLK_SLASH), false);
45 test("EXCLAIM", SDLKey::createDown(SDLK_EXCLAIM), false);
46 test("!", SDLKey::createDown(SDLK_EXCLAIM), true);
47 test("PeRiOd", SDLKey::createDown(SDLK_PERIOD), false);
48 test("CARET", SDLKey::createDown(SDLK_CARET), false);
49 test("delete", SDLKey::createDown(SDLK_DELETE), false);
50 test("Keypad 7", SDLKey::createDown(SDLK_KP_7), true);
51 test("KP7", SDLKey::createDown(SDLK_KP_7), false);
52 test("KP_ENTER", SDLKey::createDown(SDLK_KP_ENTER), false);
53 test("up", SDLKey::createDown(SDLK_UP), false);
54 test("End", SDLKey::createDown(SDLK_END), false);
55 test("pageup", SDLKey::createDown(SDLK_PAGEUP), false);
56 test("PageDown", SDLKey::createDown(SDLK_PAGEDOWN), true);
57 test("PAGEDOWN", SDLKey::createDown(SDLK_PAGEDOWN), false);
58 test("F8", SDLKey::createDown(SDLK_F8), true);
59 test("RCTRL", SDLKey::createDown(SDLK_RCTRL), false);
60 test("Right Ctrl", SDLKey::createDown(SDLK_RCTRL), true);
61
62 test("P+SHIFT", SDLKey::create(SDLK_p, true, KMOD_SHIFT), true);
63 test("q,shift", SDLKey::create(SDLK_q, true, KMOD_SHIFT), false);
64 test("R/shift", SDLKey::create(SDLK_r, true, KMOD_SHIFT), false);
65 test("ctrl+c", SDLKey::create(SDLK_c, true, KMOD_CTRL), false);
66 test("ALT/E", SDLKey::create(SDLK_e, true, KMOD_ALT), false);
67 test("E+ALT", SDLKey::create(SDLK_e, true, KMOD_ALT), true);
68 test("5,release", SDLKey::create(SDLK_5, false), false);
69}
void test(const IterableBitSet< N > &s, std::initializer_list< size_t > list)
TEST_CASE("Keys")
Definition Keys_test.cc:35
CHECK(m3==m3)
This file implemented 3 utility functions:
Definition Autofire.cc:11
SDL_Keysym sym
Definition SDLKey.hh:19
static std::optional< SDLKey > fromString(std::string_view name)
Definition SDLKey.cc:119
static SDLKey createDown(SDL_Keycode code)
Definition SDLKey.hh:31
std::string toString() const
Definition SDLKey.cc:230
static SDLKey create(SDL_Keycode code, bool down, uint16_t mod=0)
Definition SDLKey.hh:22