Skip to content

Commit 6a19d37

Browse files
authored
Merge pull request #34286 from xedin/add-sema-unittest-fixture
[unittests] Add a fixture for Sema unit tests
2 parents 5d21200 + fde1af7 commit 6a19d37

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,6 +4942,7 @@ class ConstraintSystem {
49424942
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) const;
49434943
Optional<PotentialBindings> determineBestBindings();
49444944

4945+
public:
49454946
/// Infer bindings for the given type variable based on current
49464947
/// state of the constraint system.
49474948
PotentialBindings inferBindingsFor(TypeVariableType *typeVar,

unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- BindingInferenceTests.cpp ----------------------------------------===//
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/Expr.h"
15+
#include "swift/Sema/ConstraintSystem.h"
16+
17+
using namespace swift;
18+
using namespace swift::unittest;
19+
using namespace swift::constraints;
20+
21+
TEST_F(SemaTest, TestIntLiteralBindingInference) {
22+
ConstraintSystemOptions options;
23+
options |= ConstraintSystemFlags::AllowUnresolvedTypeVariables;
24+
25+
ConstraintSystem cs(DC, options);
26+
27+
auto *intLiteral = IntegerLiteralExpr::createFromUnsigned(Context, 42);
28+
29+
auto *literalTy = cs.createTypeVariable(cs.getConstraintLocator(intLiteral),
30+
/*options=*/0);
31+
32+
cs.addConstraint(
33+
ConstraintKind::LiteralConformsTo, literalTy,
34+
Context.getProtocol(KnownProtocolKind::ExpressibleByIntegerLiteral)
35+
->getDeclaredInterfaceType(),
36+
cs.getConstraintLocator(intLiteral));
37+
38+
auto bindings = cs.inferBindingsFor(literalTy);
39+
40+
ASSERT_EQ(bindings.Bindings.size(), (unsigned)1);
41+
42+
const auto &binding = bindings.Bindings.front();
43+
44+
ASSERT_TRUE(binding.BindingType->isEqual(getStdlibType("Int")));
45+
ASSERT_TRUE(binding.hasDefaultedLiteralProtocol());
46+
}

unittests/Sema/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
add_swift_unittest(swiftSemaTests
3+
SemaFixture.cpp
4+
BindingInferenceTests.cpp)
5+
6+
target_link_libraries(swiftSemaTests
7+
PRIVATE
8+
swiftAST
9+
swiftSema
10+
swiftSerialization)
11+
12+
target_compile_definitions(swiftSemaTests PRIVATE
13+
SWIFTLIB_DIR=\"${SWIFTLIB_DIR}\")

unittests/Sema/SemaFixture.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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/Decl.h"
15+
#include "swift/AST/Import.h"
16+
#include "swift/AST/Module.h"
17+
#include "swift/AST/ParseRequests.h"
18+
#include "swift/AST/SourceFile.h"
19+
#include "swift/AST/Type.h"
20+
#include "swift/AST/Types.h"
21+
#include "swift/Basic/LLVMInitialize.h"
22+
#include "swift/ClangImporter/ClangImporter.h"
23+
#include "swift/Serialization/SerializedModuleLoader.h"
24+
#include "swift/Subsystems.h"
25+
26+
using namespace swift;
27+
using namespace swift::unittest;
28+
29+
SemaTest::SemaTest()
30+
: Context(*ASTContext::get(LangOpts, TypeCheckerOpts, SearchPathOpts,
31+
ClangImporterOpts, SourceMgr, Diags)) {
32+
INITIALIZE_LLVM();
33+
34+
registerParseRequestFunctions(Context.evaluator);
35+
registerTypeCheckerRequestFunctions(Context.evaluator);
36+
37+
Context.addModuleLoader(ImplicitSerializedModuleLoader::create(Context));
38+
Context.addModuleLoader(ClangImporter::create(Context), /*isClang=*/true);
39+
40+
auto *stdlib = Context.getStdlibModule(/*loadIfAbsent=*/true);
41+
assert(stdlib && "Failed to load standard library");
42+
43+
auto *module =
44+
ModuleDecl::create(Context.getIdentifier("SemaTests"), Context);
45+
46+
MainFile = new (Context) SourceFile(*module, SourceFileKind::Main,
47+
/*buffer=*/None);
48+
49+
AttributedImport<ImportedModule> stdlibImport{{ImportPath::Access(), stdlib},
50+
/*options=*/{}};
51+
52+
MainFile->setImports(stdlibImport);
53+
module->addFile(*MainFile);
54+
55+
DC = module;
56+
}
57+
58+
Type SemaTest::getStdlibType(StringRef name) const {
59+
auto typeName = Context.getIdentifier(name);
60+
61+
auto *stdlib = Context.getStdlibModule();
62+
63+
llvm::SmallVector<ValueDecl *, 4> results;
64+
stdlib->lookupValue(typeName, NLKind::UnqualifiedLookup, results);
65+
66+
if (results.size() != 1)
67+
return Type();
68+
69+
if (auto *decl = dyn_cast<TypeDecl>(results.front())) {
70+
if (auto *NTD = dyn_cast<NominalTypeDecl>(decl))
71+
return NTD->getDeclaredType();
72+
return decl->getDeclaredInterfaceType();
73+
}
74+
75+
return Type();
76+
}

unittests/Sema/SemaFixture.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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/AST/Type.h"
18+
#include "swift/Basic/LangOptions.h"
19+
#include "swift/Basic/Platform.h"
20+
#include "swift/Basic/SourceManager.h"
21+
#include "llvm/ADT/StringRef.h"
22+
#include "llvm/ADT/SmallString.h"
23+
#include "llvm/Support/Host.h"
24+
#include "llvm/Support/Path.h"
25+
#include "gtest/gtest.h"
26+
#include <string>
27+
28+
namespace swift {
29+
namespace unittest {
30+
31+
class SemaTestBase : public ::testing::Test {
32+
public:
33+
LangOptions LangOpts;
34+
TypeCheckerOptions TypeCheckerOpts;
35+
SearchPathOptions SearchPathOpts;
36+
ClangImporterOptions ClangImporterOpts;
37+
SourceManager SourceMgr;
38+
DiagnosticEngine Diags;
39+
40+
SemaTestBase() : Diags(SourceMgr) {
41+
LangOpts.Target = llvm::Triple(llvm::sys::getDefaultTargetTriple());
42+
43+
llvm::SmallString<128> libDir(SWIFTLIB_DIR);
44+
llvm::sys::path::append(libDir, getPlatformNameForTriple(LangOpts.Target));
45+
46+
SearchPathOpts.RuntimeResourcePath = SWIFTLIB_DIR;
47+
SearchPathOpts.RuntimeLibraryPaths.push_back(std::string(libDir.str()));
48+
SearchPathOpts.RuntimeLibraryImportPaths.push_back(
49+
std::string(libDir.str()));
50+
}
51+
};
52+
53+
/// Owns an ASTContext and the associated types.
54+
class SemaTest : public SemaTestBase {
55+
SourceFile *MainFile;
56+
57+
public:
58+
ASTContext &Context;
59+
DeclContext *DC;
60+
61+
SemaTest();
62+
63+
protected:
64+
Type getStdlibType(StringRef name) const;
65+
};
66+
67+
} // end namespace unittest
68+
} // end namespace swift

0 commit comments

Comments
 (0)