// -*- c -*- // // $Id: expat_func.moc,v 1.10 2001/02/11 22:12:55 kmacleod Exp $ // #include static void expat_tests(id, int); main () { id err; id factory; printf ("1..61\n"); factory = XML_NonOpt_Document__new(NULL, 0); expat_tests(factory, 0); factory = XML_FastSmall_Document__new(NULL, 0); expat_tests(factory, 29); Try { id parser = SAXDriver_Expat__new(NULL, 0); @parser.parse(@"does-not-exist.xml"); ok(0, 59); } Catch(err) { ok(strncmp(String_s(@err.detail), "does-not-exist.xml: ", 20) == 0, 59); } Try { id handler = MyHandler__new(NULL, 0); id parser = SAXDriver_Expat__new(NULL, 0); @parser.handler = handler; @parser.parse(@"bad.xml"); ok(0, 60); } Catch(err) { ok(strncmp(String_s(@err.detail), "bad.xml:8: no element found", 20) == 0, 60); } { id handler = MyHandler__new(NULL, 0); id parser = SAXDriver_Expat__new(NULL, 0); @parser.handler = handler; @parser.parse_string(@""); ok(@handler.test1 == MOC_TRUE, 61); } return 0; } static void expat_tests(id factory, int testnum) { id handler = MyHandler__new(NULL, 0); id parser = SAXDriver_Expat__new(NULL, 0); @parser.handler = handler; @parser.factory = factory; @parser.parse(@"test.xml"); ok(@handler.test1 == MOC_TRUE, testnum + 1); ok(@handler.test2 == MOC_TRUE, testnum + 2); ok(@handler.test3 == MOC_TRUE, testnum + 3); ok(@handler.test4 == MOC_TRUE, testnum + 4); ok(@handler.test5 == MOC_TRUE, testnum + 5); ok(@handler.test6 == MOC_TRUE, testnum + 6); ok(@handler.test7 == MOC_TRUE, testnum + 7); ok(@handler.test8 == MOC_TRUE, testnum + 8); ok(@handler.test9 == MOC_TRUE, testnum + 9); ok(@handler.test10 == MOC_TRUE, testnum + 10); ok(@handler.test11 == MOC_TRUE, testnum + 11); ok(@handler.test12 == MOC_TRUE, testnum + 12); ok(@handler.test13 == MOC_TRUE, testnum + 13); ok(@handler.test14 == MOC_TRUE, testnum + 14); ok(@handler.test15 == MOC_TRUE, testnum + 15); ok(@handler.test16 == MOC_TRUE, testnum + 16); ok(@handler.test17 == MOC_TRUE, testnum + 17); ok(@handler.test18 == MOC_TRUE, testnum + 18); ok(@handler.test19 == MOC_TRUE, testnum + 19); ok(@handler.test20 == MOC_TRUE, testnum + 20); ok(@handler.test21 == MOC_TRUE, testnum + 21); ok(@handler.test22 == MOC_TRUE, testnum + 22); ok(@handler.fail_start_document == MOC_NIL, testnum + 23); ok(@handler.fail_end_document == MOC_NIL, testnum + 24); ok(@handler.fail_start_element == MOC_NIL, testnum + 25); ok(@handler.fail_end_element == MOC_NIL, testnum + 26); ok(@handler.fail_characters == MOC_NIL, testnum + 27); ok(@handler.fail_comment == MOC_NIL, testnum + 28); ok(@handler.fail_processing_instruction == MOC_NIL, testnum + 29); } @namespace itr urn:to-be-determined typedef id MyHandler; @class MyHandler(Node) @namespace ns1 uri:ns1 @namespace ns2 uri:ns2 @namespace xmlns http://www.w3.org/2000/xmlns/ @new() { self = Node__new(NULL, 0); self->isa_ = &MyHandler_isa_; return self; } @.itr:node_type { return MyHandlerType; } @start_document(id document) { if (@document.itr:node_type == XML_DocumentType) { @self.test21 = MOC_TRUE; } else { @self.fail_start_document = MOC_TRUE; } } @end_document(id document) { if (@document.itr:node_type == XML_DocumentType) { @self.test22 = MOC_TRUE; } else { @self.fail_end_document = MOC_TRUE; } } @start_element(id el) { id attrs, attr1; if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el1") == 0) && (@el.namespace_uri == MOC_NIL) && (@el.prefix == MOC_NIL) && (strcmp(String_s(@el.local_name), "el1") == 0) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.attr1) != MOC_NIL) && (strcmp(String_s(@attr1.name), "attr1") == 0) && (strcmp(String_s(@attr1.value), "text1") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == MOC_NIL)) { @self.test1 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el2") == 0) && (@el.namespace_uri == Symbol__new_s("uri:ns1")) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el2")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.xmlns) != MOC_NIL) && (strcmp(String_s(@attr1.name), "xmlns") == 0) && (strcmp(String_s(@attr1.value), "uri:ns1") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == MOC_NIL)) { @self.test2 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el3") == 0) && (@el.namespace_uri == Symbol__new_s("uri:ns1")) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el3")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.ns1:attr2) != MOC_NIL) && (strcmp(String_s(@attr1.name), "attr2") == 0) && (strcmp(String_s(@attr1.value), "text2") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == Symbol__new_s("uri:ns1"))) { @self.test3 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el4") == 0) && (@el.namespace_uri == MOC_NIL) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el4")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.xmlns:ns2) != MOC_NIL) && (strcmp(String_s(@attr1.name), "xmlns:ns2") == 0) && (strcmp(String_s(@attr1.value), "uri:ns2") == 0) && (@attr1.prefix == Symbol__new_s("xmlns")) && (@attr1.namespace_uri == Symbol__new_s("http://www.w3.org/2000/xmlns/"))) { @self.test4 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "ns2:el5") == 0) && (@el.namespace_uri == Symbol__new_s("uri:ns2")) && (@el.prefix == Symbol__new_s("ns2")) && (@el.local_name == Symbol__new_s("el5")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.attr3) != MOC_NIL) && (strcmp(String_s(@attr1.name), "attr3") == 0) && (strcmp(String_s(@attr1.value), "text3") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == MOC_NIL) && ((attr1 = @attrs.ns2:attr4) != MOC_NIL) && (strcmp(String_s(@attr1.name), "ns2:attr4") == 0) && (strcmp(String_s(@attr1.value), "text4") == 0) && (@attr1.namespace_uri == Symbol__new_s("uri:ns2")) && (@attr1.prefix == Symbol__new_s("ns2"))) { @self.test5 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el6") == 0) && (@el.namespace_uri == MOC_NIL) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el6"))) { @self.test6 = MOC_TRUE; } else { @self.fail_start_element = MOC_TRUE; } } @end_element(id el) { id attrs, attr1; if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el1") == 0) && (@el.namespace_uri == MOC_NIL) && (@el.prefix == MOC_NIL) && (strcmp(String_s(@el.local_name), "el1") == 0) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.attr1) != MOC_NIL) && (strcmp(String_s(@attr1.name), "attr1") == 0) && (strcmp(String_s(@attr1.value), "text1") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == MOC_NIL)) { @self.test7 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el2") == 0) && (@el.namespace_uri == Symbol__new_s("uri:ns1")) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el2")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.xmlns) != MOC_NIL) && (strcmp(String_s(@attr1.name), "xmlns") == 0) && (strcmp(String_s(@attr1.value), "uri:ns1") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == MOC_NIL)) { @self.test8 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el3") == 0) && (@el.namespace_uri == Symbol__new_s("uri:ns1")) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el3")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.ns1:attr2) != MOC_NIL) && (strcmp(String_s(@attr1.name), "attr2") == 0) && (strcmp(String_s(@attr1.value), "text2") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == Symbol__new_s("uri:ns1"))) { @self.test9 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el4") == 0) && (@el.namespace_uri == MOC_NIL) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el4")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.xmlns:ns2) != MOC_NIL) && (strcmp(String_s(@attr1.name), "xmlns:ns2") == 0) && (strcmp(String_s(@attr1.value), "uri:ns2") == 0) && (@attr1.prefix == Symbol__new_s("xmlns")) && (@attr1.namespace_uri == Symbol__new_s("http://www.w3.org/2000/xmlns/"))) { @self.test10 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "ns2:el5") == 0) && (@el.namespace_uri == Symbol__new_s("uri:ns2")) && (@el.prefix == Symbol__new_s("ns2")) && (@el.local_name == Symbol__new_s("el5")) && ((attrs = @el.attributes) != MOC_NIL) && ((attr1 = @attrs.attr3) != MOC_NIL) && (strcmp(String_s(@attr1.name), "attr3") == 0) && (strcmp(String_s(@attr1.value), "text3") == 0) && (@attr1.prefix == MOC_NIL) && (@attr1.namespace_uri == MOC_NIL) && ((attr1 = @attrs.ns2:attr4) != MOC_NIL) && (strcmp(String_s(@attr1.name), "ns2:attr4") == 0) && (strcmp(String_s(@attr1.value), "text4") == 0) && (@attr1.namespace_uri == Symbol__new_s("uri:ns2")) && (@attr1.prefix == Symbol__new_s("ns2"))) { @self.test11 = MOC_TRUE; } else if ((@el.itr:node_type == XML_ElementType) && (strcmp(String_s(@el.name), "el6") == 0) && (@el.namespace_uri == MOC_NIL) && (@el.prefix == MOC_NIL) && (@el.local_name == Symbol__new_s("el6"))) { @self.test12 = MOC_TRUE; } else { @self.fail_end_element = MOC_TRUE; } } @characters(id chars) { const char *data = String_s(@chars.data); if (strspn(data, " \t\n\r") == strlen(data)) { @self.test13 = MOC_TRUE; } else if((@chars.itr:node_type == XML_CharactersType) && (strcmp(data, "text5") == 0)) { @self.test14 = MOC_TRUE; } else { @self.fail_characters = MOC_TRUE; } } @comment(id comment) { const char *data = String_s(@comment.data); if ((@comment.itr:node_type == XML_CommentType) && (strcmp(data, " comment1 ") == 0)) { @self.test15 = MOC_TRUE; } else if ((@comment.itr:node_type == XML_CommentType) && (strcmp(data, " comment2 ") == 0)) { @self.test16 = MOC_TRUE; } else if ((@comment.itr:node_type == XML_CommentType) && (strcmp(data, " comment3 ") == 0)) { @self.test17 = MOC_TRUE; } else if ((@comment.itr:node_type == XML_CommentType) && (strncmp(data, " $Id", 4) == 0)) { // nothing } else { @self.fail_comment = MOC_TRUE; } } @processing_instruction(id pi) { const char *data = String_s(@pi.data); const char *target = String_s(@pi.target); if ((@pi.itr:node_type == XML_ProcessingInstructionType) && (strcmp(target, "pi1") == 0) && (strcmp(data, "data1") == 0)) { @self.test18 = MOC_TRUE; } else if ((@pi.itr:node_type == XML_ProcessingInstructionType) && (strcmp(target, "pi2") == 0) && (strcmp(data, "data2") == 0)) { @self.test19 = MOC_TRUE; } else if ((@pi.itr:node_type == XML_ProcessingInstructionType) && (strcmp(target, "pi3") == 0) && (strcmp(data, "data3") == 0)) { @self.test20 = MOC_TRUE; } else { @self.fail_processing_instruction = MOC_TRUE; } }