-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLanguageResourcesTests.cs
173 lines (138 loc) · 6.73 KB
/
LanguageResourcesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
namespace PersonalRecord.Infrastructure.IntegrationTests.Resources.Languages
{
using System.Collections.Generic;
using System.Linq;
using System.Xml;
[TestFixture]
public class LanguageResourcesTests
{
private const string APP_RESOURCES = "AppResources";
private const string APP_RESOURCES_FILE_ENDING = "resx";
private const string DEFAULT_LANGUAGE_CODE = "en";
private const string DEFAULT_LANGUAGE_RESOURCE_FILENAME = "AppResources.resx";
private const int LANGUAGE_CODE_LENGTH = 2;
private List<string> GetLanguageResourceFiles()
{
var files = Directory.GetFiles(@"..\..\..\..\PersonalRecord.Infrastructure\Resources\Languages\", "*.resx").ToList();
return files;
}
private List<LanguageResourceEntry> ReadLanguageResourceEntries(string filePath)
{
var list = new List<LanguageResourceEntry>();
var document = new XmlDocument();
document.Load(filePath);
var node = document.GetElementsByTagName("data");
foreach (XmlElement element in node)
{
var name = element.GetAttribute("name");
var value = element.ChildNodes[1]!.InnerText;
list.Add(new LanguageResourceEntry(name, value));
}
return list;
}
private Dictionary<string, List<LanguageResourceEntry>> GetLanguageDictionary()
{
var languageDictionary = new Dictionary<string, List<LanguageResourceEntry>>();
var files = GetLanguageResourceFiles();
var defaultLanguageFile = files.Single(f => Path.GetFileName(f).Equals(DEFAULT_LANGUAGE_RESOURCE_FILENAME));
var defaultLanguageResourceEntries = ReadLanguageResourceEntries(defaultLanguageFile);
languageDictionary.Add(DEFAULT_LANGUAGE_CODE, defaultLanguageResourceEntries);
foreach (var filePath in files)
{
var fileName = Path.GetFileName(filePath);
if (fileName == DEFAULT_LANGUAGE_RESOURCE_FILENAME)
{
continue;
}
var parts = fileName.Split('.');
var language = parts[1];
var languageResourceEntries = ReadLanguageResourceEntries(filePath);
languageDictionary.Add(language, languageResourceEntries);
}
return languageDictionary;
}
[Test]
public void GetAllLanguageFiles_CheckNamingConvention_NamingIsCorrect()
{
var files = GetLanguageResourceFiles();
var defaultFileErrorMessage = $"There should be a file called '{DEFAULT_LANGUAGE_RESOURCE_FILENAME}', it actually doesn't exist";
Assert.That(files.Any(f => Path.GetFileName(f).Equals(DEFAULT_LANGUAGE_RESOURCE_FILENAME)), defaultFileErrorMessage);
foreach (var file in files)
{
var fileName = Path.GetFileName(file);
if (fileName == DEFAULT_LANGUAGE_RESOURCE_FILENAME)
{
continue;
}
var parts = fileName.Split('.');
var appResourcesPart = parts[0];
var appResourcesPartErrorMessage = $"Language resource file name has to start with '{APP_RESOURCES}', actual one is '{appResourcesPart}'";
Assert.That(appResourcesPart, Is.EqualTo(APP_RESOURCES), appResourcesPartErrorMessage);
var languageCodePart = parts[1];
var languageCodeErrorMessage = $"Language code should be {LANGUAGE_CODE_LENGTH} characters string, actual one is named '{languageCodePart}'";
Assert.That(languageCodePart, Has.Length.EqualTo(LANGUAGE_CODE_LENGTH), languageCodeErrorMessage);
var fileEndingPart = parts[2];
var fileEndingPartErrorMessage = $"The language resource file name has to end with '{APP_RESOURCES_FILE_ENDING}', actual is '{fileEndingPart}'";
Assert.That(fileEndingPart, Is.EqualTo(APP_RESOURCES_FILE_ENDING), fileEndingPartErrorMessage);
}
}
[Test]
public void ReadAllLanguageResources_CheckKeysCount_AllKeysCountAreSame()
{
// arrange
var languageDictionary = GetLanguageDictionary();
// act
languageDictionary.TryGetValue(DEFAULT_LANGUAGE_CODE, out var defaultLanguageList);
var defaultCount = defaultLanguageList!.Count;
foreach (var dictionary in languageDictionary)
{
if (dictionary.Key.Equals(DEFAULT_LANGUAGE_CODE))
{
continue;
}
var currentCount = dictionary.Value.Count;
var dictionaryCountErrorMessage = $"The language '{dictionary.Key}' should have {defaultCount} entries but actually has {currentCount} entries";
// assert
Assert.That(currentCount, Is.EqualTo(defaultCount), dictionaryCountErrorMessage);
}
}
[Test]
public void ReadAllLanguageResources_CheckAllValues_NoneOfThemIsEmpty()
{
// arrange
var languageDictionary = GetLanguageDictionary();
foreach (var dictionary in languageDictionary)
{
foreach (var entry in dictionary.Value)
{
// act
var entryHasEmptyValueErrorMessage = $"Entry with the name '{entry.Name}' has an empty value in '{dictionary.Key}' language";
// assert
Assert.That(entry.Value, Is.Not.Empty, entryHasEmptyValueErrorMessage);
}
}
}
[Test]
public void ReadAllLanguageResources_CheckOtherLanguages_AllEntriesExistInDefaultLanguage()
{
// arrange
var languageDictionary = GetLanguageDictionary();
// act
languageDictionary.TryGetValue(DEFAULT_LANGUAGE_CODE, out var defaultLanguageList);
foreach (var dictionary in languageDictionary)
{
if (dictionary.Key.Equals(DEFAULT_LANGUAGE_CODE))
{
continue;
}
foreach (var entry in dictionary.Value)
{
var exists = defaultLanguageList!.Any(l => l.Name.Equals(entry.Name));
var entryDoesntExistInDefaultLanguageMessage = $"Entry with the name '{entry.Name}' does not exist in default language but in language '{dictionary.Key}'";
// assert
Assert.That(exists, entryDoesntExistInDefaultLanguageMessage);
}
}
}
}
}