1111// ===----------------------------------------------------------------------===//
1212
1313#include " swift/Localization/LocalizationFormat.h"
14+ #include " llvm/ADT/ArrayRef.h"
1415#include " llvm/ADT/SmallString.h"
1516#include " llvm/ADT/StringExtras.h"
1617#include " llvm/ADT/StringRef.h"
18+ #include " llvm/Support/FileSystem.h"
1719#include " llvm/Support/Path.h"
1820#include " llvm/Support/Signals.h"
1921#include " llvm/Support/ToolOutputFile.h"
@@ -35,37 +37,75 @@ enum LocalDiagID : uint32_t {
3537 NumDiags
3638};
3739
40+ static constexpr const char *const diagnosticID[] = {
41+ #define DIAG (KIND, ID, Options, Text, Signature ) #ID,
42+ #include " swift/AST/DiagnosticsAll.def"
43+ };
44+
3845static constexpr const char *const diagnosticMessages[] = {
3946#define DIAG (KIND, ID, Options, Text, Signature ) Text,
4047#include " swift/AST/DiagnosticsAll.def"
4148};
4249
4350static std::string getMainExecutablePath () {
44- std::string libPath = llvm::sys::path::parent_path (SWIFTLIB_DIR);
51+ llvm::StringRef libPath = llvm::sys::path::parent_path (SWIFTLIB_DIR);
4552 llvm::SmallString<128 > MainExecutablePath (libPath);
4653 llvm::sys::path::remove_filename (MainExecutablePath); // Remove /lib
4754 llvm::sys::path::remove_filename (MainExecutablePath); // Remove /.
48- return std::string (MainExecutablePath. str () );
55+ return std::string (MainExecutablePath);
4956}
5057
5158static std::string getDefaultLocalizationPath () {
5259 llvm::SmallString<128 > DefaultDiagnosticMessagesDir (getMainExecutablePath ());
5360 llvm::sys::path::append (DefaultDiagnosticMessagesDir, " share" , " swift" ,
5461 " diagnostics" );
55- return std::string (DefaultDiagnosticMessagesDir. str () );
62+ return std::string (DefaultDiagnosticMessagesDir);
5663}
5764
58- static std::string getDefToYAMLConverterPath () {
59- llvm::SmallString<128 > defYAMLConverter (getMainExecutablePath ());
60- llvm::sys::path::append (defYAMLConverter, " bin" ,
61- " swift-def-to-yaml-converter" );
62- return std::string (defYAMLConverter.str ());
63- }
65+ struct DefToYAMLConverterTest : public ::testing::Test {
66+ std::string YAMLPath;
67+
68+ public:
69+ DefToYAMLConverterTest () {
70+ llvm::SmallString<128 > tempFilename;
71+ std::error_code error =
72+ llvm::sys::fs::createTemporaryFile (" en" , " yaml" , tempFilename);
73+ assert (!error);
74+
75+ YAMLPath = std::string (tempFilename);
76+ }
77+
78+ void SetUp () override {
79+ bool failed = convertDefIntoYAML (YAMLPath);
80+ assert (!failed && " failed to generate a YAML file" );
81+ }
82+
83+ void TearDown () override { llvm::sys::fs::remove (YAMLPath); }
84+
85+ // / Random number in [0,n)
86+ unsigned RandNumber (unsigned n) { return unsigned (rand ()) % n; }
6487
65- // / Random number in [0,n)
66- unsigned randNum (unsigned n) { return unsigned (rand ()) % n; }
88+ protected:
89+ static bool convertDefIntoYAML (std::string outputPath) {
90+ std::error_code error;
91+ llvm::raw_fd_ostream OS (outputPath, error, llvm::sys::fs::F_None);
92+ if (OS.has_error () || error)
93+ return true ;
6794
68- TEST (DefToYAMLConverterTest, missingLocalizationFiles) {
95+ llvm::ArrayRef<const char *> ids (diagnosticID, LocalDiagID::NumDiags);
96+ llvm::ArrayRef<const char *> messages (diagnosticMessages,
97+ LocalDiagID::NumDiags);
98+
99+ DefToYAMLConverter converter (ids, messages);
100+ converter.convert (OS);
101+
102+ OS.flush ();
103+
104+ return OS.has_error ();
105+ }
106+ };
107+
108+ TEST_F (DefToYAMLConverterTest, MissingLocalizationFiles) {
69109 ASSERT_TRUE (llvm::sys::fs::exists (getDefaultLocalizationPath ()));
70110 llvm::SmallString<128 > EnglishLocalization (getDefaultLocalizationPath ());
71111 llvm::sys::path::append (EnglishLocalization, " en" );
@@ -75,45 +115,23 @@ TEST(DefToYAMLConverterTest, missingLocalizationFiles) {
75115 ASSERT_TRUE (llvm::sys::fs::exists (EnglishLocalization));
76116}
77117
78- TEST (DefToYAMLConverterTest, matchDiagnosticMessagesSequentially) {
79- llvm::SmallString<128 > defYAMLConverter (getDefToYAMLConverterPath ());
80- defYAMLConverter.append (" --output-filename=" );
81-
82- llvm::SmallString<128 > tempFilename;
83- std::error_code EC =
84- llvm::sys::fs::createTemporaryFile (" en" , " yaml" , tempFilename);
85- ASSERT_FALSE (EC);
86- llvm::sys::RemoveFileOnSignal (tempFilename);
87- defYAMLConverter.append (tempFilename);
88- std::system (defYAMLConverter.c_str ());
89-
90- YAMLLocalizationProducer yaml (tempFilename.str ());
118+ TEST_F (DefToYAMLConverterTest, MatchDiagnosticMessagesSequentially) {
119+ YAMLLocalizationProducer yaml (YAMLPath);
91120 yaml.forEachAvailable ([](swift::DiagID id, llvm::StringRef translation) {
92121 llvm::StringRef msg = diagnosticMessages[static_cast <uint32_t >(id)];
93122 ASSERT_EQ (msg, translation);
94123 });
95124}
96125
97- TEST (DefToYAMLConverterTest, matchDiagnosticMessagesRandomly) {
98- llvm::SmallString<128 > defYAMLConverter (getDefToYAMLConverterPath ());
99- defYAMLConverter.append (" --output-filename=" );
100-
101- llvm::SmallString<128 > tempFilename;
102- std::error_code EC =
103- llvm::sys::fs::createTemporaryFile (" en" , " yaml" , tempFilename);
104- ASSERT_FALSE (EC);
105- llvm::sys::RemoveFileOnSignal (tempFilename);
106- defYAMLConverter.append (tempFilename);
107- std::system (defYAMLConverter.c_str ());
108-
109- YAMLLocalizationProducer yaml (tempFilename.str ());
126+ TEST_F (DefToYAMLConverterTest, MatchDiagnosticMessagesRandomly) {
127+ YAMLLocalizationProducer yaml (YAMLPath);
110128
111129 std::random_device rd;
112130 std::mt19937 gen (rd ());
113131 std::uniform_int_distribution<> distr (50 , LocalDiagID::NumDiags);
114132 unsigned numberOfQueries = distr (gen);
115133 while (numberOfQueries--) {
116- unsigned randomNum = randNum (LocalDiagID::NumDiags);
134+ unsigned randomNum = RandNumber (LocalDiagID::NumDiags);
117135 DiagID randomId = static_cast <DiagID>(randomNum);
118136 llvm::StringRef msg = diagnosticMessages[randomNum];
119137 llvm::StringRef translation = yaml.getMessageOr (randomId, " " );
0 commit comments