00001 00034 #include "linden_common.h" 00035 #include "lltut.h" 00036 00037 #include "llformat.h" 00038 #include "llsd.h" 00039 00040 namespace tut 00041 { 00042 template<> 00043 void ensure_equals(const char* msg, const LLDate& actual, 00044 const LLDate& expected) 00045 { 00046 ensure_equals(msg, 00047 actual.secondsSinceEpoch(), expected.secondsSinceEpoch()); 00048 } 00049 00050 template<> 00051 void ensure_equals(const char* msg, const LLURI& actual, 00052 const LLURI& expected) 00053 { 00054 ensure_equals(msg, 00055 actual.asString(), expected.asString()); 00056 } 00057 00058 template<> 00059 void ensure_equals(const char* msg, 00060 const std::vector<U8>& actual, const std::vector<U8>& expected) 00061 { 00062 std::string s(msg); 00063 00064 ensure_equals(s + " size", actual.size(), expected.size()); 00065 00066 std::vector<U8>::const_iterator i, j; 00067 int k; 00068 for (i = actual.begin(), j = expected.begin(), k = 0; 00069 i != actual.end(); 00070 ++i, ++j, ++k) 00071 { 00072 ensure_equals(s + " field", *i, *j); 00073 } 00074 } 00075 00076 template<> 00077 void ensure_equals(const char* m, const LLSD& actual, 00078 const LLSD& expected) 00079 { 00080 const std::string& msg = m; 00081 00082 ensure_equals(msg + " type", actual.type(), expected.type()); 00083 switch (actual.type()) 00084 { 00085 case LLSD::TypeUndefined: 00086 return; 00087 00088 case LLSD::TypeBoolean: 00089 ensure_equals(msg + " boolean", actual.asBoolean(), expected.asBoolean()); 00090 return; 00091 00092 case LLSD::TypeInteger: 00093 ensure_equals(msg + " integer", actual.asInteger(), expected.asInteger()); 00094 return; 00095 00096 case LLSD::TypeReal: 00097 ensure_equals(msg + " real", actual.asReal(), expected.asReal()); 00098 return; 00099 00100 case LLSD::TypeString: 00101 ensure_equals(msg + " string", actual.asString(), expected.asString()); 00102 return; 00103 00104 case LLSD::TypeUUID: 00105 ensure_equals(msg + " uuid", actual.asUUID(), expected.asUUID()); 00106 return; 00107 00108 case LLSD::TypeDate: 00109 ensure_equals(msg + " date", actual.asDate(), expected.asDate()); 00110 return; 00111 00112 case LLSD::TypeURI: 00113 ensure_equals(msg + " uri", actual.asURI(), expected.asURI()); 00114 return; 00115 00116 case LLSD::TypeBinary: 00117 ensure_equals(msg + " binary", actual.asBinary(), expected.asBinary()); 00118 return; 00119 00120 case LLSD::TypeMap: 00121 { 00122 ensure_equals(msg + " map size", actual.size(), expected.size()); 00123 00124 LLSD::map_const_iterator actual_iter = actual.beginMap(); 00125 LLSD::map_const_iterator expected_iter = expected.beginMap(); 00126 00127 while(actual_iter != actual.endMap()) 00128 { 00129 ensure_equals(msg + " map keys", 00130 actual_iter->first, expected_iter->first); 00131 ensure_equals(msg + "[" + actual_iter->first + "]", 00132 actual_iter->second, expected_iter->second); 00133 ++actual_iter; 00134 ++expected_iter; 00135 } 00136 return; 00137 } 00138 case LLSD::TypeArray: 00139 { 00140 ensure_equals(msg + " array size", actual.size(), expected.size()); 00141 00142 for(int i = 0; i < actual.size(); ++i) 00143 { 00144 ensure_equals(msg + llformat("[%d]", i), 00145 actual[i], expected[i]); 00146 } 00147 return; 00148 } 00149 } 00150 } 00151 00152 void ensure_starts_with(const std::string& msg, 00153 const std::string& actual, const std::string& expectedStart) 00154 { 00155 if( actual.find(expectedStart, 0) != 0 ) 00156 { 00157 std::stringstream ss; 00158 ss << msg << ": " << "expected to find " << expectedStart 00159 << " at start of actual " << actual; 00160 throw failure(ss.str().c_str()); 00161 } 00162 } 00163 00164 void ensure_ends_with(const std::string& msg, 00165 const std::string& actual, const std::string& expectedEnd) 00166 { 00167 if( actual.size() < expectedEnd.size() 00168 || actual.rfind(expectedEnd) 00169 != (actual.size() - expectedEnd.size()) ) 00170 { 00171 std::stringstream ss; 00172 ss << msg << ": " << "expected to find " << expectedEnd 00173 << " at end of actual " << actual; 00174 throw failure(ss.str().c_str()); 00175 } 00176 } 00177 00178 void ensure_contains(const std::string& msg, 00179 const std::string& actual, const std::string& expectedSubString) 00180 { 00181 if( actual.find(expectedSubString, 0) == std::string::npos ) 00182 { 00183 std::stringstream ss; 00184 ss << msg << ": " << "expected to find " << expectedSubString 00185 << " in actual " << actual; 00186 throw failure(ss.str().c_str()); 00187 } 00188 } 00189 00190 void ensure_does_not_contain(const std::string& msg, 00191 const std::string& actual, const std::string& expectedSubString) 00192 { 00193 if( actual.find(expectedSubString, 0) != std::string::npos ) 00194 { 00195 std::stringstream ss; 00196 ss << msg << ": " << "expected not to find " << expectedSubString 00197 << " in actual " << actual; 00198 throw failure(ss.str().c_str()); 00199 } 00200 } 00201 }