HTTP API: Allow binary downloads and headers (#8573)
[oweals/minetest.git] / src / unittest / test_keycode.cpp
1 /*
2 Minetest
3 Copyright (C) 2016 sfan5 <sfan5@live.de>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21
22 #include <string>
23 #include "exceptions.h"
24 #include "client/keycode.h"
25
26 class TestKeycode : public TestBase {
27 public:
28         TestKeycode() { TestManager::registerTestModule(this); }
29         const char *getName() { return "TestKeycode"; }
30
31         void runTests(IGameDef *gamedef);
32
33         void testCreateFromString();
34         void testCreateFromSKeyInput();
35         void testCompare();
36 };
37
38 static TestKeycode g_test_instance;
39
40 void TestKeycode::runTests(IGameDef *gamedef)
41 {
42         TEST(testCreateFromString);
43         TEST(testCreateFromSKeyInput);
44         TEST(testCompare);
45 }
46
47 ////////////////////////////////////////////////////////////////////////////////
48
49 #define UASSERTEQ_STR(one, two) UASSERT(strcmp(one, two) == 0)
50
51 void TestKeycode::testCreateFromString()
52 {
53         KeyPress k;
54
55         // Character key, from char
56         k = KeyPress("R");
57         UASSERTEQ_STR(k.sym(), "KEY_KEY_R");
58         UASSERTCMP(int, >, strlen(k.name()), 0); // should have human description
59
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);
64
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);
69
70         k = KeyPress("KEY_F6");
71         UASSERTEQ_STR(k.sym(), "KEY_F6");
72         UASSERTCMP(int, >, strlen(k.name()), 0);
73
74         // Irrlicht-unknown key, from char
75         k = KeyPress("/");
76         UASSERTEQ_STR(k.sym(), "/");
77         UASSERTCMP(int, >, strlen(k.name()), 0);
78 }
79
80 void TestKeycode::testCreateFromSKeyInput()
81 {
82         KeyPress k;
83         irr::SEvent::SKeyInput in;
84
85         // Character key
86         in.Key = irr::KEY_KEY_3;
87         in.Char = L'3';
88         k = KeyPress(in);
89         UASSERTEQ_STR(k.sym(), "KEY_KEY_3");
90
91         // Non-Character key
92         in.Key = irr::KEY_RSHIFT;
93         in.Char = L'\0';
94         k = KeyPress(in);
95         UASSERTEQ_STR(k.sym(), "KEY_RSHIFT");
96
97         // Irrlicht-unknown key
98         in.Key = irr::KEY_KEY_CODES_COUNT;
99         in.Char = L'?';
100         k = KeyPress(in);
101         UASSERTEQ_STR(k.sym(), "?");
102
103         // prefer_character mode
104         in.Key = irr::KEY_COMMA;
105         in.Char = L'G';
106         k = KeyPress(in, true);
107         UASSERTEQ_STR(k.sym(), "KEY_KEY_G");
108 }
109
110 void TestKeycode::testCompare()
111 {
112         // Basic comparison
113         UASSERT(KeyPress("5") == KeyPress("KEY_KEY_5"));
114         UASSERT(!(KeyPress("5") == KeyPress("KEY_NUMPAD_5")));
115
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;
120         in.Char = L'=';
121         UASSERT(KeyPress("=") == KeyPress(in));
122
123         // Matching keycode suffices
124         irr::SEvent::SKeyInput in2;
125         in.Key = in2.Key = irr::KEY_OEM_CLEAR;
126         in.Char = L'\0';
127         in2.Char = L';';
128         UASSERT(KeyPress(in) == KeyPress(in2));
129 }