Skip to content

Commit

Permalink
Add test generator as well
Browse files Browse the repository at this point in the history
  • Loading branch information
deathbeam committed Oct 25, 2024
1 parent 84e1e64 commit 93e2c7b
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ jobs:
- name: Generate Bindings
run: dotnet build -c Release src/Raylib.NET.Bindgen

- name: Test
run: dotnet test -p:SkipNatives=true -p:SkipBindgen=true

- name: Build
run: dotnet build -c Release -p:SkipNatives=true -p:SkipBindgen=true

Expand Down
9 changes: 8 additions & 1 deletion Raylib.NET.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
Expand All @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raylib.NET.Bindgen", "src\R
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raylib.NET.Native", "src\Raylib.NET.Native\Raylib.NET.Native.csproj", "{0EE0C0FD-F7B7-42C9-8B63-095A066C0800}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raylib.NET.Test", "src\Raylib.NET.Test\Raylib.NET.Test.csproj", "{29E68756-5677-461E-8E54-8017E9FBFEED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -38,11 +40,16 @@ Global
{0EE0C0FD-F7B7-42C9-8B63-095A066C0800}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0EE0C0FD-F7B7-42C9-8B63-095A066C0800}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EE0C0FD-F7B7-42C9-8B63-095A066C0800}.Release|Any CPU.Build.0 = Release|Any CPU
{29E68756-5677-461E-8E54-8017E9FBFEED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29E68756-5677-461E-8E54-8017E9FBFEED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29E68756-5677-461E-8E54-8017E9FBFEED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29E68756-5677-461E-8E54-8017E9FBFEED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0A7DD14F-8EED-4688-8931-38AA1B940DCB} = {DA7DD588-77B2-465A-8BCF-BE3295552258}
{5A3CE360-CC8F-4ADD-B342-FD6A3246C7B9} = {DA7DD588-77B2-465A-8BCF-BE3295552258}
{DE44EB33-70C2-402D-9FBB-36A6CD713B4F} = {DA7DD588-77B2-465A-8BCF-BE3295552258}
{0EE0C0FD-F7B7-42C9-8B63-095A066C0800} = {DA7DD588-77B2-465A-8BCF-BE3295552258}
{29E68756-5677-461E-8E54-8017E9FBFEED} = {DA7DD588-77B2-465A-8BCF-BE3295552258}
EndGlobalSection
EndGlobal
45 changes: 45 additions & 0 deletions src/Bindgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void Generate()
Console.WriteLine(message);
}

var types = new List<string>();
var builder = new StringBuilder();

builder.AppendLine("using System.Runtime.CompilerServices;");
Expand Down Expand Up @@ -109,6 +110,7 @@ public void Generate()
continue;
}

types.Add(name);
generated = "namespace " + options.GeneratedNamespace + ";\n\n" + generated;
var path = $"{options.OutputPath}/Enums/{name}.cs";
Console.WriteLine($"- Generated: {name} - {path}");
Expand All @@ -125,6 +127,7 @@ public void Generate()
continue;
}

types.Add(name);
var generate = "";
generate += "using System.Runtime.InteropServices;\n";
generate += "using Bindgen.Interop;\n";
Expand All @@ -148,6 +151,48 @@ public void Generate()
outPath = $"{options.OutputPath}/Interop.cs";
Console.WriteLine($"> Generated: Interop - {outPath}");
WriteFile(outPath, Interop.Generate());

if (!string.IsNullOrEmpty(options.TestPath))
{
outPath = $"{options.TestPath}/Test.cs";
Console.WriteLine($"> Generated: Test - {outPath}");
WriteFile(outPath, Test.Generate());


var testBuilder = new StringBuilder();
testBuilder.AppendLine("using Xunit;");
testBuilder.AppendLine("using Bindgen.Test;");
testBuilder.AppendLine();
testBuilder.AppendLine($"namespace {options.GeneratedNamespace}.Test;");
testBuilder.AppendLine();
testBuilder.AppendLine($"public class {options.GeneratedClass}Test");
testBuilder.AppendLine("{");

testBuilder.Append($$"""
private unsafe void CheckType<T>() where T : unmanaged
{
Assert.True(BlittableHelper.IsBlittable<T>());
}
""");

testBuilder.AppendLine();
testBuilder.AppendLine();
testBuilder.AppendLine(" [Fact]");
testBuilder.AppendLine(" public void CheckTypes()");
testBuilder.AppendLine(" {");

foreach (var type in types)
{
testBuilder.AppendLine($" CheckType<{type}>();");
}

testBuilder.AppendLine(" }");
testBuilder.AppendLine("}");

outPath = $"{options.TestPath}/{options.GeneratedClass}Test.cs";
Console.WriteLine($"> Generated: {options.GeneratedClass}Test - {outPath}");
WriteFile(outPath, testBuilder.ToString());
}
}

private String GenerateConstant(CppMacro macro, out String output)
Expand Down
1 change: 1 addition & 0 deletions src/Bindgen/GeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public struct GeneratorOptions
public string GeneratedNamespace = "";
public string GeneratedClass = "";
public string OutputPath = "";
public string TestPath = "";
public string LibraryName = "";
public string FilePath = "";
public string[] SystemIncludeFolders = { };
Expand Down
79 changes: 79 additions & 0 deletions src/Bindgen/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace Bindgen;

public class Test
{
public static string Generate()
{
return $$"""
// For more information see the blog post: https://aakinshin.net/posts/blittable/
// Original code derived from: https://github.com/AndreyAkinshin/BlittableStructs/blob/master/BlittableStructs/BlittableHelper.cs

/*
The MIT License

Copyright (c) 2015 Andrey Akinshin

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

namespace Bindgen.Test;

public static class BlittableHelper
{
public static bool IsBlittable<T>()
{
return IsBlittableCache<T>.VALUE;
}

public static bool IsBlittable(this Type type)
{
if (type == typeof(decimal))
{
return false;
}
if (type.IsArray)
{
var elementType = type.GetElementType();
return elementType != null && elementType.IsValueType && IsBlittable(elementType);
}
try
{
var instance = FormatterServices.GetUninitializedObject(type);
GCHandle.Alloc(instance, GCHandleType.Pinned).Free();
return true;
}
catch
{
return false;
}
}

private static class IsBlittableCache<T>
{
public static readonly bool VALUE = IsBlittable(typeof(T));
}
}
""";
}
}
1 change: 1 addition & 0 deletions src/Raylib.NET.Bindgen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
{ "Matrix4x4", "System.Numerics" },
},
OutputPath = "../../src/Raylib.NET",
TestPath = "../../src/Raylib.NET.Test",
GeneratedNamespace = "RaylibNET",
LibraryName = "raylib",
SystemIncludeFolders = new[]
Expand Down
89 changes: 89 additions & 0 deletions src/Raylib.NET.Test/RayguiTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Xunit;
using Bindgen.Test;

namespace RaylibNET.Test;

public class RayguiTest
{
private unsafe void CheckType<T>() where T : unmanaged
{
Assert.True(BlittableHelper.IsBlittable<T>());
}

[Fact]
public void CheckTypes()
{
CheckType<ConfigFlags>();
CheckType<TraceLogLevel>();
CheckType<KeyboardKey>();
CheckType<MouseButton>();
CheckType<MouseCursor>();
CheckType<GamepadButton>();
CheckType<GamepadAxis>();
CheckType<MaterialMapIndex>();
CheckType<ShaderLocationIndex>();
CheckType<ShaderUniformDataType>();
CheckType<ShaderAttributeDataType>();
CheckType<PixelFormat>();
CheckType<TextureFilter>();
CheckType<TextureWrap>();
CheckType<CubemapLayout>();
CheckType<FontType>();
CheckType<BlendMode>();
CheckType<Gesture>();
CheckType<CameraMode>();
CheckType<CameraProjection>();
CheckType<NPatchLayout>();
CheckType<GuiState>();
CheckType<GuiTextAlignment>();
CheckType<GuiTextAlignmentVertical>();
CheckType<GuiTextWrapMode>();
CheckType<GuiControl>();
CheckType<GuiControlProperty>();
CheckType<GuiDefaultProperty>();
CheckType<GuiToggleProperty>();
CheckType<GuiSliderProperty>();
CheckType<GuiProgressBarProperty>();
CheckType<GuiScrollBarProperty>();
CheckType<GuiCheckBoxProperty>();
CheckType<GuiComboBoxProperty>();
CheckType<GuiDropdownBoxProperty>();
CheckType<GuiTextBoxProperty>();
CheckType<GuiSpinnerProperty>();
CheckType<GuiListViewProperty>();
CheckType<GuiColorPickerProperty>();
CheckType<GuiIconName>();
CheckType<Color>();
CheckType<Image>();
CheckType<Texture>();
CheckType<RenderTexture>();
CheckType<NPatchInfo>();
CheckType<GlyphInfo>();
CheckType<Font>();
CheckType<Camera3D>();
CheckType<Camera2D>();
CheckType<Mesh>();
CheckType<Shader>();
CheckType<MaterialMap>();
CheckType<Material>();
CheckType<Transform>();
CheckType<BoneInfo>();
CheckType<Model>();
CheckType<ModelAnimation>();
CheckType<Ray>();
CheckType<RayCollision>();
CheckType<BoundingBox>();
CheckType<Wave>();
CheckType<rAudioBuffer>();
CheckType<rAudioProcessor>();
CheckType<AudioStream>();
CheckType<Sound>();
CheckType<Music>();
CheckType<VrDeviceInfo>();
CheckType<VrStereoConfig>();
CheckType<FilePathList>();
CheckType<AutomationEvent>();
CheckType<AutomationEventList>();
CheckType<GuiStyleProp>();
}
}
23 changes: 23 additions & 0 deletions src/Raylib.NET.Test/Raylib.NET.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0"/>
<PackageReference Include="coverlet.collector" Version="1.2.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Raylib.NET\Raylib.NET.csproj" />
</ItemGroup>

</Project>
69 changes: 69 additions & 0 deletions src/Raylib.NET.Test/RaylibTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Xunit;
using Bindgen.Test;

namespace RaylibNET.Test;

public class RaylibTest
{
private unsafe void CheckType<T>() where T : unmanaged
{
Assert.True(BlittableHelper.IsBlittable<T>());
}

[Fact]
public void CheckTypes()
{
CheckType<ConfigFlags>();
CheckType<TraceLogLevel>();
CheckType<KeyboardKey>();
CheckType<MouseButton>();
CheckType<MouseCursor>();
CheckType<GamepadButton>();
CheckType<GamepadAxis>();
CheckType<MaterialMapIndex>();
CheckType<ShaderLocationIndex>();
CheckType<ShaderUniformDataType>();
CheckType<ShaderAttributeDataType>();
CheckType<PixelFormat>();
CheckType<TextureFilter>();
CheckType<TextureWrap>();
CheckType<CubemapLayout>();
CheckType<FontType>();
CheckType<BlendMode>();
CheckType<Gesture>();
CheckType<CameraMode>();
CheckType<CameraProjection>();
CheckType<NPatchLayout>();
CheckType<Color>();
CheckType<Image>();
CheckType<Texture>();
CheckType<RenderTexture>();
CheckType<NPatchInfo>();
CheckType<GlyphInfo>();
CheckType<Font>();
CheckType<Camera3D>();
CheckType<Camera2D>();
CheckType<Mesh>();
CheckType<Shader>();
CheckType<MaterialMap>();
CheckType<Material>();
CheckType<Transform>();
CheckType<BoneInfo>();
CheckType<Model>();
CheckType<ModelAnimation>();
CheckType<Ray>();
CheckType<RayCollision>();
CheckType<BoundingBox>();
CheckType<Wave>();
CheckType<rAudioBuffer>();
CheckType<rAudioProcessor>();
CheckType<AudioStream>();
CheckType<Sound>();
CheckType<Music>();
CheckType<VrDeviceInfo>();
CheckType<VrStereoConfig>();
CheckType<FilePathList>();
CheckType<AutomationEvent>();
CheckType<AutomationEventList>();
}
}
Loading

0 comments on commit 93e2c7b

Please sign in to comment.