diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index d1592692088..772367f4db3 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -21,6 +21,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" #include "gtest/gtest.h" +#include "gmock/gmock.h" #ifdef LLVM_ON_WIN32 #include "llvm/ADT/ArrayRef.h" @@ -78,6 +79,7 @@ TEST(Support, Path) { paths.push_back("foo/bar"); paths.push_back("/foo/bar"); paths.push_back("//net"); + paths.push_back("//net/"); paths.push_back("//net/foo"); paths.push_back("///foo///"); paths.push_back("///foo///bar"); @@ -108,27 +110,30 @@ TEST(Support, Path) { paths.push_back("c:\\foo/"); paths.push_back("c:/foo\\bar"); - SmallVector ComponentStack; for (SmallVector::const_iterator i = paths.begin(), e = paths.end(); i != e; ++i) { + SCOPED_TRACE(*i); + SmallVector ComponentStack; for (sys::path::const_iterator ci = sys::path::begin(*i), ce = sys::path::end(*i); ci != ce; ++ci) { - ASSERT_FALSE(ci->empty()); + EXPECT_FALSE(ci->empty()); ComponentStack.push_back(*ci); } + SmallVector ReverseComponentStack; for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), ce = sys::path::rend(*i); ci != ce; ++ci) { - ASSERT_TRUE(*ci == ComponentStack.back()); - ComponentStack.pop_back(); + EXPECT_FALSE(ci->empty()); + ReverseComponentStack.push_back(*ci); } - ASSERT_TRUE(ComponentStack.empty()); + std::reverse(ReverseComponentStack.begin(), ReverseComponentStack.end()); + EXPECT_THAT(ComponentStack, testing::ContainerEq(ReverseComponentStack)); // Crash test most of the API - since we're iterating over all of our paths // here there isn't really anything reasonable to assert on in the results. @@ -171,115 +176,56 @@ TEST(Support, Path) { ASSERT_EQ("/root/foo.cpp", Relative); } -TEST(Support, RelativePathIterator) { - SmallString<64> Path(StringRef("c/d/e/foo.txt")); - typedef SmallVector PathComponents; - PathComponents ExpectedPathComponents; - PathComponents ActualPathComponents; +TEST(Support, FilenameParent) { + EXPECT_EQ("/", path::filename("/")); + EXPECT_EQ("", path::parent_path("/")); - StringRef(Path).split(ExpectedPathComponents, '/'); + EXPECT_EQ("\\", path::filename("c:\\", path::Style::windows)); + EXPECT_EQ("c:", path::parent_path("c:\\", path::Style::windows)); - for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; - ++I) { - ActualPathComponents.push_back(*I); - } + EXPECT_EQ("bar", path::filename("/foo/bar")); + EXPECT_EQ("/foo", path::parent_path("/foo/bar")); - ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); + EXPECT_EQ("foo", path::filename("/foo")); + EXPECT_EQ("/", path::parent_path("/foo")); - for (size_t i = 0; i Path(StringRef(".c/.d/../.")); - typedef SmallVector PathComponents; - PathComponents ExpectedPathComponents; - PathComponents ActualPathComponents; - - StringRef(Path).split(ExpectedPathComponents, '/'); - - for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; - ++I) { - ActualPathComponents.push_back(*I); - } - - ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); - - for (size_t i = 0; i +GetComponents(StringRef Path, path::Style S = path::Style::native) { + return {path::begin(Path, S), path::end(Path)}; } -TEST(Support, AbsolutePathIterator) { - SmallString<64> Path(StringRef("/c/d/e/foo.txt")); - typedef SmallVector PathComponents; - PathComponents ExpectedPathComponents; - PathComponents ActualPathComponents; - - StringRef(Path).split(ExpectedPathComponents, '/'); - - // The root path will also be a component when iterating - ExpectedPathComponents[0] = "/"; - - for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; - ++I) { - ActualPathComponents.push_back(*I); - } - - ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); - - for (size_t i = 0; i Path(StringRef("/.c/.d/../.")); - typedef SmallVector PathComponents; - PathComponents ExpectedPathComponents; - PathComponents ActualPathComponents; - - StringRef(Path).split(ExpectedPathComponents, '/'); - - // The root path will also be a component when iterating - ExpectedPathComponents[0] = "/"; - - for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; - ++I) { - ActualPathComponents.push_back(*I); - } - - ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); - - for (size_t i = 0; i Path(StringRef("c:\\c\\e\\foo.txt")); - typedef SmallVector PathComponents; - PathComponents ExpectedPathComponents; - PathComponents ActualPathComponents; - - StringRef(Path).split(ExpectedPathComponents, "\\"); - - // The root path (which comes after the drive name) will also be a component - // when iterating. - ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); - - for (path::const_iterator I = path::begin(Path, path::Style::windows), - E = path::end(Path); - I != E; ++I) { - ActualPathComponents.push_back(*I); - } - - ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); - - for (size_t i = 0; i