-
Notifications
You must be signed in to change notification settings - Fork 1k
/
UT_Culture.cs
108 lines (97 loc) · 4.33 KB
/
UT_Culture.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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace Neo.UnitTests
{
[TestClass]
public class UT_Culture
{
// This test runs all the other unit tests in the project, with a variety of cultures
// This test will fail when any other test in the project fails. Fix the other failing test(s) and this test should pass again.
[TestMethod]
[NotReRunnable]
public void All_Tests_Cultures()
{
// get all tests in the unit test project assembly
var testClasses = (from t in typeof(NotReRunnableAttribute).GetTypeInfo().Assembly.DefinedTypes
where t.GetCustomAttribute<TestClassAttribute>() != null && t.GetCustomAttribute<NotReRunnableAttribute>() == null
select new
{
Constructor = t.GetConstructor(new Type[] { }),
ClassInit = t.GetMethods().Where(
m => m.GetCustomAttribute<ClassInitializeAttribute>() != null).SingleOrDefault(),
TestInit = t.GetMethods().Where(
m => m.GetCustomAttribute<TestInitializeAttribute>() != null).SingleOrDefault(),
TestCleanup = t.GetMethods().Where(
m => m.GetCustomAttribute<TestCleanupAttribute>() != null).SingleOrDefault(),
ClassCleanup = t.GetMethods().Where(
m => m.GetCustomAttribute<ClassCleanupAttribute>() != null).SingleOrDefault(),
TestMethods = t.GetMethods().Where(
m => m.GetCustomAttribute<TestMethodAttribute>() != null
&& m.GetCustomAttribute<NotReRunnableAttribute>() == null).ToList()
}).ToList();
var cultures = new string[] { "en-US", "zh-CN", "de-DE", "ko-KR", "ja-JP" };
var originalUICulture = CultureInfo.CurrentCulture;
var emptyObjArray = new object[] { };
var testContext = new object[] { new UnitTestContext() };
// run all the tests, varying the culture each time.
try
{
foreach (var culture in cultures)
{
CultureInfo.CurrentCulture = new CultureInfo(culture);
foreach (var c in testClasses)
{
var instance = c.Constructor.Invoke(emptyObjArray);
if (c.ClassInit != null)
{
c.ClassInit.Invoke(instance, testContext);
}
foreach (var m in c.TestMethods)
{
if (c.TestInit != null)
{
c.TestInit.Invoke(instance, emptyObjArray);
}
m.Invoke(instance, emptyObjArray);
if (c.TestCleanup != null)
{
c.TestCleanup.Invoke(instance, emptyObjArray);
}
}
if (c.ClassCleanup != null)
{
c.ClassCleanup.Invoke(instance, emptyObjArray);
}
}
}
}
finally
{
CultureInfo.CurrentCulture = originalUICulture;
}
}
}
public class UnitTestContext : TestContext
{
public override IDictionary Properties => throw new NotImplementedException();
public override void AddResultFile(string fileName)
{
Console.WriteLine(fileName);
}
public override void WriteLine(string message)
{
Console.WriteLine(message);
}
public override void WriteLine(string format, params object[] args)
{
Console.WriteLine(format, args);
}
}
public class NotReRunnableAttribute : Attribute
{
}
}