LCOV - code coverage report
Current view: top level - tests - filepath.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 388 388 100.0 %
Date: 2019-04-24 14:10:30 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-2019  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.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 "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          16 : SCENARIO("FilePath that does not represent a file on disk", "[FilePath]")
      38             : {
      39          30 :     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           2 :             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           1 :             REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt");
      78             :         }
      79             : 
      80          10 :         WHEN("and changing the path to something else as unexistant with assignment operator works too")
      81             :         {
      82           1 :             fp = "/this/is/another/path/changed/with/assignment/operator";
      83             : 
      84           2 :             THEN("path was replaced")
      85             :             {
      86             :                 // retrieve the path
      87           2 :                 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           1 :                 REQUIRE(ss.str() == "/this/is/another/path/changed/with/assignment/operator");
     118             :             }
     119             :         }
     120             : 
     121          10 :         WHEN("still \"broken\" when appending another part (full path)")
     122             :         {
     123           2 :             zipios::FilePath path("/correct/path");
     124             : 
     125           2 :             zipios::FilePath appended(fp + path);
     126             : 
     127           2 :             THEN("path changed")
     128             :             {
     129             :                 // retrieve the concatenated path
     130           2 :                 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           2 :                     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           1 :                 REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt/correct/path");
     170             :             }
     171             :         }
     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           2 :             zipios::FilePath appended(fp + path);
     179             : 
     180           2 :             THEN("path changed")
     181             :             {
     182             :                 // retrieve the concatenated path
     183           2 :                 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           1 :                 REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt/relative/path.hpp");
     204             :             }
     205             :         }
     206             : 
     207          10 :         WHEN("still \"broken\" when appending another part (empty)")
     208             :         {
     209           2 :             zipios::FilePath path("");
     210             : 
     211             :             // append to the left...
     212           2 :             zipios::FilePath appended(fp + path);
     213             : 
     214           2 :             THEN("path changed")
     215             :             {
     216             :                 // retrieve the concatenated path
     217           2 :                 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           1 :                 REQUIRE(ss.str() == "/this/file/really/should/not/exist/period.txt");
     238             :             }
     239             :         }
     240             :     }
     241             : 
     242          30 :     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           2 :             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           1 :             REQUIRE(ss.str().empty());
     272             :         }
     273             : 
     274           8 :         WHEN("we can concatenate another empty path to it")
     275             :         {
     276           2 :             zipios::FilePath ep;
     277             : 
     278           2 :             zipios::FilePath ee(fp + ep);
     279             : 
     280           2 :             THEN("file name is still empty")
     281             :             {
     282             :                 // retrieve the path
     283           2 :                 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             :             }
     302             : 
     303           2 :             std::stringstream ss;
     304           1 :             ss << ee;
     305           1 :             REQUIRE(ss.str().empty());
     306             :         }
     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           2 :             zipios::FilePath ee(fp + ep);
     313             : 
     314           2 :             THEN("new path is equal to the concatenated path")
     315             :             {
     316             :                 // retrieve the path
     317           2 :                 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           1 :                 REQUIRE(ss.str() == "/this/is/a/regular/path");
     338             :             }
     339             :         }
     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           2 :             zipios::FilePath ee(fp + ep);
     346             : 
     347           2 :             THEN("concatenated path is the added path")
     348             :             {
     349             :                 // retrieve the path
     350           2 :                 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           1 :                 REQUIRE(ss.str() == "this/is/a/relative/path.xml");
     371             :             }
     372             :         }
     373             :     }
     374             : 
     375          30 :     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           2 :             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           1 :             REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt");
     405             :         }
     406             : 
     407           8 :         WHEN("we can concatenate an empty path to it")
     408             :         {
     409           2 :             zipios::FilePath ep;
     410             : 
     411           2 :             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           2 :                 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           1 :                 REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt");
     437             :             }
     438             :         }
     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           2 :             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           2 :                 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           1 :                 REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt/this/is/a/regular/path");
     470             :             }
     471             :         }
     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           2 :             zipios::FilePath ee(fp + ep);
     478             : 
     479           2 :             THEN("the path changed")
     480             :             {
     481             :                 // retrieve the path
     482           2 :                 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           1 :                 REQUIRE(ss.str() == "this/is/a/relative/path/file1.txt/this/is/a/relative/path.xml");
     503             :             }
     504             :         }
     505             :     }
     506             : 
     507          30 :     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           2 :             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           1 :             REQUIRE(ss.str() == "this/is/a/relative/path");
     537             :         }
     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           2 :             zipios::FilePath ee(fp + ep);
     544             : 
     545           2 :             THEN("the result is as expected without the slash")
     546             :             {
     547             :                 // retrieve the path
     548           2 :                 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           1 :                 REQUIRE(ss.str() == "this/is/a/relative/path/add/this/with/a/slash");
     569             :             }
     570             :         }
     571             :     }
     572          15 : }
     573             : 
     574             : 
     575           3 : SCENARIO("FilePath against existing files on disk", "[FilePath]")
     576             : {
     577           4 :     GIVEN("an existing file")
     578             :     {
     579             :         {
     580             :             // create a file
     581           2 :             std::fstream f("filepath-test.txt", std::ios::out | std::ios::binary);
     582           1 :             f << "This is a simple test file." << std::endl;
     583             :         }
     584             : 
     585           2 :         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           2 :                 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           1 :                 REQUIRE(ss.str() == "filepath-test.txt");
     618             :             }
     619             :         }
     620             : 
     621           1 :         unlink("filepath-test.txt");
     622             :     }
     623             : 
     624           4 :     GIVEN("an existing directory")
     625             :     {
     626             :         // make sure the directory is gone before re-creating it
     627           1 :         REQUIRE(system("rm -rf filepath-test") == 0);
     628             :         // create a directory
     629           1 :         REQUIRE(mkdir("filepath-test", 0777) == 0);
     630             : 
     631           2 :         WHEN("creating a FilePath object")
     632             :         {
     633           2 :             zipios::FilePath fp("filepath-test");
     634             : 
     635           2 :             THEN("is found")
     636             :             {
     637             :                 // retrieve the path
     638           2 :                 std::string const p(fp);
     639           1 :                 REQUIRE(p == "filepath-test");
     640             : 
     641             :                 // basename is "period.txt"
     642           1 :                 REQUIRE(static_cast<std::string>(fp.filename()) == "filepath-test");
     643             : 
     644           1 :                 REQUIRE(fp.length() == 13);
     645           1 :                 REQUIRE(fp.size() == 13);
     646             : 
     647             :                 // all flags must be false
     648           1 :                 REQUIRE(fp.exists());
     649           1 :                 REQUIRE(!fp.isRegular());
     650           1 :                 REQUIRE(fp.isDirectory());
     651           1 :                 REQUIRE(!fp.isCharSpecial());
     652           1 :                 REQUIRE(!fp.isBlockSpecial());
     653           1 :                 REQUIRE(!fp.isSocket());
     654           1 :                 REQUIRE(!fp.isFifo());
     655             : 
     656           2 :                 std::stringstream ss;
     657           1 :                 ss << fp;
     658           1 :                 REQUIRE(ss.str() == "filepath-test");
     659             :             }
     660             :         }
     661             : 
     662           1 :         rmdir("filepath-test");
     663             :     }
     664             : 
     665             :     // todo: add tests for other file types (not extremely useful
     666             :     //       for a small zip library though...)
     667           2 : }
     668             : 
     669             : 
     670           2 : TEST_CASE("Test with regular files of various sizes", "[FilePath]")
     671             : {
     672          11 :     for(int i(0); i < 10; ++i)
     673             :     {
     674             :         // create a random file
     675          10 :         int const file_size(rand() % 100 + 20);
     676             :         {
     677             :             // create a file
     678          20 :             std::fstream f("filepath-test.txt", std::ios::out | std::ios::binary);
     679         794 :             for(int j(0); j < file_size; ++j)
     680             :             {
     681         784 :                 char const c(rand());
     682         784 :                 f << c;
     683             :             }
     684             :         }
     685             : 
     686             :         {
     687          20 :             zipios::FilePath fp("filepath-test.txt");
     688             : 
     689             :             // retrieve the path
     690          20 :             std::string const p(fp);
     691          10 :             REQUIRE(p == "filepath-test.txt");
     692             : 
     693             :             // basename is "period.txt"
     694          10 :             REQUIRE(static_cast<std::string>(fp.filename()) == "filepath-test.txt");
     695             : 
     696          10 :             REQUIRE(fp.length() == 17);
     697          10 :             REQUIRE(fp.size() == 17);
     698          10 :             REQUIRE(fp.fileSize() == file_size);
     699             : 
     700             :             struct stat file_stats;
     701          10 :             REQUIRE(stat("filepath-test.txt", &file_stats) == 0);
     702          10 :             REQUIRE(fp.lastModificationTime() == file_stats.st_mtime);
     703             : 
     704             :             // all flags must be false
     705          10 :             REQUIRE(fp.exists());
     706          10 :             REQUIRE(fp.isRegular());
     707          10 :             REQUIRE(!fp.isDirectory());
     708          10 :             REQUIRE(!fp.isCharSpecial());
     709          10 :             REQUIRE(!fp.isBlockSpecial());
     710          10 :             REQUIRE(!fp.isSocket());
     711          10 :             REQUIRE(!fp.isFifo());
     712             : 
     713          20 :             std::stringstream ss;
     714          10 :             ss << fp;
     715          10 :             REQUIRE(ss.str() == "filepath-test.txt");
     716             :         }
     717             : 
     718          10 :         unlink("filepath-test.txt");
     719             :     }
     720           4 : }
     721             : 
     722             : 
     723             : // Local Variables:
     724             : // mode: cpp
     725             : // indent-tabs-mode: nil
     726             : // c-basic-offset: 4
     727             : // tab-width: 4
     728             : // End:
     729             : 
     730             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12