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

Generated by: LCOV version 1.10