Line data Source code
1 : // Copyright (c) 2011-2023 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // https://snapwebsites.org/project/as2js 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 3 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 17 : // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 : 19 : // self 20 : // 21 : #include "catch_main.h" 22 : 23 : 24 : // as2js 25 : // 26 : #include <as2js/position.h> 27 : #include <as2js/exception.h> 28 : 29 : 30 : // C++ 31 : // 32 : #include <cstring> 33 : #include <algorithm> 34 : #include <sstream> 35 : 36 : 37 : // last include 38 : // 39 : #include <snapdev/poison.h> 40 : 41 : 42 : 43 : 44 : 45 : 46 : 47 : 48 2 : CATCH_TEST_CASE("position_names", "[position]") 49 : { 50 2 : CATCH_START_SECTION("position_names: check filename") 51 : { 52 1 : as2js::position pos; 53 : 54 : // by default it is empty 55 1 : CATCH_REQUIRE(pos.get_filename() == ""); 56 : 57 : // some long filename 58 1 : pos.set_filename("the/filename/can really/be anything.test"); 59 1 : CATCH_REQUIRE(pos.get_filename() == "the/filename/can really/be anything.test"); 60 : 61 : // reset back to empty 62 1 : pos.set_filename(""); 63 1 : CATCH_REQUIRE(pos.get_filename() == ""); 64 : 65 : // set to another value 66 1 : pos.set_filename("file.js"); 67 1 : CATCH_REQUIRE(pos.get_filename() == "file.js"); 68 : 69 1 : as2js::position other; 70 1 : CATCH_REQUIRE_FALSE(pos == other); 71 1 : other = pos; 72 1 : CATCH_REQUIRE(pos == other); 73 1 : } 74 2 : CATCH_END_SECTION() 75 : 76 2 : CATCH_START_SECTION("position_names: function") 77 : { 78 1 : as2js::position pos; 79 : 80 : // by default it is empty 81 1 : CATCH_REQUIRE(pos.get_function() == ""); 82 : 83 : // some long function name 84 1 : pos.set_function("as2js::super::function::name"); 85 1 : CATCH_REQUIRE(pos.get_function() == "as2js::super::function::name"); 86 : 87 : // reset back to empty 88 1 : pos.set_function(""); 89 1 : CATCH_REQUIRE(pos.get_function() == ""); 90 : 91 : // set to another value 92 1 : pos.set_function("add"); 93 1 : CATCH_REQUIRE(pos.get_function() == "add"); 94 1 : } 95 2 : CATCH_END_SECTION() 96 2 : } 97 : 98 : 99 3 : CATCH_TEST_CASE("position_counters", "[position]") 100 : { 101 3 : CATCH_START_SECTION("position_counters: default counters") 102 : { 103 1 : as2js::position pos; 104 : 105 1 : CATCH_REQUIRE(pos.get_page() == 1); 106 1 : CATCH_REQUIRE(pos.get_page_line() == 1); 107 1 : CATCH_REQUIRE(pos.get_paragraph() == 1); 108 1 : CATCH_REQUIRE(pos.get_line() == 1); 109 1 : CATCH_REQUIRE(pos.get_column() == 1); 110 1 : } 111 3 : CATCH_END_SECTION() 112 : 113 3 : CATCH_START_SECTION("position_counters: increase counters") 114 : { 115 1 : as2js::position pos; 116 : 117 1 : int total_line(1); 118 100 : for(int page(1); page < 100; ++page) 119 : { 120 99 : int paragraphs(rand() % 10 + 10); 121 99 : int page_line(1); 122 99 : int paragraph(1); 123 99000 : for(int line(1); line < 1000; ++line) 124 : { 125 98901 : CATCH_REQUIRE(pos.get_page() == page); 126 98901 : CATCH_REQUIRE(pos.get_page_line() == page_line); 127 98901 : CATCH_REQUIRE(pos.get_paragraph() == paragraph); 128 98901 : CATCH_REQUIRE(pos.get_line() == total_line); 129 : 130 98901 : constexpr int const max_column(256); 131 25318656 : for(int column(1); column < max_column; ++column) 132 : { 133 25219755 : CATCH_REQUIRE(pos.get_column() == column); 134 25219755 : pos.new_column(); 135 : } 136 98901 : CATCH_REQUIRE(pos.get_column() == max_column); 137 : 138 98901 : if(line % paragraphs == 0) 139 : { 140 7130 : pos.new_paragraph(); 141 7130 : ++paragraph; 142 : } 143 98901 : pos.new_line(); 144 98901 : ++total_line; 145 98901 : ++page_line; 146 : } 147 99 : pos.new_page(); 148 : } 149 : 150 : // reset counters back to 1 151 : // 152 1 : pos.reset_counters(); 153 1 : CATCH_REQUIRE(pos.get_page() == 1); 154 1 : CATCH_REQUIRE(pos.get_page_line() == 1); 155 1 : CATCH_REQUIRE(pos.get_paragraph() == 1); 156 1 : CATCH_REQUIRE(pos.get_line() == 1); 157 1 : CATCH_REQUIRE(pos.get_column() == 1); 158 1 : } 159 3 : CATCH_END_SECTION() 160 : 161 3 : CATCH_START_SECTION("position_counters: test reseting line number") 162 : { 163 1 : as2js::position pos; 164 : 165 : // we can also define the start line 166 1 : int last_line(1); 167 250 : for(int idx(1); idx < 250; ++idx) 168 : { 169 249 : int line(rand() % 20000); 170 249 : if(idx % 13 == 0) 171 : { 172 : // force a negative number to test the throw 173 19 : line = -line; 174 : } 175 249 : if(line < 1) 176 : { 177 : // this throws because the line # is not valid 178 19 : CATCH_REQUIRE_THROWS_MATCHES( 179 : pos.reset_counters(line) 180 : , as2js::internal_error 181 : , Catch::Matchers::ExceptionMessage( 182 : "internal_error: the line parameter of the position object cannot be less than 1.")); 183 : 184 : // the counters are unchanged in that case 185 19 : CATCH_REQUIRE(pos.get_page() == 1); 186 19 : CATCH_REQUIRE(pos.get_page_line() == 1); 187 19 : CATCH_REQUIRE(pos.get_paragraph() == 1); 188 19 : CATCH_REQUIRE(pos.get_line() == last_line); 189 19 : CATCH_REQUIRE(pos.get_column() == 1); 190 : } 191 : else 192 : { 193 230 : pos.reset_counters(line); 194 230 : CATCH_REQUIRE(pos.get_page() == 1); 195 230 : CATCH_REQUIRE(pos.get_page_line() == 1); 196 230 : CATCH_REQUIRE(pos.get_paragraph() == 1); 197 230 : CATCH_REQUIRE(pos.get_line() == line); 198 230 : CATCH_REQUIRE(pos.get_column() == 1); 199 230 : last_line = line; 200 : } 201 : } 202 1 : } 203 3 : CATCH_END_SECTION() 204 3 : } 205 : 206 : 207 2 : CATCH_TEST_CASE("position_output", "[position]") 208 : { 209 2 : CATCH_START_SECTION("position_output: output without a filename") 210 : { 211 1 : as2js::position pos; 212 : 213 1 : int total_line(1); 214 100 : for(int page(1); page < 100; ++page) 215 : { 216 99 : int paragraphs(rand() % 10 + 10); 217 99 : int page_line(1); 218 99 : int paragraph(1); 219 99000 : for(int line(1); line < 1000; ++line) 220 : { 221 98901 : CATCH_REQUIRE(pos.get_page() == page); 222 98901 : CATCH_REQUIRE(pos.get_page_line() == page_line); 223 98901 : CATCH_REQUIRE(pos.get_paragraph() == paragraph); 224 98901 : CATCH_REQUIRE(pos.get_line() == total_line); 225 : 226 98901 : int const max_column(rand() % 200 + 1); 227 9949982 : for(int column(1); column < max_column; ++column) 228 : { 229 9851081 : std::stringstream pos_str; 230 9851081 : pos_str << pos; 231 9851081 : std::stringstream test_str; 232 9851081 : test_str << "line " << total_line << ":"; 233 9851081 : if(pos.get_column() != 1) 234 : { 235 9752668 : test_str << pos.get_column() << ":"; 236 : } 237 9851081 : CATCH_REQUIRE(pos_str.str() == test_str.str()); 238 : 239 9851081 : pos.new_column(); 240 9851081 : } 241 : 242 98901 : if(line % paragraphs == 0) 243 : { 244 7036 : pos.new_paragraph(); 245 7036 : ++paragraph; 246 : } 247 98901 : pos.new_line(); 248 98901 : ++total_line; 249 98901 : ++page_line; 250 : } 251 99 : pos.new_page(); 252 : } 253 1 : } 254 2 : CATCH_END_SECTION() 255 : 256 2 : CATCH_START_SECTION("position_output: with a filename") 257 : { 258 1 : as2js::position pos; 259 : 260 1 : pos.set_filename("file.js"); 261 1 : int total_line(1); 262 100 : for(int page(1); page < 100; ++page) 263 : { 264 99 : int paragraphs(rand() % 10 + 10); 265 99 : int page_line(1); 266 99 : int paragraph(1); 267 99000 : for(int line(1); line < 1000; ++line) 268 : { 269 98901 : CATCH_REQUIRE(pos.get_page() == page); 270 98901 : CATCH_REQUIRE(pos.get_page_line() == page_line); 271 98901 : CATCH_REQUIRE(pos.get_paragraph() == paragraph); 272 98901 : CATCH_REQUIRE(pos.get_line() == total_line); 273 : 274 98901 : std::stringstream pos_str; 275 98901 : pos_str << pos; 276 98901 : std::stringstream test_str; 277 98901 : test_str << "file.js:" << total_line << ":"; 278 98901 : CATCH_REQUIRE(pos_str.str() == test_str.str()); 279 : 280 98901 : if(line % paragraphs == 0) 281 : { 282 6676 : pos.new_paragraph(); 283 6676 : ++paragraph; 284 : } 285 98901 : pos.new_line(); 286 98901 : ++total_line; 287 98901 : ++page_line; 288 98901 : } 289 99 : pos.new_page(); 290 : } 291 1 : } 292 2 : CATCH_END_SECTION() 293 2 : } 294 : 295 : 296 : // vim: ts=4 sw=4 et