ProteoWizard
XMLWriterTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: XMLWriterTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2007 Spielberg Family Center for Applied Proteomics
8 // Cedars-Sinai Medical Center, Los Angeles, California 90048
9 //
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 //
14 // http://www.apache.org/licenses/LICENSE-2.0
15 //
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //
22 
23 
24 #include "XMLWriter.hpp"
27 
28 
29 using namespace pwiz::util;
30 using namespace pwiz::minimxml;
31 
32 
33 ostream* os_ = 0;
34 
35 
36 const char* targetXML =
37  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
38  "<root name1=\"value1\" name2=\"420\" name3=\"0.666\">\n"
39  " <record name=\"nixon\"\n"
40  " color=\"red\"\n"
41  " number=\"37\">\n"
42  " <quote>I'm not a crook.</quote>\n"
43  " <resigned/>\n"
44  " </record>\n"
45  " <record name=\"&quot;Penn &amp; Teller&quot;\">\n"
46  " <quote>'Bull&lt;shit!'</quote>\n"
47  " </record>\n"
48  " <record name=\"clinton\"\n"
49  " color=\"blue\"\n"
50  " number=\"42\">\n"
51  " <quote>I did <em>not</em> have sexual relations with that woman.</quote>\n"
52  " <impeached/>\n"
53  " </record>\n"
54  " <record name=\"bush\"\n"
55  " color=\"red\"\n"
56  " number=\"43\">\n"
57  " <quote>Mission accomplished.</quote>\n"
58  " </record>\n"
59  "</root>\n";
60 
61 
63 {
64  virtual void update(const string& output)
65  {
66  cache += output;
67  }
68 
69  string cache;
70 };
71 
72 
73 void test()
74 {
75  ostringstream oss;
76 
77  TestOutputObserver outputObserver;
78  XMLWriter::Config config;
79  config.indentationStep = 4;
80  config.outputObserver = &outputObserver;
81 
82  XMLWriter writer(oss, config);
83  if (os_) *os_ << "target:\n" << targetXML << endl;
84 
85  unit_assert(writer.position() < 1); // 0 or -1 depending on platform
86 
87  const char* piData = "version=\"1.0\" encoding=\"UTF-8\"";
88  writer.processingInstruction("xml", piData);
89 
90  unit_assert(writer.position() == 39);
91 
92  XMLWriter::Attributes attributes;
93  attributes.push_back(make_pair("name1", "value1"));
94  attributes.push_back(make_pair("name2", "420"));
95  attributes.push_back(make_pair("name3", "0.666"));
96  writer.startElement("root", attributes);
97 
98  unit_assert(writer.position() == 87);
99  unit_assert(writer.positionNext() == 91);
100 
101  attributes.clear();
102  attributes.push_back(make_pair("name", "nixon"));
103  attributes.push_back(make_pair("color", "red"));
104  attributes.push_back(make_pair("number", "37"));
105  writer.pushStyle(XMLWriter::StyleFlag_AttributesOnMultipleLines);
106  writer.startElement("record", attributes);
108  writer.startElement("quote");
109  writer.characters("I'm not a crook.");
110  writer.endElement();
111  writer.startElement("resigned", XMLWriter::Attributes(), XMLWriter::EmptyElement);
112  writer.popStyle();
113  writer.endElement();
114  writer.popStyle();
115 
116  attributes.clear();
117  attributes.push_back(make_pair("name", "\"Penn & Teller\""));
118  writer.startElement("record", attributes);
120  writer.startElement("quote");
121  writer.characters("'Bull<shit!'");
122  writer.endElement();
123  writer.popStyle();
124  writer.endElement();
125 
126  attributes.clear();
127  attributes.push_back(make_pair("name", "clinton"));
128  attributes.push_back(make_pair("color", "blue"));
129  attributes.push_back(make_pair("number", "42"));
130  writer.pushStyle(XMLWriter::StyleFlag_AttributesOnMultipleLines);
131  writer.startElement("record", attributes);
133  writer.startElement("quote");
134  writer.characters("I did ");
136  unit_assert(writer.position() == writer.positionNext()); // no indentation
137  writer.startElement("em");
138  writer.characters("not");
139  writer.endElement();
140  writer.popStyle();
141  writer.characters(" have sexual relations with that woman.");
142  writer.endElement();
143  writer.popStyle();
144  writer.startElement("impeached", XMLWriter::Attributes(), XMLWriter::EmptyElement);
145  writer.endElement();
146  writer.popStyle();
147 
148  attributes.clear();
149  attributes.push_back(make_pair("name", "bush"));
150  attributes.push_back(make_pair("color", "red"));
151  attributes.push_back(make_pair("number", "43"));
152  writer.pushStyle(XMLWriter::StyleFlag_AttributesOnMultipleLines);
153  writer.startElement("record", attributes);
155  writer.startElement("quote");
156  writer.characters("Mission accomplished.");
157  writer.endElement();
158  writer.popStyle();
159  writer.endElement();
160  writer.popStyle();
161 
162  writer.endElement();
163 
164  if (os_) *os_ << "test: (" << oss.str().size() << ")\n" << oss.str() << endl;
165 
166  unit_assert(targetXML == oss.str());
167  unit_assert(writer.position() == (int)oss.str().size());
168  unit_assert(writer.position() == 671);
169 
170  if (os_) *os_ << "outputObserver cache:\n" << outputObserver.cache << endl;
171  unit_assert(targetXML == outputObserver.cache);
172 
173 
174  // test id encoding
175 
176  string id1("1invalid ID");
177  unit_assert(encode_xml_id_copy(id1) == "_x0031_invalid_x0020_ID");
178  unit_assert(&encode_xml_id(id1) == &id1);
179  unit_assert(id1 == "_x0031_invalid_x0020_ID");
180 
181  string id2("_invalid-ID_#2_<3>");
182  unit_assert(encode_xml_id_copy(id2) == "_invalid-ID__x0023_2__x003c_3_x003e_");
183  unit_assert(encode_xml_id(id2) == "_invalid-ID__x0023_2__x003c_3_x003e_");
184 
185  string crazyId("!!!");
186  unit_assert(encode_xml_id(crazyId) == "_x0021__x0021__x0021_");
187 }
188 
190 {
191 #ifndef __APPLE__ // TODO: how to test that this works with Darwin's compiler?
192  // test that subnormal values are clamped and provide 12 decimal places
193  XMLWriter::Attributes attributes;
194  attributes.add("1", 2.2250738585072014e-309);
195  attributes.add("2", -2.2250738585072014e-309);
196  unit_assert_operator_equal(2.225073858507e-308, lexical_cast<double>(attributes[0].second));
197  unit_assert_operator_equal(-2.225073858507e-308, lexical_cast<double>(attributes[1].second));
198 #endif
199 }
200 
201 
202 int main(int argc, const char* argv[])
203 {
204  TEST_PROLOG(argc, argv)
205 
206  try
207  {
208  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
209  test();
211  }
212  catch (exception& e)
213  {
214  TEST_FAILED(e.what())
215  }
216  catch (...)
217  {
218  TEST_FAILED("Caught unknown exception.")
219  }
220 
222 }
223