Line data Source code
1 : // Copyright (c) 2015-2022 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // https://snapwebsites.org/project/csspp 4 : // contact@m2osw.com 5 : // 6 : // This program is free software; you can redistribute it and/or modify 7 : // it under the terms of the GNU General Public License as published by 8 : // the Free Software Foundation; either version 2 of the License, or 9 : // (at your option) any later version. 10 : // 11 : // This program is distributed in the hope that it will be useful, 12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : // GNU General Public License for more details. 15 : // 16 : // You should have received a copy of the GNU General Public License along 17 : // with this program; if not, write to the Free Software Foundation, Inc., 18 : // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 : 20 : /** \file 21 : * \brief Test the error.cpp file. 22 : * 23 : * This test runs a battery of tests agains the error.cpp 24 : * implementation to ensure full coverage. 25 : */ 26 : 27 : // csspp 28 : // 29 : #include <csspp/error.h> 30 : 31 : #include <csspp/exception.h> 32 : #include <csspp/lexer.h> 33 : #include <csspp/unicode_range.h> 34 : 35 : 36 : // self 37 : // 38 : #include "catch_main.h" 39 : 40 : 41 : // C++ 42 : // 43 : #include <sstream> 44 : 45 : 46 : // C 47 : // 48 : #include <string.h> 49 : 50 : 51 : // last include 52 : // 53 : #include <snapdev/poison.h> 54 : 55 : 56 : 57 1 : CATCH_TEST_CASE("Error names", "[error]") 58 : { 59 1 : csspp::error_mode_t e(csspp::error_mode_t::ERROR_DEC); 60 7 : while(e <= csspp::error_mode_t::ERROR_WARNING) 61 : { 62 6 : std::stringstream ss; 63 6 : ss << e; 64 6 : std::string const name(ss.str()); 65 : 66 6 : switch(e) 67 : { 68 0 : case csspp::error_mode_t::ERROR_DEBUG: 69 0 : CATCH_REQUIRE(name == "debug"); 70 0 : break; 71 : 72 1 : case csspp::error_mode_t::ERROR_DEC: 73 1 : CATCH_REQUIRE(name == "dec"); 74 1 : break; 75 : 76 1 : case csspp::error_mode_t::ERROR_ERROR: 77 1 : CATCH_REQUIRE(name == "error"); 78 1 : break; 79 : 80 1 : case csspp::error_mode_t::ERROR_FATAL: 81 1 : CATCH_REQUIRE(name == "fatal"); 82 1 : break; 83 : 84 1 : case csspp::error_mode_t::ERROR_HEX: 85 1 : CATCH_REQUIRE(name == "hex"); 86 1 : break; 87 : 88 1 : case csspp::error_mode_t::ERROR_INFO: 89 1 : CATCH_REQUIRE(name == "info"); 90 1 : break; 91 : 92 1 : case csspp::error_mode_t::ERROR_WARNING: 93 1 : CATCH_REQUIRE(name == "warning"); 94 1 : break; 95 : 96 : } 97 : 98 6 : e = static_cast<csspp::error_mode_t>(static_cast<int>(e) + 1); 99 6 : } 100 : 101 : // no error left over 102 1 : VERIFY_ERRORS(""); 103 1 : } 104 : 105 1 : CATCH_TEST_CASE("Error messages", "[error] [output]") 106 : { 107 1 : csspp::error_count_t error_count(csspp::error::instance().get_error_count()); 108 1 : csspp::error_count_t warning_count(csspp::error::instance().get_warning_count()); 109 : 110 3 : csspp::position p("test.css"); 111 : 112 : { 113 1 : csspp::error_happened_t happened; 114 : 115 1 : csspp::error::instance() << p << "testing errors: " 116 1 : << 123 117 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 118 1 : << "." 119 1 : << csspp::error_mode_t::ERROR_FATAL; 120 1 : VERIFY_ERRORS("test.css(1): fatal: testing errors: 123 U+7b.\n"); 121 1 : ++error_count; 122 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 123 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 124 : 125 1 : CATCH_REQUIRE(happened.error_happened()); 126 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 127 : } 128 : 129 : { 130 1 : csspp::error_happened_t happened; 131 : 132 1 : int64_t cs(83); 133 3 : csspp::error::instance() << p << std::string("testing errors:") 134 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << cs 135 1 : << " (" << csspp::error_mode_t::ERROR_DEC << 133 136 1 : << ")." 137 2 : << csspp::error_mode_t::ERROR_ERROR; 138 1 : VERIFY_ERRORS("test.css(1): error: testing errors: U+53 (133).\n"); 139 1 : ++error_count; 140 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 141 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 142 : 143 1 : CATCH_REQUIRE(happened.error_happened()); 144 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 145 : } 146 : 147 : { 148 1 : csspp::safe_error_t safe_error; 149 : 150 : { 151 1 : csspp::error_happened_t happened; 152 : 153 1 : csspp::error::instance() << p << "testing warnings:" 154 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 155 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 123.25 156 1 : << "." 157 1 : << csspp::error_mode_t::ERROR_WARNING; 158 1 : VERIFY_ERRORS("test.css(1): warning: testing warnings: U+7b decimal: 123.25.\n"); 159 1 : ++warning_count; 160 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 161 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 162 : 163 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 164 1 : CATCH_REQUIRE(happened.warning_happened()); 165 : } 166 : 167 : { 168 1 : csspp::error_happened_t happened; 169 : 170 1 : csspp::error::instance().set_count_warnings_as_errors(true); 171 1 : csspp::error::instance() << p << "testing warnings:" 172 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 173 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 123.25 174 1 : << "." 175 1 : << csspp::error_mode_t::ERROR_WARNING; 176 1 : VERIFY_ERRORS("test.css(1): warning: testing warnings: U+7b decimal: 123.25.\n"); 177 1 : ++error_count; 178 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 179 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 180 1 : csspp::error::instance().set_count_warnings_as_errors(false); 181 : 182 1 : CATCH_REQUIRE(happened.error_happened()); 183 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 184 : } 185 : 186 : { 187 1 : csspp::error_happened_t happened; 188 : 189 1 : csspp::error::instance() << p << "testing warnings:" 190 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 191 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 123.25 192 1 : << "." 193 1 : << csspp::error_mode_t::ERROR_WARNING; 194 1 : VERIFY_ERRORS("test.css(1): warning: testing warnings: U+7b decimal: 123.25.\n"); 195 1 : ++warning_count; 196 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 197 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 198 : 199 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 200 1 : CATCH_REQUIRE(happened.warning_happened()); 201 : } 202 1 : } 203 : // the safe_error restores the counters to what they were before the '{' 204 1 : --error_count; 205 1 : warning_count -= 2; 206 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 207 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 208 : 209 : { 210 1 : csspp::error_happened_t happened; 211 : 212 1 : csspp::error::instance() << p << "testing info:" 213 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 120 214 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 213.25 215 1 : << "." 216 1 : << csspp::error_mode_t::ERROR_INFO; 217 1 : VERIFY_ERRORS("test.css(1): info: testing info: U+78 decimal: 213.25.\n"); 218 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 219 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 220 : 221 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 222 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 223 : } 224 : 225 : { 226 1 : csspp::error_happened_t happened; 227 : 228 1 : csspp::error::instance() << p << "testing debug:" 229 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 112 230 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 13.25 231 1 : << "." 232 1 : << csspp::error_mode_t::ERROR_DEBUG; 233 1 : VERIFY_ERRORS(""); 234 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 235 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 236 : 237 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 238 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 239 : } 240 : 241 : { 242 1 : csspp::error_happened_t happened; 243 : 244 1 : csspp::error::instance().set_show_debug(true); 245 1 : csspp::error::instance() << p << "testing debug:" 246 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 112 247 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 13.25 248 1 : << "." 249 1 : << csspp::error_mode_t::ERROR_DEBUG; 250 1 : VERIFY_ERRORS("test.css(1): debug: testing debug: U+70 decimal: 13.25.\n"); 251 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 252 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 253 1 : csspp::error::instance().set_show_debug(false); 254 : 255 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 256 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 257 : } 258 : 259 : { 260 1 : csspp::error_happened_t happened; 261 : 262 1 : csspp::error::instance() << p << "testing debug:" 263 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 112 264 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 13.25 265 1 : << "." 266 1 : << csspp::error_mode_t::ERROR_DEBUG; 267 1 : VERIFY_ERRORS(""); 268 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 269 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 270 : 271 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 272 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 273 : } 274 : 275 : { 276 1 : csspp::error_happened_t happened; 277 : 278 1 : csspp::error::instance().set_verbose(true); 279 1 : csspp::error::instance() << p << "verbose message to debug the compiler." 280 1 : << csspp::error_mode_t::ERROR_INFO; 281 1 : VERIFY_ERRORS("test.css(1): info: verbose message to debug the compiler.\n"); 282 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 283 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 284 : 285 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 286 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 287 : 288 1 : csspp::error::instance().set_verbose(false); 289 : } 290 : 291 : // no error left over 292 1 : VERIFY_ERRORS(""); 293 2 : } 294 : 295 1 : CATCH_TEST_CASE("Error messages when hidden", "[error] [output] [hidden]") 296 : { 297 1 : csspp::error_count_t error_count(csspp::error::instance().get_error_count()); 298 1 : csspp::error_count_t warning_count(csspp::error::instance().get_warning_count()); 299 : 300 1 : csspp::error::instance().set_hide_all(true); 301 : 302 3 : csspp::position p("test.css"); 303 : 304 : { 305 1 : csspp::error_happened_t happened; 306 : 307 1 : csspp::error::instance() << p << "testing errors: " 308 1 : << 123 309 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 310 1 : << "." 311 1 : << csspp::error_mode_t::ERROR_FATAL; 312 1 : VERIFY_ERRORS("test.css(1): fatal: testing errors: 123 U+7b.\n"); 313 1 : ++error_count; 314 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 315 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 316 : 317 1 : CATCH_REQUIRE(happened.error_happened()); 318 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 319 : } 320 : 321 : { 322 1 : csspp::error_happened_t happened; 323 : 324 1 : int64_t cs(83); 325 3 : csspp::error::instance() << p << std::string("testing errors:") 326 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << cs 327 1 : << " (" << csspp::error_mode_t::ERROR_DEC << 133 328 1 : << ")." 329 2 : << csspp::error_mode_t::ERROR_ERROR; 330 1 : VERIFY_ERRORS("test.css(1): error: testing errors: U+53 (133).\n"); 331 1 : ++error_count; 332 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 333 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 334 : 335 1 : CATCH_REQUIRE(happened.error_happened()); 336 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 337 : } 338 : 339 : { 340 1 : csspp::safe_error_t safe_error; 341 : 342 : { 343 1 : csspp::error_happened_t happened; 344 : 345 1 : csspp::error::instance() << p << "testing warnings:" 346 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 347 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 123.25 348 1 : << "." 349 1 : << csspp::error_mode_t::ERROR_WARNING; 350 1 : VERIFY_ERRORS(""); 351 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 352 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 353 : 354 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 355 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 356 : } 357 : 358 : { 359 1 : csspp::error_happened_t happened; 360 : 361 1 : csspp::error::instance().set_count_warnings_as_errors(true); 362 1 : csspp::error::instance() << p << "testing warnings:" 363 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 364 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 123.25 365 1 : << "." 366 1 : << csspp::error_mode_t::ERROR_WARNING; 367 1 : VERIFY_ERRORS("test.css(1): warning: testing warnings: U+7b decimal: 123.25.\n"); 368 1 : ++error_count; 369 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 370 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 371 1 : csspp::error::instance().set_count_warnings_as_errors(false); 372 : 373 1 : CATCH_REQUIRE(happened.error_happened()); 374 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 375 : } 376 : 377 : { 378 1 : csspp::error_happened_t happened; 379 : 380 1 : csspp::error::instance() << p << "testing warnings:" 381 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 123 382 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 123.25 383 1 : << "." 384 1 : << csspp::error_mode_t::ERROR_WARNING; 385 1 : VERIFY_ERRORS(""); 386 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 387 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 388 : 389 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 390 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 391 : } 392 1 : } 393 : // the safe_error restores the counters to what they were before the '{' 394 1 : --error_count; 395 1 : warning_count -= 0; 396 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 397 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 398 : 399 : { 400 1 : csspp::error_happened_t happened; 401 : 402 1 : csspp::error::instance() << p << "testing info:" 403 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 120 404 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 213.25 405 1 : << "." 406 1 : << csspp::error_mode_t::ERROR_INFO; 407 1 : VERIFY_ERRORS(""); 408 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 409 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 410 : 411 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 412 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 413 : } 414 : 415 : { 416 1 : csspp::error_happened_t happened; 417 : 418 1 : csspp::error::instance() << p << "testing debug:" 419 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 112 420 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 13.25 421 1 : << "." 422 1 : << csspp::error_mode_t::ERROR_DEBUG; 423 1 : VERIFY_ERRORS(""); 424 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 425 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 426 : 427 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 428 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 429 : } 430 : 431 : { 432 1 : csspp::error_happened_t happened; 433 : 434 1 : csspp::error::instance().set_show_debug(true); 435 1 : csspp::error::instance() << p << "testing debug:" 436 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 112 437 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 13.25 438 1 : << "." 439 1 : << csspp::error_mode_t::ERROR_DEBUG; 440 1 : VERIFY_ERRORS(""); 441 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 442 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 443 1 : csspp::error::instance().set_show_debug(false); 444 : 445 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 446 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 447 : } 448 : 449 : { 450 1 : csspp::error_happened_t happened; 451 : 452 1 : csspp::error::instance() << p << "testing debug:" 453 1 : << " U+" << csspp::error_mode_t::ERROR_HEX << 112 454 1 : << " decimal: " << csspp::error_mode_t::ERROR_DEC << 13.25 455 1 : << "." 456 1 : << csspp::error_mode_t::ERROR_DEBUG; 457 1 : VERIFY_ERRORS(""); 458 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 459 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 460 : 461 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 462 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 463 : } 464 : 465 : { 466 1 : csspp::error_happened_t happened; 467 : 468 1 : csspp::error::instance().set_verbose(true); 469 1 : csspp::error::instance() << p << "verbose message to debug the compiler." 470 1 : << csspp::error_mode_t::ERROR_INFO; 471 1 : VERIFY_ERRORS(""); 472 1 : CATCH_REQUIRE(error_count == csspp::error::instance().get_error_count()); 473 1 : CATCH_REQUIRE(warning_count == csspp::error::instance().get_warning_count()); 474 : 475 1 : CATCH_REQUIRE_FALSE(happened.error_happened()); 476 1 : CATCH_REQUIRE_FALSE(happened.warning_happened()); 477 : 478 1 : csspp::error::instance().set_verbose(false); 479 : } 480 : 481 1 : csspp::error::instance().set_hide_all(false); 482 : 483 : // no error left over 484 1 : VERIFY_ERRORS(""); 485 2 : } 486 : 487 1 : CATCH_TEST_CASE("Error stream", "[error] [stream]") 488 : { 489 : { 490 1 : std::stringstream ss; 491 1 : std::ostream & errout(csspp::error::instance().get_error_stream()); 492 1 : CATCH_REQUIRE(&errout != &ss); 493 : { 494 1 : csspp::safe_error_stream_t safe_stream(ss); 495 1 : CATCH_REQUIRE(&csspp::error::instance().get_error_stream() == &ss); 496 1 : } 497 1 : CATCH_REQUIRE(&csspp::error::instance().get_error_stream() != &ss); 498 1 : CATCH_REQUIRE(&csspp::error::instance().get_error_stream() == &errout); 499 1 : } 500 1 : } 501 : 502 : // vim: ts=4 sw=4 et