|
| 1 | +//===--- ImportTests.cpp - Tests for representation of imports ------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "TestContext.h" |
| 14 | +#include "swift/AST/Import.h" |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +using namespace swift; |
| 18 | +using namespace swift::unittest; |
| 19 | + |
| 20 | +namespace { |
| 21 | +/// Helper class used to create ImportPath and hold all strings for identifiers |
| 22 | +class ImportPathContext { |
| 23 | + TestContext Ctx; |
| 24 | + |
| 25 | +public: |
| 26 | + ImportPathContext() = default; |
| 27 | + |
| 28 | + /// Hepler routine for building ImportPath |
| 29 | + /// Build() |
| 30 | + /// @see ImportPathBuilder |
| 31 | + inline ImportPath Build(StringRef Name) noexcept { |
| 32 | + return ImportPath::Builder(Ctx.Ctx, Name, '.').copyTo(Ctx.Ctx); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +} // namespace |
| 37 | + |
| 38 | +TEST(ImportPath, Comparison) { |
| 39 | + ImportPathContext ctx; |
| 40 | + |
| 41 | + /// Simple sanity check: |
| 42 | + EXPECT_FALSE(ctx.Build("A.B.C") < ctx.Build("A.B.C")); |
| 43 | + |
| 44 | + /// Check order chain: |
| 45 | + /// A < A.A < A.A.A < A.A.B < A.B < A.B.A < AA < B < B.A |
| 46 | + EXPECT_LT(ctx.Build("A"), ctx.Build("A.A")); |
| 47 | + EXPECT_LT(ctx.Build("A.A"), ctx.Build("A.A.A")); |
| 48 | + EXPECT_LT(ctx.Build("A.A.A"), ctx.Build("A.A.B")); |
| 49 | + EXPECT_LT(ctx.Build("A.A.B"), ctx.Build("A.B")); |
| 50 | + EXPECT_LT(ctx.Build("A.B"), ctx.Build("A.B.A")); |
| 51 | + EXPECT_LT(ctx.Build("A.B.A"), ctx.Build("AA")); |
| 52 | + EXPECT_LT(ctx.Build("B"), ctx.Build("B.A")); |
| 53 | + |
| 54 | + /// Further ImportPaths are semantically incorrect, but we must |
| 55 | + /// check that comparing them does not cause compiler to crash. |
| 56 | + EXPECT_LT(ctx.Build("."), ctx.Build("A")); |
| 57 | + EXPECT_LT(ctx.Build("A"), ctx.Build("AA.")); |
| 58 | + EXPECT_LT(ctx.Build("A"), ctx.Build("AA..")); |
| 59 | + EXPECT_LT(ctx.Build(".A"), ctx.Build("AA")); |
| 60 | +} |
0 commit comments