-
Notifications
You must be signed in to change notification settings - Fork 50
/
BaseGeneratorTest.cs
168 lines (143 loc) · 5.89 KB
/
BaseGeneratorTest.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Java.Interop.Tools.JavaCallableWrappers;
using NUnit.Framework;
using Xamarin.Android.Binder;
namespace generatortests
{
public class BaseGeneratorTest
{
StringWriter sw = null;
[SetUp]
public void Setup ()
{
Options = new CodeGeneratorOptions ();
Options.ApiLevel = "4";
Options.GlobalTypeNames = true;
Options.EnumFieldsMapFile = null;
Options.EnumMethodsMapFile = null;
Options.AssemblyQualifiedName = "Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
Options.OnlyBindPublicTypes = true;
sw = new StringWriter ();
AdditionalSourceDirectories = new List<string> ();
}
protected CodeGeneratorOptions Options = null;
protected Assembly BuiltAssembly = null;
protected List<string> AdditionalSourceDirectories;
protected bool AllowWarnings;
public void Execute ()
{
CodeGenerator.Run (Options);
var output = sw.ToString ();
if (output.Contains ("error")) {
Assert.Fail (output);
}
bool hasErrors;
string compilerOutput;
BuiltAssembly = Compiler.Compile (Options, FullPath ("Mono.Android.dll"), AdditionalSourceDirectories,
out hasErrors, out compilerOutput, AllowWarnings);
Assert.AreEqual (false, hasErrors, compilerOutput);
Assert.IsNotNull (BuiltAssembly);
}
protected void CompareOutputs (string sourceDir, string destinationDir)
{
if (!Path.IsPathRooted (sourceDir))
sourceDir = FullPath (sourceDir);
if (!Path.IsPathRooted (destinationDir))
destinationDir = FullPath (destinationDir);
var files = Directory.GetFiles (sourceDir);
foreach (var file in files) {
var extension = Path.GetExtension (file);
if (extension == ".xml" || extension == ".fixed")
continue;
var filename = Path.GetFileName (file);
var dest = Path.Combine (destinationDir, filename);
if (!File.Exists (dest)) {
Assert.Fail (string.Format ("Expected {0} but it was not generated.", dest));
} else if (!FileCompare (file, dest)) {
var fullSource = Path.GetFullPath (file);
var fullDest = Path.GetFullPath (dest);
//Error message for diff in powershell vs bash
string message = Environment.OSVersion.Platform == PlatformID.Win32NT ?
$"File contents differ; run: diff (cat {fullSource}) `{Environment.NewLine}\t(cat {fullDest})" :
$"File contents differ; run: diff -u {fullSource} \\{Environment.NewLine}\t{fullDest}";
Assert.Fail (message);
}
}
}
protected void Cleanup (string path)
{
if (!Path.IsPathRooted (path))
path = FullPath (path);
if (Directory.Exists (path))
Directory.Delete (path, true);
}
protected bool FileCompare (string file1, string file2)
{
bool result = false;
result = File.Exists (file1) && File.Exists (file2);
if (result) {
byte[] f1 = ReadAllBytesIgnoringLineEndings (file1);
byte[] f2 = ReadAllBytesIgnoringLineEndings (file2);
using (var hash = new Crc64 ()) {
var f1hash = Convert.ToBase64String (hash.ComputeHash (f1));
var f2hash = Convert.ToBase64String (hash.ComputeHash (f2));
result = string.Equals (f1hash, f2hash, StringComparison.Ordinal);
}
}
return result;
}
private byte[] ReadAllBytesIgnoringLineEndings (string path)
{
using (var memoryStream = new MemoryStream ()) {
using (var file = File.OpenRead (path)) {
int readByte;
while ((readByte = file.ReadByte()) != -1) {
byte b = (byte)readByte;
if (b != '\r' && b != '\n' && b != ' ' && b != '\t') {
memoryStream.WriteByte (b);
}
}
}
return memoryStream.ToArray ();
}
}
protected void RunAllTargets (string outputRelativePath, string apiDescriptionFile, string expectedRelativePath, string[] additionalSupportPaths = null, string enumFieldsMapFile = null, string enumMethodMapFile = null)
{
Run (CodeGenerationTarget.XamarinAndroid, Path.Combine ("out", outputRelativePath), apiDescriptionFile, Path.Combine ("expected", expectedRelativePath), additionalSupportPaths, enumFieldsMapFile, enumMethodMapFile);
Run (CodeGenerationTarget.JavaInterop1, Path.Combine ("out.ji", outputRelativePath), apiDescriptionFile, Path.Combine ("expected.ji", expectedRelativePath), additionalSupportPaths, enumFieldsMapFile, enumMethodMapFile);
}
protected string FullPath (string path)
{
var dir = Path.GetDirectoryName (GetType ().Assembly.Location);
return Path.Combine (dir, path.Replace ('/', Path.DirectorySeparatorChar));
}
protected void Run (CodeGenerationTarget target, string outputPath, string apiDescriptionFile, string expectedPath, string[] additionalSupportPaths = null, string enumFieldsMapFile = null, string enumMethodMapFile = null)
{
Cleanup (outputPath);
Options.CodeGenerationTarget = target;
Options.ApiDescriptionFile = FullPath (apiDescriptionFile);
Options.ManagedCallableWrapperSourceOutputDirectory = FullPath (outputPath);
if (!string.IsNullOrWhiteSpace (enumFieldsMapFile))
Options.EnumFieldsMapFile = FullPath (enumFieldsMapFile);
if (!string.IsNullOrWhiteSpace (enumMethodMapFile))
Options.EnumMethodsMapFile = FullPath (enumMethodMapFile);
var adjuster_output = Path.Combine (Path.GetTempPath (), "generator-tests");
Directory.CreateDirectory (adjuster_output);
// Put this in a temp folder so it doesn't end up in "expected", which breaks the compare.
Options.ApiXmlAdjusterOutput = Path.Combine (adjuster_output, Path.GetFileName (apiDescriptionFile) + ".adjusted");
if (additionalSupportPaths != null) {
AdditionalSourceDirectories.AddRange (additionalSupportPaths.Select (p => FullPath (p)));
}
Execute ();
// Try to clean up after ourselves.
try {
Directory.Delete (adjuster_output, true);
} catch { }
CompareOutputs (expectedPath, outputPath);
}
}
}