Browse Source

Make unit tests independent of the generator

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1537/head
Dimitar Dobrev 5 years ago
parent
commit
4405525f3a
  1. 89
      src/Generator.Tests/GeneratorTestFixture.cs
  2. 4
      tests/CLI/CLI.Tests.cs
  3. 6
      tests/CSharp/CSharp.Tests.cs
  4. 4
      tests/Common/Common.Tests.cs
  5. 6
      tests/Encodings/Encodings.Tests.cs
  6. 6
      tests/StandardLib/StandardLib.Tests.cs
  7. 3
      tests/Test.props
  8. 4
      tests/VTables/VTables.Tests.cs

89
src/Generator.Tests/GeneratorTestFixture.cs

@ -1,89 +0,0 @@ @@ -1,89 +0,0 @@
using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using CppSharp.Generators;
using NUnit.Framework;
namespace CppSharp.Utils
{
/// <summary>
/// The main NUnit fixture base class for a generator-based tests project.
/// Provides support for a text-based test system that looks for lines
/// in the native test declarations that match a certain pattern, which
/// are used for certain kinds of tests that cannot be done with just
/// C# code and using the generated wrappers.
/// </summary>
[TestFixture]
public abstract class GeneratorTestFixture
{
readonly string assemblyName;
protected GeneratorTestFixture()
{
var location = Assembly.GetCallingAssembly().Location;
assemblyName = Path.GetFileNameWithoutExtension(location);
}
static bool GetGeneratorKindFromLang(string lang, out GeneratorKind kind)
{
kind = GeneratorKind.CSharp;
switch(lang)
{
case "CSharp":
case "C#":
kind = GeneratorKind.CSharp;
return true;
case "CLI":
kind = GeneratorKind.CLI;
return true;
}
return false;
}
[Test]
public void CheckDirectives()
{
var name = assemblyName.Substring(0, assemblyName.IndexOf('.'));
var kind = assemblyName.Substring(assemblyName.LastIndexOf('.') + 1);
GeneratorKind testKind;
if (!GetGeneratorKindFromLang(kind, out testKind))
throw new NotSupportedException("Unknown language generator");
var path = Path.GetFullPath(GeneratorTest.GetTestsDirectory(name));
foreach (var header in Directory.EnumerateFiles(path, "*.h"))
{
var headerText = File.ReadAllText(header);
// Parse the header looking for suitable lines to test.
foreach (var line in File.ReadAllLines(header))
{
var match = Regex.Match(line, @"^\s*///*\s*(\S+)\s*:\s*(.*)");
if (!match.Success)
continue;
var matchLang = match.Groups[1].Value;
GeneratorKind matchKind;
if (!GetGeneratorKindFromLang(matchLang.ToUpper(), out matchKind))
continue;
if (matchKind != testKind)
continue;
var matchText = match.Groups[2].Value;
if (string.IsNullOrWhiteSpace(matchText))
continue;
var matchIndex = headerText.IndexOf(matchText, StringComparison.Ordinal);
Assert.IsTrue(matchIndex != -1,
string.Format("Could not match '{0}' in file '{1}'",
matchText, header));
}
}
}
}
}

4
tests/CLI/CLI.Tests.cs

@ -1,10 +1,10 @@ @@ -1,10 +1,10 @@
using CppSharp.Utils;
using NUnit.Framework;
using CLI;
using System.Text;
using System;
public class CLITests : GeneratorTestFixture
[TestFixture]
public class CLITests
{
[Test]
public void TestTypes()

6
tests/CSharp/CSharp.Tests.cs

@ -3,13 +3,11 @@ using System.Collections.Generic; @@ -3,13 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using CppSharp.Utils;
using CSharp;
using NUnit.Framework;
public unsafe class CSharpTests : GeneratorTestFixture
[TestFixture]
public unsafe class CSharpTests
{
public class ExtendsWrapper : TestOverrideFromSecondaryBase
{

4
tests/Common/Common.Tests.cs

@ -1,13 +1,11 @@ @@ -1,13 +1,11 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using CommonTest;
using CppSharp.Utils;
using NUnit.Framework;
using Enum = CommonTest.Enum;
[TestFixture]
public class CommonTests : GeneratorTestFixture
public class CommonTests
{
[Test]
public unsafe void TestCodeGeneration()

6
tests/Encodings/Encodings.Tests.cs

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
using CppSharp.Utils;
using NUnit.Framework;
using NUnit.Framework;
using Foo = Encodings.Foo;
public class EncodingsTests : GeneratorTestFixture
[TestFixture]
public class EncodingsTests
{
[Test]
public void TestFoo()

6
tests/StandardLib/StandardLib.Tests.cs

@ -1,12 +1,12 @@ @@ -1,12 +1,12 @@
using CppSharp.Utils;
using NUnit.Framework;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using StandardLib;
public class StandardLibTests : GeneratorTestFixture
[TestFixture]
public class StandardLibTests
{
[Test]
public void TestVectors()

3
tests/Test.props

@ -8,7 +8,6 @@ @@ -8,7 +8,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(SrcDir)Generator.Tests\CppSharp.Generator.Tests.csproj" />
<ProjectReference Include="$(TestName).CSharp.csproj" Condition="$(MSBuildProjectName.EndsWith('CSharp'))" />
<ProjectReference Include="$(NativeProjectsDir)$(TestName).Native.vcxproj" Condition="$(IsWindows)" ReferenceOutputAssembly="false" />
<ProjectReference Include="$(NativeProjectsDir)$(TestName).CLI.vcxproj" Condition="$(MSBuildProjectName.EndsWith('CLI')) AND $(IsWindows)" />
@ -16,5 +15,7 @@ @@ -16,5 +15,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
</ItemGroup>
</Project>

4
tests/VTables/VTables.Tests.cs

@ -1,5 +1,4 @@ @@ -1,5 +1,4 @@
using System;
using CppSharp.Utils;
using NUnit.Framework;
using VTables;
@ -31,7 +30,8 @@ public class ManagedDerivedClassVirtualRetBase : DerivedClassVirtual @@ -31,7 +30,8 @@ public class ManagedDerivedClassVirtualRetBase : DerivedClassVirtual
}
}
public class VTablesTests : GeneratorTestFixture
[TestFixture]
public class VTablesTests
{
[Test]
public void TestFoo()

Loading…
Cancel
Save