1 /**
2  * testhelper.h
3  * * Copyright (c) 2012
4  *	libchewing Core Team. See ChangeLog for details.
5  *
6  * See the file "COPYING" for information on usage and redistribution
7  * of this file.
8  */
9 #ifdef HAVE_CONFIG_H
10 #    include <config.h>
11 #endif
12 
13 #include <stddef.h>
14 #include <stdio.h>
15 
16 #include "chewing.h"
17 #include "chewingio.h"
18 #include "chewing-utf8-util.h"
19 
20 #include "plat_path.h"
21 
22 #define KEY_DBLTAB	892     // <TT>
23 #define KEY_SSPACE	893     // <SS>
24 #define KEY_PPAGE	894     // <PU>
25 #define KEY_NPAGE	895     // <PD>
26 #define KEY_SLEFT	896     // <SL>
27 #define KEY_SRIGHT	897     // <SR>
28 #define KEY_LEFT	898     // <L>
29 #define KEY_RIGHT	899     // <R>
30 #define KEY_UP		990     // <U>
31 #define KEY_DOWN	991     // <D>
32 #define KEY_SPACE	' '
33 #define KEY_ENTER	992     // <E>
34 #define KEY_BACKSPACE	993     // <B>
35 #define KEY_ESC		994     // <EE>
36 #define KEY_DELETE	995     // <DC>
37 #define KEY_HOME	996     // <H>
38 #define KEY_END		997     // <EN>
39 #define KEY_TAB		998     // <T>
40 #define KEY_CAPSLOCK	999     // <CB>
41 #define KEY_CTRL_BASE	1000    // <C0>..<C9>
42 #define KEY_NUMPAD_BASE	1100    // <N0>..<N9>,<N+>,<N->,<N*>,<N/>,<N.>
43 #define END 2000
44 
45 #ifdef __GNUC__
46 #    define ARRAY_SIZE( array ) ( sizeof(array) / sizeof(((typeof(array)){})[0]) )
47 #else
48 #    define ARRAY_SIZE( array ) ( sizeof(array) / sizeof(array[0] ) )
49 #endif
50 
51 #define ok(test, fmt, ...) \
52     internal_ok(__FILE__, __LINE__, !!(test), #test, fmt, ##__VA_ARGS__)
53 #define ok_commit_buffer(ctx, expected) \
54     internal_ok_buffer(__FILE__, __LINE__, ctx, expected, &COMMIT_BUFFER)
55 #define ok_preedit_buffer(ctx, expected) \
56     internal_ok_buffer(__FILE__, __LINE__, ctx, expected, &PREEDIT_BUFFER)
57 #define ok_bopomofo_buffer(ctx, expected) \
58     internal_ok_buffer(__FILE__, __LINE__, ctx, expected, &BOPOMOFO_BUFFER)
59 #define ok_aux_buffer(ctx, expected) \
60     internal_ok_buffer(__FILE__, __LINE__, ctx, expected, &AUX_BUFFER)
61 #define ok_candidate(ctx, cand, cand_len) \
62     internal_ok_candidate(__FILE__, __LINE__, ctx, cand, cand_len)
63 #define ok_candidate_len(ctx, expected_len) \
64     internal_ok_candidate_len(__FILE__, __LINE__, ctx, expected_len)
65 #define ok_keystroke_rtn(ctx, rtn) \
66     internal_ok_keystroke_rtn(__FILE__, __LINE__, ctx, rtn)
67 #define has_userphrase(ctx, bopomofo, phrase) \
68     internal_has_userphrase(__FILE__, __LINE__, ctx, bopomofo, phrase)
69 #define start_testcase(ctx, file) \
70     internal_start_testcase(__func__, ctx, file)
71 
72 typedef struct TestData {
73     char *token;
74     char *expected;
75 } TestData;
76 
77 typedef struct BufferType {
78     const char *name;
79     int (*check) (const ChewingContext *ctx);
80     int (*check_alt) (const ChewingContext *ctx);
81     int (*get_length) (const ChewingContext *ctx);
82     char *(*get_string) (const ChewingContext *ctx);
83     char *(*get_string_alt) (const ChewingContext *ctx, int *len);
84     const char *(*get_string_static) (const ChewingContext *ctx);
85 } BufferType;
86 
87 extern BufferType COMMIT_BUFFER;
88 extern BufferType PREEDIT_BUFFER;
89 extern BufferType BOPOMOFO_BUFFER;
90 extern BufferType AUX_BUFFER;
91 
92 typedef int (*get_char_func) (void *param);
93 
94 int get_keystroke(get_char_func get_char, void *param);
95 void type_keystroke_by_string(ChewingContext *ctx, const char *keystroke);
96 void type_single_keystroke(ChewingContext *ctx, int ch);
97 int exit_status();
98 void clean_userphrase();
99 
100 // The internal_xxx function shall be used indirectly by macro in order to
101 // get correct __FILE__ and __LINE__ information.
102 void internal_ok_buffer(const char *file, int line, ChewingContext *ctx,
103                         const char *expected, const BufferType *buffer);
104 void internal_ok(const char *file, int line, int test, const char *test_txt, const char *message, ...);
105 void internal_ok_candidate(const char *file, int line, ChewingContext *ctx, const char *cand[], size_t cand_len);
106 void internal_ok_candidate_len(const char *file, int line, ChewingContext *ctx, size_t expected_len);
107 void internal_ok_keystroke_rtn(const char *file, int line, ChewingContext *ctx, int rtn);
108 int internal_has_userphrase(const char *file, int line, ChewingContext *ctx, const char *bopomofo, const char *phrase);
109 void internal_start_testcase(const char *func, ChewingContext *ctx, FILE * file);
110 void logger(void *data, int level, const char *fmt, ...);
111