-
Notifications
You must be signed in to change notification settings - Fork 13
/
test-queries.c
80 lines (63 loc) · 2.11 KB
/
test-queries.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "test.h"
#include "query.h"
#include "query-parser.h"
TEST(empty_queries) {
wp_query* q;
RELAY_ERROR(wp_query_parse("", "body", &q));
ASSERT(q);
return NO_ERROR;
}
/* most of the query parsing code is exercised by the
search tests, so this is just for fun. */
TEST(query_parsing) {
wp_query* q;
RELAY_ERROR(wp_query_parse("i eat mice", "body", &q));
ASSERT_EQUALS_UINT(3, q->num_children);
ASSERT_EQUALS_UINT(WP_QUERY_CONJ, q->type);
ASSERT_EQUALS_PTR(NULL, q->field);
ASSERT_EQUALS_PTR(NULL, q->word);
ASSERT(q->children != NULL);
ASSERT_EQUALS_UINT(WP_QUERY_TERM, q->children->type);
ASSERT(!strcmp(q->children->field, "body"));
ASSERT(!strcmp(q->children->word, "i"));
return NO_ERROR;
}
TEST(query_cloning) {
wp_query* q;
RELAY_ERROR(wp_query_parse("i eat mice OR \"muffin pants\" bob:pumpkin", "body", &q));
char buf[100];
wp_query_to_s(q, 100, buf);
ASSERT(!strcmp(buf, "(AND body:\"i\" body:\"eat\" (OR body:\"mice\" (PHRASE body:\"muffin\" body:\"pants\")) bob:\"pumpkin\")"));
wp_query* q2;
RELAY_ERROR(wp_query_parse("i eat mice OR \"muffin pants\" bob:pumpkin", "body", &q));
q2 = wp_query_clone(q);
ASSERT(q2);
ASSERT(q2 != q);
wp_query_to_s(q2, 100, buf);
ASSERT(!strcmp(buf, "(AND body:\"i\" body:\"eat\" (OR body:\"mice\" (PHRASE body:\"muffin\" body:\"pants\")) bob:\"pumpkin\")"));
return NO_ERROR;
}
static const char* substituter(const char* field, const char* term) {
(void)field;
char* ret = calloc(strlen(term) + 3, sizeof(char));
sprintf(ret, "X%sY", term);
return ret;
}
TEST(query_substitution) {
wp_query* q;
RELAY_ERROR(wp_query_parse("i eat mice", "body", &q));
wp_query* q2;
q2 = wp_query_substitute(q, substituter);
ASSERT(q2);
ASSERT(q2 != q);
char buf[100];
wp_query_to_s(q2, 100, buf);
ASSERT(!strcmp(buf, "(AND body:\"XiY\" body:\"XeatY\" body:\"XmiceY\")"));
RELAY_ERROR(wp_query_parse("i eat ~mice", "body", &q));
q2 = wp_query_substitute(q, substituter);
ASSERT(q2);
ASSERT(q2 != q);
wp_query_to_s(q2, 100, buf);
ASSERT(!strcmp(buf, "(AND body:\"XiY\" body:\"XeatY\" ~mice)"));
return NO_ERROR;
}