11
11
// ===----------------------------------------------------------------------===//
12
12
13
13
#include " swift/Localization/LocalizationFormat.h"
14
+ #include " llvm/ADT/ArrayRef.h"
14
15
#include " llvm/ADT/SmallString.h"
15
16
#include " llvm/ADT/StringExtras.h"
16
17
#include " llvm/ADT/StringRef.h"
18
+ #include " llvm/Support/FileSystem.h"
17
19
#include " llvm/Support/Path.h"
18
20
#include " llvm/Support/Signals.h"
19
21
#include " llvm/Support/ToolOutputFile.h"
@@ -35,37 +37,75 @@ enum LocalDiagID : uint32_t {
35
37
NumDiags
36
38
};
37
39
40
+ static constexpr const char *const diagnosticID[] = {
41
+ #define DIAG (KIND, ID, Options, Text, Signature ) #ID,
42
+ #include " swift/AST/DiagnosticsAll.def"
43
+ };
44
+
38
45
static constexpr const char *const diagnosticMessages[] = {
39
46
#define DIAG (KIND, ID, Options, Text, Signature ) Text,
40
47
#include " swift/AST/DiagnosticsAll.def"
41
48
};
42
49
43
50
static 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);
45
52
llvm::SmallString<128 > MainExecutablePath (libPath);
46
53
llvm::sys::path::remove_filename (MainExecutablePath); // Remove /lib
47
54
llvm::sys::path::remove_filename (MainExecutablePath); // Remove /.
48
- return std::string (MainExecutablePath. str () );
55
+ return std::string (MainExecutablePath);
49
56
}
50
57
51
58
static std::string getDefaultLocalizationPath () {
52
59
llvm::SmallString<128 > DefaultDiagnosticMessagesDir (getMainExecutablePath ());
53
60
llvm::sys::path::append (DefaultDiagnosticMessagesDir, " share" , " swift" ,
54
61
" diagnostics" );
55
- return std::string (DefaultDiagnosticMessagesDir. str () );
62
+ return std::string (DefaultDiagnosticMessagesDir);
56
63
}
57
64
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; }
64
87
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 ;
67
94
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) {
69
109
ASSERT_TRUE (llvm::sys::fs::exists (getDefaultLocalizationPath ()));
70
110
llvm::SmallString<128 > EnglishLocalization (getDefaultLocalizationPath ());
71
111
llvm::sys::path::append (EnglishLocalization, " en" );
@@ -75,45 +115,23 @@ TEST(DefToYAMLConverterTest, missingLocalizationFiles) {
75
115
ASSERT_TRUE (llvm::sys::fs::exists (EnglishLocalization));
76
116
}
77
117
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);
91
120
yaml.forEachAvailable ([](swift::DiagID id, llvm::StringRef translation) {
92
121
llvm::StringRef msg = diagnosticMessages[static_cast <uint32_t >(id)];
93
122
ASSERT_EQ (msg, translation);
94
123
});
95
124
}
96
125
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);
110
128
111
129
std::random_device rd;
112
130
std::mt19937 gen (rd ());
113
131
std::uniform_int_distribution<> distr (50 , LocalDiagID::NumDiags);
114
132
unsigned numberOfQueries = distr (gen);
115
133
while (numberOfQueries--) {
116
- unsigned randomNum = randNum (LocalDiagID::NumDiags);
134
+ unsigned randomNum = RandNumber (LocalDiagID::NumDiags);
117
135
DiagID randomId = static_cast <DiagID>(randomNum);
118
136
llvm::StringRef msg = diagnosticMessages[randomNum];
119
137
llvm::StringRef translation = yaml.getMessageOr (randomId, " " );
0 commit comments