2 test_description="message property API"
4 . ./test-lib.sh || exit 1
13 #include <notmuch-test.h>
15 void print_properties (notmuch_message_t *message, const char *prefix, notmuch_bool_t exact) {
16 notmuch_message_properties_t *list;
17 for (list = notmuch_message_get_properties (message, prefix, exact);
18 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
19 printf("%s\n", notmuch_message_properties_value(list));
21 notmuch_message_properties_destroy (list);
24 int main (int argc, char** argv)
26 notmuch_database_t *db;
27 notmuch_message_t *message = NULL;
29 notmuch_status_t stat;
31 EXPECT0(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));
32 EXPECT0(notmuch_database_find_message(db, "4EFC743A.3060609@april.org", &message));
33 if (message == NULL) {
34 fprintf (stderr, "unable to find message");
40 EXPECT0(notmuch_database_destroy(db));
44 test_begin_subtest "notmuch_message_{add,get,remove}_property"
45 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
47 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue1"));
48 EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
49 printf("testkey1[1] = %s\n", val);
50 EXPECT0(notmuch_message_add_property (message, "testkey2", "this value has spaces and = sign"));
51 EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
52 printf("testkey1[2] = %s\n", val);
53 EXPECT0(notmuch_message_get_property (message, "testkey1", &val));
55 EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
56 printf("testkey2 = %s\n", val);
58 /* Add second value for key */
59 EXPECT0(notmuch_message_add_property (message, "testkey2", "zztestvalue3"));
60 EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
61 printf("testkey2 = %s\n", val);
63 /* remove first value for key */
64 EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
65 EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
66 printf("testkey2 = %s\n", val);
68 /* remove non-existant value for key */
69 EXPECT0(notmuch_message_remove_property (message, "testkey2", "this value has spaces and = sign"));
70 EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
71 printf("testkey2 = %s\n", val);
73 /* remove only value for key */
74 EXPECT0(notmuch_message_remove_property (message, "testkey2", "zztestvalue3"));
75 EXPECT0(notmuch_message_get_property (message, "testkey2", &val));
76 printf("testkey2 = %s\n", val == NULL ? "NULL" : val);
81 testkey1[1] = testvalue1
82 testkey1[2] = testvalue1
83 testkey2 = this value has spaces and = sign
84 testkey2 = this value has spaces and = sign
85 testkey2 = zztestvalue3
86 testkey2 = zztestvalue3
90 test_expect_equal_file EXPECTED OUTPUT
92 test_begin_subtest "notmuch_message_get_properties: empty list"
93 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
95 notmuch_message_properties_t *list;
96 list = notmuch_message_get_properties (message, "nonexistent", TRUE);
97 printf("valid = %d\n", notmuch_message_properties_valid (list));
98 notmuch_message_properties_destroy (list);
101 cat <<'EOF' >EXPECTED
106 test_expect_equal_file EXPECTED OUTPUT
108 test_begin_subtest "notmuch_message_properties: one value"
109 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
110 print_properties (message, "testkey1", TRUE);
112 cat <<'EOF' >EXPECTED
117 test_expect_equal_file EXPECTED OUTPUT
119 test_begin_subtest "notmuch_message_properties: multiple values"
120 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
121 EXPECT0(notmuch_message_add_property (message, "testkey1", "bob"));
122 EXPECT0(notmuch_message_add_property (message, "testkey1", "testvalue2"));
123 EXPECT0(notmuch_message_add_property (message, "testkey1", "alice"));
124 print_properties (message, "testkey1", TRUE);
126 cat <<'EOF' >EXPECTED
134 test_expect_equal_file EXPECTED OUTPUT
136 test_begin_subtest "notmuch_message_properties: prefix"
137 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
138 EXPECT0(notmuch_message_add_property (message, "testkey3", "bob3"));
139 EXPECT0(notmuch_message_add_property (message, "testkey3", "testvalue3"));
140 EXPECT0(notmuch_message_add_property (message, "testkey3", "alice3"));
141 print_properties (message, "testkey", FALSE);
143 cat <<'EOF' >EXPECTED
154 test_expect_equal_file EXPECTED OUTPUT
156 test_begin_subtest "notmuch_message_properties: modify during iteration"
157 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
159 const char *keys[1000] = {NULL};
160 const char *vals[1000] = {NULL};
161 notmuch_message_properties_t *properties;
164 for (properties = notmuch_message_get_properties (message, "", FALSE), i=0;
165 notmuch_message_properties_valid (properties);
166 notmuch_message_properties_move_to_next (properties), i++)
168 const char *key, *value;
170 keys[i]=talloc_strdup(message,
171 notmuch_message_properties_key (properties));
172 vals[i]=talloc_strdup(message,
173 notmuch_message_properties_value (properties));
175 EXPECT0(notmuch_message_remove_property (message, keys[i], vals[i]));
178 print_properties (message, "", FALSE);
180 for (i = 0; keys[i] && vals[i]; i++) {
181 EXPECT0(notmuch_message_add_property (message, keys[i], vals[i]));
185 cat <<'EOF' >EXPECTED
189 test_expect_equal_file EXPECTED OUTPUT