| /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying | |
| file LICENSE.rst or https://cmake.org/licensing for details. */ | |
| #include <string> | |
| #include "cmOutputConverter.h" | |
| #include "cmSystemTools.h" | |
| #include "testCommon.h" | |
| namespace { | |
| bool testPathEqOrSubDir() | |
| { | |
| std::cout << "testPathEqOrSubDir()\n"; | |
| // A leading tilde stands for HOME. | |
| cmSystemTools::PutEnv("HOME=/h"); | |
| struct Case | |
| { | |
| char const* A; | |
| char const* B; | |
| }; | |
| // The first ten cases take the fast path, the others do not. | |
| static Case const cases[] = { | |
| { "/a/b", "/a/b" }, | |
| { "/a/b/c", "/a/b" }, | |
| { "/a/bc", "/a/b" }, | |
| { "/a/b.c", "/a/b" }, | |
| { "/a", "/a/b" }, | |
| { "/a/b", "/a/c" }, | |
| { "/a/b/../c", "/a/b" }, | |
| { "/A/B/c", "/a/b" }, | |
| { "C:/a/b", "C:/a" }, | |
| { "C:/a/b", "c:/A" }, | |
| { "/a/b", "/" }, | |
| { "C:/a", "C:/" }, | |
| { "/a/b/", "/a/b" }, | |
| { "/a/b", "/a/b/" }, | |
| { "/a//b/c", "/a/b" }, | |
| { "//s/h/x", "//s/h" }, | |
| { "\\a\\b\\c", "/a/b" }, | |
| { "/a/b\\c", "/a/b" }, | |
| { "~/x", "~" }, | |
| { "/h/x", "~" }, | |
| { "", "/a" }, | |
| { "/a", "" }, | |
| { "", "" }, | |
| }; | |
| for (Case const& c : cases) { | |
| bool const expected = cmSystemTools::ComparePath(c.A, c.B) || | |
| cmSystemTools::IsSubDirectory(c.A, c.B); | |
| bool const actual = cmOutputConverter::PathEqOrSubDir(c.A, c.B); | |
| if (actual != expected) { | |
| std::cout << "PathEqOrSubDir(\"" << c.A << "\", \"" << c.B << "\") is " | |
| << BOOL_STRING(actual) << ", expected " | |
| << BOOL_STRING(expected) << '\n'; | |
| return false; | |
| } | |
| } | |
| // Results that do not depend on the platform. | |
| ASSERT_TRUE(cmOutputConverter::PathEqOrSubDir("/a/b", "/a/b")); | |
| ASSERT_TRUE(cmOutputConverter::PathEqOrSubDir("/a/b/c", "/a/b")); | |
| ASSERT_TRUE(!cmOutputConverter::PathEqOrSubDir("/a/bc", "/a/b")); | |
| ASSERT_TRUE(!cmOutputConverter::PathEqOrSubDir("/a", "/a/b")); | |
| return true; | |
| } | |
| } | |
| int testOutputConverter(int /*unused*/, char* /*unused*/[]) | |
| { | |
| return runTests({ testPathEqOrSubDir }); | |
| } |