Skip to content

Commit dbe7995

Browse files
committed
[Localization] Add localization serialization tests
1. YAML <-> DB format + verification 2. Empty DB to write/read and fallback verification 3. Random holes in the DB file test write/read and fallback
1 parent 0baf844 commit dbe7995

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

unittests/Localization/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_swift_unittest(swiftLocalizationTests
2-
DefToYAMLConverterTests.cpp)
2+
DefToYAMLConverterTests.cpp
3+
SerializationTests.cpp)
34

45
target_link_libraries(swiftLocalizationTests
56
PRIVATE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//===--- LocalizationProducerTests.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 "LocalizationTest.h"
14+
#include "swift/Localization/LocalizationFormat.h"
15+
#include "llvm/ADT/SmallBitVector.h"
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Support/FileSystem.h"
18+
#include "llvm/Support/MemoryBuffer.h"
19+
#include "llvm/Support/raw_ostream.h"
20+
#include "gtest/gtest.h"
21+
#include <string>
22+
#include <random>
23+
24+
using namespace swift::diag;
25+
using namespace swift::unittests;
26+
27+
TEST_F(LocalizationTest, TestYAMLSerialization) {
28+
YAMLLocalizationProducer yaml(YAMLPath);
29+
30+
auto dbFile = createTemporaryFile("en", "db");
31+
32+
// First, let's serialize English translations
33+
{
34+
SerializedLocalizationWriter writer;
35+
36+
yaml.forEachAvailable([&writer](swift::DiagID id, llvm::StringRef translation) {
37+
writer.insert(id, translation);
38+
});
39+
40+
ASSERT_FALSE(writer.emit(dbFile));
41+
}
42+
43+
// Now, let's make sure that serialized version matches "source" YAML
44+
auto dbContent = llvm::MemoryBuffer::getFile(dbFile);
45+
ASSERT_TRUE(dbContent);
46+
47+
SerializedLocalizationProducer db(std::move(dbContent.get()));
48+
yaml.forEachAvailable([&db](swift::DiagID id, llvm::StringRef translation) {
49+
ASSERT_EQ(translation, db.getMessageOr(id, "<no-fallback>"));
50+
});
51+
}
52+
53+
TEST_F(LocalizationTest, TestSerializationOfEmptyFile) {
54+
auto dbFile = createTemporaryFile("by", "db");
55+
SerializedLocalizationWriter writer;
56+
ASSERT_FALSE(writer.emit(dbFile));
57+
58+
YAMLLocalizationProducer yaml(YAMLPath);
59+
60+
// Reading of the empty `db` file should always return default message.
61+
{
62+
auto dbContent = llvm::MemoryBuffer::getFile(dbFile);
63+
ASSERT_TRUE(dbContent);
64+
65+
SerializedLocalizationProducer db(std::move(dbContent.get()));
66+
yaml.forEachAvailable([&db](swift::DiagID id, llvm::StringRef translation) {
67+
ASSERT_EQ("<<<default-fallback>>>",
68+
db.getMessageOr(id, "<<<default-fallback>>>"));
69+
});
70+
}
71+
}
72+
73+
TEST_F(LocalizationTest, TestSerializationWithGaps) {
74+
// Initially all of the messages are included.
75+
llvm::SmallBitVector includedMessages(LocalDiagID::NumDiags, true);
76+
77+
// Let's punch some holes in the diagnostic content.
78+
for (unsigned i = 0, n = 200; i != n; ++i) {
79+
unsigned position = RandNumber(LocalDiagID::NumDiags);
80+
includedMessages.flip(position);
81+
}
82+
83+
YAMLLocalizationProducer yaml(YAMLPath);
84+
auto dbFile = createTemporaryFile("en", "db");
85+
86+
{
87+
SerializedLocalizationWriter writer;
88+
89+
yaml.forEachAvailable([&](swift::DiagID id, llvm::StringRef translation) {
90+
if (includedMessages.test((unsigned)id))
91+
writer.insert(id, translation);
92+
});
93+
94+
ASSERT_FALSE(writer.emit(dbFile));
95+
}
96+
97+
98+
{
99+
auto dbContent = llvm::MemoryBuffer::getFile(dbFile);
100+
ASSERT_TRUE(dbContent);
101+
102+
SerializedLocalizationProducer db(std::move(dbContent.get()));
103+
yaml.forEachAvailable([&](swift::DiagID id, llvm::StringRef translation) {
104+
auto position = (unsigned)id;
105+
106+
std::string expectedMessage = includedMessages.test(position)
107+
? std::string(translation)
108+
: "<<<default-fallback>>>";
109+
110+
ASSERT_EQ(expectedMessage, db.getMessageOr(id, "<<<default-fallback>>>"));
111+
});
112+
}
113+
}

0 commit comments

Comments
 (0)