Debian Codegraph @

src/tests/player_test.c

raw symbols SCIP
1 #define SDL_MAIN_HANDLED
2 #include <cbehave/cbehave.h>
3
4 #include <player.h>
5
6 #include <SDL_joystick.h>
7
8 #include <actors.h>
9 #include <game_mode.h>
10 #include <net_client.h>
11 #include <player_template.h>
12 #include <utils.h>
13
14
15 FEATURE(assign_unused, "Assign unused input device")
16 // This feature is used to assign input devices to players, before the game
17 // begins. All input devices can emit the "fire" command, at which point
18 // those input devices are assigned to the next unassigned player. This
19 // lets us easily assign different input devices to different players,
20 // regardless of the number and type of input devices and players.
21 PlayerDataInit(&gPlayerDatas);
22 gConfig = ConfigDefault();
23 NPlayerData pd = PlayerDataDefault(0);
24 PlayerDataAddOrUpdate(pd);
25 pd.UID = 1;
26 PlayerDataAddOrUpdate(pd);
27 SCENARIO("Assign device to unset player")
28 GIVEN("a player with an unset input device")
29 PlayerData *p = CArrayGet(&gPlayerDatas, 0);
30 p->inputDevice = INPUT_DEVICE_UNSET;
31 WHEN("I assign it with an input device")
32 const input_device_e d = INPUT_DEVICE_KEYBOARD;
33 const int idx = 0;
34 bool res = PlayerTrySetUnusedInputDevice(p, d, idx);
35 THEN("the assignment should succeed")
36 SHOULD_BE_TRUE(res);
37 AND("the player should have the input device set")
38 SHOULD_INT_EQUAL((int)p->inputDevice, (int)d);
39 SHOULD_INT_EQUAL(p->deviceIndex, idx);
40 SCENARIO_END
41 SCENARIO("Assign device to set player")
42 GIVEN("a player with an input device")
43 PlayerData *p = CArrayGet(&gPlayerDatas, 0);
44 const input_device_e d = INPUT_DEVICE_KEYBOARD;
45 p->inputDevice = d;
46 const int idx = 0;
47 p->deviceIndex = idx;
48 WHEN("I assign it with a different input device")
49 bool res = PlayerTrySetUnusedInputDevice(p, INPUT_DEVICE_JOYSTICK, 0);
50 THEN("the assignment should fail")
51 SHOULD_BE_FALSE(res);
52 AND("the player's input device should be unchanged")
53 SHOULD_INT_EQUAL((int)p->inputDevice, (int)d);
54 SHOULD_INT_EQUAL(p->deviceIndex, idx);
55 SCENARIO_END
56 SCENARIO("Assign already used device")
57 GIVEN("a player with an unset input device")
58 PlayerData *p = CArrayGet(&gPlayerDatas, 0);
59 p->inputDevice = INPUT_DEVICE_UNSET;
60 AND("a player with a keyboard device")
61 PlayerData *p2 = CArrayGet(&gPlayerDatas, 1);
62 p2->inputDevice = INPUT_DEVICE_KEYBOARD;
63 p2->deviceIndex = 0;
64 WHEN("I assign the first player with the second's input device")
65 bool res = PlayerTrySetUnusedInputDevice(
66 p, p2->inputDevice, p2->deviceIndex);
67 THEN("the assignment should fail")
68 SHOULD_BE_FALSE(res);
69 AND("the player's input device should be unset")
70 SHOULD_INT_EQUAL((int)p->inputDevice, (int)INPUT_DEVICE_UNSET);
71 SCENARIO_END
72 PlayerDataTerminate(&gPlayerDatas);
73 FEATURE_END
74
75 CBEHAVE_RUN("Player features are:", TEST_FEATURE(assign_unused))