3 Copyright (C) 2016 sfan5 <sfan5@live.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "exceptions.h"
26 class TestKeycode : public TestBase {
28 TestKeycode() { TestManager::registerTestModule(this); }
29 const char *getName() { return "TestKeycode"; }
31 void runTests(IGameDef *gamedef);
33 void testCreateFromString();
34 void testCreateFromSKeyInput();
38 static TestKeycode g_test_instance;
40 void TestKeycode::runTests(IGameDef *gamedef)
42 TEST(testCreateFromString);
43 TEST(testCreateFromSKeyInput);
47 ////////////////////////////////////////////////////////////////////////////////
49 #define UASSERTEQ_STR(one, two) UASSERT(strcmp(one, two) == 0)
51 void TestKeycode::testCreateFromString()
55 // Character key, from char
57 UASSERTEQ_STR(k.sym(), "KEY_KEY_R");
58 UASSERTCMP(int, >, strlen(k.name()), 0); // should have human description
60 // Character key, from identifier
61 k = KeyPress("KEY_KEY_B");
62 UASSERTEQ_STR(k.sym(), "KEY_KEY_B");
63 UASSERTCMP(int, >, strlen(k.name()), 0);
65 // Non-Character key, from identifier
66 k = KeyPress("KEY_UP");
67 UASSERTEQ_STR(k.sym(), "KEY_UP");
68 UASSERTCMP(int, >, strlen(k.name()), 0);
70 k = KeyPress("KEY_F6");
71 UASSERTEQ_STR(k.sym(), "KEY_F6");
72 UASSERTCMP(int, >, strlen(k.name()), 0);
74 // Irrlicht-unknown key, from char
76 UASSERTEQ_STR(k.sym(), "/");
77 UASSERTCMP(int, >, strlen(k.name()), 0);
80 void TestKeycode::testCreateFromSKeyInput()
83 irr::SEvent::SKeyInput in;
86 in.Key = irr::KEY_KEY_3;
89 UASSERTEQ_STR(k.sym(), "KEY_KEY_3");
92 in.Key = irr::KEY_RSHIFT;
95 UASSERTEQ_STR(k.sym(), "KEY_RSHIFT");
97 // Irrlicht-unknown key
98 in.Key = irr::KEY_KEY_CODES_COUNT;
101 UASSERTEQ_STR(k.sym(), "?");
103 // prefer_character mode
104 in.Key = irr::KEY_COMMA;
106 k = KeyPress(in, true);
107 UASSERTEQ_STR(k.sym(), "KEY_KEY_G");
110 void TestKeycode::testCompare()
113 UASSERT(KeyPress("5") == KeyPress("KEY_KEY_5"));
114 UASSERT(!(KeyPress("5") == KeyPress("KEY_NUMPAD_5")));
116 // Matching char suffices
117 // note: This is a real-world example, Irrlicht maps XK_equal to irr::KEY_PLUS on Linux
118 irr::SEvent::SKeyInput in;
119 in.Key = irr::KEY_PLUS;
121 UASSERT(KeyPress("=") == KeyPress(in));
123 // Matching keycode suffices
124 irr::SEvent::SKeyInput in2;
125 in.Key = in2.Key = irr::KEY_OEM_CLEAR;
128 UASSERT(KeyPress(in) == KeyPress(in2));