• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestXPathParse.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/xml/xpath/private/XPathParser.h>
00018 
00019 #include <spl/Debug.h>
00020 
00021 #if defined(DEBUG) || defined(_DEBUG)
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <spl/Log.h>
00026 #include <spl/xml/xpath/private/XPathParser.h>
00027 
00028 static void _TestXPathParse1()
00029 {
00030         XPathParser parser;
00031         DEBUG_CLEAR_MEM_CHECK_POINTS();
00032         parser.CheckMem();
00033         UNIT_ASSERT_MEM_NOTED("_TestPath1 1.0");
00034 
00035         Array<XPathOperatorPtr> prog = parser.Parse("/");
00036         DEBUG_CLEAR_MEM_CHECK_POINTS();
00037         parser.CheckMem();
00038         prog.CheckMem();
00039         UNIT_ASSERT_MEM_NOTED("_TestPath1 1.1");
00040 
00041         prog = parser.Parse("/node1/node2");
00042         DEBUG_CLEAR_MEM_CHECK_POINTS();
00043         parser.CheckMem();
00044         prog.CheckMem();
00045         UNIT_ASSERT_MEM_NOTED("_TestPath1 1.2");
00046 
00047         prog = parser.Parse("node2");
00048         DEBUG_CLEAR_MEM_CHECK_POINTS();
00049         parser.CheckMem();
00050         prog.CheckMem();
00051         UNIT_ASSERT_MEM_NOTED("_TestPath1 1.3");
00052 
00053         prog = parser.Parse("../node2");
00054         DEBUG_CLEAR_MEM_CHECK_POINTS();
00055         parser.CheckMem();
00056         prog.CheckMem();
00057         UNIT_ASSERT_MEM_NOTED("_TestPath1 1.4");
00058 
00059         prog = parser.Parse("//");
00060         DEBUG_CLEAR_MEM_CHECK_POINTS();
00061         parser.CheckMem();
00062         prog.CheckMem();
00063         UNIT_ASSERT_MEM_NOTED("_TestPath1 1.5");
00064 
00065         prog = parser.Parse("//nodex/node2");
00066         DEBUG_CLEAR_MEM_CHECK_POINTS();
00067         parser.CheckMem();
00068         prog.CheckMem();
00069         UNIT_ASSERT_MEM_NOTED("_TestPath1 1.6");
00070 
00071         Log::SWriteOkFail( "XPath path parse" );
00072 }
00073 
00074 static void _TestXPathParseSyntaxError()
00075 {
00076         XPathParser parser;
00077 
00078         try
00079         {
00080                 Array<XPathOperatorPtr> prog = parser.Parse("///");
00081                 UNIT_ASSERT("/// should have thrown an error", false);
00082         }
00083         catch (Exception *ex)
00084         {
00085                 delete ex;
00086         }
00087         DEBUG_CLEAR_MEM_CHECK_POINTS();
00088         parser.CheckMem();
00089         UNIT_ASSERT_MEM_NOTED("_TestXPathParseSyntaxError 1.0");
00090 
00091         try
00092         {
00093                 Array<XPathOperatorPtr> prog = parser.Parse("^");
00094                 UNIT_ASSERT("^ should have thrown an error", false);
00095         }
00096         catch (Exception *ex)
00097         {
00098                 delete ex;
00099         }
00100         DEBUG_CLEAR_MEM_CHECK_POINTS();
00101         parser.CheckMem();
00102         UNIT_ASSERT_MEM_NOTED("_TestXPathParseSyntaxError 1.0");
00103         
00104         try
00105         {
00106                 Array<XPathOperatorPtr> prog = parser.Parse("/bookstore/");
00107                 UNIT_ASSERT("/bookstore/ should have thrown an error", false);
00108         }
00109         catch (Exception *ex)
00110         {
00111                 delete ex;
00112         }
00113 
00114         DEBUG_CLEAR_MEM_CHECK_POINTS();
00115         parser.CheckMem();
00116         UNIT_ASSERT_MEM_NOTED("_TestXPathParseSyntaxError 1.0");
00117         
00118         try
00119         {
00120                 Array<XPathOperatorPtr> prog = parser.Parse("/bookstore//");
00121                 UNIT_ASSERT("/bookstore// should have thrown an error", false);
00122         }
00123         catch (Exception *ex)
00124         {
00125                 delete ex;
00126         }
00127 
00128         DEBUG_CLEAR_MEM_CHECK_POINTS();
00129         parser.CheckMem();
00130         UNIT_ASSERT_MEM_NOTED("_TestXPathParseSyntaxError 1.0");
00131 
00132         Log::SWriteOkFail( "XPath parse syntax errors" );
00133 }
00134 
00135 static void _TestXPathParseW3cSchoolExamples()
00136 {
00137         XPathParser parser;
00138 
00139         parser.Parse("/bookstore/book[1]/title");
00140         DEBUG_CLEAR_MEM_CHECK_POINTS();
00141         parser.CheckMem();
00142         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.0");
00143 
00144         // The following example selects the text from all the price nodes:
00145         parser.Parse("/bookstore/book/price/text()");
00146         DEBUG_CLEAR_MEM_CHECK_POINTS();
00147         parser.CheckMem();
00148         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.1");
00149 
00150         // Select price nodes with price>35
00151         parser.Parse("/bookstore/book[price>35]/price");
00152         DEBUG_CLEAR_MEM_CHECK_POINTS();
00153         parser.CheckMem();
00154         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.2");
00155 
00156         // Select title nodes with price>35
00157         parser.Parse("/bookstore/book[price>35]/title");
00158         DEBUG_CLEAR_MEM_CHECK_POINTS();
00159         parser.CheckMem();
00160         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.3");
00161 
00162         // Selects all book nodes that are children of the current node
00163         parser.Parse("child::book");
00164         DEBUG_CLEAR_MEM_CHECK_POINTS();
00165         parser.CheckMem();
00166         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.4");
00167 
00168         // Selects all children of the current node
00169         parser.Parse("child::*");
00170         DEBUG_CLEAR_MEM_CHECK_POINTS();
00171         parser.CheckMem();
00172         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.5");
00173 
00174         // Selects all text child nodes of the current node
00175         parser.Parse("hild::text()");
00176         DEBUG_CLEAR_MEM_CHECK_POINTS();
00177         parser.CheckMem();
00178         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.6");
00179 
00180         // Selects all book ancestors of the current node - and the current as well if it is a book node
00181         parser.Parse("ancestor-or-self::book");
00182         DEBUG_CLEAR_MEM_CHECK_POINTS();
00183         parser.CheckMem();
00184         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.7");
00185 
00186         // Selects all price grandchildren of the current node
00187         parser.Parse("child::*/child::price");
00188         DEBUG_CLEAR_MEM_CHECK_POINTS();
00189         parser.CheckMem();
00190         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples 1.8");
00191 
00192         Log::SWriteOkFail( "XPath w3c school exmaples parse" );
00193 }
00194 
00195 static void _TestXPathParsePathAttibutes()
00196 {
00197         XPathParser parser;
00198 
00199         parser.Parse("/@");
00200         DEBUG_CLEAR_MEM_CHECK_POINTS();
00201         parser.CheckMem();
00202         UNIT_ASSERT_MEM_NOTED("XPath attributes");
00203         
00204         parser.Parse("//@");
00205         DEBUG_CLEAR_MEM_CHECK_POINTS();
00206         parser.CheckMem();
00207         UNIT_ASSERT_MEM_NOTED("XPath attributes");
00208         
00209         parser.Parse("/bookstore@");
00210         DEBUG_CLEAR_MEM_CHECK_POINTS();
00211         parser.CheckMem();
00212         UNIT_ASSERT_MEM_NOTED("XPath attributes");
00213 
00214         parser.Parse("/bookstore/book@lang");
00215         DEBUG_CLEAR_MEM_CHECK_POINTS();
00216         parser.CheckMem();
00217         UNIT_ASSERT_MEM_NOTED("XPath attributes");
00218 
00219         Log::SWriteOkFail( "XPath attributes" );
00220 }
00221 
00222 void _TestXPathParse(  )
00223 {
00224         _TestXPathParse1();
00225         DEBUG_CLEAR_MEM_CHECK_POINTS();
00226         UNIT_ASSERT_MEM_NOTED("_TestXPathParse");
00227 
00228         _TestXPathParseSyntaxError();
00229         DEBUG_CLEAR_MEM_CHECK_POINTS();
00230         UNIT_ASSERT_MEM_NOTED("_TestXPathParseSyntaxError");
00231 
00232         _TestXPathParseW3cSchoolExamples();
00233         DEBUG_CLEAR_MEM_CHECK_POINTS();
00234         UNIT_ASSERT_MEM_NOTED("_TestXPathParseW3cSchoolExamples");
00235         
00236         _TestXPathParsePathAttibutes();
00237         DEBUG_CLEAR_MEM_CHECK_POINTS();
00238         UNIT_ASSERT_MEM_NOTED("_TestXPathParsePathAttibutes");
00239 }
00240 
00241 #endif