Merge pull request #902 from libexpat/tests-reduce-use-of-global-parser

Tests: Reduce use of global parser
This commit is contained in:
Sebastian Pipping 2024-09-23 20:16:31 +02:00 committed by GitHub
commit 99b6fde742
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 7 deletions

View file

@ -2357,11 +2357,20 @@ START_TEST(test_attributes) {
info[0].attributes = doc_info;
info[1].attributes = tag_info;
XML_SetStartElementHandler(g_parser, counting_start_element_handler);
XML_SetUserData(g_parser, info);
if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
XML_Parser parser = XML_ParserCreate(NULL);
assert_true(parser != NULL);
ParserAndElementInfo parserAndElementInfos = {
parser,
info,
};
XML_SetStartElementHandler(parser, counting_start_element_handler);
XML_SetUserData(parser, &parserAndElementInfos);
if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE)
== XML_STATUS_ERROR)
xml_failure(g_parser);
xml_failure(parser);
XML_ParserFree(parser);
}
END_TEST

View file

@ -103,7 +103,9 @@ end_element_event_handler2(void *userData, const XML_Char *name) {
void XMLCALL
counting_start_element_handler(void *userData, const XML_Char *name,
const XML_Char **atts) {
ElementInfo *info = (ElementInfo *)userData;
ParserAndElementInfo *const parserAndElementInfos
= (ParserAndElementInfo *)userData;
ElementInfo *info = parserAndElementInfos->info;
AttrInfo *attr;
int count, id, i;
@ -120,12 +122,12 @@ counting_start_element_handler(void *userData, const XML_Char *name,
* is possibly a little unexpected, but it is what the
* documentation in expat.h tells us to expect.
*/
count = XML_GetSpecifiedAttributeCount(g_parser);
count = XML_GetSpecifiedAttributeCount(parserAndElementInfos->parser);
if (info->attr_count * 2 != count) {
fail("Not got expected attribute count");
return;
}
id = XML_GetIdAttributeIndex(g_parser);
id = XML_GetIdAttributeIndex(parserAndElementInfos->parser);
if (id == -1 && info->id_name != NULL) {
fail("ID not present");
return;

View file

@ -92,6 +92,11 @@ typedef struct elementInfo {
AttrInfo *attributes;
} ElementInfo;
typedef struct StructParserAndElementInfo {
XML_Parser parser;
ElementInfo *info;
} ParserAndElementInfo;
extern void XMLCALL counting_start_element_handler(void *userData,
const XML_Char *name,
const XML_Char **atts);