Skip to content

Commit 4f464ff

Browse files
committed
[Localization] Extract testing fixture and diagnostic definitions into a header
Doing so make it easy to share common bits of localization testing infrastructure.
1 parent 8c2e423 commit 4f464ff

File tree

2 files changed

+99
-64
lines changed

2 files changed

+99
-64
lines changed

unittests/Localization/DefToYAMLConverterTests.cpp

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "LocalizationTest.h"
1314
#include "swift/Localization/LocalizationFormat.h"
1415
#include "llvm/ADT/ArrayRef.h"
1516
#include "llvm/ADT/SmallString.h"
@@ -19,8 +20,6 @@
1920
#include "llvm/Support/Path.h"
2021
#include "llvm/Support/Signals.h"
2122
#include "llvm/Support/ToolOutputFile.h"
22-
#include "llvm/Support/YAMLParser.h"
23-
#include "llvm/Support/YAMLTraits.h"
2423
#include "llvm/Support/raw_ostream.h"
2524
#include "gtest/gtest.h"
2625
#include <cstdlib>
@@ -30,22 +29,7 @@
3029

3130
using namespace swift;
3231
using namespace swift::diag;
33-
34-
enum LocalDiagID : uint32_t {
35-
#define DIAG(KIND, ID, Options, Text, Signature) ID,
36-
#include "swift/AST/DiagnosticsAll.def"
37-
NumDiags
38-
};
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-
45-
static constexpr const char *const diagnosticMessages[] = {
46-
#define DIAG(KIND, ID, Options, Text, Signature) Text,
47-
#include "swift/AST/DiagnosticsAll.def"
48-
};
32+
using namespace swift::unittests;
4933

5034
static std::string getMainExecutablePath() {
5135
llvm::StringRef libPath = llvm::sys::path::parent_path(SWIFTLIB_DIR);
@@ -62,50 +46,7 @@ static std::string getDefaultLocalizationPath() {
6246
return std::string(DefaultDiagnosticMessagesDir);
6347
}
6448

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; }
87-
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;
94-
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) {
49+
TEST_F(LocalizationTest, MissingLocalizationFiles) {
10950
ASSERT_TRUE(llvm::sys::fs::exists(getDefaultLocalizationPath()));
11051
llvm::SmallString<128> EnglishLocalization(getDefaultLocalizationPath());
11152
llvm::sys::path::append(EnglishLocalization, "en");
@@ -115,15 +56,15 @@ TEST_F(DefToYAMLConverterTest, MissingLocalizationFiles) {
11556
ASSERT_TRUE(llvm::sys::fs::exists(EnglishLocalization));
11657
}
11758

118-
TEST_F(DefToYAMLConverterTest, MatchDiagnosticMessagesSequentially) {
59+
TEST_F(LocalizationTest, ConverterTestMatchDiagnosticMessagesSequentially) {
11960
YAMLLocalizationProducer yaml(YAMLPath);
12061
yaml.forEachAvailable([](swift::DiagID id, llvm::StringRef translation) {
12162
llvm::StringRef msg = diagnosticMessages[static_cast<uint32_t>(id)];
12263
ASSERT_EQ(msg, translation);
12364
});
12465
}
12566

126-
TEST_F(DefToYAMLConverterTest, MatchDiagnosticMessagesRandomly) {
67+
TEST_F(LocalizationTest, ConverterTestMatchDiagnosticMessagesRandomly) {
12768
YAMLLocalizationProducer yaml(YAMLPath);
12869

12970
std::random_device rd;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===--- LocalizationTest.h - Helper for setting up locale tests -*- C++-*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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 LOCALIZATION_TEST_H
14+
#define LOCALIZATION_TEST_H
15+
16+
#include "swift/Localization/LocalizationFormat.h"
17+
#include "llvm/ADT/ArrayRef.h"
18+
#include "llvm/ADT/SmallString.h"
19+
#include "llvm/ADT/StringExtras.h"
20+
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/Support/FileSystem.h"
22+
#include "llvm/Support/raw_ostream.h"
23+
#include "gtest/gtest.h"
24+
#include <random>
25+
#include <string>
26+
#include <system_error>
27+
28+
using namespace swift::diag;
29+
30+
namespace swift {
31+
namespace unittests {
32+
33+
enum LocalDiagID : uint32_t {
34+
#define DIAG(KIND, ID, Options, Text, Signature) ID,
35+
#include "swift/AST/DiagnosticsAll.def"
36+
NumDiags
37+
};
38+
39+
static constexpr const char *const diagnosticID[] = {
40+
#define DIAG(KIND, ID, Options, Text, Signature) #ID,
41+
#include "swift/AST/DiagnosticsAll.def"
42+
};
43+
44+
static constexpr const char *const diagnosticMessages[] = {
45+
#define DIAG(KIND, ID, Options, Text, Signature) Text,
46+
#include "swift/AST/DiagnosticsAll.def"
47+
};
48+
49+
struct LocalizationTest : public ::testing::Test {
50+
std::string YAMLPath;
51+
52+
LocalizationTest() {
53+
llvm::SmallString<128> tempFilename;
54+
std::error_code error =
55+
llvm::sys::fs::createTemporaryFile("en", "yaml", tempFilename);
56+
assert(!error);
57+
58+
YAMLPath = std::string(tempFilename);
59+
}
60+
61+
void SetUp() override {
62+
bool failed = convertDefIntoYAML(YAMLPath);
63+
assert(!failed && "failed to generate a YAML file");
64+
}
65+
66+
void TearDown() override { llvm::sys::fs::remove(YAMLPath); }
67+
68+
/// Random number in [0,n)
69+
unsigned RandNumber(unsigned n) { return unsigned(rand()) % n; }
70+
71+
protected:
72+
static bool convertDefIntoYAML(std::string outputPath) {
73+
std::error_code error;
74+
llvm::raw_fd_ostream OS(outputPath, error, llvm::sys::fs::F_None);
75+
if (OS.has_error() || error)
76+
return true;
77+
78+
llvm::ArrayRef<const char *> ids(diagnosticID, LocalDiagID::NumDiags);
79+
llvm::ArrayRef<const char *> messages(diagnosticMessages,
80+
LocalDiagID::NumDiags);
81+
82+
DefToYAMLConverter converter(ids, messages);
83+
converter.convert(OS);
84+
85+
OS.flush();
86+
87+
return OS.has_error();
88+
}
89+
};
90+
91+
} // end namespace unittests
92+
} // end namespace swift
93+
94+
#endif

0 commit comments

Comments
 (0)