Commit cdb8fe7d827842c31f682008539c1e21464fce9e
1 parent
9219f873
Handle pre-defined entities in XML module.
The XML module now handles predefined entities. Special characters, such as " will now be transformed into their predefined entities, e.g. ", when writing the XML file. Upon reading, predefined entities will be converted into special chacaters, so that the user always deals with a human readable string.
Showing
9 changed files
with
284 additions
and
48 deletions
ChangeLog
| ... | ... | @@ -16,6 +16,7 @@ |
| 16 | 16 | Add utc() methods to GTime class |
| 17 | 17 | Add hierachical element access methods to GXml and GXmlNode |
| 18 | 18 | Add table creation and saving methods to GCsv class |
| 19 | + Add predefined entity support to XML module | |
| 19 | 20 | Read/write altitude and azimuth attributes of CTA data |
| 20 | 21 | Refactor CTA response class, add GCTAResponseCube class |
| 21 | 22 | Improve CTA response computation for extended models | ... | ... |
NEWS
| ... | ... | @@ -163,6 +163,9 @@ array values. |
| 163 | 163 | Methods have been added to the GCsv class that allow the creation and the |
| 164 | 164 | saving of CSV tables. |
| 165 | 165 | |
| 166 | +The str2xml() and xml2str() functions have been added to GTools to handle | |
| 167 | +predefined entities in XML files. | |
| 168 | + | |
| 166 | 169 | |
| 167 | 170 | 14. XML module |
| 168 | 171 | -------------- |
| ... | ... | @@ -174,6 +177,12 @@ in case that multiple elements with the same name exist. If squared brackets |
| 174 | 177 | are omitted, an index of 0 is assumed. If the indicated element does not |
| 175 | 178 | exist, the method returns a pointer to NULL. |
| 176 | 179 | |
| 180 | +The XML module now handles predefined entities. Special characters, such as | |
| 181 | +" will now be transformed into their predefined entities, e.g. ", when | |
| 182 | +writing the XML file. Upon reading, predefined entities will be converted | |
| 183 | +into special chacaters, so that the user always deals with a human readable | |
| 184 | +string. | |
| 185 | + | |
| 177 | 186 | |
| 178 | 187 | 15. VO module |
| 179 | 188 | ------------- | ... | ... |
include/GTools.hpp
| ... | ... | @@ -105,8 +105,11 @@ namespace gammalib { |
| 105 | 105 | const std::string& message); |
| 106 | 106 | GXmlElement* parameter(GXmlElement& xml, |
| 107 | 107 | const std::string& name); |
| 108 | + std::string xml2str(const std::string& arg); | |
| 109 | + std::string str2xml(const std::string& arg); | |
| 108 | 110 | } |
| 109 | 111 | |
| 112 | + | |
| 110 | 113 | /***********************************************************************//** |
| 111 | 114 | * @brief Signal if argument is infinite |
| 112 | 115 | * | ... | ... |
pyext/GTools.i
| ... | ... | @@ -45,29 +45,6 @@ namespace gammalib { |
| 45 | 45 | std::string expand_env(const std::string& arg); |
| 46 | 46 | std::string filepath(const std::string& pathname, |
| 47 | 47 | const std::string& filename); |
| 48 | -/* | |
| 49 | - std::string str(const unsigned short int& value); | |
| 50 | - std::string str(const unsigned int& value); | |
| 51 | - std::string str(const unsigned long int& value); | |
| 52 | - std::string str(const unsigned long long int& value); | |
| 53 | - std::string str(const short int& value); | |
| 54 | - std::string str(const int& value); | |
| 55 | - std::string str(const long int& value); | |
| 56 | - std::string str(const long long int& value); | |
| 57 | - std::string str(const float& value); | |
| 58 | - std::string str(const double& value); | |
| 59 | - char* tochar(const std::string& arg); | |
| 60 | - short toshort(const std::string& arg); | |
| 61 | - unsigned short toushort(const std::string& arg); | |
| 62 | - int toint(const std::string& arg); | |
| 63 | - unsigned int touint(const std::string& arg); | |
| 64 | - long tolong(const std::string& arg); | |
| 65 | - unsigned long toulong(const std::string& arg); | |
| 66 | - long long tolonglong(const std::string& arg); | |
| 67 | - unsigned long long toulonglong(const std::string& arg); | |
| 68 | - float tofloat(const std::string& arg); | |
| 69 | - double todouble(const std::string& arg); | |
| 70 | -*/ | |
| 71 | 48 | std::string toupper(const std::string& s); |
| 72 | 49 | std::string tolower(const std::string& s); |
| 73 | 50 | std::vector<std::string> split(const std::string& s, const std::string& sep); |
| ... | ... | @@ -84,8 +61,17 @@ namespace gammalib { |
| 84 | 61 | const double& emax, |
| 85 | 62 | const double& epivot, |
| 86 | 63 | const double& gamma); |
| 64 | + GEnergy elogmean(const GEnergy& a, const GEnergy& b); | |
| 87 | 65 | bool file_exists(const std::string& filename); |
| 88 | 66 | bool dir_exists(const std::string& dirname); |
| 89 | 67 | bool is_infinite(const double& x); |
| 90 | 68 | bool is_notanumber(const double& x); |
| 69 | + bool contains(const std::string& str, | |
| 70 | + const std::string& substring); | |
| 71 | + void warning(const std::string& origin, | |
| 72 | + const std::string& message); | |
| 73 | + GXmlElement* parameter(GXmlElement& xml, | |
| 74 | + const std::string& name); | |
| 75 | + std::string xml2str(const std::string& arg); | |
| 76 | + std::string str2xml(const std::string& arg); | |
| 91 | 77 | } | ... | ... |
src/support/GTools.cpp
| ... | ... | @@ -39,9 +39,13 @@ |
| 39 | 39 | #include <sstream> |
| 40 | 40 | #include <algorithm> |
| 41 | 41 | #include "GTools.hpp" |
| 42 | +#include "GException.hpp" | |
| 42 | 43 | |
| 43 | 44 | /* __ Compile options ____________________________________________________ */ |
| 44 | 45 | |
| 46 | +/* __ Function name definitions __________________________________________ */ | |
| 47 | +#define G_XML2STRING "gammalib::xml2string(std::string&)" | |
| 48 | + | |
| 45 | 49 | /* __ Coding definitions _________________________________________________ */ |
| 46 | 50 | #define G_PARFORMAT_LENGTH 29 |
| 47 | 51 | |
| ... | ... | @@ -1017,3 +1021,184 @@ GXmlElement* gammalib::parameter(GXmlElement& xml, const std::string& name) |
| 1017 | 1021 | // Return |
| 1018 | 1022 | return par; |
| 1019 | 1023 | } |
| 1024 | + | |
| 1025 | + | |
| 1026 | +/***********************************************************************//** | |
| 1027 | + * @brief Convert XML character references in string to characters | |
| 1028 | + * | |
| 1029 | + * @param[in] arg String containing XML character references. | |
| 1030 | + * @return String with character reference replaced by respective characters. | |
| 1031 | + * | |
| 1032 | + * Converts all character references found in a string in their respective | |
| 1033 | + * characters. For more information about XML character references read | |
| 1034 | + * http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references | |
| 1035 | + ***************************************************************************/ | |
| 1036 | +std::string gammalib::xml2str(const std::string& arg) | |
| 1037 | +{ | |
| 1038 | + // Initialise string | |
| 1039 | + std::string result; | |
| 1040 | + | |
| 1041 | + // Iitialise position, lenghts and flags | |
| 1042 | + size_t length = arg.length(); | |
| 1043 | + size_t pos = 0; | |
| 1044 | + size_t start = 0; | |
| 1045 | + size_t stop = 0; | |
| 1046 | + size_t len = 0; // Length of string preceeding char. reference | |
| 1047 | + bool found = false; | |
| 1048 | + | |
| 1049 | + // Loop over string | |
| 1050 | + while (pos < length) { | |
| 1051 | + | |
| 1052 | + // If we have not yet found a character reference then search for | |
| 1053 | + // the next one. If we do not find one we break here. | |
| 1054 | + if (!found) { | |
| 1055 | + start = arg.find("&", pos); | |
| 1056 | + if (start != std::string::npos) { | |
| 1057 | + len = start - pos; | |
| 1058 | + pos = start; | |
| 1059 | + found = true; | |
| 1060 | + } | |
| 1061 | + else { | |
| 1062 | + break; | |
| 1063 | + } | |
| 1064 | + } | |
| 1065 | + | |
| 1066 | + // ... otherwise search for end of the actual character reference. | |
| 1067 | + // Throw an exception if no end is found. | |
| 1068 | + else { | |
| 1069 | + stop = arg.find(";", pos); | |
| 1070 | + if (stop != std::string::npos) { | |
| 1071 | + | |
| 1072 | + // First attach leading string to the result | |
| 1073 | + if (len > 0) { | |
| 1074 | + result += arg.substr(start-len, len); | |
| 1075 | + } | |
| 1076 | + | |
| 1077 | + // Next extract character reference | |
| 1078 | + std::string cref = arg.substr(start+1, stop-start-1); | |
| 1079 | + len = cref.length(); | |
| 1080 | + | |
| 1081 | + // Check for a numerical character reference | |
| 1082 | + //TODO: Check that there are only valid characters in | |
| 1083 | + // numerical field | |
| 1084 | + if (len >= 2 && cref[0] == '#') { | |
| 1085 | + int number = -1; | |
| 1086 | + if (cref[1] == 'x') { | |
| 1087 | + number = (int)std::strtol(cref.substr(2,len-2).c_str(), NULL, 16); | |
| 1088 | + } | |
| 1089 | + else { | |
| 1090 | + number = toint(cref.substr(1,len-1)); | |
| 1091 | + } | |
| 1092 | + if (number != -1) { | |
| 1093 | + result.push_back((char)number); | |
| 1094 | + } | |
| 1095 | + else { | |
| 1096 | + std::string msg = "Could not extract number from " | |
| 1097 | + "numerical character reference &"+ | |
| 1098 | + cref+";"; | |
| 1099 | + throw GException::invalid_argument(G_XML2STRING, msg); | |
| 1100 | + } | |
| 1101 | + } | |
| 1102 | + | |
| 1103 | + // ... otherwise check for a character entity reference | |
| 1104 | + // and push back the corresponding character to the result | |
| 1105 | + // string | |
| 1106 | + else { | |
| 1107 | + if (cref == "quot") { | |
| 1108 | + result.push_back((char)34); | |
| 1109 | + } | |
| 1110 | + else if (cref == "amp") { | |
| 1111 | + result.push_back((char)38); | |
| 1112 | + } | |
| 1113 | + else if (cref == "apos") { | |
| 1114 | + result.push_back((char)39); | |
| 1115 | + } | |
| 1116 | + else if (cref == "lt") { | |
| 1117 | + result.push_back((char)60); | |
| 1118 | + } | |
| 1119 | + else if (cref == "gt") { | |
| 1120 | + result.push_back((char)62); | |
| 1121 | + } | |
| 1122 | + else { | |
| 1123 | + std::string msg = "Unknown character entity reference " | |
| 1124 | + "&"+cref+"; encountered in XML string \""+ | |
| 1125 | + arg+"\"."; | |
| 1126 | + throw GException::invalid_argument(G_XML2STRING, msg); | |
| 1127 | + } | |
| 1128 | + } | |
| 1129 | + | |
| 1130 | + // Signal that we're done and that we search for the | |
| 1131 | + // next character reference | |
| 1132 | + found = false; | |
| 1133 | + pos = stop + 1; | |
| 1134 | + | |
| 1135 | + } // endif: end of character reference found | |
| 1136 | + | |
| 1137 | + // ... otherwise throw an exception | |
| 1138 | + else { | |
| 1139 | + std::string msg = "Missing ; character at end of character " | |
| 1140 | + "reference in XML string \""+arg+"\"."; | |
| 1141 | + throw GException::invalid_argument(G_XML2STRING, msg); | |
| 1142 | + } | |
| 1143 | + | |
| 1144 | + } // endelse: search for end of character reference | |
| 1145 | + | |
| 1146 | + } // endwhile | |
| 1147 | + | |
| 1148 | + // Append any pending string to the result | |
| 1149 | + if (pos < length) { | |
| 1150 | + len = length - pos; | |
| 1151 | + result += arg.substr(pos, len); | |
| 1152 | + } | |
| 1153 | + | |
| 1154 | + // Return result | |
| 1155 | + return result; | |
| 1156 | +} | |
| 1157 | + | |
| 1158 | + | |
| 1159 | +/***********************************************************************//** | |
| 1160 | + * @brief Convert special characters in string to XML character references | |
| 1161 | + * | |
| 1162 | + * @param[in] arg String. | |
| 1163 | + * @return String with special characters replaced by character references. | |
| 1164 | + * | |
| 1165 | + * Converts all special characters found in a string into character | |
| 1166 | + * references. For more information about XML character references read | |
| 1167 | + * http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references | |
| 1168 | + ***************************************************************************/ | |
| 1169 | +std::string gammalib::str2xml(const std::string& arg) | |
| 1170 | +{ | |
| 1171 | + // Initialise string | |
| 1172 | + std::string result; | |
| 1173 | + | |
| 1174 | + // Loop over string | |
| 1175 | + for (int i = 0; i < arg.length(); ++i) { | |
| 1176 | + | |
| 1177 | + // Initialize string to add | |
| 1178 | + std::string character = arg.substr(i, 1); | |
| 1179 | + | |
| 1180 | + // Replace special characters | |
| 1181 | + if (character == "\"") { | |
| 1182 | + character = """; | |
| 1183 | + } | |
| 1184 | + else if (character == "&") { | |
| 1185 | + character = "&"; | |
| 1186 | + } | |
| 1187 | + else if (character == "'") { | |
| 1188 | + character = "'"; | |
| 1189 | + } | |
| 1190 | + else if (character == "<") { | |
| 1191 | + character = "<"; | |
| 1192 | + } | |
| 1193 | + else if (character == ">") { | |
| 1194 | + character = ">"; | |
| 1195 | + } | |
| 1196 | + | |
| 1197 | + // Append character | |
| 1198 | + result += character; | |
| 1199 | + | |
| 1200 | + } | |
| 1201 | + | |
| 1202 | + // Return result | |
| 1203 | + return result; | |
| 1204 | +} | ... | ... |
src/xml/GXmlAttribute.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GXmlAttribute.cpp - XML attribute class implementation * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2014 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -29,6 +29,7 @@ |
| 29 | 29 | #include <config.h> |
| 30 | 30 | #endif |
| 31 | 31 | #include "GException.hpp" |
| 32 | +#include "GTools.hpp" | |
| 32 | 33 | #include "GXmlAttribute.hpp" |
| 33 | 34 | |
| 34 | 35 | /* __ Method name definitions ____________________________________________ */ |
| ... | ... | @@ -84,7 +85,9 @@ GXmlAttribute::GXmlAttribute(const GXmlAttribute& attr) |
| 84 | 85 | * @param[in] name Attribute name. |
| 85 | 86 | * @param[in] value Attribute value. |
| 86 | 87 | * |
| 87 | - * Construct attribute form a p@ name and a @p value. | |
| 88 | + * Construct attribute form a p@ name and a @p value. Predefined entities | |
| 89 | + * (e.g. ") in attribute values are automatically converted into normal | |
| 90 | + * characters. | |
| 88 | 91 | ***************************************************************************/ |
| 89 | 92 | GXmlAttribute::GXmlAttribute(const std::string& name, const std::string& value) |
| 90 | 93 | { |
| ... | ... | @@ -93,7 +96,7 @@ GXmlAttribute::GXmlAttribute(const std::string& name, const std::string& value) |
| 93 | 96 | |
| 94 | 97 | // Set attribute |
| 95 | 98 | this->name(name); |
| 96 | - this->value(value); | |
| 99 | + this->value(gammalib::xml2str(value)); | |
| 97 | 100 | |
| 98 | 101 | // Return |
| 99 | 102 | return; |
| ... | ... | @@ -187,12 +190,19 @@ GXmlAttribute* GXmlAttribute::clone(void) const |
| 187 | 190 | * |
| 188 | 191 | * @param[in] url Unified Resource Locator. |
| 189 | 192 | * |
| 190 | - * Writes the element attribute into the @p url. | |
| 193 | + * Writes the element attribute into the @p url. Special characters are | |
| 194 | + * automatically transformed into predefined entities (e.g. "). | |
| 191 | 195 | ***************************************************************************/ |
| 192 | 196 | void GXmlAttribute::write(GUrl& url) const |
| 193 | 197 | { |
| 198 | + // Get value without quotation marks | |
| 199 | + std::string value = m_value.substr(1, m_value.length()-2); | |
| 200 | + | |
| 201 | + // Convert value to XML format | |
| 202 | + value = "\""+gammalib::str2xml(value)+"\""; | |
| 203 | + | |
| 194 | 204 | // Write attribute into URL |
| 195 | - url.printf(" %s=%s", m_name.c_str(), m_value.c_str()); | |
| 205 | + url.printf(" %s=%s", m_name.c_str(), value.c_str()); | |
| 196 | 206 | |
| 197 | 207 | // Return |
| 198 | 208 | return; | ... | ... |
src/xml/GXmlText.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GXmlText.cpp - XML text node class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2013 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2014 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -82,7 +82,8 @@ GXmlText::GXmlText(const GXmlText& node) : GXmlNode(node) |
| 82 | 82 | * |
| 83 | 83 | * @param[in] text Text string. |
| 84 | 84 | * |
| 85 | - * Construct object by attributing a text string. | |
| 85 | + * Construct object by attributing a text string. Predefined entities | |
| 86 | + * (e.g. ") are automatically converted into normal characters. | |
| 86 | 87 | ***************************************************************************/ |
| 87 | 88 | GXmlText::GXmlText(const std::string& text) : GXmlNode() |
| 88 | 89 | { |
| ... | ... | @@ -90,7 +91,7 @@ GXmlText::GXmlText(const std::string& text) : GXmlNode() |
| 90 | 91 | init_members(); |
| 91 | 92 | |
| 92 | 93 | // Set text |
| 93 | - m_text = text; | |
| 94 | + m_text = gammalib::xml2str(text); | |
| 94 | 95 | |
| 95 | 96 | // Return |
| 96 | 97 | return; |
| ... | ... | @@ -190,12 +191,13 @@ GXmlText* GXmlText::clone(void) const |
| 190 | 191 | * @param[in] url Unified Resource Locator. |
| 191 | 192 | * @param[in] indent Text indentation (parameter ignored). |
| 192 | 193 | * |
| 193 | - * Writes the text into the URL. | |
| 194 | + * Writes the text into the URL. Special characters are automatically | |
| 195 | + * transformed into predefined entities (e.g. "). | |
| 194 | 196 | ***************************************************************************/ |
| 195 | 197 | void GXmlText::write(GUrl& url, const int& indent) const |
| 196 | 198 | { |
| 197 | 199 | // Write text |
| 198 | - url.printf("%s", m_text.c_str()); | |
| 200 | + url.printf("%s", gammalib::str2xml(m_text).c_str()); | |
| 199 | 201 | |
| 200 | 202 | // Return |
| 201 | 203 | return; | ... | ... |
test/test_GSupport.cpp
| ... | ... | @@ -49,6 +49,7 @@ void TestGSupport::set(void){ |
| 49 | 49 | name("GSupport"); |
| 50 | 50 | |
| 51 | 51 | // Append tests |
| 52 | + append(static_cast<pfunction>(&TestGSupport::test_tools), "Test GTools"); | |
| 52 | 53 | append(static_cast<pfunction>(&TestGSupport::test_expand_env), "Test Environment variable"); |
| 53 | 54 | append(static_cast<pfunction>(&TestGSupport::test_node_array), "Test GNodeArray"); |
| 54 | 55 | append(static_cast<pfunction>(&TestGSupport::test_url_file), "Test GUrlFile"); |
| ... | ... | @@ -72,6 +73,30 @@ TestGSupport* TestGSupport::clone(void) const |
| 72 | 73 | |
| 73 | 74 | |
| 74 | 75 | /***********************************************************************//** |
| 76 | + * @brief Test GTools | |
| 77 | + * | |
| 78 | + * Test the GTools functions | |
| 79 | + ***************************************************************************/ | |
| 80 | +void TestGSupport::test_tools(void) | |
| 81 | +{ | |
| 82 | + // Test XML to string conversion | |
| 83 | + std::string s_in = "Hallo World, you \"are\" my 'nice' <planet> & place"; | |
| 84 | + std::string s_ref = "Hallo World, you "are" my 'nice'" | |
| 85 | + " <planet> & place"; | |
| 86 | + std::string s_out; | |
| 87 | + s_out = gammalib::str2xml(s_in); | |
| 88 | + test_assert(s_out == s_ref, "gammalib::str2xml()", | |
| 89 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 90 | + s_out = gammalib::xml2str(s_out); | |
| 91 | + test_assert(s_out == s_in, "gammalib::str2xml()", | |
| 92 | + "Unexpected string \""+s_out+"\" (expected \""+s_in+"\")"); | |
| 93 | + | |
| 94 | + // Return | |
| 95 | + return; | |
| 96 | +} | |
| 97 | + | |
| 98 | + | |
| 99 | +/***********************************************************************//** | |
| 75 | 100 | * @brief Test expand_env function |
| 76 | 101 | * |
| 77 | 102 | * Test the environment variable expansion function. |
| ... | ... | @@ -88,7 +113,8 @@ void TestGSupport::test_expand_env(void) |
| 88 | 113 | s_in = "This string has no environment variable."; |
| 89 | 114 | s_ref = s_in; |
| 90 | 115 | s_out = gammalib::expand_env(s_in); |
| 91 | - test_assert(s_out == s_ref,"no environment variable","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 116 | + test_assert(s_out == s_ref,"no environment variable", | |
| 117 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 92 | 118 | |
| 93 | 119 | // Get home environment variable |
| 94 | 120 | std::string home(std::getenv("HOME")); |
| ... | ... | @@ -97,79 +123,92 @@ void TestGSupport::test_expand_env(void) |
| 97 | 123 | s_in = "My $ENV{HOME} is my castle."; |
| 98 | 124 | s_ref = "My "+home+" is my castle."; |
| 99 | 125 | s_out = gammalib::expand_env(s_in); |
| 100 | - test_assert(s_out == s_ref,"$ENV{HOME} environment variable within string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 126 | + test_assert(s_out == s_ref,"$ENV{HOME} environment variable within string", | |
| 127 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 101 | 128 | |
| 102 | 129 | // ${HOME} environment variable within string |
| 103 | 130 | s_in = "My ${HOME} is my castle."; |
| 104 | 131 | s_ref = "My "+home+" is my castle."; |
| 105 | 132 | s_out = gammalib::expand_env(s_in); |
| 106 | - test_assert(s_out == s_ref,"${HOME} environment variable within string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 133 | + test_assert(s_out == s_ref,"${HOME} environment variable within string", | |
| 134 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 107 | 135 | |
| 108 | 136 | // $HOME/path environment variable within string |
| 109 | 137 | s_in = "My $HOME/path is my castle."; |
| 110 | 138 | s_ref = "My "+home+"/path is my castle."; |
| 111 | 139 | s_out = gammalib::expand_env(s_in); |
| 112 | - test_assert(s_out == s_ref,"$HOME/path environment variable within string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 140 | + test_assert(s_out == s_ref,"$HOME/path environment variable within string", | |
| 141 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 113 | 142 | |
| 114 | 143 | // $HOME environment variable at end of string |
| 115 | 144 | s_in = "My $HOME"; |
| 116 | 145 | s_ref = "My "+home; |
| 117 | 146 | s_out = gammalib::expand_env(s_in); |
| 118 | - test_assert(s_out == s_ref,"$HOME environment variable at end of string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 147 | + test_assert(s_out == s_ref,"$HOME environment variable at end of string", | |
| 148 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 119 | 149 | |
| 120 | 150 | // Environment variable within single quotes |
| 121 | 151 | s_in = "My '$(HOME)' is my castle."; |
| 122 | 152 | s_ref = s_in; |
| 123 | 153 | s_out = gammalib::expand_env(s_in); |
| 124 | - test_assert(s_out == s_ref,"Environment variable within single quotes","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 154 | + test_assert(s_out == s_ref,"Environment variable within single quotes", | |
| 155 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 125 | 156 | |
| 126 | 157 | // Environment variable within double quotes |
| 127 | 158 | s_in = "My \"$(HOME)\" is my castle."; |
| 128 | 159 | s_ref = "My \""+home+"\" is my castle."; |
| 129 | 160 | s_out = gammalib::expand_env(s_in); |
| 130 | - test_assert(s_out == s_ref,"Environment variable within double quotes","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 161 | + test_assert(s_out == s_ref,"Environment variable within double quotes", | |
| 162 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 131 | 163 | |
| 132 | 164 | // Non existing environment variable within string |
| 133 | 165 | s_in = "My $(HOMEIXYZ) is my castle."; |
| 134 | 166 | s_ref = "My is my castle."; |
| 135 | 167 | s_out = gammalib::expand_env(s_in); |
| 136 | - test_assert(s_out == s_ref,"Non existing environment variable within string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 168 | + test_assert(s_out == s_ref,"Non existing environment variable within string", | |
| 169 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 137 | 170 | |
| 138 | 171 | // Empty environment variable within string |
| 139 | 172 | s_in = "My $() is my castle."; |
| 140 | 173 | s_ref = "My is my castle."; |
| 141 | 174 | s_out = gammalib::expand_env(s_in); |
| 142 | - test_assert(s_out == s_ref,"Empty environment variable within string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 175 | + test_assert(s_out == s_ref,"Empty environment variable within string", | |
| 176 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 143 | 177 | |
| 144 | 178 | // Two successive environment variables within string |
| 145 | 179 | s_in = "My $(HOME)$ENV{HOME} is my castle."; |
| 146 | 180 | s_ref = "My "+home+home+" is my castle."; |
| 147 | 181 | s_out = gammalib::expand_env(s_in); |
| 148 | - test_assert(s_out == s_ref,"Two successive environment variables within string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 182 | + test_assert(s_out == s_ref,"Two successive environment variables within string", | |
| 183 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 149 | 184 | |
| 150 | 185 | // $(HOME) at beginning of string |
| 151 | 186 | s_in = "$(HOME) is my castle."; |
| 152 | 187 | s_ref = home+" is my castle."; |
| 153 | 188 | s_out = gammalib::expand_env(s_in); |
| 154 | - test_assert(s_out == s_ref,"$(HOME) at beginning of string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 189 | + test_assert(s_out == s_ref,"$(HOME) at beginning of string", | |
| 190 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 155 | 191 | |
| 156 | 192 | // $ENV(HOME) at beginning of string |
| 157 | 193 | s_in = "$ENV(HOME) is my castle."; |
| 158 | 194 | s_ref = home+" is my castle."; |
| 159 | 195 | s_out = gammalib::expand_env(s_in); |
| 160 | - test_assert(s_out == s_ref,"$ENV(HOME) at beginning of string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 196 | + test_assert(s_out == s_ref,"$ENV(HOME) at beginning of string", | |
| 197 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 161 | 198 | |
| 162 | 199 | // $(HOME) at end of string |
| 163 | 200 | s_in = "My castle is $(HOME)"; |
| 164 | 201 | s_ref = "My castle is "+home; |
| 165 | 202 | s_out = gammalib::expand_env(s_in); |
| 166 | - test_assert(s_out == s_ref,"$(HOME) at end of string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 203 | + test_assert(s_out == s_ref,"$(HOME) at end of string", | |
| 204 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 167 | 205 | |
| 168 | 206 | // $ENV{HOME}${HOME}$ENV(HOME)$(HOME) only string |
| 169 | 207 | s_in = "$ENV{HOME}${HOME}$ENV(HOME)$(HOME)"; |
| 170 | 208 | s_ref = home+home+home+home; |
| 171 | 209 | s_out = gammalib::expand_env(s_in); |
| 172 | - test_assert(s_out == s_ref,"$ENV{HOME}${HOME}$ENV(HOME)$(HOME) only string","Unexpected string '"+s_out+"' (expected '"+s_ref+"')"); | |
| 210 | + test_assert(s_out == s_ref,"$ENV{HOME}${HOME}$ENV(HOME)$(HOME) only string", | |
| 211 | + "Unexpected string \""+s_out+"\" (expected \""+s_ref+"\")"); | |
| 173 | 212 | |
| 174 | 213 | // Debugging |
| 175 | 214 | //std::cout << std::endl; | ... | ... |
test/test_GSupport.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * test_GSupport.hpp - test support module * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2012-2013 by Jean-Baptiste Cayrou * | |
| 4 | + * copyright (C) 2012-2014 by Jean-Baptiste Cayrou * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -51,6 +51,7 @@ public: |
| 51 | 51 | void test_node_array(void); |
| 52 | 52 | void test_url_file(void); |
| 53 | 53 | void test_url_string(void); |
| 54 | + void test_tools(void); | |
| 54 | 55 | |
| 55 | 56 | private: |
| 56 | 57 | // Private methods | ... | ... |