Skip to content

Commit

Permalink
Add cmdline tests
Browse files Browse the repository at this point in the history
  • Loading branch information
csinkers committed Oct 27, 2024
1 parent 5710171 commit 96ad367
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 27 deletions.
66 changes: 42 additions & 24 deletions src/CodeGen.Veldrid/ResourceSetGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,7 @@ static class ResourceSetGenerator

public static void Generate(StringBuilder sb, VeldridTypeInfo type, GenerationContext context)
{
/* e.g.
new ResourceLayoutElementDescription("uSprite", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
new ResourceLayoutElementDescription("uSpriteSampler", ResourceKind.Sampler, ShaderStages.Fragment),
new ResourceLayoutElementDescription("_Uniform", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment));
*/
sb.AppendLine(" public static readonly ResourceLayoutDescription Layout = new(");
bool first = true;
foreach (var member in type.Members.Where(x => x.Resource != null))
{
if (!first)
sb.AppendLine(",");

var shaderStages = member.Resource.Stages.ToString(CultureInfo.InvariantCulture); // Util.FormatFlagsEnum(member.Resource.Stages);
var kindString = context.Symbols.Veldrid.ResourceKind.KnownKindString(member.Resource.Kind);
var resourceName =
member.Resource.Kind is KnownResourceKind.StructuredBufferReadOnly or KnownResourceKind.StructuredBufferReadWrite
? member.Resource.Name + "Buffer"
: member.Resource.Name;

sb.Append($" new ResourceLayoutElementDescription(\"{resourceName}\", global::{kindString}, (ShaderStages){shaderStages})");
first = false;
}
sb.AppendLine(");");
sb.AppendLine();
BuildLayout(sb, type, context);

foreach (var member in type.Members.Where(x => x.Resource != null))
{
Expand Down Expand Up @@ -71,6 +48,41 @@ member.Resource.Kind is KnownResourceKind.StructuredBufferReadOnly or KnownResou
_view.DeviceBuffer,
_palette.DeviceTexture)); */

BuildBuild(sb, type, context);
BuildResubscribe(sb, type, context);
BuildDispose(sb, type, context);
}

static void BuildLayout(StringBuilder sb, VeldridTypeInfo type, GenerationContext context)
{
/* e.g.
new ResourceLayoutElementDescription("uSprite", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
new ResourceLayoutElementDescription("uSpriteSampler", ResourceKind.Sampler, ShaderStages.Fragment),
new ResourceLayoutElementDescription("_Uniform", ResourceKind.UniformBuffer, ShaderStages.Vertex | ShaderStages.Fragment));
*/
sb.AppendLine(" public static readonly ResourceLayoutDescription Layout = new(");
bool first = true;
foreach (var member in type.Members.Where(x => x.Resource != null))
{
if (!first)
sb.AppendLine(",");

var shaderStages = member.Resource.Stages.ToString(CultureInfo.InvariantCulture); // Util.FormatFlagsEnum(member.Resource.Stages);
var kindString = context.Symbols.Veldrid.ResourceKind.KnownKindString(member.Resource.Kind);
var resourceName =
member.Resource.Kind is KnownResourceKind.StructuredBufferReadOnly or KnownResourceKind.StructuredBufferReadWrite
? member.Resource.Name + "Buffer"
: member.Resource.Name;

sb.Append($" new ResourceLayoutElementDescription(\"{resourceName}\", global::{kindString}, (ShaderStages){shaderStages})");
first = false;
}
sb.AppendLine(");");
sb.AppendLine();
}

static void BuildBuild(StringBuilder sb, VeldridTypeInfo type, GenerationContext context)
{
sb.AppendLine(@" protected override ResourceSet Build(GraphicsDevice device, ResourceLayout layout)
{
#if DEBUG");
Expand Down Expand Up @@ -113,7 +125,10 @@ member.Resource.Kind is KnownResourceKind.StructuredBufferReadOnly or KnownResou

sb.AppendLine("));");
sb.AppendLine(" }");
}

static void BuildResubscribe(StringBuilder sb, VeldridTypeInfo type, GenerationContext context)
{
sb.AppendLine(@"
protected override void Resubscribe()
{");
Expand All @@ -129,7 +144,10 @@ protected override void Resubscribe()
}

sb.AppendLine(" }");
}

static void BuildDispose(StringBuilder sb, VeldridTypeInfo type, GenerationContext context)
{
sb.AppendLine(@"
protected override void Dispose(bool disposing)
{
Expand Down
220 changes: 220 additions & 0 deletions src/Tests/UAlbion.Tests/CommandLineOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
using System;
using System.Collections.Generic;
using UAlbion.Config;
using Veldrid;
using Xunit;

namespace UAlbion.Tests
{
public class CommandLineOptionsTests
{
[Fact]
public void DefaultInitializationTest()
{
var options = new CommandLineOptions(Array.Empty<string>());
Assert.Equal(ExecutionMode.Game, options.Mode);
Assert.Equal(GraphicsBackend.Vulkan, options.Backend);
Assert.False(options.DebugMenus);
Assert.False(options.Mute);
Assert.False(options.StartupOnly);
Assert.False(options.UseRenderDoc);
Assert.Null(options.ConvertFrom);
Assert.Null(options.ConvertTo);
Assert.Null(options.ConvertFilePattern);
Assert.Null(options.Commands);
Assert.Null(options.DumpIds);
Assert.Null(options.Mods);
Assert.Equal(DumpFormats.Json, options.DumpFormats);
Assert.Null(options.DumpAssetTypes);
Assert.Null(options.DumpLanguages);
}

[Fact]
public void GameModeTest()
{
var options = new CommandLineOptions(new[] { "--game" });
Assert.Equal(ExecutionMode.Game, options.Mode);
}

[Fact]
public void DumpModeTest()
{
var options = new CommandLineOptions(new[] { "--dump" });
Assert.Equal(ExecutionMode.DumpData, options.Mode);
}

[Fact]
public void DumpModeTest2()
{
var options = new CommandLineOptions(new[] { "-d" });
Assert.Equal(ExecutionMode.DumpData, options.Mode);
}

[Fact]
public void IsoModeTest()
{
var options = new CommandLineOptions(new[] { "--iso" });
Assert.Equal(ExecutionMode.BakeIsometric, options.Mode);
}

[Fact]
public void ConvertModeTest()
{
var options = new CommandLineOptions(new[] { "--convert", "ModA", "ModB" });
Assert.Equal(ExecutionMode.ConvertAssets, options.Mode);
Assert.Equal(new[] { "ModA" }, options.ConvertFrom);
Assert.Equal("ModB", options.ConvertTo);
}

[Fact]
public void ConvertModeTest2()
{
var options = new CommandLineOptions(new[] { "-b", "ModA ModB", "ModC" });
Assert.Equal(ExecutionMode.ConvertAssets, options.Mode);
Assert.Equal(new[] { "ModA", "ModB" }, options.ConvertFrom);
Assert.Equal("ModC", options.ConvertTo);
}

[Fact]
public void Direct3DBackendTest()
{
var options = new CommandLineOptions(new[] { "--direct3d" });
Assert.Equal(GraphicsBackend.Direct3D11, options.Backend);
}

[Fact]
public void Direct3DBackendTest2()
{
var options = new CommandLineOptions(new[] { "-d3d" });
Assert.Equal(GraphicsBackend.Direct3D11, options.Backend);
}

[Fact]
public void OpenGLBackendTest()
{
var options = new CommandLineOptions(new[] { "--opengl" });
Assert.Equal(GraphicsBackend.OpenGL, options.Backend);
}

[Fact]
public void OpenGLBackendTest2()
{
var options = new CommandLineOptions(new[] { "-gl" });
Assert.Equal(GraphicsBackend.OpenGL, options.Backend);
}

[Fact]
public void OpenGLESBackendTest()
{
var options = new CommandLineOptions(new[] { "--opengles" });
Assert.Equal(GraphicsBackend.OpenGLES, options.Backend);
}

[Fact]
public void OpenGLESBackendTest2()
{
var options = new CommandLineOptions(new[] { "-gles" });
Assert.Equal(GraphicsBackend.OpenGLES, options.Backend);
}

[Fact]
public void MetalBackendTest()
{
var options = new CommandLineOptions(new[] { "--metal" });
Assert.Equal(GraphicsBackend.Metal, options.Backend);
}

[Fact]
public void VulkanBackendTest()
{
var options = new CommandLineOptions(new[] { "--vulkan" });
Assert.Equal(GraphicsBackend.Vulkan, options.Backend);
}

[Fact]
public void VulkanBackendTest2()
{
var options = new CommandLineOptions(new[] { "-vk" });
Assert.Equal(GraphicsBackend.Vulkan, options.Backend);
}

[Fact]
public void DebugMenusTest()
{
var options = new CommandLineOptions(new[] { "--menus" });
Assert.True(options.DebugMenus);
}

[Fact]
public void MuteTest()
{
var options = new CommandLineOptions(new[] { "--mute" });
Assert.True(options.Mute);
}

[Fact]
public void StartupOnlyTest()
{
var options = new CommandLineOptions(new[] { "--startuponly" });
Assert.True(options.StartupOnly);
}

[Fact]
public void RenderDocTest()
{
var options = new CommandLineOptions(new[] { "--renderdoc" });
Assert.True(options.UseRenderDoc);
}

[Fact]
public void RenderDocTest2()
{
var options = new CommandLineOptions(new[] { "-rd" });
Assert.True(options.UseRenderDoc);
}

[Fact]
public void CommandsTest()
{
var options = new CommandLineOptions(new[] { "--commands", "cmd1;cmd2" });
Assert.Equal(new[] { "cmd1", "cmd2" }, options.Commands);
}

[Fact]
public void FormatsTest()
{
var options = new CommandLineOptions(new[] { "--formats", "Json Text" });
Assert.Equal(DumpFormats.Json | DumpFormats.Text, options.DumpFormats);
}

[Fact]
public void IdsTest()
{
var options = new CommandLineOptions(new[] { "--ids", "id1 id2" });
Assert.Equal(new[] { "id1", "id2" }, options.DumpIds);
}

[Fact]
public void TypeTest()
{
var options = new CommandLineOptions(new[] { "--type", "ItemGfx Map" });
Assert.Contains(AssetType.ItemGfx, options.DumpAssetTypes);
Assert.Contains(AssetType.Map, options.DumpAssetTypes);
}

[Fact]
public void FilesTest()
{
var options = new CommandLineOptions(new[] { "--files", ".*\\.txt" });
Assert.Equal(".*\\.txt", options.ConvertFilePattern.ToString());
}

[Fact]
public void ModsTest()
{
var options = new CommandLineOptions(new[] { "--mods", "Mod1 Mod2" });
Assert.Equal(new List<string> { "Mod1", "Mod2" }, options.Mods);
}
}
}

27 changes: 27 additions & 0 deletions src/Tests/UAlbion.Tests/UAlbion.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>10</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Veldrid" Version="4.9.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\UAlbion\UAlbion.csproj" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/UAlbion/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace UAlbion;

sealed class CommandLineOptions
public sealed class CommandLineOptions
{
public ExecutionMode Mode { get; }
public GraphicsBackend Backend { get; }
Expand Down Expand Up @@ -66,7 +66,7 @@ public CommandLineOptions(string[] args)

if (arg == "--MENUS") DebugMenus = true;
if (arg is "--NO-AUDIO" or "-MUTE" or "--MUTE") Mute = true;
if (arg is "--STARTUPONlY" or "-S") StartupOnly = true;
if (arg is "--STARTUPONLY" or "-S") StartupOnly = true;
if (arg is "--RENDERDOC" or "-RD") UseRenderDoc = true;

if (arg is "--COMMANDS" or "-C")
Expand Down
7 changes: 7 additions & 0 deletions src/ualbion.ci.sln
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Shaders", "..\mods\Shaders\
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Unpacked", "..\mods\Unpacked\Unpacked.shproj", "{63A4D9A8-4AE4-4BC4-9047-E242706B3C82}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UAlbion.Tests", "Tests\UAlbion.Tests\UAlbion.Tests.csproj", "{705C1EEB-9B87-4F75-A96C-299644B05388}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -195,6 +197,10 @@ Global
{74B9CB4A-DD58-4970-BF46-EA30E65EA5C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74B9CB4A-DD58-4970-BF46-EA30E65EA5C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74B9CB4A-DD58-4970-BF46-EA30E65EA5C7}.Release|Any CPU.Build.0 = Release|Any CPU
{705C1EEB-9B87-4F75-A96C-299644B05388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{705C1EEB-9B87-4F75-A96C-299644B05388}.Debug|Any CPU.Build.0 = Debug|Any CPU
{705C1EEB-9B87-4F75-A96C-299644B05388}.Release|Any CPU.ActiveCfg = Release|Any CPU
{705C1EEB-9B87-4F75-A96C-299644B05388}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -222,6 +228,7 @@ Global
{BBE1D977-E9B9-4D0B-A6DF-AD91EC1AD15A} = {E4F7A1D2-3BB2-46C4-B8E5-4FD4CADB94CA}
{852FA5D7-3FFF-415C-A20C-F682BBFCE0C7} = {E4F7A1D2-3BB2-46C4-B8E5-4FD4CADB94CA}
{63A4D9A8-4AE4-4BC4-9047-E242706B3C82} = {E4F7A1D2-3BB2-46C4-B8E5-4FD4CADB94CA}
{705C1EEB-9B87-4F75-A96C-299644B05388} = {EC61DB68-353D-4825-B221-FADABA7ADFF3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2759A62D-227A-4D68-B6B5-966B9141AA74}
Expand Down
Loading

0 comments on commit 96ad367

Please sign in to comment.