Example #1
0
 Path operator+(const Path & p1, const Path & p2)
 {
   Path::StringVec sv;
   sv.push_back(std::string(p1));
   sv.push_back(std::string(p2));
   return Path::join(sv.begin(), sv.end());
 }
Example #2
0
 Path & Path::operator+=(const Path & path)
 {
   Path::StringVec sv;
   sv.push_back(std::string(path_));
   sv.push_back(std::string(path.path_));
   path_ = Path::join(sv.begin(), sv.end());
   return *this;
 }
Example #3
0
  std::string Path::getParent(const std::string & path)
  {
    if (path == "") 
      return "";

    std::string np = Path::normalize(path);
    Path::StringVec sv = Path::split(np);
    sv.push_back("..");

    return Path::normalize(Path::join(sv.begin(), sv.end()));
  }
Example #4
0
// test static split()
TEST_F(PathTest, split)
{
#if defined(NTA_OS_WINDOWS)
// no tests defined
#else
  Path::StringVec sv;
  sv = Path::split("/foo/bar");
  ASSERT_EQ(3U, sv.size()) << "split1 size";
  if (sv.size() == 3) {
    ASSERT_EQ(sv[0], "/") << "split1.1";
    ASSERT_EQ(sv[1], "foo") << "split1.2";
    ASSERT_EQ(sv[2], "bar") << "split1.3";
  }
  EXPECT_STREQ("/foo/bar", Path::join(sv.begin(), sv.end()).c_str()) << "split1.4";

  sv = Path::split("foo/bar");
  ASSERT_EQ(2U, sv.size()) << "split2 size";
  if (sv.size() == 2) 
  {
    ASSERT_EQ(sv[0], "foo") << "split2.2";
    ASSERT_EQ(sv[1], "bar") << "split2.3";
  }
  EXPECT_STREQ("foo/bar", Path::join(sv.begin(), sv.end()).c_str())
    << "split2.3";

  sv = Path::split("foo//bar/");
  ASSERT_EQ(2U, sv.size()) << "split3 size";
  if (sv.size() == 2) 
  {
    ASSERT_EQ(sv[0], "foo") << "split3.2";
    ASSERT_EQ(sv[1], "bar") << "split3.3";
  }
  EXPECT_STREQ("foo/bar", Path::join(sv.begin(), sv.end()).c_str())
    << "split3.4";

#endif 


}
Example #5
0
void PathTest::RunTests()
{
  std::string sep(Path::sep);
  
  // test static exists()
  {
  }

  // test static getParent()
  {
#ifdef NTA_PLATFORM_win32
// no tests defined
#else
    std::string g = "/a/b/c/g.ext";
    g = Path::getParent(g);
    TESTEQUAL2_STR("getParent1", "/a/b/c", g.c_str());

    g = Path::getParent(g);
    TESTEQUAL2_STR("getParent2", "/a/b", g.c_str());

    g = Path::getParent(g);
    TESTEQUAL2_STR("getParent3", "/a", g.c_str());

    g = Path::getParent(g);
    TESTEQUAL2_STR("getParent4", "/", g.c_str());

    g = Path::getParent(g);
    TESTEQUAL2_STR("getParent5", "/", g.c_str());
    
    // Parent should normalize first, to avoid parent(a/b/..)->(a/b)
    g = "/a/b/..";
    TESTEQUAL2_STR("getParent6", "/", Path::getParent(g).c_str());

    // getParent() of a relative directory may be a bit non-intuitive
    g = "a/b";
    TESTEQUAL2_STR("getParent7", "a", Path::getParent(g).c_str());

    g = "a";
    TESTEQUAL2_STR("getParent8", ".", Path::getParent(g).c_str());
    
    // getParent() of a relative directory above us should work
    g = "../../a";
    TESTEQUAL2_STR("getParent9", "../..", Path::getParent(g).c_str());

    g = ".";
    TESTEQUAL2_STR("getParent10", "..", Path::getParent(g).c_str());
    
#endif

    
    std::string x = Path::join("someDir", "X");
    x = Path::makeAbsolute(x);
    std::string y = Path::join(x, "Y");

    
    std::string parent = Path::getParent(y);
    TEST(x == parent);

  }

  // test static getFilename()
  {
  }

  // test static getBasename()
  {
#ifdef NTA_PLATFORM_win32
// no tests defined
#else
    TESTEQUAL2_STR("basename1", "bar", Path::getBasename("/foo/bar").c_str());
    TESTEQUAL2_STR("basename2", "", Path::getBasename("/foo/bar/").c_str());
    TESTEQUAL2_STR("basename3", "bar.ext", Path::getBasename("/this is a long dir / foo$/bar.ext").c_str());
#endif
  }
  
  // test static getExtension()
  {
    std::string ext = Path::getExtension("abc" + sep + "def.ext");
    TEST(ext == "ext");
  }

  // test static normalize()
  {
#ifdef NTA_PLATFORM_win32
// no tests defined
#else
    TESTEQUAL2_STR("normalize1", "/foo/bar", Path::normalize("//foo/quux/..//bar").c_str());
    TESTEQUAL2_STR("normalize2", "/foo/contains a lot of spaces", 
         Path::normalize("///foo/a/b/c/../../d/../../contains a lot of spaces/g.tgz/..").c_str());
    TESTEQUAL2_STR("normalize3", "../..", Path::normalize("../foo/../..").c_str());
    TESTEQUAL2_STR("normalize4", "/", Path::normalize("/../..").c_str());
#endif         

  }

  // test static makeAbsolute()
  {
  }

  // test static split()
  {
#ifdef NTA_PLATFORM_win32
// no tests defined
#else
    Path::StringVec sv;
    sv = Path::split("/foo/bar");
    TESTEQUAL2("split1 size", 3U, sv.size());
    if (sv.size() == 3) {
      TESTEQUAL2("split1.1", sv[0], "/");
      TESTEQUAL2("split1.2", sv[1], "foo");
      TESTEQUAL2("split1.3", sv[2], "bar");
    }
    TESTEQUAL2_STR("split1.4", "/foo/bar", Path::join(sv.begin(), sv.end()).c_str());

    sv = Path::split("foo/bar");
    TESTEQUAL2("split2 size", 2U, sv.size());
    if (sv.size() == 2) 
    {
      TESTEQUAL2("split2.2", sv[0], "foo");
      TESTEQUAL2("split2.3", sv[1], "bar");
    }
    TESTEQUAL2_STR("split2.3", "foo/bar", Path::join(sv.begin(), sv.end()).c_str());

    sv = Path::split("foo//bar/");
    TESTEQUAL2("split3 size", 2U, sv.size());
    if (sv.size() == 2) 
    {
      TESTEQUAL2("split3.2", sv[0], "foo");
      TESTEQUAL2("split3.3", sv[1], "bar");
    }
    TESTEQUAL2_STR("split3.4", "foo/bar", Path::join(sv.begin(), sv.end()).c_str());

#endif 


  }

  // test static join()
  {
  }

  // test static remove()
  {
  }

  // test static rename()
  {
  }
  
  // test static copy()
  {
    {
      OFStream f("a.txt");
      f << "12345";
    }

    {
      std::string s;
      IFStream f("a.txt");
      f >> s;
      TEST(s == "12345");
    }
    
    {
      if (Path::exists("b.txt"))
        Path::remove("b.txt");
      TEST(!Path::exists("b.txt"));
      Path::copy("a.txt", "b.txt");
      TEST(Path::exists("b.txt"));
      std::string s;
      IFStream f("b.txt");
      f >> s;
      TEST(s == "12345");
    }
    
    Path::remove("a.txt");
    Path::remove("b.txt");
    TEST(!Path::exists("a.txt"));
    TEST(!Path::exists("b.txt"));
  }    

  // test static copy() in temp directory
  {
    {
      OFStream f("a.txt");
      f << "12345";
    }

    {
      std::string s;
      IFStream f("a.txt");
      f >> s;
      TEST(s == "12345");
    }
    
    string destination = fromTestOutputDir("pathtest_dir");
    {
      destination += "b.txt";
      if (Path::exists(destination))
        Path::remove(destination);
      TEST(!Path::exists(destination));
      Path::copy("a.txt", destination);
      TEST(Path::exists(destination));
      std::string s;
      IFStream f(destination.c_str());
      f >> s;
      TEST(s == "12345");
    }
    
    Path::remove("a.txt");
    Path::remove(destination);
    TEST(!Path::exists("a.txt"));
    TEST(!Path::exists(destination));
  }    
  
  //test static isRootdir()
  {
  }

  //test static isAbsolute()
  {
  #ifdef NTA_PLATFORM_win32
    TEST(Path::isAbsolute("c:"));
    TEST(Path::isAbsolute("c:\\"));
    TEST(Path::isAbsolute("c:\\foo\\"));
    TEST(Path::isAbsolute("c:\\foo\\bar"));    
    
    TEST(Path::isAbsolute("\\\\foo"));    
    TEST(Path::isAbsolute("\\\\foo\\"));    
    TEST(Path::isAbsolute("\\\\foo\\bar"));
    TEST(Path::isAbsolute("\\\\foo\\bar\\baz"));
       
    TEST(!Path::isAbsolute("foo"));        
    TEST(!Path::isAbsolute("foo\\bar"));        
    TEST(!Path::isAbsolute("\\"));
    TEST(!Path::isAbsolute("\\\\"));
    TEST(!Path::isAbsolute("\\foo"));
  #else
    TEST(Path::isAbsolute("/"));
    TEST(Path::isAbsolute("/foo"));
    TEST(Path::isAbsolute("/foo/"));
    TEST(Path::isAbsolute("/foo/bar"));    
        
    TEST(!Path::isAbsolute("foo"));        
    TEST(!Path::isAbsolute("foo/bar"));        
  #endif 
  }

  { 
    // test static getExecutablePath
    std::string path = Path::getExecutablePath();
    std::cout << "Executable path: '" << path << "'\n";
    TEST(Path::exists(path));

    std::string basename = Path::getBasename(path);
#ifdef NTA_PLATFORM_win32
    TESTEQUAL2_STR("basename should be unit_tests", basename.c_str(), "unit_tests.exe");
#else
    TESTEQUAL2_STR("basename should be unit_tests", basename.c_str(), "unit_tests");
#endif
  }    


}