1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
libpsyc/test/test_text.c

102 lines
2.4 KiB
C
Raw Normal View History

2011-05-03 20:21:26 +00:00
#include <stdio.h>
2011-05-08 21:40:26 +00:00
#include <lib.h>
2011-05-03 20:18:35 +00:00
#include <psyc/text.h>
#define BUFSIZE 512
uint8_t verbose;
2011-10-31 19:26:47 +00:00
PsycTextValueRC getValueFooBar (const char *name, size_t len, PsycString *value, void *extra)
2011-05-03 20:18:35 +00:00
{
2011-05-04 15:46:20 +00:00
if (verbose)
printf("> getValue: %.*s\n", (int)len, name);
2011-11-01 11:06:58 +00:00
value->data = "Foo Bar";
2011-05-03 20:18:35 +00:00
value->length = 7;
return PSYC_TEXT_VALUE_FOUND;
}
2011-10-31 19:26:47 +00:00
PsycTextValueRC getValueEmpty (const char *name, size_t len, PsycString *value, void *extra)
2011-05-03 20:18:35 +00:00
{
2011-05-04 15:46:20 +00:00
if (verbose)
printf("> getValue: %.*s\n", (int)len, name);
2011-11-01 11:06:58 +00:00
value->data = "";
2011-05-03 20:18:35 +00:00
value->length = 0;
return PSYC_TEXT_VALUE_FOUND;
}
2011-10-31 19:26:47 +00:00
PsycTextValueRC getValueNotFound (const char *name, size_t len, PsycString *value, void *extra)
2011-05-03 20:18:35 +00:00
{
2011-05-04 15:46:20 +00:00
if (verbose)
printf("> getValue: %.*s\n", (int)len, name);
2011-05-03 20:18:35 +00:00
return PSYC_TEXT_VALUE_NOT_FOUND;
}
2011-10-31 19:26:47 +00:00
int testText (char *template, size_t tmplen, char *buffer, size_t buflen, PsycString *result, PsycTextCB getValue)
2011-05-03 20:18:35 +00:00
{
2011-10-31 19:26:47 +00:00
PsycTextState state;
2011-05-03 20:18:35 +00:00
size_t length = 0;
2011-10-31 19:26:47 +00:00
PsycTextRC ret;
2011-05-03 20:18:35 +00:00
2011-10-31 19:04:16 +00:00
psyc_text_state_init(&state, template, tmplen, buffer, buflen);
2011-05-03 20:18:35 +00:00
do
{
2011-06-12 19:04:11 +00:00
ret = psyc_text(&state, getValue, NULL);
2011-10-31 19:04:16 +00:00
length += psyc_text_bytes_written(&state);
2011-05-03 20:18:35 +00:00
switch (ret)
{
case PSYC_TEXT_INCOMPLETE:
if (verbose)
printf("# %.*s...\n", (int)length, buffer);
2011-10-31 19:04:16 +00:00
psyc_text_buffer_set(&state, buffer + length, BUFSIZE - length);
2011-05-03 20:18:35 +00:00
break;
case PSYC_TEXT_COMPLETE:
if (verbose)
printf("%.*s\n", (int)length, buffer);
result->length = length;
2011-11-01 11:06:58 +00:00
result->data = buffer;
2011-05-03 20:18:35 +00:00
return ret;
case PSYC_TEXT_NO_SUBST:
if (verbose)
printf("%.*s\n", (int)tmplen, template);
return ret;
}
}
while (ret == PSYC_TEXT_INCOMPLETE);
return -2; // shouldn't be reached
}
int main(int argc, char **argv)
{
verbose = argc > 1;
char buffer[BUFSIZE];
2011-10-31 19:26:47 +00:00
PsycString result;
2011-05-03 20:18:35 +00:00
char *str = "Hello [_foo] & [_bar]!";
size_t len = strlen(str);
int i;
testText(str, len, buffer, BUFSIZE, &result, &getValueFooBar);
2011-11-01 11:06:58 +00:00
if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
2011-05-03 20:18:35 +00:00
return 1;
testText(str, len, buffer, BUFSIZE, &result, &getValueEmpty);
2011-11-01 11:06:58 +00:00
if (memcmp(result.data, PSYC_C2ARG("Hello & !")))
2011-05-03 20:18:35 +00:00
return 2;
if (testText(str, len, buffer, BUFSIZE, &result, &getValueNotFound) != PSYC_TEXT_NO_SUBST)
return 3;
for (i = 1; i < 22; i++)
{
testText(str, len, buffer, i, &result, &getValueFooBar);
2011-11-01 11:06:58 +00:00
if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
2011-05-03 20:18:35 +00:00
return 10 + i;
}
puts("psyc_text passed all tests.");
2011-05-03 20:18:35 +00:00
return 0;
}