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 : #include <csspp/exception.h> 31 : #include <csspp/lexer.h> 32 : #include <csspp/unicode_range.h> 33 : 34 : 35 : // self 36 : // 37 : #include "catch_main.h" 38 : 39 : 40 : // C++ 41 : // 42 : #include <sstream> 43 : 44 : 45 : // C 46 : // 47 : #include <string.h> 48 : 49 : 50 : // last include 51 : // 52 : #include <snapdev/poison.h> 53 : 54 : 55 : 56 : namespace 57 : { 58 : 59 : 60 : } // no name namespace 61 : 62 : 63 1 : CATCH_TEST_CASE("Position defaults", "[position] [defaults]") 64 : { 65 : { 66 3 : csspp::position pos("pos.css"); 67 : 68 1 : CATCH_REQUIRE(pos.get_filename() == "pos.css"); 69 1 : CATCH_REQUIRE(pos.get_line() == 1); 70 1 : CATCH_REQUIRE(pos.get_page() == 1); 71 1 : CATCH_REQUIRE(pos.get_total_line() == 1); 72 : 73 3 : csspp::position other("other.css"); 74 : 75 1 : CATCH_REQUIRE(other.get_filename() == "other.css"); 76 1 : CATCH_REQUIRE(other.get_line() == 1); 77 1 : CATCH_REQUIRE(other.get_page() == 1); 78 1 : CATCH_REQUIRE(other.get_total_line() == 1); 79 : 80 : // copy works as expected? 81 1 : other = pos; 82 : 83 1 : CATCH_REQUIRE(pos.get_filename() == "pos.css"); 84 1 : CATCH_REQUIRE(pos.get_line() == 1); 85 1 : CATCH_REQUIRE(pos.get_page() == 1); 86 1 : CATCH_REQUIRE(pos.get_total_line() == 1); 87 : 88 1 : CATCH_REQUIRE(other.get_filename() == "pos.css"); 89 1 : CATCH_REQUIRE(other.get_line() == 1); 90 1 : CATCH_REQUIRE(other.get_page() == 1); 91 1 : CATCH_REQUIRE(other.get_total_line() == 1); 92 1 : } 93 : 94 : // no error left over 95 1 : VERIFY_ERRORS(""); 96 1 : } 97 : 98 1 : CATCH_TEST_CASE("Position counters", "[position] [count]") 99 : { 100 : // simple check to verify there is no interaction between 101 : // a copy and the original 102 : { 103 3 : csspp::position pos("pos.css"); 104 : 105 1 : CATCH_REQUIRE(pos.get_filename() == "pos.css"); 106 1 : CATCH_REQUIRE(pos.get_line() == 1); 107 1 : CATCH_REQUIRE(pos.get_page() == 1); 108 1 : CATCH_REQUIRE(pos.get_total_line() == 1); 109 : 110 3 : csspp::position other("other.css"); 111 : 112 1 : CATCH_REQUIRE(other.get_filename() == "other.css"); 113 1 : CATCH_REQUIRE(other.get_line() == 1); 114 1 : CATCH_REQUIRE(other.get_page() == 1); 115 1 : CATCH_REQUIRE(other.get_total_line() == 1); 116 : 117 : // copy works as expected? 118 1 : other = pos; 119 : 120 1 : CATCH_REQUIRE(pos.get_filename() == "pos.css"); 121 1 : CATCH_REQUIRE(pos.get_line() == 1); 122 1 : CATCH_REQUIRE(pos.get_page() == 1); 123 1 : CATCH_REQUIRE(pos.get_total_line() == 1); 124 : 125 1 : CATCH_REQUIRE(other.get_filename() == "pos.css"); // filename changed! 126 1 : CATCH_REQUIRE(other.get_line() == 1); 127 1 : CATCH_REQUIRE(other.get_page() == 1); 128 1 : CATCH_REQUIRE(other.get_total_line() == 1); 129 : 130 : // increment does not affect another position 131 1 : other.next_line(); 132 : 133 1 : CATCH_REQUIRE(pos.get_filename() == "pos.css"); 134 1 : CATCH_REQUIRE(pos.get_line() == 1); 135 1 : CATCH_REQUIRE(pos.get_page() == 1); 136 1 : CATCH_REQUIRE(pos.get_total_line() == 1); 137 : 138 1 : CATCH_REQUIRE(other.get_filename() == "pos.css"); 139 1 : CATCH_REQUIRE(other.get_line() == 2); 140 1 : CATCH_REQUIRE(other.get_page() == 1); 141 1 : CATCH_REQUIRE(other.get_total_line() == 2); 142 : 143 : // increment does not affect another position 144 1 : other.next_page(); 145 : 146 1 : CATCH_REQUIRE(pos.get_filename() == "pos.css"); 147 1 : CATCH_REQUIRE(pos.get_line() == 1); 148 1 : CATCH_REQUIRE(pos.get_page() == 1); 149 1 : CATCH_REQUIRE(pos.get_total_line() == 1); 150 : 151 1 : CATCH_REQUIRE(other.get_filename() == "pos.css"); 152 1 : CATCH_REQUIRE(other.get_line() == 1); 153 1 : CATCH_REQUIRE(other.get_page() == 2); 154 1 : CATCH_REQUIRE(other.get_total_line() == 2); 155 1 : } 156 : 157 : // loop and increment line/page counters 158 : { 159 3 : csspp::position pos("counters.css"); 160 1 : int line(1); 161 1 : int page(1); 162 1 : int total_line(1); 163 : 164 1001 : for(int i(0); i < 1000; ++i) 165 : { 166 1000 : if(rand() & 1) 167 : { 168 523 : pos.next_line(); 169 523 : ++line; 170 523 : ++total_line; 171 : } 172 : else 173 : { 174 477 : pos.next_page(); 175 477 : line = 1; 176 477 : ++page; 177 : //++total_line; -- should this happen? 178 : } 179 : 180 1000 : CATCH_REQUIRE(pos.get_filename() == "counters.css"); 181 1000 : CATCH_REQUIRE(pos.get_line() == line); 182 1000 : CATCH_REQUIRE(pos.get_page() == page); 183 1000 : CATCH_REQUIRE(pos.get_total_line() == total_line); 184 : } 185 1 : } 186 : 187 : // no error left over 188 1 : VERIFY_ERRORS(""); 189 1 : } 190 : 191 : // vim: ts=4 sw=4 et