| 1 |
/** |
| 2 |
* Test suite for DinkC parser and bindings |
| 3 |
|
| 4 |
* Copyright (C) 2014, 2015 Sylvain Beucler |
| 5 |
|
| 6 |
* This file is part of GNU FreeDink |
| 7 |
|
| 8 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU General Public License as |
| 10 |
* published by the Free Software Foundation; either version 3 of the |
| 11 |
* License, or (at your option) any later version. |
| 12 |
|
| 13 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* General Public License for more details. |
| 17 |
|
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see |
| 20 |
* <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "dinkc.h" |
| 28 |
|
| 29 |
int get_parms(char proc_name[20], int script, char *str_params, int* spec); |
| 30 |
|
| 31 |
/* mocks / link seams */ |
| 32 |
#include "game_engine.h" |
| 33 |
int dversion = 107; |
| 34 |
struct player_info play; |
| 35 |
#include "live_sprites_manager.h" |
| 36 |
struct sp spr[MAX_SPRITES_AT_ONCE]; |
| 37 |
#include "game_choice.h" |
| 38 |
struct game_choice_struct game_choice; |
| 39 |
int last_sprite_created; |
| 40 |
int input_get_button_action(int button_index) { return -1; } |
| 41 |
void game_choice_start(int script, int nb_choices) {} |
| 42 |
void game_choice_clear() {} |
| 43 |
void kill_text_owned_by(int sprite) {} |
| 44 |
Uint32 game_GetTicks(void) {return 0; } |
| 45 |
|
| 46 |
|
| 47 |
class TestDinkC : public CxxTest::TestSuite { |
| 48 |
public: |
| 49 |
int script_id; |
| 50 |
void setUp() { |
| 51 |
dinkc_init(); |
| 52 |
script_id = ts_script_init("unit test", strdup("")); |
| 53 |
} |
| 54 |
void tearDown() { |
| 55 |
kill_script(script_id); |
| 56 |
dinkc_quit(); |
| 57 |
} |
| 58 |
|
| 59 |
void test_dinkc_getparms_bounds() { |
| 60 |
// memory bounds |
| 61 |
char* str_params = strdup("(\""); |
| 62 |
int spec[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 63 |
TS_ASSERT_EQUALS(get_parms("ignored", script_id, str_params, spec), 0); |
| 64 |
free(str_params); |
| 65 |
} |
| 66 |
void test_dinkc_getparms_int() { |
| 67 |
// Basic int test |
| 68 |
char str_params[] = "(21,22050, 0,0,0);"; |
| 69 |
int spec[] = { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }; |
| 70 |
TS_ASSERT_EQUALS(get_parms("ignored", script_id, str_params, spec), 1); |
| 71 |
} |
| 72 |
void test_dinkc_getparms_emptyint() { |
| 73 |
{ |
| 74 |
// [empty] is considered a valid int |
| 75 |
char str_params[] = "(,)"; |
| 76 |
int spec[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 77 |
TS_ASSERT(get_parms("ignored", script_id, str_params, spec)); |
| 78 |
} |
| 79 |
// e.g. it's OK to have empty arguments list when a single int is expected |
| 80 |
{ |
| 81 |
char str_params[] = "()"; |
| 82 |
int spec[] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 83 |
TS_ASSERT(get_parms("ignored", script_id, str_params, spec)); |
| 84 |
} |
| 85 |
// this doesn't apply to strings |
| 86 |
{ |
| 87 |
char str_params[] = "()"; |
| 88 |
int spec[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 89 |
TS_ASSERT(!get_parms("ignored", script_id, str_params, spec)); |
| 90 |
} |
| 91 |
// nor does this make parameters optional |
| 92 |
{ |
| 93 |
char str_params[] = "(1)"; |
| 94 |
int spec[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 95 |
TS_ASSERT(!get_parms("ignored", script_id, str_params, spec)); |
| 96 |
} |
| 97 |
// Good test |
| 98 |
{ |
| 99 |
char str_params[] = "(1,1)"; |
| 100 |
int spec[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 101 |
TS_ASSERT(get_parms("ignored", script_id, str_params, spec)); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void test_dinkc_getparms_parens() { |
| 106 |
// Opening paren is mandatory |
| 107 |
{ |
| 108 |
char str_params[] = "sp_dir[1,2)"; |
| 109 |
int spec[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 110 |
TS_ASSERT(!get_parms("ignored", script_id, str_params, spec)); |
| 111 |
} |
| 112 |
// Closing paren is mandatory |
| 113 |
{ |
| 114 |
char str_params[] = "(1,1"; |
| 115 |
int spec[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 116 |
TS_ASSERT(!get_parms("ignored", script_id, str_params, spec)); |
| 117 |
} |
| 118 |
{ |
| 119 |
char str_params[] = "(1,1,)"; |
| 120 |
int spec[] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 121 |
TS_ASSERT(!get_parms("ignored", script_id, str_params, spec)); |
| 122 |
} |
| 123 |
{ |
| 124 |
char str_params[] = "(1,1;"; |
| 125 |
int spec[] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 126 |
TS_ASSERT(!get_parms("ignored", script_id, str_params, spec)); |
| 127 |
} |
| 128 |
// Good test |
| 129 |
{ |
| 130 |
char str_params[] = "(1,\"a\")"; |
| 131 |
int spec[] = { 1, 2, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 132 |
TS_ASSERT(get_parms("ignored", script_id, str_params, spec)); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void lookup_var_ext() { |
| 137 |
TS_ASSERT(!lookup_var("&toto", DINKC_GLOBAL_SCOPE)); |
| 138 |
make_int("&toto", 1, DINKC_GLOBAL_SCOPE, script_id); |
| 139 |
TS_ASSERT(lookup_var("&toto", DINKC_GLOBAL_SCOPE)); |
| 140 |
make_int("&tata", 1, script_id, script_id); |
| 141 |
TS_ASSERT(!lookup_var("&tata", DINKC_GLOBAL_SCOPE)); |
| 142 |
TS_ASSERT(lookup_var("&tata", script_id)); |
| 143 |
} |
| 144 |
void test_dinkc_lookup_var_107() { |
| 145 |
dversion = 107; |
| 146 |
lookup_var_ext(); |
| 147 |
|
| 148 |
// v107 has no scope priority |
| 149 |
int var_id; |
| 150 |
make_int("&titi", 1, DINKC_GLOBAL_SCOPE, script_id); |
| 151 |
make_int("&titi", 2, script_id, script_id); |
| 152 |
TS_ASSERT_RELATION(std::greater<int>, var_id = lookup_var("&titi", script_id), 0); |
| 153 |
TS_ASSERT_EQUALS(play.var[var_id].var, 2); |
| 154 |
TS_ASSERT_RELATION(std::greater<int>, var_id = ts_lookup_var_local_global("&titi", script_id), 0); |
| 155 |
TS_ASSERT_EQUALS(play.var[var_id].var, 1); |
| 156 |
|
| 157 |
// needed for CK_FORK=no (woe) |
| 158 |
kill_all_vars(); |
| 159 |
} |
| 160 |
void test_dinkc_lookup_var_108() { |
| 161 |
dversion = 108; |
| 162 |
lookup_var_ext(); |
| 163 |
|
| 164 |
// v108 has scope priority |
| 165 |
int var_id; |
| 166 |
make_int("&titi", 1, DINKC_GLOBAL_SCOPE, script_id); |
| 167 |
make_int("&titi", 2, script_id, script_id); |
| 168 |
TS_ASSERT_RELATION(std::greater<int>, var_id = lookup_var("&titi", script_id), 0); |
| 169 |
TS_ASSERT_EQUALS(play.var[var_id].var, 2); |
| 170 |
TS_ASSERT_RELATION(std::greater<int>, var_id = ts_lookup_var_local_global("&titi", script_id), 0); |
| 171 |
TS_ASSERT_EQUALS(play.var[var_id].var, 2); |
| 172 |
|
| 173 |
// needed for CK_FORK=no (woe) |
| 174 |
kill_all_vars(); |
| 175 |
} |
| 176 |
|
| 177 |
void test_dinkc_dont_return_same_script_id_twice() { |
| 178 |
int script_id1 = ts_script_init("script1", strdup("")); |
| 179 |
int script_id2 = ts_script_init("script2", strdup("")); |
| 180 |
TS_ASSERT_DIFFERS(script_id1, script_id2); |
| 181 |
kill_script(script_id1); |
| 182 |
kill_script(script_id2); |
| 183 |
} |
| 184 |
}; |