[Expat-checkins] CVS: expat/tests runtests.c,1.12,1.13
Fred L. Drake
fdrake@users.sourceforge.net
Mon Apr 22 11:47:02 2002
Update of /cvsroot/expat/expat/tests
In directory usw-pr-cvs1:/tmp/cvs-serv24905/tests
Modified Files:
runtests.c
Log Message:
Test support enhancements:
_xml_failure(): Clean up the output message so the first line doesn't
wrap in the typical case.
CharData typedef: If count is less than 0, consider it "unset". This
is used in the new support to check attribute values.
accumulate_characters(): Handle storage->count < 0. Allow XML_Char
to be more than one byte.
accumulate_attribute(): New function: store the value of the first
attribute encountered in parsing. This needs the concept of a
CharData that is unset to allow capturing a value that is an empty
string.
check_characters(): Handle storage->count < 0. Allow XML_Char to be
more than one byte.
run_character_check(): Create a local parser so that both
run_character_check() and run_attribute_check() can be used in a
single test function.
run_attribute_check(): New function: check that the value of the
first attribute in a document matches the expected value.
Index: runtests.c
===================================================================
RCS file: /cvsroot/expat/expat/tests/runtests.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** runtests.c 20 Apr 2002 13:19:40 -0000 1.12
--- runtests.c 22 Apr 2002 18:46:00 -0000 1.13
***************
*** 34,38 ****
{
char buffer[1024];
! sprintf(buffer, "%s (line %d, offset %d)\n reported from %s, line %d",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser),
--- 34,39 ----
{
char buffer[1024];
! sprintf(buffer,
! "\n %s (line %d, offset %d)\n reported from %s, line %d",
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser),
***************
*** 118,122 ****
typedef struct
{
! int count;
XML_Char data[1024];
} CharData;
--- 119,123 ----
typedef struct
{
! int count; /* # of chars, < 0 if not set */
XML_Char data[1024];
} CharData;
***************
*** 126,131 ****
{
CharData *storage = (CharData *)userData;
if (len + storage->count < sizeof(storage->data)) {
! memcpy(storage->data + storage->count, s, len);
storage->count += len;
}
--- 127,135 ----
{
CharData *storage = (CharData *)userData;
+ if (storage->count < 0)
+ storage->count = 0;
if (len + storage->count < sizeof(storage->data)) {
! memcpy(storage->data + storage->count, s,
! len * sizeof(storage->data[0]));
storage->count += len;
}
***************
*** 133,140 ****
--- 137,165 ----
static void
+ accumulate_attribute(void *userData, const XML_Char *name,
+ const XML_Char **atts)
+ {
+ CharData *storage = (CharData *)userData;
+ if (storage->count < 0 && atts != NULL && atts[0] != NULL) {
+ /* "accumulate" the value of the first attribute we see */
+ int maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
+ int i;
+ for (i = 0; i < maxchars; ++i) {
+ XML_Char ch = atts[1][i];
+ if (ch == 0)
+ break;
+ storage->data[i] = ch;
+ }
+ storage->count = i;
+ }
+ }
+
+ static void
check_characters(CharData *storage, XML_Char *expected)
{
char buffer[1024];
int len = strlen(expected);
+ if (storage->count < 0)
+ storage->count = 0;
if (len != storage->count) {
sprintf(buffer, "wrong number of data characters: got %d, expected %d",
***************
*** 143,147 ****
return;
}
! if (memcmp(expected, storage->data, len) != 0)
fail("got bad data bytes");
}
--- 168,172 ----
return;
}
! if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0)
fail("got bad data bytes");
}
***************
*** 151,155 ****
{
CharData storage;
! storage.count = 0;
XML_SetUserData(parser, &storage);
XML_SetCharacterDataHandler(parser, accumulate_characters);
--- 176,185 ----
{
CharData storage;
! XML_Parser parser = XML_ParserCreate(NULL);
!
! if (parser == NULL)
! fail("Parser not created.");
!
! storage.count = -1;
XML_SetUserData(parser, &storage);
XML_SetCharacterDataHandler(parser, accumulate_characters);
***************
*** 157,160 ****
--- 187,209 ----
xml_failure();
check_characters(&storage, expected);
+ XML_ParserFree(parser);
+ }
+
+ static void
+ run_attribute_check(XML_Char *text, XML_Char *expected)
+ {
+ CharData storage;
+ XML_Parser parser = XML_ParserCreate(NULL);
+
+ if (parser == NULL)
+ fail("Parser not created.");
+
+ storage.count = -1; /* magical "not-set" value */
+ XML_SetUserData(parser, &storage);
+ XML_SetStartElementHandler(parser, accumulate_attribute);
+ if (!XML_Parse(parser, text, strlen(text), 1))
+ xml_failure();
+ check_characters(&storage, expected);
+ XML_ParserFree(parser);
}