ProteoWizard
Classes | Functions | Variables
SAXParserTest.cpp File Reference
#include "pwiz/utility/misc/unit.hpp"
#include "SAXParser.hpp"
#include "pwiz/utility/misc/Std.hpp"
#include "pwiz/utility/misc/Filesystem.hpp"
#include <cstring>

Go to the source code of this file.

Classes

struct  PrintAttribute
 
class  PrintEventHandler
 
struct  First
 
struct  Second
 
struct  Fifth
 
struct  Root
 
class  FirstHandler
 
class  SecondHandler
 
class  FifthHandler
 
class  RootHandler
 
class  AnotherRootHandler
 
struct  NestedHandler
 

Functions

void demo ()
 
void readAttribute (const Handler::Attributes &attributes, const string &attributeName, string &result)
 
void test ()
 
void testNoAutoUnescape ()
 
void testDone ()
 
void testBadXML ()
 
void testNested ()
 
void testRootElement ()
 
void testDecoding ()
 
void testSaxParserString ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_
 
const char * sampleXML
 

Function Documentation

§ demo()

void demo ( )

Definition at line 120 of file SAXParserTest.cpp.

References os_, pwiz::msdata::id::parse(), and sampleXML.

Referenced by main().

121 {
122  if (os_)
123  {
124  *os_ << "sampleXML:\n" << sampleXML << endl;
125 
126  istringstream is(sampleXML);
127  PrintEventHandler handler(*os_);
128 
129  *os_ << "first parse events:\n";
130  parse(is, handler);
131  *os_ << endl;
132 
133  *os_ << "second parse events:\n";
134  parse(is, handler);
135  *os_ << endl;
136  }
137 }
const char * sampleXML
PWIZ_API_DECL std::map< std::string, std::string > parse(const std::string &id)
parses an id string into a map<string,string>
ostream * os_

§ readAttribute()

void readAttribute ( const Handler::Attributes attributes,
const string &  attributeName,
string &  result 
)

Definition at line 182 of file SAXParserTest.cpp.

References pwiz::minimxml::SAXParser::Handler::Attributes::end(), and pwiz::minimxml::SAXParser::Handler::Attributes::find().

Referenced by FirstHandler::startElement(), SecondHandler::startElement(), and RootHandler::startElement().

185 {
186  Handler::Attributes::attribute_list::const_iterator it = attributes.find(attributeName);
187  if (it != attributes.end())
188  result = it->getValue();
189 }
attribute_list::const_iterator end() const
Definition: SAXParser.hpp:532
attribute_list::const_iterator find(const std::string &name) const
Definition: SAXParser.hpp:537

§ test()

void test ( )

Definition at line 367 of file SAXParserTest.cpp.

References First::escaped_attribute, Root::fifth, Root::first, Fifth::leeloo, Fifth::mr_zorg, os_, Root::param, Second::param2, Second::param3, pwiz::msdata::id::parse(), sampleXML, Root::second, First::text, Second::text, and unit_assert_operator_equal.

Referenced by main().

368 {
369  if (os_) *os_ << "test()\n";
370 
371  istringstream is(sampleXML);
372  Root root;
373  RootHandler rootHandler(root);
374  parse(is, rootHandler);
375 
376  if (os_)
377  {
378  *os_ << "root.param: " << root.param << endl
379  << "first.escaped_attribute: " << root.first.escaped_attribute << endl
380  << "first.text: " << root.first.text << endl
381  << "second.param2: " << root.second.param2 << endl
382  << "second.param3: " << root.second.param3 << endl
383  << "second.text: ";
384  copy(root.second.text.begin(), root.second.text.end(), ostream_iterator<string>(*os_,"|"));
385  *os_ << "\nfifth.leeloo: " << root.fifth.leeloo << endl
386  << "fifth.mr_zorg: " << root.fifth.mr_zorg << endl
387  << "\n";
388  }
389 
390  unit_assert_operator_equal("value", root.param);
392  unit_assert_operator_equal("Some Text with Entity References: <&>", root.first.text);
393  unit_assert_operator_equal("something", root.second.param2);
394  unit_assert_operator_equal("something.else 1234-56", root.second.param3);
395  unit_assert_operator_equal(4, root.second.text.size());
396  unit_assert_operator_equal("Pre-Text", root.second.text[0]);
397  unit_assert_operator_equal("Inlined text with", root.second.text[1]);
398  unit_assert_operator_equal("<&\">", root.second.text[2]);
399  unit_assert_operator_equal("Post-text.", root.second.text[3]);
400  unit_assert_operator_equal(">Leeloo > mul-\"tipass", root.fifth.leeloo);
401  unit_assert_operator_equal("You're a monster, Zorg.>I know.", root.fifth.mr_zorg);
402 }
string escaped_attribute
const char * sampleXML
string param3
Fifth fifth
Second second
string param2
PWIZ_API_DECL std::map< std::string, std::string > parse(const std::string &id)
parses an id string into a map<string,string>
string leeloo
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
string mr_zorg
string param
string text
vector< string > text
ostream * os_
First first

§ testNoAutoUnescape()

void testNoAutoUnescape ( )

Definition at line 405 of file SAXParserTest.cpp.

References First::escaped_attribute, Root::first, os_, Root::param, Second::param2, Second::param3, pwiz::msdata::id::parse(), sampleXML, Root::second, First::text, Second::text, and unit_assert_operator_equal.

Referenced by main().

406 {
407  if (os_) *os_ << "testNoAutoUnescape()\n";
408 
409  istringstream is(sampleXML);
410  Root root;
411  RootHandler rootHandler(root, false, false);
412  parse(is, rootHandler);
413 
414  if (os_)
415  {
416  *os_ << "root.param: " << root.param << endl
417  << "first.escaped_attribute: " << root.first.escaped_attribute << endl
418  << "first.text: " << root.first.text << endl
419  << "second.param2: " << root.second.param2 << endl
420  << "second.param3: " << root.second.param3 << endl
421  << "second.text: ";
422  copy(root.second.text.begin(), root.second.text.end(), ostream_iterator<string>(*os_,"|"));
423  *os_ << "\n\n";
424  }
425 
426  unit_assert_operator_equal("value", root.param);
427  unit_assert_operator_equal("&quot;&lt;&amp;lt;&gt;&quot;", root.first.escaped_attribute);
428  unit_assert_operator_equal("Some Text with Entity References: &lt;&amp;&gt;", root.first.text);
429  unit_assert_operator_equal("something", root.second.param2);
430  unit_assert_operator_equal("something.else 1234-56", root.second.param3);
431  unit_assert_operator_equal(4, root.second.text.size());
432  unit_assert_operator_equal("Pre-Text", root.second.text[0]);
433  unit_assert_operator_equal("Inlined text with", root.second.text[1]);
434  unit_assert_operator_equal("<&\">", root.second.text[2]);
435  unit_assert_operator_equal("Post-text.", root.second.text[3]);
436 }
string escaped_attribute
const char * sampleXML
string param3
Second second
string param2
PWIZ_API_DECL std::map< std::string, std::string > parse(const std::string &id)
parses an id string into a map<string,string>
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
string param
string text
vector< string > text
ostream * os_
First first

§ testDone()

void testDone ( )

Definition at line 458 of file SAXParserTest.cpp.

References os_, pwiz::msdata::id::parse(), sampleXML, and unit_assert_operator_equal.

Referenced by main().

459 {
460  if (os_) *os_ << "testDone()\n";
461 
462  istringstream is(sampleXML);
463  AnotherRootHandler handler;
464  parse(is, handler); // parses <RootElement> ... </RootElement>
465  parse(is, handler); // parses <AnotherRootElement> and aborts
466 
467  string buffer;
468  getline(is, buffer, '<');
469 
470  if (os_) *os_ << "buffer: " << buffer << "\n\n";
471  unit_assert_operator_equal("The quick brown fox jumps over the lazy dog.", buffer);
472 }
const char * sampleXML
PWIZ_API_DECL std::map< std::string, std::string > parse(const std::string &id)
parses an id string into a map<string,string>
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
ostream * os_

§ testBadXML()

void testBadXML ( )

Definition at line 475 of file SAXParserTest.cpp.

References os_, and pwiz::msdata::id::parse().

Referenced by main().

476 {
477  if (os_) *os_ << "testBadXML()\n";
478 
479  const char* bad = "<A><B></A></B>";
480  istringstream is(bad);
481  Handler handler;
482 
483  try
484  {
485  parse(is, handler);
486  }
487  catch (exception& e)
488  {
489  if (os_) *os_ << e.what() << "\nOK: Parser caught bad XML.\n\n";
490  return;
491  }
492 
493  throw runtime_error("Parser failed to catch bad XML.");
494 }
SAX event handler interface.
Definition: SAXParser.hpp:315
PWIZ_API_DECL std::map< std::string, std::string > parse(const std::string &id)
parses an id string into a map<string,string>
ostream * os_

§ testNested()

void testNested ( )

Definition at line 510 of file SAXParserTest.cpp.

References NestedHandler::count, os_, pwiz::msdata::id::parse(), and unit_assert_operator_equal.

Referenced by main().

511 {
512  if (os_) *os_ << "testNested()\n";
513  const char* nested = "<a><a></a></a>";
514  istringstream is(nested);
515 
516  NestedHandler nestedHandler;
517  parse(is, nestedHandler);
518  if (os_) *os_ << "count: " << nestedHandler.count << "\n\n";
519  unit_assert_operator_equal(2, nestedHandler.count);
520 }
PWIZ_API_DECL std::map< std::string, std::string > parse(const std::string &id)
parses an id string into a map<string,string>
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
ostream * os_

§ testRootElement()

void testRootElement ( )

Definition at line 523 of file SAXParserTest.cpp.

References os_, sampleXML, unit_assert_operator_equal, unit_assert_throws, pwiz::minimxml::xml_root_element(), and pwiz::minimxml::xml_root_element_from_file().

Referenced by main().

524 {
525  if (os_) *os_ << "testRootElement()\n";
526 
527  string RootElement = "RootElement";
529 
530  istringstream sampleXMLStream(sampleXML);
531  unit_assert_operator_equal(RootElement, xml_root_element(sampleXMLStream));
532 
533  {ofstream sampleXMLFile("testRootElement.xml"); sampleXMLFile << sampleXML;}
534  unit_assert_operator_equal(RootElement, xml_root_element_from_file("testRootElement.xml"));
535  bfs::remove("testRootElement.xml");
536 
537  unit_assert_operator_equal(RootElement, xml_root_element("<?xml?><RootElement>"));
538  unit_assert_operator_equal(RootElement, xml_root_element("<?xml?><RootElement name='value'"));
539 
540  unit_assert_throws(xml_root_element("not-xml"), runtime_error);
541 }
const char * sampleXML
PWIZ_API_DECL std::string xml_root_element_from_file(const std::string &filepath)
Returns the root element from an XML file; throws runtime_error if no element is found.
#define unit_assert_throws(x, exception)
Definition: unit.hpp:106
PWIZ_API_DECL std::string xml_root_element(const std::string &fileheader)
Returns the root element from an XML buffer; throws runtime_error if no element is found...
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
ostream * os_

§ testDecoding()

void testDecoding ( )

Definition at line 544 of file SAXParserTest.cpp.

References pwiz::minimxml::decode_xml_id(), pwiz::minimxml::decode_xml_id_copy(), and unit_assert_operator_equal.

Referenced by main().

545 {
546  string id1("_x0031_invalid_x0020_ID");
547  unit_assert_operator_equal("1invalid ID", decode_xml_id_copy(id1));
548  unit_assert_operator_equal((void *)&id1, (void *)&decode_xml_id(id1)); // should return reference to id1
549  unit_assert_operator_equal("1invalid ID", id1);
550 
551  string id2("_invalid-ID__x0023_2__x003c_3_x003e_");
552  unit_assert_operator_equal("_invalid-ID_#2_<3>", decode_xml_id_copy(id2));
553  unit_assert_operator_equal("_invalid-ID_#2_<3>", decode_xml_id(id2));
554 
555  string crazyId("_x0021__x0021__x0021_");
556  unit_assert_operator_equal("!!!", decode_xml_id(crazyId));
557 }
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
PWIZ_API_DECL std::string & decode_xml_id(std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.
PWIZ_API_DECL std::string decode_xml_id_copy(const std::string &str)
Decodes any characters encoded with their hexadecimal value, e.g.

§ testSaxParserString()

void testSaxParserString ( )

Definition at line 559 of file SAXParserTest.cpp.

References pwiz::minimxml::SAXParser::saxstring::c_str(), pwiz::minimxml::SAXParser::saxstring::data(), pwiz::minimxml::SAXParser::saxstring::length(), pwiz::minimxml::SAXParser::saxstring::trim_lead_ws(), pwiz::minimxml::SAXParser::saxstring::trim_trail_ws(), and unit_assert_operator_equal.

Referenced by main().

560 {
561  std::string str = " \t foo \n";
562  saxstring xstr = str;
563  unit_assert_operator_equal(xstr,str);
564  unit_assert_operator_equal(xstr,str.c_str());
565  unit_assert_operator_equal(str.length(),xstr.length());
566  xstr.trim_lead_ws();
567  unit_assert_operator_equal(xstr.length(),str.length()-3);
568  unit_assert_operator_equal(xstr,str.substr(3));
569  xstr.trim_trail_ws();
570  unit_assert_operator_equal(xstr.length(),str.length()-5);
571  unit_assert_operator_equal(xstr,str.substr(3,3));
572  unit_assert_operator_equal(xstr[1],'o');
573  xstr[1] = '0';
574  unit_assert_operator_equal(xstr[1],'0');
575  std::string str2(xstr.data());
576  unit_assert_operator_equal(str2,"f0o");
577  std::string str3(xstr.c_str());
578  unit_assert_operator_equal(str2,str3);
579  saxstring xstr2(xstr);
580  unit_assert_operator_equal(xstr2,xstr);
581  saxstring xstr3;
582  unit_assert_operator_equal(xstr3.c_str(),std::string());
583 }
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92

§ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 585 of file SAXParserTest.cpp.

References demo(), os_, test(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testBadXML(), testDecoding(), testDone(), testNested(), testNoAutoUnescape(), testRootElement(), and testSaxParserString().

586 {
587  TEST_PROLOG(argc, argv)
588 
589  try
590  {
591  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
592  demo();
594  test();
596  testDone();
597  testBadXML();
598  testNested();
599  testRootElement();
600  testDecoding();
601  }
602  catch (exception& e)
603  {
604  TEST_FAILED(e.what())
605  }
606  catch (...)
607  {
608  TEST_FAILED("Caught unknown exception.")
609  }
610 
612 }
void test()
void testDecoding()
void testRootElement()
void testDone()
#define TEST_EPILOG
Definition: unit.hpp:182
void testSaxParserString()
void testBadXML()
ostream * os_
#define TEST_FAILED(x)
Definition: unit.hpp:176
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174
void testNested()
void testNoAutoUnescape()
void demo()

Variable Documentation

§ os_

ostream* os_

§ sampleXML

const char* sampleXML
Initial value:
=
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE foo>\n"
"<RootElement param=\"value\">\n"
" <FirstElement escaped_attribute=\"&quot;&lt;&amp;lt;&gt;&quot;\">\n"
" Some Text with Entity References: &lt;&amp;&gt;\n"
" </FirstElement>\n"
" <SecondElement param2=\"something\" param3=\"something.else 1234-56\">\n"
" Pre-Text <Inline>Inlined text with <![CDATA[<&\">]]></Inline> Post-text. <br/>\n"
" </SecondElement>\n"
" <prefix:ThirdElement goober:name=\"value\">\n"
" <!--this is a comment-->\n"
" <empty_with_space />\n"
" </prefix:ThirdElement>\n"
" <FifthElement leeloo='>Leeloo > mul-\"tipass'>\n"
" You're a monster, Zorg.>I know.\n"
" </FifthElement>\n"
"</RootElement>\n"
"<AnotherRoot>The quick brown fox jumps over the lazy dog.</AnotherRoot>\n"

Definition at line 39 of file SAXParserTest.cpp.

Referenced by demo(), test(), testDone(), testNoAutoUnescape(), and testRootElement().