Debian Codegraph @

src/main-test.c

raw symbols SCIP
1 /**
2 * \file main-test.c
3 * \brief Pseudo-UI for end-to-end testing.
4 *
5 * Copyright (c) 2011 Elly <[email protected]>
6 *
7 * This work is free software; you can redistribute it and/or modify it
8 * under the terms of either:
9 *
10 * a) the GNU General Public License as published by the Free Software
11 * Foundation, version 2, or
12 *
13 * b) the "Angband licence":
14 * This software may be copied and distributed for educational, research,
15 * and not for profit purposes provided that this copyright and statement
16 * are included in all such copies. Other copyrights may also apply.
17 */
18
19 #include "angband.h"
20 #include "buildid.h"
21 #include "main.h"
22 #include "player.h"
23 #include "player-birth.h"
24 #include "ui-game.h"
25
26 #ifdef USE_TEST
27
28 static int prompt = 0;
29 static int verbose = 0;
30 static int nextkey = 0;
31
32 static void c_key(char *rest) {
33 if (streq(rest, "left")) {
34 nextkey = ARROW_LEFT;
35 } else if (streq(rest, "right")) {
36 nextkey = ARROW_RIGHT;
37 } else if (streq(rest, "up")) {
38 nextkey = ARROW_UP;
39 } else if (streq(rest, "down")) {
40 nextkey = ARROW_DOWN;
41 } else if (streq(rest, "space")) {
42 nextkey = ' ';
43 } else if (streq(rest, "enter")) {
44 nextkey = '\n';
45 } else if (rest[0] == 'C' && rest[1] == '-') {
46 nextkey = KTRL(rest[2]);
47 } else {
48 nextkey = rest[0];
49 }
50 }
51
52 static void c_noop(char *rest) {
53
54 }
55
56 static void c_quit(char *rest) {
57 quit(NULL);
58 }
59
60 static void c_verbose(char *rest) {
61 if (rest && streq(rest, "0")) {
62 printf("cmd-verbose: off\n");
63 verbose = 0;
64 } else {
65 printf("cmd-verbose: on\n");
66 verbose = 1;
67 }
68 }
69
70 static void c_version(char *rest) {
71 printf("cmd-version: %s\n", buildid);
72 }
73
74 /**
75 * Player commands
76 */
77 static void c_player_birth(char *rest) {
78 const char *race = strtok(rest, " ");
79 const char *class = strtok(NULL, " ");
80 struct player_class *c;
81 struct player_race *r;
82
83 if (!race) race = "Human";
84 if (!class) class = "Warrior";
85
86 for (r = races; r; r = r->next)
87 if (streq(race, r->name))
88 break;
89 if (!r) {
90 printf("player-birth: bad race '%s'\n", race);
91 return;
92 }
93
94 for (c = classes; c; c = c->next)
95 if (streq(class, c->name))
96 break;
97
98 if (!c) {
99 printf("player-birth: bad class '%s'\n", class);
100 return;
101 }
102
103 player_generate(player, r, c, false);
104 }
105
106 static void c_player_class(char *rest) {
107 printf("player-class: %s\n", player->class->name);
108 }
109
110 static void c_player_race(char *rest) {
111 printf("player-race: %s\n", player->race->name);
112 }
113
114 typedef struct {
115 const char *name;
116 void (*func)(char *args);
117 } test_cmd;
118
119 static test_cmd cmds[] = {
120 { "#", c_noop },
121 { "key", c_key },
122 { "noop", c_noop },
123 { "quit", c_quit },
124 { "verbose", c_verbose },
125 { "version?", c_version },
126
127 { "player-birth", c_player_birth },
128 { "player-class?", c_player_class },
129 { "player-race?", c_player_race },
130
131 { NULL, NULL }
132 };
133
134 static errr test_docmd(void) {
135 char buf[1024];
136 char *cmd;
137 char *rest;
138 int i;
139
140 memset(buf, 0, sizeof(buf));
141
142 if (prompt) {
143 printf("test> ");
144 fflush(stdout);
145 }
146 if (!fgets(buf, sizeof(buf), stdin)) {
147 return -1;
148 }
149 if (strchr(buf, '\n')) {
150 *strchr(buf, '\n') = '\0';
151 }
152
153 if (verbose) printf("test-docmd: %s\n", buf);
154 cmd = strtok(buf, " ");
155 if (!cmd) return 0;
156 rest = strtok(NULL, "");
157
158 for (i = 0; cmds[i].name; i++) {
159 if (streq(cmds[i].name, cmd)) {
160 cmds[i].func(rest);
161 return 0;
162 }
163 }
164
165 return 0;
166 }
167
168 typedef struct term_data term_data;
169 struct term_data {
170 term t;
171 };
172
173 static term_data td;
174 typedef struct {
175 int key;
176 errr (*func)(int v);
177 } term_xtra_func;
178
179 static void term_init_test(term *t) {
180 if (verbose) printf("term-init %s\n", buildid);
181 }
182
183 static void term_nuke_test(term *t) {
184 if (verbose) printf("term-end\n");
185 }
186
187 static errr term_xtra_clear(int v) {
188 if (verbose) printf("term-xtra-clear %d\n", v);
189 return 0;
190 }
191
192 static errr term_xtra_noise(int v) {
193 if (verbose) printf("term-xtra-noise %d\n", v);
194 return 0;
195 }
196
197 static errr term_xtra_fresh(int v) {
198 if (verbose) printf("term-xtra-fresh %d\n", v);
199 return 0;
200 }
201
202 static errr term_xtra_shape(int v) {
203 if (verbose) printf("term-xtra-shape %d\n", v);
204 return 0;
205 }
206
207 static errr term_xtra_alive(int v) {
208 if (verbose) printf("term-xtra-alive %d\n", v);
209 return 0;
210 }
211
212 static errr term_xtra_event(int v) {
213 if (verbose) printf("term-xtra-event %d\n", v);
214 if (nextkey) {
215 Term_keypress(nextkey, 0);
216 nextkey = 0;
217 }
218 return test_docmd();
219 }
220
221 static errr term_xtra_flush(int v) {
222 if (verbose) printf("term-xtra-flush %d\n", v);
223 return 0;
224 }
225
226 static errr term_xtra_delay(int v) {
227 if (verbose) printf("term-xtra-delay %d\n", v);
228 return 0;
229 }
230
231 static errr term_xtra_react(int v) {
232 if (verbose) printf("term-xtra-react\n");
233 return 0;
234 }
235
236 static term_xtra_func xtras[] = {
237 { TERM_XTRA_CLEAR, term_xtra_clear },
238 { TERM_XTRA_NOISE, term_xtra_noise },
239 { TERM_XTRA_FRESH, term_xtra_fresh },
240 { TERM_XTRA_SHAPE, term_xtra_shape },
241 { TERM_XTRA_ALIVE, term_xtra_alive },
242 { TERM_XTRA_EVENT, term_xtra_event },
243 { TERM_XTRA_FLUSH, term_xtra_flush },
244 { TERM_XTRA_DELAY, term_xtra_delay },
245 { TERM_XTRA_REACT, term_xtra_react },
246 { 0, NULL },
247 };
248
249 static errr term_xtra_test(int n, int v) {
250 int i;
251 for (i = 0; xtras[i].func; i++) {
252 if (xtras[i].key == n) {
253 return xtras[i].func(v);
254 }
255 }
256 if (verbose) printf("term-xtra-unknown %d %d\n", n, v);
257 return 0;
258 }
259
260 static errr term_curs_test(int x, int y) {
261 if (verbose) printf("term-curs %d %d\n", x, y);
262 return 0;
263 }
264
265 static errr term_wipe_test(int x, int y, int n) {
266 if (verbose) printf("term-wipe %d %d %d\n", x, y, n);
267 return 0;
268 }
269
270 static errr term_text_test(int x, int y, int n, int a, const wchar_t *s) {
271 if (verbose) {
272 char str[256];
273 wcstombs(str, s, 256);
274 printf("term-text %d %d %d %02x %s\n", x, y, n, a, str);
275 }
276 return 0;
277 }
278
279 static void term_data_link(int i) {
280 term *t = &td.t;
281
282 term_init(t, 80, 24, 256);
283
284 t->init_hook = term_init_test;
285 t->nuke_hook = term_nuke_test;
286
287 t->xtra_hook = term_xtra_test;
288 t->curs_hook = term_curs_test;
289 t->wipe_hook = term_wipe_test;
290 t->text_hook = term_text_test;
291
292 t->data = &td;
293
294 Term_activate(t);
295
296 angband_term[i] = t;
297 }
298
299 const char help_test[] = "Test mode, subopts -p(rompt)";
300
301 errr init_test(int argc, char *argv[]) {
302 int i;
303
304 /* Skip over argv[0] */
305 for (i = 1; i < argc; i++) {
306 if (streq(argv[i], "-p")) {
307 prompt = 1;
308 continue;
309 }
310 printf("init-test: bad argument '%s'\n", argv[i]);
311 }
312
313 /*
314 * Reset savefile set by main.c: don't want it to interfere with the
315 * test.
316 */
317 savefile[0] = '\0';
318
319 term_data_link(0);
320 return 0;
321 }
322 #endif