Skip to content

Commit 2fdd456

Browse files
committed
[unittests] Factor out TestContext helper for AST tests.
This is used to set up a very simple AST context so that AST nodes can be allocated as usual. No functionality change.
1 parent 888a32b commit 2fdd456

File tree

4 files changed

+118
-72
lines changed

4 files changed

+118
-72
lines changed

unittests/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_swift_unittest(SwiftASTTests
22
OverrideTests.cpp
3+
TestContext.cpp
34
VersionRangeLattice.cpp
45
)
56

unittests/AST/OverrideTests.cpp

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "TestContext.h"
1314
#include "swift/AST/ASTContext.h"
1415
#include "swift/AST/DiagnosticEngine.h"
1516
#include "swift/AST/Module.h"
@@ -24,78 +25,7 @@
2425
#include "gtest/gtest.h"
2526

2627
using namespace swift;
27-
28-
namespace {
29-
/// Helper class used to set the LangOpts target before initializing the
30-
/// ASTContext.
31-
///
32-
/// \see TestContext
33-
class TestContextBase {
34-
public:
35-
LangOptions LangOpts;
36-
SearchPathOptions SearchPathOpts;
37-
SourceManager SourceMgr;
38-
DiagnosticEngine Diags;
39-
40-
TestContextBase() : Diags(SourceMgr) {
41-
LangOpts.Target = llvm::Triple(llvm::sys::getProcessTriple());
42-
}
43-
};
44-
45-
enum ShouldDeclareOptionalTypes : bool {
46-
DoNotDeclareOptionalTypes,
47-
DeclareOptionalTypes
48-
};
49-
50-
/// Owns an ASTContext and the associated types.
51-
class TestContext : public TestContextBase {
52-
SourceFile *FileForLookups;
53-
54-
void declareOptionalType(Identifier name) {
55-
auto wrapped = new (Ctx) GenericTypeParamDecl(FileForLookups,
56-
Ctx.getIdentifier("Wrapped"),
57-
SourceLoc(), /*depth*/0,
58-
/*index*/0);
59-
auto params = GenericParamList::create(Ctx, SourceLoc(), wrapped,
60-
SourceLoc());
61-
auto decl = new (Ctx) EnumDecl(SourceLoc(), name, SourceLoc(),
62-
/*inherited*/{}, params, FileForLookups);
63-
wrapped->setDeclContext(decl);
64-
FileForLookups->Decls.push_back(decl);
65-
}
66-
67-
public:
68-
ASTContext Ctx;
69-
70-
TestContext(ShouldDeclareOptionalTypes optionals = DoNotDeclareOptionalTypes)
71-
: Ctx(LangOpts, SearchPathOpts, SourceMgr, Diags) {
72-
auto stdlibID = Ctx.getIdentifier(STDLIB_NAME);
73-
auto *module = ModuleDecl::create(stdlibID, Ctx);
74-
Ctx.LoadedModules[stdlibID] = module;
75-
76-
using ImplicitModuleImportKind = SourceFile::ImplicitModuleImportKind;
77-
FileForLookups = new (Ctx) SourceFile(*module, SourceFileKind::Library,
78-
/*buffer*/None,
79-
ImplicitModuleImportKind::None);
80-
module->addFile(*FileForLookups);
81-
82-
if (optionals == DeclareOptionalTypes) {
83-
declareOptionalType(Ctx.getIdentifier("Optional"));
84-
declareOptionalType(Ctx.getIdentifier("ImplicitlyUnwrappedOptional"));
85-
}
86-
}
87-
88-
template <typename Nominal>
89-
Nominal *makeNominal(StringRef name,
90-
GenericParamList *genericParams = nullptr) {
91-
auto result = new (Ctx) Nominal(SourceLoc(), Ctx.getIdentifier(name),
92-
SourceLoc(), /*inherited*/{},
93-
genericParams, FileForLookups);
94-
result->setAccessibility(Accessibility::Internal);
95-
return result;
96-
}
97-
};
98-
} // end anonymous namespace
28+
using namespace swift::unittest;
9929

10030
TEST(Override, IdenticalTypes) {
10131
TestContext C;

unittests/AST/TestContext.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===--- TestContext.cpp - Helper for setting up ASTContexts --------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "TestContext.h"
14+
#include "swift/AST/Module.h"
15+
#include "swift/Strings.h"
16+
17+
using namespace swift;
18+
using namespace swift::unittest;
19+
20+
21+
static void declareOptionalType(ASTContext &ctx, SourceFile *fileForLookups,
22+
Identifier name) {
23+
auto wrapped = new (ctx) GenericTypeParamDecl(fileForLookups,
24+
ctx.getIdentifier("Wrapped"),
25+
SourceLoc(), /*depth*/0,
26+
/*index*/0);
27+
auto params = GenericParamList::create(ctx, SourceLoc(), wrapped,
28+
SourceLoc());
29+
auto decl = new (ctx) EnumDecl(SourceLoc(), name, SourceLoc(),
30+
/*inherited*/{}, params, fileForLookups);
31+
wrapped->setDeclContext(decl);
32+
fileForLookups->Decls.push_back(decl);
33+
}
34+
35+
TestContext::TestContext(ShouldDeclareOptionalTypes optionals)
36+
: Ctx(LangOpts, SearchPathOpts, SourceMgr, Diags) {
37+
auto stdlibID = Ctx.getIdentifier(STDLIB_NAME);
38+
auto *module = ModuleDecl::create(stdlibID, Ctx);
39+
Ctx.LoadedModules[stdlibID] = module;
40+
41+
using ImplicitModuleImportKind = SourceFile::ImplicitModuleImportKind;
42+
FileForLookups = new (Ctx) SourceFile(*module, SourceFileKind::Library,
43+
/*buffer*/None,
44+
ImplicitModuleImportKind::None);
45+
module->addFile(*FileForLookups);
46+
47+
if (optionals == DeclareOptionalTypes) {
48+
declareOptionalType(Ctx, FileForLookups, Ctx.getIdentifier("Optional"));
49+
declareOptionalType(Ctx, FileForLookups,
50+
Ctx.getIdentifier("ImplicitlyUnwrappedOptional"));
51+
}
52+
}

unittests/AST/TestContext.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===--- TestContext.h - Helper for setting up ASTContexts ------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/ASTContext.h"
14+
#include "swift/AST/DiagnosticEngine.h"
15+
#include "swift/Basic/LangOptions.h"
16+
#include "swift/Basic/SourceManager.h"
17+
18+
namespace swift {
19+
namespace unittest {
20+
21+
/// Helper class used to set the LangOpts target before initializing the
22+
/// ASTContext.
23+
///
24+
/// \see TestContext
25+
class TestContextBase {
26+
public:
27+
LangOptions LangOpts;
28+
SearchPathOptions SearchPathOpts;
29+
SourceManager SourceMgr;
30+
DiagnosticEngine Diags;
31+
32+
TestContextBase() : Diags(SourceMgr) {
33+
LangOpts.Target = llvm::Triple(llvm::sys::getProcessTriple());
34+
}
35+
};
36+
37+
enum ShouldDeclareOptionalTypes : bool {
38+
DoNotDeclareOptionalTypes,
39+
DeclareOptionalTypes
40+
};
41+
42+
/// Owns an ASTContext and the associated types.
43+
class TestContext : public TestContextBase {
44+
SourceFile *FileForLookups;
45+
46+
public:
47+
ASTContext Ctx;
48+
49+
TestContext(ShouldDeclareOptionalTypes optionals = DoNotDeclareOptionalTypes);
50+
51+
template <typename Nominal>
52+
Nominal *makeNominal(StringRef name,
53+
GenericParamList *genericParams = nullptr) {
54+
auto result = new (Ctx) Nominal(SourceLoc(), Ctx.getIdentifier(name),
55+
SourceLoc(), /*inherited*/{},
56+
genericParams, FileForLookups);
57+
result->setAccessibility(Accessibility::Internal);
58+
return result;
59+
}
60+
};
61+
62+
} // end namespace unittest
63+
} // end namespace swift

0 commit comments

Comments
 (0)