-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathCodeGeneratorTestBase.cs
144 lines (117 loc) · 4.67 KB
/
CodeGeneratorTestBase.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using MonoDroid.Generation;
using NUnit.Framework;
namespace generatortests
{
abstract class CodeGeneratorTestBase
{
protected JavaInteropCodeGenerator generator;
protected StringBuilder builder;
protected StringWriter writer;
protected CodeGenerationOptions options;
[SetUp]
public void SetUp ()
{
builder = new StringBuilder ();
writer = new StringWriter (builder);
options = CreateOptions ();
generator = options.CreateCodeGenerator (writer);
}
[TearDown]
public void TearDown ()
{
writer.Dispose ();
}
protected virtual CodeGenerationOptions CreateOptions ()
{
return new CodeGenerationOptions {
CodeGenerationTarget = Target,
EmitLegacyInterfaceInvokers = Target == Xamarin.Android.Binder.CodeGenerationTarget.XAJavaInterop1,
};
}
protected abstract Xamarin.Android.Binder.CodeGenerationTarget Target { get; }
// Optionally override the directory where the expected test results are located.
// For example, we duplicate the "XAJavaInterop1" tests with NRT as "XAJavaInterop1-NRT".
protected virtual string TargetedDirectoryOverride => null;
protected virtual string CommonDirectoryOverride => null;
// Get the test results from "Common" for tests with the same results regardless of Target
protected string GetExpected (string testName) => GetOriginalExpected (testName).NormalizeLineEndings ();
string GetOriginalExpected (string testName) => GetExpectedResults (testName, CommonDirectoryOverride ?? "Common");
// Get the test results from "JavaInterop1" or "XamarinAndroid" for tests with different results per Target
protected string GetTargetedExpected (string testName) => GetOriginalTargetExpected (testName).NormalizeLineEndings ();
string GetOriginalTargetExpected (string testName) => GetExpectedResults (testName, TargetedDirectoryOverride ?? Target.ToString ());
string GetExpectedResults (string testName, string target)
{
var root = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
var path = Path.Combine (root, "Unit-Tests", "CodeGeneratorExpectedResults", target, $"{testName}.txt");
Console.WriteLine ($"# jonp: test {GetType().Name}.{testName}: expected path: {path}");
return File.ReadAllText (path);
}
protected List<GenBase> ParseApiDefinition (string xml)
{
var doc = XDocument.Parse (xml);
var gens = XmlApiImporter.Parse (doc, options);
foreach (var gen in gens)
options.SymbolTable.AddType (gen);
foreach (var gen in gens)
gen.FixupAccessModifiers (options);
foreach (var gen in gens)
gen.Validate (options, new GenericParameterDefinitionList (), generator.Context);
foreach (var gen in gens)
gen.FillProperties ();
foreach (var gen in gens)
gen.FixupMethodOverrides (options);
return gens;
}
protected void AssertTargetedExpected (string testName, string actual)
{
WriteActualContents (testName, actual);
var expected = GetTargetedExpected (testName);
Assert.AreEqual (expected, actual.NormalizeLineEndings ());
}
protected void AssertOriginalExpected (string testName, string actual)
{
WriteActualContents (testName, actual);
var expected = GetOriginalExpected (testName);
Assert.AreEqual (expected.NormalizeLineEndings (), actual.NormalizeLineEndings (),
GetAssertionMessage ($"Test `{testName}` failed.", expected, actual));
}
protected void WriteActualContents (string testName, string contents)
{
var t = this.TargetedDirectoryOverride;
if (string.IsNullOrEmpty (t))
t = this.CommonDirectoryOverride;
if (string.IsNullOrEmpty (t))
t = GetType ().Name;
var dir = Path.Combine ("__jonp", t);
Directory.CreateDirectory (dir);
File.WriteAllText (Path.Combine (dir, testName + ".txt"), contents);
}
protected void AssertOriginalTargetExpected (string testName, string actual)
{
WriteActualContents (testName, actual);
var expected = GetOriginalTargetExpected (testName);
Assert.AreEqual (expected.NormalizeLineEndings (), actual.NormalizeLineEndings (),
GetAssertionMessage ($"Test `{testName}` failed.", expected, actual));
}
protected static string GetAssertionMessage (string header, string expected, string actual)
{
return header + "\n" +
$"Expected:\n```\n{expected}\n```\n" +
$"Actual:\n```\n{actual}\n```";
}
protected string GetGeneratedTypeOutput (GenBase gen)
{
generator.Context.ContextTypes.Push (gen);
generator.WriteType (gen, string.Empty, new GenerationInfo ("", "", "MyAssembly"));
generator.Context.ContextTypes.Pop ();
return writer.ToString ();
}
}
}