Skip to content

Commit 3a10c87

Browse files
committed
[generator] Create base class for unit tests.
1 parent 5ad6ae0 commit 3a10c87

File tree

5 files changed

+75
-69
lines changed

5 files changed

+75
-69
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using System.Text;
5+
using MonoDroid.Generation;
6+
using NUnit.Framework;
7+
8+
namespace generatortests.Unit_Tests
9+
{
10+
abstract class CodeGeneratorTestBase
11+
{
12+
protected CodeGenerator generator;
13+
protected StringBuilder builder;
14+
protected StringWriter writer;
15+
protected CodeGenerationOptions options;
16+
17+
[SetUp]
18+
public void SetUp ()
19+
{
20+
builder = new StringBuilder ();
21+
writer = new StringWriter (builder);
22+
options = CreateOptions ();
23+
24+
generator = options.CreateCodeGenerator (writer);
25+
}
26+
27+
[TearDown]
28+
public void TearDown ()
29+
{
30+
writer.Dispose ();
31+
}
32+
33+
protected virtual CodeGenerationOptions CreateOptions ()
34+
{
35+
return new CodeGenerationOptions {
36+
CodeGenerationTarget = Target,
37+
};
38+
}
39+
40+
protected abstract Xamarin.Android.Binder.CodeGenerationTarget Target { get; }
41+
42+
// Get the test results from "Common" for tests with the same results regardless of Target
43+
protected string GetExpected (string testName)
44+
{
45+
var root = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
46+
47+
return File.ReadAllText (Path.Combine (root, "Unit-Tests", "CodeGeneratorExpectedResults", "Common", $"{testName}.txt")).NormalizeLineEndings ();
48+
}
49+
50+
// Get the test results from "JavaInterop1" or "XamarinAndroid" for tests with the different results per Target
51+
protected string GetTargetedExpected (string testName)
52+
{
53+
var target = Target.ToString ();
54+
var root = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
55+
56+
return File.ReadAllText (Path.Combine (root, "Unit-Tests", "CodeGeneratorExpectedResults", target, $"{testName}.txt")).NormalizeLineEndings ();
57+
}
58+
}
59+
}

tools/generator/Tests/Unit-Tests/CodeGeneratorTests.cs

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
14
using generatortests.Unit_Tests;
25
using MonoDroid.Generation;
36
using NUnit.Framework;
4-
using System.Collections.Generic;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Reflection;
8-
using System.Text;
7+
using Xamarin.Android.Binder;
98

109
namespace generatortests
1110
{
12-
abstract class CodeGeneratorTests
11+
[TestFixture]
12+
class JavaInteropCodeGeneratorTests : CodeGeneratorTests
1313
{
14-
protected CodeGenerator generator;
15-
protected StringBuilder builder;
16-
protected StringWriter writer;
17-
protected CodeGenerationOptions options;
18-
19-
[SetUp]
20-
public void SetUp ()
21-
{
22-
builder = new StringBuilder ();
23-
writer = new StringWriter (builder);
24-
options = new CodeGenerationOptions {
25-
CodeGenerationTarget = Target
26-
};
27-
generator = options.CreateCodeGenerator (writer);
28-
}
29-
30-
[TearDown]
31-
public void TearDown ()
32-
{
33-
writer.Dispose ();
34-
}
14+
protected override CodeGenerationTarget Target => CodeGenerationTarget.JavaInterop1;
15+
}
3516

36-
protected abstract Xamarin.Android.Binder.CodeGenerationTarget Target { get; }
17+
[TestFixture]
18+
class XamarinAndroidCodeGeneratorTests : CodeGeneratorTests
19+
{
20+
protected override CodeGenerationTarget Target => CodeGenerationTarget.XamarinAndroid;
21+
}
3722

23+
abstract class CodeGeneratorTests : CodeGeneratorTestBase
24+
{
3825
[Test]
3926
public void WriteCharSequenceEnumerator ()
4027
{
@@ -840,22 +827,5 @@ public void WritePropertyInvoker ()
840827

841828
Assert.AreEqual (GetExpected (nameof (WritePropertyInvoker)), writer.ToString ().NormalizeLineEndings ());
842829
}
843-
844-
// Get the test results from "Common" for tests with the same results regardless of Target
845-
string GetExpected (string testName)
846-
{
847-
var root = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
848-
849-
return File.ReadAllText (Path.Combine (root, "Unit-Tests", "CodeGeneratorExpectedResults", "Common", $"{testName}.txt")).NormalizeLineEndings ();
850-
}
851-
852-
// Get the test results from "JavaInterop1" or "XamarinAndroid" for tests with the different results per Target
853-
string GetTargetedExpected (string testName)
854-
{
855-
var target = Target.ToString ();
856-
var root = Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
857-
858-
return File.ReadAllText (Path.Combine (root, "Unit-Tests", "CodeGeneratorExpectedResults", target, $"{testName}.txt")).NormalizeLineEndings ();
859-
}
860830
}
861831
}

tools/generator/Tests/Unit-Tests/JavaInteropCodeGeneratorTests.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tools/generator/Tests/Unit-Tests/XamarinAndroidCodeGeneratorTests.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

tools/generator/Tests/generator-Tests.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@
6969
<Compile Include="Integration-Tests\GenericArguments.cs" />
7070
<Compile Include="Integration-Tests\InterfaceMethodsConflict.cs" />
7171
<Compile Include="SupportFiles\RegisterAttribute.cs" />
72+
<Compile Include="Unit-Tests\CodeGeneratorTestBase.cs" />
7273
<Compile Include="Unit-Tests\CodeGeneratorTests.cs" />
7374
<Compile Include="Unit-Tests\AdjusterTests.cs" />
74-
<Compile Include="Unit-Tests\JavaInteropCodeGeneratorTests.cs" />
7575
<Compile Include="Unit-Tests\ManagedTests.cs" />
7676
<Compile Include="Unit-Tests\SupportTypes.cs" />
7777
<Compile Include="Unit-Tests\TestExtensions.cs" />
78-
<Compile Include="Unit-Tests\XamarinAndroidCodeGeneratorTests.cs" />
7978
<Compile Include="Unit-Tests\XmlTests.cs" />
8079
</ItemGroup>
8180
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)