// -*- c -*- // // $Id: xpath_unit.moc,v 1.7 2001/01/27 23:13:12 kmacleod Exp $ // @namespace xmlns http://www.w3.org/2000/xmlns/ @namespace ns2 uri:ns2 @namespace itr urn:to-be-determined #define test_qname (Symbol__new_s("test_qname")) #define test_ncwild (Symbol__new_s("test_ncwild")) #define test_any (Symbol__new_s("test_any")) #define test_attr_qname (Symbol__new_s("test_attr_qname")) #define test_attr_ncwild (Symbol__new_s("test_attr_ncwild")) #define test_attr_any (Symbol__new_s("test_attr_any")) #define test_nt_comment (Symbol__new_s("test_nt_comment")) #define test_nt_text (Symbol__new_s("test_nt_text")) #define test_nt_pi (Symbol__new_s("test_nt_pi")) #define test_nt_node (Symbol__new_s("test_nt_node")) static id findnode(id context, id type, char *literal_s) { id contents = @context.contents; id literal = Symbol__new_s(literal_s); int ii; for (ii = 0; ii < Int_i(@contents.length()); ii ++) { id node = List_ati_i(contents, ii); if (@node.itr:node_type == type && type == XML_ElementType && @node.local_name == literal) return node; if (@node.itr:node_type == type && type == XML_ProcessingInstructionType && @node.target == literal) return node; } printf("did not find expected node.\n"); exit(1); } main() { id err; id step = XPath_Step__new(NULL, 0); id results = List__new(NULL, 0); id factory = XML_FastSmall_Document__new(NULL, 0); id orchard, el1, el2, el3, el4, el5, el6, attributes, pi1; id namespace_uri = Symbol__new_s("urn:foo"); id handler = TreeBuilder__new(NULL, 0); id parser = SAXDriver_Expat__new(NULL, 0); printf ("1..79\n"); @parser.handler = handler; orchard = @parser.parse(@"test.xml"); el1 = @orchard.root; el2 = findnode(el1, XML_ElementType, "el2"); el3 = findnode(el2, XML_ElementType, "el3"); el4 = findnode(el1, XML_ElementType, "el4"); el5 = findnode(el4, XML_ElementType, "el5"); el6 = findnode(el1, XML_ElementType, "el6"); pi1 = findnode(orchard, XML_ProcessingInstructionType, "pi1"); // // @test_node() and @get_namespace() // @step.test = test_nt_node; ok(@step.test_node(@factory.createElement()) == MOC_TRUE, 1); @step.test = test_any; ok(@step.test_node(@factory.createElement()) == MOC_TRUE, 2); ok(@step.test_node(@factory.createDocument()) == MOC_FALSE, 3); ok(@step.test_node(@factory.createComment()) == MOC_FALSE, 4); // XXX test_ncwild maybe should depend on prefix, and prefix may be // locally scoped. Depends on XPath spec. // XXX idea: if literal is a NodeKey, uri/lname is fixed, if literal // is a string, prefix is locally scoped @step.test = test_ncwild; ok(@step.test_node(@factory.createComment()) == MOC_FALSE, 5); @step.literal = MOC_NIL; ok(@step.test_node(el1) == MOC_TRUE, 6); @step.literal = Symbol__new_s("ns1"); ok(@step.test_node(el1) == MOC_FALSE, 7); // XXX requires non-context-sensitive matching //ok(@step.test_node(el2) == MOC_TRUE, 8); ok(1, 8); // XXX test_qname maybe should depend on prefix, and prefix may be // locally scoped. Depends on XPath spec. // XXX this is not testing the case where prefix is registered to // the parser and not locally scoped // XXX not testing late binding of default namespace @step.test = test_qname; ok(@step.test_node(@factory.createComment()) == MOC_FALSE, 9); @step.literal = Symbol__new_s("foo"); ok(@step.test_node(el1) == MOC_FALSE, 10); @step.literal = Symbol__new_s("el1"); ok(@step.test_node(el1) == MOC_TRUE, 11); @step.literal = Symbol__new_s("bar:foo"); ok(@step.test_node(el5) == MOC_FALSE, 12); @step.literal = Symbol__new_s("el2"); ok(@step.test_node(el2) == MOC_TRUE, 13); @step.literal = Symbol__new_s("ns2:el4"); ok(@step.test_node(el4) == MOC_FALSE, 14); ok(strcmp(String_s(@step.get_namespace(@el5.prefix, el4)), String_s(@el5.namespace_uri)) == 0, 15); ok(strcmp(String_s(@step.get_namespace(@el5.prefix, el5)), String_s(@el5.namespace_uri)) == 0, 16); @step.literal = Symbol__new_s("ns2:el5"); ok(@step.test_node(el5) == MOC_TRUE, 17); @step.literal = Key__new_ss("uri:ns1", "el2"); ok(@step.test_node(el2) == MOC_TRUE, 18); @step.test = test_nt_text; ok(@step.test_node(@factory.createComment()) == MOC_FALSE, 19); ok(@step.test_node(@factory.createCharacters()) == MOC_TRUE, 20); @step.test = test_nt_comment; ok(@step.test_node(@factory.createCharacters()) == MOC_FALSE, 21); ok(@step.test_node(@factory.createComment()) == MOC_TRUE, 22); @step.test = test_nt_pi; @step.literal = MOC_NIL; ok(@step.test_node(@factory.createComment()) == MOC_FALSE, 23); ok(@step.test_node(pi1) == MOC_TRUE, 24); @step.literal = Symbol__new_s("foo"); ok(@step.test_node(pi1) == MOC_FALSE, 25); @step.literal = Symbol__new_s("pi1"); ok(@step.test_node(pi1) == MOC_TRUE, 26); // test_attribute @step.test = test_nt_node; ok(@step.test_attribute(el1, @el1.attr1) == MOC_TRUE, 27); @step.test = test_attr_any; ok(@step.test_attribute(el1, @el1.attr1) == MOC_TRUE, 28); // XXX test_attr_ncwild maybe should depend on prefix, and prefix // may be locally scoped. Depends on XPath spec. @step.test = test_attr_ncwild; @step.literal = MOC_NIL; attributes = @el1.attributes; ok(@step.test_attribute(el1, @attributes.attr1) == MOC_TRUE, 29); @step.literal = Symbol__new_s("ns2"); ok(@step.test_attribute(el1, @attributes.attr1) == MOC_FALSE, 30); attributes = @el5.attributes; ok(@step.test_attribute(el5, @attributes.ns2:attr4) == MOC_TRUE, 31); @step.test = test_attr_qname; @step.literal = Symbol__new_s("foo"); attributes = @el1.attributes; ok(@step.test_attribute(el1, @attributes.attr1) == MOC_FALSE, 32); @step.literal = Symbol__new_s("attr1"); ok(@step.test_attribute(el1, @attributes.attr1) == MOC_TRUE, 33); attributes = @el5.attributes; @step.literal = Symbol__new_s("bar:foo"); ok(@step.test_attribute(el5, @attributes.ns2:attr4) == MOC_FALSE, 34); @step.literal = Symbol__new_s("ns2:attr4"); ok(@step.test_attribute(el5, @attributes.ns2:attr4) == MOC_TRUE, 35); // test_namespace @step.test = test_any; attributes = @el4.attributes; ok(@step.test_namespace(@attributes.xmlns:ns2) == MOC_TRUE, 36); @step.test = test_qname; @step.literal = MOC_NIL; ok(@step.test_namespace(@attributes.xmlns:ns2) == MOC_FALSE, 37); attributes = @el2.attributes; ok(@step.test_namespace(@attributes.xmlns) == MOC_TRUE, 38); attributes = @el4.attributes; @step.literal = Symbol__new_s("foo"); ok(@step.test_namespace(@attributes.xmlns:ns2) == MOC_FALSE, 39); @step.literal = Symbol__new_s("ns2"); ok(@step.test_namespace(@attributes.xmlns:ns2) == MOC_TRUE, 40); // // axis_* // // axis_descendant, of several types just to make sure results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el4"); @step.axis_descendant(orchard, results); ok(Int_i(@results.length()) == 1, 41); results = List__new(NULL, 0); @step.test = test_ncwild; @step.literal = Symbol__new_s("ns1"); @step.axis_descendant(orchard, results); // XXX requires non-context-sensitive matching //ok(Int_i(@results.length()) == 2, 42); ok(1, 41); results = List__new(NULL, 0); @step.test = test_nt_pi; @step.literal = MOC_NIL; @step.axis_descendant(orchard, results); ok(Int_i(@results.length()) == 3, 43); results = List__new(NULL, 0); @step.test = test_nt_comment; @step.axis_descendant(orchard, results); ok(Int_i(@results.length()) == 4, 44); // axis_descendant_or_self results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el4"); @step.axis_descendant(orchard, results); results = List__new(NULL, 0); @step.axis_descendant_or_self(el4, results); ok(Int_i(@results.length()) == 1, 45); // axis_child results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el1"); @step.axis_descendant(el1, results); results = List__new(NULL, 0); @step.literal = Symbol__new_s("el4"); @step.axis_child(el1, results); ok(Int_i(@results.length()) == 1, 46); // axis_ancestor results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el1"); @step.axis_ancestor(el4, results); ok(List_ati_i(results, 0) == el1, 47); results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el4"); @step.axis_ancestor(el4, results); ok(Int_i(@results.length()) == 0, 48); // axis_ancestor_or_self results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el1"); @step.axis_ancestor_or_self(el4, results); ok(List_ati_i(results, 0) == el1, 49); results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el4"); @step.axis_ancestor_or_self(el4, results); ok(Int_i(@results.length()) == 1, 50); // axis_attribute results = List__new(NULL, 0); @step.test = test_attr_any; @step.axis_attribute(el5, results); ok(Int_i(@results.length()) == 2, 51); results = List__new(NULL, 0); @step.test = test_attr_qname; @step.literal = Symbol__new_s("ns2:attr4"); @step.axis_attribute(el5, results); ok(Int_i(@results.length()) == 1, 52); // axis_following results = List__new(NULL, 0); @step.test = test_any; @step.axis_following(el5, results); ok(Int_i(@results.length()) == 1, 53); results = List__new(NULL, 0); @step.axis_following(el2, results); ok(Int_i(@results.length()) == 3, 54); // axis_following_sibling results = List__new(NULL, 0); @step.test = test_any; @step.axis_following_sibling(el5, results); ok(Int_i(@results.length()) == 0, 55); results = List__new(NULL, 0); @step.axis_following_sibling(el2, results); ok(Int_i(@results.length()) == 2, 56); // axis_namespace // XXX semantic difference: Namespace "nodes" are attribute nodes // owned by the declaring element // possible conformance: lazily convert to namespace nodes upon returning results = List__new(NULL, 0); @step.test = test_any; @step.axis_namespace(pi1, results); ok(Int_i(@results.length()) == 0, 57); results = List__new(NULL, 0); @step.axis_namespace(el6, results); ok(Int_i(@results.length()) == 0, 58); results = List__new(NULL, 0); @step.axis_namespace(el5, results); ok(Int_i(@results.length()) == 1, 59); @step.test = test_qname; @step.literal = MOC_NIL; results = List__new(NULL, 0); @step.axis_namespace(el3, results); ok(Int_i(@results.length()) == 1, 60); results = List__new(NULL, 0); @step.axis_namespace(el6, results); ok(Int_i(@results.length()) == 0, 61); results = List__new(NULL, 0); @step.literal = Symbol__new_s("ns2"); @step.axis_namespace(el5, results); ok(Int_i(@results.length()) == 1, 62); // axis_parent results = List__new(NULL, 0); @step.test = test_any; @step.axis_parent(el5, results); ok(Int_i(@results.length()) == 1, 63); results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el2"); @step.axis_parent(el3, results); ok(Int_i(@results.length()) == 1, 64); results = List__new(NULL, 0); @step.axis_parent(el2, results); ok(Int_i(@results.length()) == 0, 65); // axis_preceding results = List__new(NULL, 0); @step.test = test_any; @step.axis_preceding(el6, results); ok(Int_i(@results.length()) == 4, 66); results = List__new(NULL, 0); @step.axis_preceding(el3, results); ok(Int_i(@results.length()) == 0, 67); // axis_preceding_sibling results = List__new(NULL, 0); @step.test = test_any; @step.axis_preceding_sibling(el6, results); ok(Int_i(@results.length()) == 2, 68); results = List__new(NULL, 0); @step.axis_preceding_sibling(el3, results); ok(Int_i(@results.length()) == 0, 69); // axis_self results = List__new(NULL, 0); @step.test = test_any; @step.axis_self(el6, results); ok(Int_i(@results.length()) == 1, 70); results = List__new(NULL, 0); @step.test = test_qname; @step.literal = Symbol__new_s("el4"); @step.axis_self(el6, results); ok(Int_i(@results.length()) == 0, 71); // // cmp_order // ok(Int_i(@step.cmp_order(el1, el1)) == 0, 72); ok(Int_i(@step.cmp_order(el3, el3)) == 0, 73); ok(Int_i(@step.cmp_order(el1, el2)) == -1, 74); ok(Int_i(@step.cmp_order(el2, el1)) == 1, 75); ok(Int_i(@step.cmp_order(el2, el6)) == -1, 76); ok(Int_i(@step.cmp_order(el6, el2)) == 1, 77); @step.test = Symbol__new_s("xyzzy"); Try { @step.test_attribute(el1, @""); ok(0, 78); } Catch(err) { ok(strcmp(String_s(@err.detail), "Called XPath_Step:test_attribute with unknown test xyzzy") == 0, 78); } Try { @step.test_node(el1, @""); ok(0, 79); } Catch(err) { ok(strcmp(String_s(@err.detail), "Called XPath_Step:test_node with unknown test xyzzy") == 0, 79); } return 0; }