ProteoWizard
XMLWriterTest.cpp
Go to the documentation of this file.
1//
2// $Id$
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
29using namespace pwiz::util;
30using namespace pwiz::minimxml;
31
32
33ostream* os_ = 0;
34
35
36const 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
73void 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);
107 writer.pushStyle(XMLWriter::StyleFlag_InlineInner);
108 writer.startElement("quote");
109 writer.characters("I'm not a crook.");
110 writer.endElement();
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);
119 writer.pushStyle(XMLWriter::StyleFlag_InlineInner);
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);
132 writer.pushStyle(XMLWriter::StyleFlag_InlineInner);
133 writer.startElement("quote");
134 writer.characters("I did ");
135 writer.pushStyle(XMLWriter::StyleFlag_Inline);
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();
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);
154 writer.pushStyle(XMLWriter::StyleFlag_InlineInner);
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
202int main(int argc, 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
int main(int argc, char *argv[])
void testNormalization()
const char * targetXML
ostream * os_
void test()
vector of name/value pairs to be written as XML attributes
Definition XMLWriter.hpp:82
void add(const std::string &name, const double &value)
interface to allow outside observation of data sent to output stream
Definition XMLWriter.hpp:62
The XMLWriter class provides simple, tag-level XML syntax writing.
Definition XMLWriter.hpp:48
void characters(const std::string &text, bool autoEscape=true)
writes character data; autoEscape writes reserved XML characters in the input text in their escaped f...
void startElement(const std::string &name, const Attributes &attributes=Attributes(), EmptyElementTag emptyElementTag=NotEmptyElement)
writes element start tag
void endElement()
writes element end tag
stream_offset positionNext() const
returns stream position of next element start tag
void popStyle()
pops the style stack
void processingInstruction(const std::string &name, const std::string &data)
writes a processing instruction
stream_offset position() const
returns current stream position
void pushStyle(unsigned int flags)
pushes style flags onto the internal style stack
PWIZ_API_DECL std::string encode_xml_id_copy(const std::string &str)
Encodes any characters not suitable in an xml:ID or xml:IDREF with their hexadecimal value,...
PWIZ_API_DECL std::string & encode_xml_id(std::string &str)
Encodes any characters not suitable in an xml:ID or xml:IDREF with their hexadecimal value,...
virtual void update(const string &output)
initial configuration of the XMLWriter
Definition XMLWriter.hpp:70
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define unit_assert_operator_equal(expected, actual)
Definition unit.hpp:92
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175