Skip to content

Commit f0cfc0b

Browse files
committed
[NFC] Add unit tests for feature option parsing
1 parent 90d1365 commit f0cfc0b

10 files changed

+602
-3
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
/unittests/AST/ @hborla @slavapestov @xedin
270270
/unittests/AST/*Evaluator* @CodaFi @slavapestov
271271
/unittests/DependencyScan/ @artemcm @cachemeifyoucan
272-
/unittests/FrontendTool/ @artemcm @tshortli
272+
/unittests/Frontend*/ @artemcm @tshortli
273273
/unittests/Parse/ @ahoppen @bnbarham @CodaFi @DougGregor @hamishknight @rintaro
274274
/unittests/Reflection/ @slavapestov
275275
/unittests/SIL/ @jckarter

unittests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if(SWIFT_INCLUDE_TOOLS)
88
add_subdirectory(Basic)
99
add_subdirectory(ClangImporter)
1010
add_subdirectory(DependencyScan)
11+
add_subdirectory(Frontend)
1112
add_subdirectory(FrontendTool)
1213
add_subdirectory(Localization)
1314
add_subdirectory(IDE)

unittests/Frontend/ArgParsingTest.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===-- ArgParsingTest.cpp --------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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 "ArgParsingTest.h"
14+
15+
using namespace swift;
16+
17+
ArgParsingTest::ArgParsingTest() : diags(sourceMgr) {}
18+
19+
void ArgParsingTest::parseArgs(const Args &args) {
20+
std::vector<const char *> adjustedArgs;
21+
22+
if (this->langMode) {
23+
adjustedArgs.reserve(args.size() + 2);
24+
adjustedArgs.push_back("-swift-version");
25+
adjustedArgs.push_back(this->langMode->data());
26+
} else {
27+
adjustedArgs.reserve(args.size());
28+
}
29+
30+
for (auto &arg : args) {
31+
adjustedArgs.push_back(arg.data());
32+
}
33+
34+
this->invocation.parseArgs(adjustedArgs, this->diags);
35+
}
36+
37+
LangOptions &ArgParsingTest::getLangOptions() {
38+
return this->invocation.getLangOptions();
39+
}
40+
41+
void PrintTo(const Args &value, std::ostream *os) {
42+
*os << '"';
43+
44+
if (!value.empty()) {
45+
const auto lastIdx = value.size() - 1;
46+
for (size_t idx = 0; idx != lastIdx; ++idx) {
47+
*os << value[idx] << ' ';
48+
}
49+
*os << value[lastIdx];
50+
}
51+
52+
*os << '"';
53+
}

unittests/Frontend/ArgParsingTest.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===-- ArgParsingTest.h ----------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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+
#ifndef ARG_PARSING_TEST_H
14+
#define ARG_PARSING_TEST_H
15+
16+
#include "swift/Frontend/Frontend.h"
17+
#include "gtest/gtest.h"
18+
19+
using Args = std::vector<std::string>;
20+
21+
class ArgParsingTest : public ::testing::Test {
22+
swift::CompilerInvocation invocation;
23+
swift::SourceManager sourceMgr;
24+
swift::DiagnosticEngine diags;
25+
26+
protected:
27+
std::optional<std::string> langMode;
28+
29+
public:
30+
ArgParsingTest();
31+
32+
void parseArgs(const Args &args);
33+
34+
swift::LangOptions &getLangOptions();
35+
};
36+
37+
template <typename T>
38+
struct ArgParsingTestCase final {
39+
Args args;
40+
T expectedResult;
41+
42+
ArgParsingTestCase(Args args, T expectedResult)
43+
: args(std::move(args)), expectedResult(std::move(expectedResult)) {}
44+
};
45+
46+
// MARK: - Printers
47+
48+
void PrintTo(const Args &, std::ostream *);
49+
50+
template <typename T>
51+
void PrintTo(const ArgParsingTestCase<T> &value, std::ostream *os) {
52+
PrintTo(value.args, os);
53+
}
54+
55+
#endif // ARG_PARSING_TEST_H

unittests/Frontend/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_swift_unittest(SwiftFrontendTests
2+
ArgParsingTest.cpp
3+
FeatureParsingTest.cpp
4+
IsFeatureEnabledTests.cpp
5+
StrictConcurrencyTests.cpp)
6+
7+
target_link_libraries(SwiftFrontendTests
8+
PRIVATE
9+
swiftFrontend)
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===-- FeatureParsingTest.cpp ----------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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 "FeatureParsingTest.h"
14+
15+
using namespace swift;
16+
17+
const std::string FeatureParsingTest::defaultLangMode = "5";
18+
19+
FeatureParsingTest::FeatureParsingTest() : ArgParsingTest() {
20+
this->langMode = defaultLangMode;
21+
}
22+
23+
FeatureWrapper::FeatureWrapper(Feature id)
24+
: id(id), name(getFeatureName(id).data()) {
25+
auto langMode = getFeatureLanguageVersion(id);
26+
if (langMode) {
27+
this->langMode = std::to_string(*langMode);
28+
}
29+
}
30+
31+
void swift::PrintTo(const StrictConcurrency &value, std::ostream *os) {
32+
switch (value) {
33+
case StrictConcurrency::Minimal:
34+
*os << "Minimal";
35+
break;
36+
case StrictConcurrency::Targeted:
37+
*os << "Targeted";
38+
break;
39+
case StrictConcurrency::Complete:
40+
*os << "Complete";
41+
break;
42+
}
43+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===-- FeatureParsingTest.h ------------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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+
#ifndef FEATURE_PARSING_TEST_H
14+
#define FEATURE_PARSING_TEST_H
15+
16+
#include "ArgParsingTest.h"
17+
#include "swift/Basic/Feature.h"
18+
19+
struct FeatureParsingTest : public ArgParsingTest {
20+
static const std::string defaultLangMode;
21+
22+
FeatureParsingTest();
23+
};
24+
25+
struct FeatureWrapper final {
26+
swift::Feature id;
27+
std::string name;
28+
std::string langMode;
29+
30+
FeatureWrapper(swift::Feature id);
31+
32+
operator swift::Feature() const { return id; }
33+
};
34+
35+
// MARK: - Printers
36+
37+
// Google Test requires custom printers to be declared in the same namespace as
38+
// the type they take.
39+
namespace swift {
40+
void PrintTo(const StrictConcurrency &, std::ostream *);
41+
} // end namespace swift
42+
43+
#endif // FEATURE_PARSING_TEST_H

0 commit comments

Comments
 (0)