Skip to content

Commit

Permalink
Meta: Add a simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
skairunner committed Nov 18, 2019
1 parent d6ff983 commit 97a5fa6
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kanimal-tests/EmbeddedUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO;
using System.Reflection;

namespace kanimal_tests
{
public static class EmbeddedUtilities
{
private static Assembly assembly = Assembly.GetExecutingAssembly();
public static Stream GetResource(string resourceName)
{
return assembly.GetManifestResourceStream(resourceName);
}
}
}
45 changes: 45 additions & 0 deletions kanimal-tests/KTestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using kanimal;

namespace kanimal_tests
{
// A virtual scenario of memorystreams to substitute for actual files
public class KTestContext
{

}

public class ScmlContext : KTestContext
{
public Stream Scml;
public Dictionary<string, Bitmap> Sprites;

// Construct an ScmlContext given a directory (aka namespace) for
// files embedded into the assembly.
public ScmlContext(string namespaceName)
{
var assembly = Assembly.GetExecutingAssembly();
var files = assembly.GetManifestResourceNames()
.Where(name => name.EndsWith("scml") || name.EndsWith("png"));
Sprites = new Dictionary<string, Bitmap>();
foreach (var filename in files)
{
if (filename.EndsWith("scml"))
{
Scml = EmbeddedUtilities.GetResource(filename);
}
else
{
// Need to get the base filename by chopping off the namespace
var realName = filename.Substring("kanimal_tests.".Length + namespaceName.Length + 1);
Sprites[Utilities.WithoutExtension(realName)] =
new Bitmap(EmbeddedUtilities.GetResource(filename));
}
}
}
}
}
42 changes: 42 additions & 0 deletions kanimal-tests/ScmlReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
using kanimal;
using Microsoft.VisualStudio.TestPlatform.Common.Utilities;
using NUnit.Framework;

namespace kanimal_tests
{
public class ScmlReaderTests
{
private static ScmlContext Minimal;

[SetUp]
public void Setup()
{
Minimal = new ScmlContext("testcases.minimal");
}

[Test]
// The minimal case has a simple build content and thus a known state.
public void TestMinimalBuildCorrect()
{
var reader = new ScmlReader(Minimal.Scml, Minimal.Sprites);
reader.Read();

var buildData = reader.BuildData;
Assert.AreEqual(1, buildData.SymbolCount);
Assert.AreEqual(1, buildData.FrameCount);
Assert.AreEqual(1696137821, buildData.Symbols[0].Hash);
var frames = buildData.Symbols[0].Frames;
Assert.AreEqual(1, frames.Count);
Assert.AreEqual(1, frames[0].Duration);
Assert.AreEqual(0, frames[0].BuildImageIndex);
Assert.AreEqual(100, frames[0].PivotX);
Assert.AreEqual(100, frames[0].PivotY);

var hashtable = reader.BuildHashes;
Assert.AreEqual(1, hashtable.Count);
Assert.AreEqual(1696137821, hashtable.Keys.First());
Assert.AreEqual("square", hashtable.Values.First());
}
}
}
29 changes: 29 additions & 0 deletions kanimal-tests/kanimal-tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<RootNamespace>kanimal_tests</RootNamespace>

<IsPackable>false</IsPackable>
<DebugType>full</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0-preview1.19504.10" />
</ItemGroup>

<ItemGroup>
<None Remove="testcases\minimal\square_0.png" />
<EmbeddedResource Include="testcases\minimal\square_0.png" />
<None Remove="testcases\minimal\test_minimal.scml" />
<EmbeddedResource Include="testcases\minimal\test_minimal.scml" />
</ItemGroup>

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

</Project>
Empty file.
Binary file added kanimal-tests/testcases/minimal/square_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions kanimal-tests/testcases/minimal/test_minimal.scml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<spriter_data scml_version="1.0" generator="BrashMonkey Spriter" generator_version="r11">
<folder id="0">
<file id="0" name="square_0.png" width="100" height="100" pivot_x="0" pivot_y="1"/>
</folder>
<entity id="0" name="test">
<animation id="0" name="place" length="400" interval="100">
<mainline>
<key id="0">
<object_ref id="0" timeline="0" key="0" z_index="0"/>
</key>
<key id="1" time="100">
<object_ref id="0" timeline="0" key="0" z_index="0"/>
</key>
<key id="2" time="200">
<object_ref id="0" timeline="0" key="0" z_index="0"/>
</key>
<key id="3" time="300">
<object_ref id="0" timeline="0" key="0" z_index="0"/>
</key>
</mainline>
<timeline id="0" name="square_0">
<key id="0" spin="0">
<object folder="0" file="0" x="-49" y="100" angle="0"/>
</key>
</timeline>
</animation>
</entity>
</spriter_data>
6 changes: 6 additions & 0 deletions kanimal.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "kanimal", "kanimal\kanimal.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "kanimal-cli", "kanimal-cli\kanimal-cli.csproj", "{387A2060-D187-491D-A600-867A59FC9E23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "kanimal-tests", "kanimal-tests\kanimal-tests.csproj", "{EC46A6C4-DAC7-44CB-A2CF-8A602DEB25D4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +20,9 @@ Global
{387A2060-D187-491D-A600-867A59FC9E23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{387A2060-D187-491D-A600-867A59FC9E23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{387A2060-D187-491D-A600-867A59FC9E23}.Release|Any CPU.Build.0 = Release|Any CPU
{EC46A6C4-DAC7-44CB-A2CF-8A602DEB25D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EC46A6C4-DAC7-44CB-A2CF-8A602DEB25D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC46A6C4-DAC7-44CB-A2CF-8A602DEB25D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC46A6C4-DAC7-44CB-A2CF-8A602DEB25D4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 97a5fa6

Please sign in to comment.