-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Doing some minor code cleanup and adding some PInvokeGenerator tests (#…
…50) * Moving the IsFromMainFile check from the traverser to the writer * Fixing up the structured traverser to always build a full tree for release mode. * Modifying the command option aliases to be a single dash * Adding a ClangSharpPInvokeGenerator test project
- Loading branch information
1 parent
efd0434
commit 413a1c8
Showing
16 changed files
with
711 additions
and
404 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
ClangSharpPInvokeGenerator.Test/ClangSharpPInvokeGenerator.Test.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ClangSharpPInvokeGenerator\ClangSharpPInvokeGenerator.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace ClangSharpPInvokeGenerator.Test | ||
{ | ||
public sealed class EnumDeclarationTest : PInvokeGeneratorTest | ||
{ | ||
[Fact] | ||
public async Task BasicTest() | ||
{ | ||
var inputContents = @"enum MyEnum | ||
{ | ||
MyEnum_Value0, | ||
MyEnum_Value1, | ||
MyEnum_Value2, | ||
}; | ||
"; | ||
|
||
var expectedOutputContents = @"namespace ClangSharpPInvokeGenerator.Test | ||
{ | ||
public enum MyEnum | ||
{ | ||
MyEnum_Value0, | ||
MyEnum_Value1, | ||
MyEnum_Value2, | ||
} | ||
} | ||
"; | ||
|
||
await ValidateGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
|
||
[Fact] | ||
public async Task BasicValueTest() | ||
{ | ||
var inputContents = @"enum MyEnum | ||
{ | ||
MyEnum_Value1 = 1, | ||
MyEnum_Value2, | ||
MyEnum_Value3, | ||
}; | ||
"; | ||
|
||
var expectedOutputContents = @"namespace ClangSharpPInvokeGenerator.Test | ||
{ | ||
public enum MyEnum | ||
{ | ||
MyEnum_Value1 = 1, | ||
MyEnum_Value2, | ||
MyEnum_Value3, | ||
} | ||
} | ||
"; | ||
|
||
await ValidateGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
|
||
[Fact] | ||
public async Task BasicTypedTest() | ||
{ | ||
var inputContents = @"enum MyEnum : unsigned char | ||
{ | ||
MyEnum_Value0, | ||
MyEnum_Value1, | ||
MyEnum_Value2, | ||
}; | ||
"; | ||
|
||
var expectedOutputContents = @"namespace ClangSharpPInvokeGenerator.Test | ||
{ | ||
public enum MyEnum : byte | ||
{ | ||
MyEnum_Value0, | ||
MyEnum_Value1, | ||
MyEnum_Value2, | ||
} | ||
} | ||
"; | ||
|
||
await ValidateGeneratedBindings(inputContents, expectedOutputContents); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace ClangSharpPInvokeGenerator.Test | ||
{ | ||
public abstract class PInvokeGeneratorTest : IDisposable | ||
{ | ||
private TemporaryFile _inputFile = new TemporaryFile(); | ||
private TemporaryFile _outputFile = new TemporaryFile(); | ||
|
||
public TemporaryFile InputFile => _inputFile; | ||
|
||
public TemporaryFile OutputFile => _outputFile; | ||
|
||
public void Dispose() | ||
{ | ||
if (_inputFile != null) | ||
{ | ||
_inputFile.Dispose(); | ||
_inputFile = null; | ||
} | ||
|
||
if (_outputFile != null) | ||
{ | ||
_outputFile.Dispose(); | ||
_outputFile = null; | ||
} | ||
} | ||
|
||
protected async Task ValidateGeneratedBindings(string inputContents, string expectedOutputContents) | ||
{ | ||
await InputFile.WriteAllText(inputContents); | ||
await GenerateBindings(); | ||
|
||
var actualOutputContents = await OutputFile.ReadAllText(); | ||
Assert.Equal(expectedOutputContents, actualOutputContents); | ||
} | ||
|
||
protected async Task GenerateBindings() | ||
{ | ||
await Program.Main( | ||
"-f", InputFile.Path, | ||
"-l", "ClangSharpPInvokeGenerator.Test", | ||
"-n", "ClangSharpPInvokeGenerator.Test", | ||
"-o", OutputFile.Path | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using IOPath = System.IO.Path; | ||
|
||
namespace ClangSharpPInvokeGenerator.Test | ||
{ | ||
public sealed class TemporaryFile : IDisposable | ||
{ | ||
private string _path; | ||
|
||
public TemporaryFile() | ||
{ | ||
_path = IOPath.GetTempFileName(); | ||
} | ||
|
||
public string Path => _path; | ||
|
||
public void Dispose() | ||
{ | ||
if (_path != null) | ||
{ | ||
File.Delete(_path); | ||
_path = null; | ||
} | ||
} | ||
|
||
public Task<string[]> ReadAllLines(CancellationToken cancellationToken = default) | ||
{ | ||
return File.ReadAllLinesAsync(_path, cancellationToken); | ||
} | ||
|
||
public Task<string> ReadAllText(CancellationToken cancellationToken = default) | ||
{ | ||
return File.ReadAllTextAsync(_path, cancellationToken); | ||
} | ||
|
||
public Task WriteAllLines(IEnumerable<string> contents, CancellationToken cancellationToken = default) | ||
{ | ||
return File.WriteAllLinesAsync(_path, contents, cancellationToken); | ||
} | ||
|
||
public Task WriteAllText(string contents, CancellationToken cancellationToken = default) | ||
{ | ||
return File.WriteAllTextAsync(_path, contents, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.