Skip to content

Commit 9239692

Browse files
committed
[unittests] Add a fixture for Sema unit tests
1 parent 0b456ba commit 9239692

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

unittests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ if(SWIFT_INCLUDE_TOOLS)
1212
add_subdirectory(Localization)
1313
add_subdirectory(IDE)
1414
add_subdirectory(Parse)
15+
add_subdirectory(Sema)
1516
add_subdirectory(SwiftDemangle)
1617
add_subdirectory(Syntax)
1718
if(SWIFT_BUILD_SYNTAXPARSERLIB)

unittests/Sema/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
add_swift_unittest(swiftSemaTests
3+
SemaFixture.cpp)
4+
5+
target_link_libraries(swiftSemaTests
6+
PRIVATE
7+
swiftSema)

unittests/Sema/SemaFixture.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===--- SemaFixture.cpp - Helper for setting up Sema context --------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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 "SemaFixture.h"
14+
#include "swift/AST/Module.h"
15+
#include "swift/AST/ParseRequests.h"
16+
#include "swift/Strings.h"
17+
#include "swift/Subsystems.h"
18+
19+
using namespace swift;
20+
using namespace swift::unittest;
21+
22+
SemaTest::SemaTest()
23+
: Context(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts,
24+
ClangImporterOpts, SourceMgr, Diags)) {
25+
registerParseRequestFunctions(Context.evaluator);
26+
registerTypeCheckerRequestFunctions(Context.evaluator);
27+
auto stdlibID = Context.getIdentifier(STDLIB_NAME);
28+
auto *module = ModuleDecl::create(stdlibID, Context);
29+
Context.addLoadedModule(module);
30+
31+
FileForLookups = new (Context) SourceFile(*module, SourceFileKind::Library,
32+
/*buffer*/ None);
33+
module->addFile(*FileForLookups);
34+
35+
DC = module;
36+
}

unittests/Sema/SemaFixture.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===--- SemaFixture.h - Helper for setting up Sema context -----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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 "swift/AST/ASTContext.h"
14+
#include "swift/AST/DiagnosticEngine.h"
15+
#include "swift/AST/Module.h"
16+
#include "swift/AST/SourceFile.h"
17+
#include "swift/Basic/LangOptions.h"
18+
#include "swift/Basic/SourceManager.h"
19+
#include "llvm/Support/Host.h"
20+
#include "gtest/gtest.h"
21+
22+
namespace swift {
23+
namespace unittest {
24+
25+
class SemaTestBase : public ::testing::Test {
26+
public:
27+
LangOptions LangOpts;
28+
TypeCheckerOptions TypeCheckerOpts;
29+
SearchPathOptions SearchPathOpts;
30+
ClangImporterOptions ClangImporterOpts;
31+
SourceManager SourceMgr;
32+
DiagnosticEngine Diags;
33+
34+
SemaTestBase() : Diags(SourceMgr) {
35+
LangOpts.Target = llvm::Triple(llvm::sys::getProcessTriple());
36+
}
37+
};
38+
39+
/// Owns an ASTContext and the associated types.
40+
class SemaTest : public SemaTestBase {
41+
SourceFile *FileForLookups;
42+
43+
public:
44+
ASTContext &Context;
45+
DeclContext *DC;
46+
47+
SemaTest();
48+
};
49+
50+
} // end namespace unittest
51+
} // end namespace swift

0 commit comments

Comments
 (0)