LCOV - code coverage report
Current view: top level - tests - catch_filepath.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 434 434 100.0 %
Date: 2024-06-15 08:26:09 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   Zipios -- a small C++ library that provides easy access to .zip files.
       3             : 
       4             :   Copyright (C) 2000-2007  Thomas Sondergaard
       5             :   Copyright (c) 2015-2022  Made to Order Software Corp.  All Rights Reserved
       6             : 
       7             :   This library is free software; you can redistribute it and/or
       8             :   modify it under the terms of the GNU Lesser General Public
       9             :   License as published by the Free Software Foundation; either
      10             :   version 2.1 of the License, or (at your option) any later version.
      11             : 
      12             :   This library is distributed in the hope that it will be useful,
      13             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :   Lesser General Public License for more details.
      16             : 
      17             :   You should have received a copy of the GNU Lesser General Public
      18             :   License along with this library; if not, write to the Free Software
      19             :   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      20             : */
      21             : 
      22             : /** \file
      23             :  *
      24             :  * Zipios unit tests used to verify the FilePath class.
      25             :  */
      26             : 
      27             : #include "catch_main.hpp"
      28             : 
      29             : #include <zipios/filepath.hpp>
      30             : 
      31             : #include <fstream>
      32             : 
      33             : #include <sys/stat.h>
      34             : #include <unistd.h>
      35             : 
      36             : 
      37          15 : CATCH_SCENARIO("FilePath that does not represent a file on disk", "[FilePath]")
      38             : {
      39          15 :     CATCH_GIVEN("test a fantom file (path that \"cannot\" exists)")
      40             :     {
      41          10 :         zipios::FilePath fp("/this/file/really/should/not/exist/period.txt");
      42             : 
      43             :         // first, check that the object is setup as expected
      44           5 :         CATCH_START_SECTION("verify that the object looks as expected")
      45             :         {
      46             :             // retrieve the path
      47           1 :             std::string const p(fp);
      48           1 :             CATCH_REQUIRE(p == "/this/file/really/should/not/exist/period.txt");
      49             : 
      50           1 :             CATCH_REQUIRE(fp == "/this/file/really/should/not/exist/period.txt");
      51           1 :             CATCH_REQUIRE("/this/file/really/should/not/exist/period.txt" == fp);
      52           1 :             CATCH_REQUIRE(fp == std::string("/this/file/really/should/not/exist/period.txt"));
      53           1 :             CATCH_REQUIRE(std::string("/this/file/really/should/not/exist/period.txt") == fp);
      54             : 
      55           1 :             CATCH_REQUIRE(!(fp == "/this/file/really/should/not/exist/period"));
      56           1 :             CATCH_REQUIRE(!("/file/really/should/not/exist/period.txt" == fp));
      57           1 :             CATCH_REQUIRE(!(fp == std::string("/this/file/really/should/exist/period.txt")));
      58           1 :             CATCH_REQUIRE(!(std::string("/this/file/should/not/exist/period.txt") == fp));
      59             : 
      60             :             // basename is "period.txt"
      61           1 :             CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "period.txt");
      62             : 
      63           1 :             CATCH_REQUIRE(fp.length() == 45);
      64           1 :             CATCH_REQUIRE(fp.size() == 45);
      65             : 
      66             :             // all flags must be false
      67           1 :             CATCH_REQUIRE(!fp.exists());
      68           1 :             CATCH_REQUIRE(!fp.isRegular());
      69           1 :             CATCH_REQUIRE(!fp.isDirectory());
      70           1 :             CATCH_REQUIRE(!fp.isCharSpecial());
      71           1 :             CATCH_REQUIRE(!fp.isBlockSpecial());
      72           1 :             CATCH_REQUIRE(!fp.isSocket());
      73           1 :             CATCH_REQUIRE(!fp.isFifo());
      74             : 
      75           1 :             std::stringstream ss;
      76           1 :             ss << fp;
      77           1 :             CATCH_REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt");
      78           1 :         }
      79           5 :         CATCH_END_SECTION()
      80             : 
      81           5 :         CATCH_WHEN("and changing the path to something else as unexistant with assignment operator works too")
      82             :         {
      83           1 :             fp = "/this/is/another/path/changed/with/assignment/operator";
      84             : 
      85           1 :             CATCH_THEN("path was replaced")
      86             :             {
      87             :                 // retrieve the path
      88           1 :                 std::string const p(fp);
      89           1 :                 CATCH_REQUIRE(p == "/this/is/another/path/changed/with/assignment/operator");
      90             : 
      91           1 :                 CATCH_REQUIRE(fp == "/this/is/another/path/changed/with/assignment/operator");
      92           1 :                 CATCH_REQUIRE("/this/is/another/path/changed/with/assignment/operator" == fp);
      93           1 :                 CATCH_REQUIRE(fp == std::string("/this/is/another/path/changed/with/assignment/operator"));
      94           1 :                 CATCH_REQUIRE(std::string("/this/is/another/path/changed/with/assignment/operator") == fp);
      95             : 
      96           1 :                 CATCH_REQUIRE(!(fp == "this/is/another/path/changed/with/assignment/operator"));
      97           1 :                 CATCH_REQUIRE(!("/this/is/another/path/chnged/with/assignment/operator" == fp));
      98           1 :                 CATCH_REQUIRE(!(fp == std::string("/this/is/another/path/changed/with/asignment/operator")));
      99           1 :                 CATCH_REQUIRE(!(std::string("/this/is/another/path/changed/with/assignment/oprator") == fp));
     100             : 
     101             :                 // check basename
     102           1 :                 CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "operator");
     103             : 
     104           1 :                 CATCH_REQUIRE(fp.length() == 54);
     105           1 :                 CATCH_REQUIRE(fp.size() == 54);
     106             : 
     107             :                 // all flags must be false
     108           1 :                 CATCH_REQUIRE(!fp.exists());
     109           1 :                 CATCH_REQUIRE(!fp.isRegular());
     110           1 :                 CATCH_REQUIRE(!fp.isDirectory());
     111           1 :                 CATCH_REQUIRE(!fp.isCharSpecial());
     112           1 :                 CATCH_REQUIRE(!fp.isBlockSpecial());
     113           1 :                 CATCH_REQUIRE(!fp.isSocket());
     114           1 :                 CATCH_REQUIRE(!fp.isFifo());
     115             : 
     116           1 :                 std::stringstream ss;
     117           1 :                 ss << fp;
     118           1 :                 CATCH_REQUIRE(ss.str() == "/this/is/another/path/changed/with/assignment/operator");
     119           2 :             }
     120           5 :         }
     121             : 
     122           5 :         CATCH_WHEN("still \"broken\" when appending another part (full path)")
     123             :         {
     124           2 :             zipios::FilePath path("/correct/path");
     125             : 
     126           1 :             zipios::FilePath appended(fp + path);
     127             : 
     128           1 :             CATCH_THEN("path changed")
     129             :             {
     130             :                 // retrieve the concatenated path
     131           1 :                 std::string const p(appended);
     132           1 :                 CATCH_REQUIRE(p == "/this/file/really/should/not/exist/period.txt/correct/path");
     133             : 
     134           1 :                 CATCH_REQUIRE(appended == "/this/file/really/should/not/exist/period.txt/correct/path");
     135           1 :                 CATCH_REQUIRE("/this/file/really/should/not/exist/period.txt/correct/path" == appended);
     136           1 :                 CATCH_REQUIRE(appended == std::string("/this/file/really/should/not/exist/period.txt/correct/path"));
     137           1 :                 CATCH_REQUIRE(std::string("/this/file/really/should/not/exist/period.txt/correct/path") == appended);
     138             : 
     139           1 :                 CATCH_REQUIRE(!(appended == "/this/file/really/not/exist/period.txt/correct/path"));
     140           1 :                 CATCH_REQUIRE(!("/this/file/really/should/not/exist/period/correct/path" == appended));
     141           1 :                 CATCH_REQUIRE(!(appended == std::string("/this/file/really/should/not/exist/period.txt/correct")));
     142           1 :                 CATCH_REQUIRE(!(std::string("/this/file/should/not/exist/period.txt/correct/path") == appended));
     143             : 
     144           1 :                 CATCH_REQUIRE(!(fp == path));
     145           1 :                 CATCH_REQUIRE(!(path == fp));
     146             : 
     147             :                 {
     148           2 :                     zipios::FilePath equal("/this/file/really/should/not/exist/period.txt");
     149           1 :                     CATCH_REQUIRE(fp == equal);
     150           1 :                     CATCH_REQUIRE(equal == fp);
     151           1 :                 }
     152             : 
     153             :                 // still the same basename
     154           1 :                 CATCH_REQUIRE(static_cast<std::string>(appended.filename()) == "path");
     155             : 
     156           1 :                 CATCH_REQUIRE(appended.length() == 58);
     157           1 :                 CATCH_REQUIRE(appended.size() == 58);
     158             : 
     159             :                 // still all flags are false
     160           1 :                 CATCH_REQUIRE(!appended.exists());
     161           1 :                 CATCH_REQUIRE(!appended.isRegular());
     162           1 :                 CATCH_REQUIRE(!appended.isDirectory());
     163           1 :                 CATCH_REQUIRE(!appended.isCharSpecial());
     164           1 :                 CATCH_REQUIRE(!appended.isBlockSpecial());
     165           1 :                 CATCH_REQUIRE(!appended.isSocket());
     166           1 :                 CATCH_REQUIRE(!appended.isFifo());
     167             : 
     168           1 :                 std::stringstream ss;
     169           1 :                 ss << appended;
     170           1 :                 CATCH_REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt/correct/path");
     171           2 :             }
     172           6 :         }
     173             : 
     174           5 :         CATCH_WHEN("still \"broken\" when appending another part (relative)")
     175             :         {
     176           2 :             zipios::FilePath path("relative/path.hpp");
     177             : 
     178             :             // append to the left...
     179           1 :             zipios::FilePath appended(fp + path);
     180             : 
     181           1 :             CATCH_THEN("path changed")
     182             :             {
     183             :                 // retrieve the concatenated path
     184           1 :                 std::string const p(appended);
     185           1 :                 CATCH_REQUIRE(p == "/this/file/really/should/not/exist/period.txt/relative/path.hpp");
     186             : 
     187             :                 // check basename
     188           1 :                 CATCH_REQUIRE(static_cast<std::string>(appended.filename()) == "path.hpp");
     189             : 
     190           1 :                 CATCH_REQUIRE(appended.length() == 63);
     191           1 :                 CATCH_REQUIRE(appended.size() == 63);
     192             : 
     193             :                 // still all flags are false
     194           1 :                 CATCH_REQUIRE(!appended.exists());
     195           1 :                 CATCH_REQUIRE(!appended.isRegular());
     196           1 :                 CATCH_REQUIRE(!appended.isDirectory());
     197           1 :                 CATCH_REQUIRE(!appended.isCharSpecial());
     198           1 :                 CATCH_REQUIRE(!appended.isBlockSpecial());
     199           1 :                 CATCH_REQUIRE(!appended.isSocket());
     200           1 :                 CATCH_REQUIRE(!appended.isFifo());
     201             : 
     202           1 :                 std::stringstream ss;
     203           1 :                 ss << appended;
     204           1 :                 CATCH_REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt/relative/path.hpp");
     205           2 :             }
     206           6 :         }
     207             : 
     208           5 :         CATCH_WHEN("still \"broken\" when appending another part (empty)")
     209             :         {
     210           2 :             zipios::FilePath path("");
     211             : 
     212             :             // append to the left...
     213           1 :             zipios::FilePath appended(fp + path);
     214             : 
     215           1 :             CATCH_THEN("path changed")
     216             :             {
     217             :                 // retrieve the concatenated path
     218           1 :                 std::string const p(appended);
     219           1 :                 CATCH_REQUIRE(p == "/this/file/really/should/not/exist/period.txt");
     220             : 
     221             :                 // check basename
     222           1 :                 CATCH_REQUIRE(static_cast<std::string>(appended.filename()) == "period.txt");
     223             : 
     224           1 :                 CATCH_REQUIRE(appended.length() == 45);
     225           1 :                 CATCH_REQUIRE(appended.size() == 45);
     226             : 
     227             :                 // still all flags are false
     228           1 :                 CATCH_REQUIRE(!appended.exists());
     229           1 :                 CATCH_REQUIRE(!appended.isRegular());
     230           1 :                 CATCH_REQUIRE(!appended.isDirectory());
     231           1 :                 CATCH_REQUIRE(!appended.isCharSpecial());
     232           1 :                 CATCH_REQUIRE(!appended.isBlockSpecial());
     233           1 :                 CATCH_REQUIRE(!appended.isSocket());
     234           1 :                 CATCH_REQUIRE(!appended.isFifo());
     235             : 
     236           1 :                 std::stringstream ss;
     237           1 :                 ss << appended;
     238           1 :                 CATCH_REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt");
     239           2 :             }
     240           6 :         }
     241          20 :     }
     242             : 
     243          15 :     CATCH_GIVEN("an empty path")
     244             :     {
     245           4 :         zipios::FilePath fp;
     246             : 
     247             :         // first, check that the object is setup as expected
     248           4 :         CATCH_START_SECTION("verify that the object looks as expected")
     249             :         {
     250             :             // retrieve the path
     251           1 :             std::string const p(fp);
     252           1 :             CATCH_REQUIRE(p == "");
     253             : 
     254             :             // check basename
     255           1 :             CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "");
     256             : 
     257           1 :             CATCH_REQUIRE(fp.length() == 0);
     258           1 :             CATCH_REQUIRE(fp.size() == 0);
     259             : 
     260             :             // all flags must be false when empty
     261             :             // (because empty does not represent ".")
     262           1 :             CATCH_REQUIRE(!fp.exists());
     263           1 :             CATCH_REQUIRE(!fp.isRegular());
     264           1 :             CATCH_REQUIRE(!fp.isDirectory());
     265           1 :             CATCH_REQUIRE(!fp.isCharSpecial());
     266           1 :             CATCH_REQUIRE(!fp.isBlockSpecial());
     267           1 :             CATCH_REQUIRE(!fp.isSocket());
     268           1 :             CATCH_REQUIRE(!fp.isFifo());
     269             : 
     270           1 :             std::stringstream ss;
     271           1 :             ss << fp;
     272           1 :             CATCH_REQUIRE(ss.str().empty());
     273           1 :         }
     274           4 :         CATCH_END_SECTION()
     275             : 
     276           4 :         CATCH_WHEN("we can concatenate another empty path to it")
     277             :         {
     278           1 :             zipios::FilePath ep;
     279             : 
     280           1 :             zipios::FilePath ee(fp + ep);
     281             : 
     282           1 :             CATCH_THEN("file name is still empty")
     283             :             {
     284             :                 // retrieve the path
     285           1 :                 std::string const p(ee);
     286           1 :                 CATCH_REQUIRE(p == "");
     287             : 
     288             :                 // check basename
     289           1 :                 CATCH_REQUIRE(static_cast<std::string>(ee.filename()) == "");
     290             : 
     291           1 :                 CATCH_REQUIRE(ee.length() == 0);
     292           1 :                 CATCH_REQUIRE(ee.size() == 0);
     293             : 
     294             :                 // all flags must be false when empty
     295             :                 // (because empty does not represent ".")
     296           1 :                 CATCH_REQUIRE(!ee.exists());
     297           1 :                 CATCH_REQUIRE(!ee.isRegular());
     298           1 :                 CATCH_REQUIRE(!ee.isDirectory());
     299           1 :                 CATCH_REQUIRE(!ee.isCharSpecial());
     300           1 :                 CATCH_REQUIRE(!ee.isBlockSpecial());
     301           1 :                 CATCH_REQUIRE(!ee.isSocket());
     302           1 :                 CATCH_REQUIRE(!ee.isFifo());
     303           2 :             }
     304             : 
     305           1 :             std::stringstream ss;
     306           1 :             ss << ee;
     307           1 :             CATCH_REQUIRE(ss.str().empty());
     308           5 :         }
     309             : 
     310           4 :         CATCH_WHEN("we can concatenate a full regular path to it")
     311             :         {
     312           2 :             zipios::FilePath ep("/this/is/a/regular/path");
     313             : 
     314           1 :             zipios::FilePath ee(fp + ep);
     315             : 
     316           1 :             CATCH_THEN("new path is equal to the concatenated path")
     317             :             {
     318             :                 // retrieve the path
     319           1 :                 std::string const p(ee);
     320           1 :                 CATCH_REQUIRE(p == "/this/is/a/regular/path");
     321             : 
     322             :                 // check basename
     323           1 :                 CATCH_REQUIRE(static_cast<std::string>(ee.filename()) == "path");
     324             : 
     325           1 :                 CATCH_REQUIRE(ee.length() == 23);
     326           1 :                 CATCH_REQUIRE(ee.size() == 23);
     327             : 
     328             :                 // all flags must be false
     329           1 :                 CATCH_REQUIRE(!ee.exists());
     330           1 :                 CATCH_REQUIRE(!ee.isRegular());
     331           1 :                 CATCH_REQUIRE(!ee.isDirectory());
     332           1 :                 CATCH_REQUIRE(!ee.isCharSpecial());
     333           1 :                 CATCH_REQUIRE(!ee.isBlockSpecial());
     334           1 :                 CATCH_REQUIRE(!ee.isSocket());
     335           1 :                 CATCH_REQUIRE(!ee.isFifo());
     336             : 
     337           1 :                 std::stringstream ss;
     338           1 :                 ss << ee;
     339           1 :                 CATCH_REQUIRE(ss.str() == "/this/is/a/regular/path");
     340           2 :             }
     341           5 :         }
     342             : 
     343           4 :         CATCH_WHEN("we can concatenate a relative path to it")
     344             :         {
     345           2 :             zipios::FilePath ep("this/is/a/relative/path.xml");
     346             : 
     347           1 :             zipios::FilePath ee(fp + ep);
     348             : 
     349           1 :             CATCH_THEN("concatenated path is the added path")
     350             :             {
     351             :                 // retrieve the path
     352           1 :                 std::string const p(ee);
     353           1 :                 CATCH_REQUIRE(p == "this/is/a/relative/path.xml");
     354             : 
     355             :                 // check basename
     356           1 :                 CATCH_REQUIRE(static_cast<std::string>(ee.filename()) == "path.xml");
     357             : 
     358           1 :                 CATCH_REQUIRE(ee.length() == 27);
     359           1 :                 CATCH_REQUIRE(ee.size() == 27);
     360             : 
     361             :                 // all flags must be false
     362           1 :                 CATCH_REQUIRE(!ee.exists());
     363           1 :                 CATCH_REQUIRE(!ee.isRegular());
     364           1 :                 CATCH_REQUIRE(!ee.isDirectory());
     365           1 :                 CATCH_REQUIRE(!ee.isCharSpecial());
     366           1 :                 CATCH_REQUIRE(!ee.isBlockSpecial());
     367           1 :                 CATCH_REQUIRE(!ee.isSocket());
     368           1 :                 CATCH_REQUIRE(!ee.isFifo());
     369             : 
     370           1 :                 std::stringstream ss;
     371           1 :                 ss << ee;
     372           1 :                 CATCH_REQUIRE(ss.str() == "this/is/a/relative/path.xml");
     373           2 :             }
     374           5 :         }
     375          19 :     }
     376             : 
     377          15 :     CATCH_GIVEN("a fantom relative path")
     378             :     {
     379           8 :         zipios::FilePath fp("this/is/a/relative/path/file1.txt");
     380             : 
     381             :         // first, check that the object is setup as expected
     382           4 :         CATCH_START_SECTION("verify that the object looks as expected")
     383             :         {
     384             :             // retrieve the path
     385           1 :             std::string const p(fp);
     386           1 :             CATCH_REQUIRE(p == "this/is/a/relative/path/file1.txt");
     387             : 
     388             :             // check basename
     389           1 :             CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "file1.txt");
     390             : 
     391           1 :             CATCH_REQUIRE(fp.length() == 33);
     392           1 :             CATCH_REQUIRE(fp.size() == 33);
     393             : 
     394             :             // all flags must be false when empty
     395             :             // (because empty does not represent ".")
     396           1 :             CATCH_REQUIRE(!fp.exists());
     397           1 :             CATCH_REQUIRE(!fp.isRegular());
     398           1 :             CATCH_REQUIRE(!fp.isDirectory());
     399           1 :             CATCH_REQUIRE(!fp.isCharSpecial());
     400           1 :             CATCH_REQUIRE(!fp.isBlockSpecial());
     401           1 :             CATCH_REQUIRE(!fp.isSocket());
     402           1 :             CATCH_REQUIRE(!fp.isFifo());
     403             : 
     404           1 :             std::stringstream ss;
     405           1 :             ss << fp;
     406           1 :             CATCH_REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt");
     407           1 :         }
     408           4 :         CATCH_END_SECTION()
     409             : 
     410           4 :         CATCH_WHEN("we can concatenate an empty path to it")
     411             :         {
     412           1 :             zipios::FilePath ep;
     413             : 
     414           1 :             zipios::FilePath ee(fp + ep);
     415             : 
     416           1 :             CATCH_THEN("the result is the same as the left hand-side")
     417             :             {
     418             :                 // retrieve the path
     419           1 :                 std::string const p(ee);
     420           1 :                 CATCH_REQUIRE(p == "this/is/a/relative/path/file1.txt");
     421             : 
     422             :                 // check basename
     423           1 :                 CATCH_REQUIRE(static_cast<std::string>(ee.filename()) == "file1.txt");
     424             : 
     425           1 :                 CATCH_REQUIRE(ee.length() == 33);
     426           1 :                 CATCH_REQUIRE(ee.size() == 33);
     427             : 
     428             :                 // all flags must be false when empty
     429           1 :                 CATCH_REQUIRE(!ee.exists());
     430           1 :                 CATCH_REQUIRE(!ee.isRegular());
     431           1 :                 CATCH_REQUIRE(!ee.isDirectory());
     432           1 :                 CATCH_REQUIRE(!ee.isCharSpecial());
     433           1 :                 CATCH_REQUIRE(!ee.isBlockSpecial());
     434           1 :                 CATCH_REQUIRE(!ee.isSocket());
     435           1 :                 CATCH_REQUIRE(!ee.isFifo());
     436             : 
     437           1 :                 std::stringstream ss;
     438           1 :                 ss << ee;
     439           1 :                 CATCH_REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt");
     440           2 :             }
     441           5 :         }
     442             : 
     443           4 :         CATCH_WHEN("we can concatenate a full regular path to it")
     444             :         {
     445           2 :             zipios::FilePath ep("/this/is/a/regular/path");
     446             : 
     447           1 :             zipios::FilePath ee(fp + ep);
     448             : 
     449           1 :             CATCH_THEN("path is the resulting concatenation with a slash at the end")
     450             :             {
     451             :                 // retrieve the path
     452           1 :                 std::string const p(ee);
     453           1 :                 CATCH_REQUIRE(p == "this/is/a/relative/path/file1.txt/this/is/a/regular/path");
     454             : 
     455             :                 // check basename
     456           1 :                 CATCH_REQUIRE(static_cast<std::string>(ee.filename()) == "path");
     457             : 
     458           1 :                 CATCH_REQUIRE(ee.length() == 56);
     459           1 :                 CATCH_REQUIRE(ee.size() == 56);
     460             : 
     461             :                 // all flags must be false
     462           1 :                 CATCH_REQUIRE(!ee.exists());
     463           1 :                 CATCH_REQUIRE(!ee.isRegular());
     464           1 :                 CATCH_REQUIRE(!ee.isDirectory());
     465           1 :                 CATCH_REQUIRE(!ee.isCharSpecial());
     466           1 :                 CATCH_REQUIRE(!ee.isBlockSpecial());
     467           1 :                 CATCH_REQUIRE(!ee.isSocket());
     468           1 :                 CATCH_REQUIRE(!ee.isFifo());
     469             : 
     470           1 :                 std::stringstream ss;
     471           1 :                 ss << ee;
     472           1 :                 CATCH_REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt/this/is/a/regular/path");
     473           2 :             }
     474           5 :         }
     475             : 
     476           4 :         CATCH_WHEN("we can concatenate a relative path to it")
     477             :         {
     478           2 :             zipios::FilePath ep("this/is/a/relative/path.xml");
     479             : 
     480           1 :             zipios::FilePath ee(fp + ep);
     481             : 
     482           1 :             CATCH_THEN("the path changed")
     483             :             {
     484             :                 // retrieve the path
     485           1 :                 std::string const p(ee);
     486           1 :                 CATCH_REQUIRE(p == "this/is/a/relative/path/file1.txt/this/is/a/relative/path.xml");
     487             : 
     488             :                 // basename is "period.txt"
     489           1 :                 CATCH_REQUIRE(static_cast<std::string>(ee.filename()) == "path.xml");
     490             : 
     491           1 :                 CATCH_REQUIRE(ee.length() == 61);
     492           1 :                 CATCH_REQUIRE(ee.size() == 61);
     493             : 
     494             :                 // all flags must be false
     495           1 :                 CATCH_REQUIRE(!ee.exists());
     496           1 :                 CATCH_REQUIRE(!ee.isRegular());
     497           1 :                 CATCH_REQUIRE(!ee.isDirectory());
     498           1 :                 CATCH_REQUIRE(!ee.isCharSpecial());
     499           1 :                 CATCH_REQUIRE(!ee.isBlockSpecial());
     500           1 :                 CATCH_REQUIRE(!ee.isSocket());
     501           1 :                 CATCH_REQUIRE(!ee.isFifo());
     502             : 
     503           1 :                 std::stringstream ss;
     504           1 :                 ss << ee;
     505           1 :                 CATCH_REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt/this/is/a/relative/path.xml");
     506           2 :             }
     507           5 :         }
     508          19 :     }
     509             : 
     510          15 :     CATCH_GIVEN("a fantom path that ends with /")
     511             :     {
     512           4 :         zipios::FilePath fp("this/is/a/relative/path/");
     513             : 
     514             :         // first, check that the object is setup as expected
     515           2 :         CATCH_START_SECTION("verify that the object looks as expected")
     516             :         {
     517             :             // retrieve the path
     518           1 :             std::string const p(fp);
     519           1 :             CATCH_REQUIRE(p == "this/is/a/relative/path");
     520             : 
     521             :             // check basename
     522           1 :             CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "path");
     523             : 
     524           1 :             CATCH_REQUIRE(fp.length() == 23);
     525           1 :             CATCH_REQUIRE(fp.size() == 23);
     526             : 
     527             :             // all flags must be false when empty
     528             :             // (because empty does not represent ".")
     529           1 :             CATCH_REQUIRE(!fp.exists());
     530           1 :             CATCH_REQUIRE(!fp.isRegular());
     531           1 :             CATCH_REQUIRE(!fp.isDirectory());
     532           1 :             CATCH_REQUIRE(!fp.isCharSpecial());
     533           1 :             CATCH_REQUIRE(!fp.isBlockSpecial());
     534           1 :             CATCH_REQUIRE(!fp.isSocket());
     535           1 :             CATCH_REQUIRE(!fp.isFifo());
     536             : 
     537           1 :             std::stringstream ss;
     538           1 :             ss << fp;
     539           1 :             CATCH_REQUIRE(ss.str() == "this/is/a/relative/path");
     540           1 :         }
     541           2 :         CATCH_END_SECTION()
     542             : 
     543           2 :         CATCH_WHEN("we can concatenate another path, it also prune the /")
     544             :         {
     545           2 :             zipios::FilePath ep("add/this/with/a/slash/");
     546             : 
     547           1 :             zipios::FilePath ee(fp + ep);
     548             : 
     549           1 :             CATCH_THEN("the result is as expected without the slash")
     550             :             {
     551             :                 // retrieve the path
     552           1 :                 std::string const p(ee);
     553           1 :                 CATCH_REQUIRE(p == "this/is/a/relative/path/add/this/with/a/slash");
     554             : 
     555             :                 // check basename
     556           1 :                 CATCH_REQUIRE(static_cast<std::string>(ee.filename()) == "slash");
     557             : 
     558           1 :                 CATCH_REQUIRE(ee.length() == 45);
     559           1 :                 CATCH_REQUIRE(ee.size() == 45);
     560             : 
     561             :                 // all flags must be false when empty
     562           1 :                 CATCH_REQUIRE(!ee.exists());
     563           1 :                 CATCH_REQUIRE(!ee.isRegular());
     564           1 :                 CATCH_REQUIRE(!ee.isDirectory());
     565           1 :                 CATCH_REQUIRE(!ee.isCharSpecial());
     566           1 :                 CATCH_REQUIRE(!ee.isBlockSpecial());
     567           1 :                 CATCH_REQUIRE(!ee.isSocket());
     568           1 :                 CATCH_REQUIRE(!ee.isFifo());
     569             : 
     570           1 :                 std::stringstream ss;
     571           1 :                 ss << ee;
     572           1 :                 CATCH_REQUIRE(ss.str() == "this/is/a/relative/path/add/this/with/a/slash");
     573           2 :             }
     574           3 :         }
     575          17 :     }
     576          15 : }
     577             : 
     578             : 
     579           2 : CATCH_SCENARIO("FilePath against existing files on disk", "[FilePath]")
     580             : {
     581           2 :     zipios_test::safe_chdir cwd(SNAP_CATCH2_NAMESPACE::g_tmp_dir());
     582             : 
     583           2 :     CATCH_GIVEN("an existing file")
     584             :     {
     585           2 :         zipios_test::auto_unlink_t auto_unlink("filepath-test.txt", true);
     586             : 
     587             :         {
     588             :             // create a file
     589           1 :             std::ofstream f("filepath-test.txt", std::ios::out | std::ios::binary);
     590           1 :             f << "This is a simple test file." << std::endl;
     591           1 :         }
     592             : 
     593           1 :         CATCH_WHEN("creating a FilePath object")
     594             :         {
     595           2 :             zipios::FilePath fp("filepath-test.txt");
     596             : 
     597           1 :             CATCH_THEN("is found")
     598             :             {
     599             :                 // retrieve the path
     600           1 :                 std::string const p(fp);
     601           1 :                 CATCH_REQUIRE(p == "filepath-test.txt");
     602             : 
     603             :                 // basename is "period.txt"
     604           1 :                 CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "filepath-test.txt");
     605             : 
     606           1 :                 CATCH_REQUIRE(fp.length() == 17);
     607           1 :                 CATCH_REQUIRE(fp.size() == 17);
     608           1 :                 CATCH_REQUIRE(fp.fileSize() == 28);
     609             : 
     610             :                 struct stat file_stats;
     611           1 :                 CATCH_REQUIRE(stat("filepath-test.txt", &file_stats) == 0);
     612           1 :                 CATCH_REQUIRE(fp.lastModificationTime() == file_stats.st_mtime);
     613             : 
     614             :                 // all flags must be false
     615           1 :                 CATCH_REQUIRE(fp.exists());
     616           1 :                 CATCH_REQUIRE(fp.isRegular());
     617           1 :                 CATCH_REQUIRE(!fp.isDirectory());
     618           1 :                 CATCH_REQUIRE(!fp.isCharSpecial());
     619           1 :                 CATCH_REQUIRE(!fp.isBlockSpecial());
     620           1 :                 CATCH_REQUIRE(!fp.isSocket());
     621           1 :                 CATCH_REQUIRE(!fp.isFifo());
     622             : 
     623           1 :                 std::stringstream ss;
     624           1 :                 ss << fp;
     625           1 :                 CATCH_REQUIRE(ss.str() == "filepath-test.txt");
     626           2 :             }
     627           2 :         }
     628           3 :     }
     629             : 
     630           2 :     CATCH_GIVEN("an existing directory")
     631             :     {
     632           2 :         zipios_test::auto_unlink_t auto_unlink("filepath-test", true);
     633             : 
     634             :         // create a directory
     635           1 :         CATCH_REQUIRE(mkdir("filepath-test", 0777) == 0);
     636             : 
     637           1 :         CATCH_WHEN("creating a FilePath object")
     638             :         {
     639           2 :             zipios::FilePath fp("filepath-test");
     640             : 
     641           1 :             CATCH_THEN("is found")
     642             :             {
     643             :                 // retrieve the path
     644           1 :                 std::string const p(fp);
     645           1 :                 CATCH_REQUIRE(p == "filepath-test");
     646             : 
     647             :                 // basename is "period.txt"
     648           1 :                 CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "filepath-test");
     649             : 
     650           1 :                 CATCH_REQUIRE(fp.length() == 13);
     651           1 :                 CATCH_REQUIRE(fp.size() == 13);
     652             : 
     653             :                 // all flags must be false
     654           1 :                 CATCH_REQUIRE(fp.exists());
     655           1 :                 CATCH_REQUIRE(!fp.isRegular());
     656           1 :                 CATCH_REQUIRE(fp.isDirectory());
     657           1 :                 CATCH_REQUIRE(!fp.isCharSpecial());
     658           1 :                 CATCH_REQUIRE(!fp.isBlockSpecial());
     659           1 :                 CATCH_REQUIRE(!fp.isSocket());
     660           1 :                 CATCH_REQUIRE(!fp.isFifo());
     661             : 
     662           1 :                 std::stringstream ss;
     663           1 :                 ss << fp;
     664           1 :                 CATCH_REQUIRE(ss.str() == "filepath-test");
     665           2 :             }
     666           2 :         }
     667           3 :     }
     668             : 
     669             :     // todo: add tests for other file types (not extremely useful
     670             :     //       for a small zip library though...)
     671           2 : }
     672             : 
     673             : 
     674           1 : CATCH_TEST_CASE("Test with regular files of various sizes", "[FilePath]")
     675             : {
     676           1 :     zipios_test::safe_chdir cwd(SNAP_CATCH2_NAMESPACE::g_tmp_dir());
     677             : 
     678          11 :     for(int i(0); i < 10; ++i)
     679             :     {
     680          20 :         zipios_test::auto_unlink_t auto_unlink("filepath-test.txt", true);
     681             : 
     682             :         // create a random file
     683          10 :         int const file_size(rand() % 100 + 20);
     684             :         {
     685             :             // create a file
     686          10 :             std::ofstream f("filepath-test.txt", std::ios::out | std::ios::binary);
     687         672 :             for(int j(0); j < file_size; ++j)
     688             :             {
     689         662 :                 char const c(rand());
     690         662 :                 f << c;
     691             :             }
     692          10 :         }
     693             : 
     694             :         {
     695          20 :             zipios::FilePath fp("filepath-test.txt");
     696             : 
     697             :             // retrieve the path
     698          10 :             std::string const p(fp);
     699          10 :             CATCH_REQUIRE(p == "filepath-test.txt");
     700             : 
     701             :             // basename is "period.txt"
     702          10 :             CATCH_REQUIRE(static_cast<std::string>(fp.filename()) == "filepath-test.txt");
     703             : 
     704          10 :             CATCH_REQUIRE(fp.length() == 17);
     705          10 :             CATCH_REQUIRE(fp.size() == 17);
     706          10 :             CATCH_REQUIRE(fp.fileSize() == static_cast<std::size_t>(file_size));
     707             : 
     708             :             struct stat file_stats;
     709          10 :             CATCH_REQUIRE(stat("filepath-test.txt", &file_stats) == 0);
     710          10 :             CATCH_REQUIRE(fp.lastModificationTime() == file_stats.st_mtime);
     711             : 
     712             :             // all flags must be false
     713          10 :             CATCH_REQUIRE(fp.exists());
     714          10 :             CATCH_REQUIRE(fp.isRegular());
     715          10 :             CATCH_REQUIRE(!fp.isDirectory());
     716          10 :             CATCH_REQUIRE(!fp.isCharSpecial());
     717          10 :             CATCH_REQUIRE(!fp.isBlockSpecial());
     718          10 :             CATCH_REQUIRE(!fp.isSocket());
     719          10 :             CATCH_REQUIRE(!fp.isFifo());
     720             : 
     721          10 :             std::stringstream ss;
     722          10 :             ss << fp;
     723          10 :             CATCH_REQUIRE(ss.str() == "filepath-test.txt");
     724          10 :         }
     725          10 :     }
     726           1 : }
     727             : 
     728             : 
     729             : // Local Variables:
     730             : // mode: cpp
     731             : // indent-tabs-mode: nil
     732             : // c-basic-offset: 4
     733             : // tab-width: 4
     734             : // End:
     735             : 
     736             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.14

Snap C++ | List of projects | List of versions