Introduced NUnit and some tests.

This commit is contained in:
huwpascoe
2014-10-19 19:54:38 +01:00
parent 19c1f4ca99
commit dfc6ebd3ba
10 changed files with 11334 additions and 2 deletions

View File

@@ -47,7 +47,7 @@ namespace OpenRA
public bool IsImmediate;
public bool SuppressVisualFeedback;
public Player Player { get { return Subject.Owner; } }
public Player Player { get { return Subject != null ? Subject.Owner : null; } }
Order(string orderString, Actor subject,
Actor targetActor, CPos targetLocation, string targetString, bool queued, CPos extraLocation, uint extraData)
@@ -79,6 +79,9 @@ namespace OpenRA
var extraLocation = (CPos)(flags.HasField(OrderFields.ExtraLocation) ? r.ReadInt2() : int2.Zero);
var extraData = flags.HasField(OrderFields.ExtraData) ? r.ReadUInt32() : 0;
if (world == null)
return new Order(order, null, null, targetLocation, targetString, queued, extraLocation, extraData);
Actor subject, targetActor;
if (!TryGetActorFromUInt(world, subjectId, out subject) || !TryGetActorFromUInt(world, targetActorId, out targetActor))
return null;

View File

@@ -122,7 +122,7 @@ namespace OpenRA
path = "^" + path.Substring(SupportDir.Length);
if (path.StartsWith(GameDir))
path = "." + path.Substring(GameDir.Length);
path = "./" + path.Substring(GameDir.Length);
return path;
}

View File

@@ -0,0 +1,103 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
using NUnit.Framework;
namespace OpenRA.Test
{
[TestFixture]
public class ActorInfoTest
{
ActorInfo actorInfo;
Dictionary<string, MiniYaml> allUnits;
interface IMock : ITraitInfo { }
class MockTrait : ITraitInfo { public object Create(ActorInitializer init) { return null; } }
class MockInherit : MockTrait { }
class MockA : MockInherit, IMock { }
class MockB : MockTrait, Requires<MockA>, Requires<IMock>, Requires<MockInherit> { }
class MockC : MockTrait, Requires<MockB> { }
class MockD : MockTrait, Requires<MockE> { }
class MockE : MockTrait, Requires<MockF> { }
class MockF : MockTrait, Requires<MockD> { }
[SetUp]
public void SetUp()
{
allUnits = new Dictionary<string, MiniYaml>();
actorInfo = new ActorInfo("", new MiniYaml(""), allUnits);
}
[TestCase(TestName = "Sort traits in order of dependency")]
public void TraitsInConstructOrderA()
{
actorInfo.Traits.Add(new MockC());
actorInfo.Traits.Add(new MockB());
actorInfo.Traits.Add(new MockA());
var i = new List<ITraitInfo> (actorInfo.TraitsInConstructOrder());
Assert.That(i[0], Is.InstanceOf<MockA>());
Assert.That(i[1], Is.InstanceOf<MockB>());
Assert.That(i[2], Is.InstanceOf<MockC>());
}
[TestCase(TestName = "Exception reports missing dependencies")]
public void TraitsInConstructOrderB()
{
actorInfo.Traits.Add(new MockB());
actorInfo.Traits.Add(new MockC());
try
{
var i = actorInfo.TraitsInConstructOrder();
throw new Exception("Exception not thrown!");
}
catch (Exception e)
{
Assert.That(e.Message, Is.StringContaining("MockA"));
Assert.That(e.Message, Is.StringContaining("MockB"));
Assert.That(e.Message, Is.StringContaining("MockC"));
Assert.That(e.Message, Is.StringContaining("MockInherit"), "Should recognize base classes");
Assert.That(e.Message, Is.StringContaining("IMock"), "Should recognize interfaces");
}
}
[TestCase(TestName = "Exception reports cyclic dependencies")]
public void TraitsInConstructOrderC()
{
actorInfo.Traits.Add(new MockD());
actorInfo.Traits.Add(new MockE());
actorInfo.Traits.Add(new MockF());
try
{
var i = actorInfo.TraitsInConstructOrder();
throw new Exception("Exception not thrown!");
}
catch (Exception e)
{
var count = (
new Regex("MockD").Matches(e.Message).Count +
new Regex("MockE").Matches(e.Message).Count +
new Regex("MockF").Matches(e.Message).Count
) / 3.0;
Assert.That(count, Is.EqualTo(Math.Floor(count)), "Should be symmetrical");
}
}
}
}

View File

@@ -0,0 +1,86 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
using NUnit.Framework;
namespace OpenRA.Test
{
[TestFixture]
public class MiniYamlTest
{
readonly string yamlForParent = @"
^Parent:
FromParent:
FromParentRemove:
";
readonly string yamlForChild = @"
Child:
Inherits: ^Parent
FromChild:
-FromParentRemove:
";
List<MiniYamlNode> parentList;
List<MiniYamlNode> childList;
MiniYaml parent;
MiniYaml child;
[SetUp]
public void SetUp()
{
parentList = MiniYaml.FromString(yamlForParent);
childList = MiniYaml.FromString(yamlForChild);
parent = parentList.First().Value;
child = childList.First().Value;
}
void InheritanceTest(List<MiniYamlNode> nodes)
{
Assert.That(nodes.Any(n => n.Key == "FromParent"), Is.True, "Node from parent");
Assert.That(nodes.Any(n => n.Key == "FromChild"), Is.True, "Node from child");
Assert.That(nodes.Any(n => n.Key == "FromParentRemove"), Is.Not.True, "Node from parent - node from child");
}
[TestCase(TestName = "MergeStrict(MiniYaml, MiniYaml)")]
public void MergeYamlA()
{
var res = MiniYaml.MergeStrict(parent, child);
InheritanceTest(res.Nodes);
}
[TestCase(TestName = "MergeLiberal(MiniYaml, MiniYaml)")]
public void MergeYamlB()
{
var res = MiniYaml.MergeLiberal(parent, child);
InheritanceTest(res.Nodes);
}
[TestCase(TestName = "MergeStrict(List<MiniYamlNode>, List<MiniYamlNode>)")]
public void MergeYamlC()
{
var res = MiniYaml.MergeStrict(parentList, childList).Last();
Assert.That(res.Key, Is.EqualTo("Child"));
InheritanceTest(res.Value.Nodes);
}
[TestCase(TestName = "MergeLiberal(List<MiniYamlNode>, List<MiniYamlNode>)")]
public void MergeYamlD()
{
var res = MiniYaml.MergeLiberal(parentList, childList).Last();
Assert.That(res.Key, Is.EqualTo("Child"));
InheritanceTest(res.Value.Nodes);
}
}
}

View File

@@ -0,0 +1,56 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.IO;
using NUnit.Framework;
namespace OpenRA.Test
{
[TestFixture]
public class OrderTest
{
Order order;
Order immediateOrder;
[SetUp]
public void SetUp()
{
order = new Order("TestOrder", null, false) {
TargetString = "TestTarget",
TargetLocation = new CPos(1234, 5678),
ExtraData = 1234,
ExtraLocation = new CPos(555, 555)
};
immediateOrder = new Order("TestOrderImmediate", null, false) {
IsImmediate = true,
TargetString = "TestTarget"
};
}
[TestCase(TestName = "Data persists over serialization")]
public void SerializeA()
{
var serializedData = new MemoryStream(order.Serialize());
var result = Order.Deserialize(null, new BinaryReader(serializedData));
Assert.That(result.ToString(), Is.EqualTo(order.ToString()));
}
[TestCase(TestName = "Data persists over serialization immediate")]
public void SerializeB()
{
var serializedData = new MemoryStream(immediateOrder.Serialize());
var result = Order.Deserialize(null, new BinaryReader(serializedData));
Assert.That(result.ToString(), Is.EqualTo(immediateOrder.ToString()));
}
}
}

View File

@@ -0,0 +1,58 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.IO;
using NUnit.Framework;
namespace OpenRA.Test
{
[TestFixture]
public class PlatformTest
{
string supportDir;
string gameDir;
[SetUp]
public void SetUp()
{
supportDir = Platform.SupportDir;
gameDir = Platform.GameDir;
}
[TestCase(TestName = "Returns literal paths")]
public void ResolvePath()
{
Assert.That(Platform.ResolvePath("^testpath"),
Is.EqualTo(Path.Combine(supportDir, "testpath")));
Assert.That(Platform.ResolvePath(".\\testpath"),
Is.EqualTo(Path.Combine(gameDir, "testpath")));
Assert.That(Platform.ResolvePath("./testpath"),
Is.EqualTo(Path.Combine(gameDir, "testpath")));
Assert.That(Platform.ResolvePath("testpath"),
Is.EqualTo("testpath"));
}
[TestCase(TestName = "Returns encoded paths")]
public void UnresolvePath()
{
Assert.That(Platform.UnresolvePath(Path.Combine(supportDir, "testpath")),
Is.EqualTo("^testpath"));
Assert.That(Platform.UnresolvePath(Path.Combine(gameDir, "testpath")),
Is.EqualTo("./testpath"));
Assert.That(Platform.UnresolvePath("testpath"),
Is.EqualTo("testpath"));
}
}
}

View File

@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProjectGuid>{6CB8E1B7-6B36-4D93-8633-7C573E194AC4}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OpenRA.Test</RootNamespace>
<AssemblyName>OpenRA.Test</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>ManagedMinimumRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Eluant, Version=1.0.5229.27703, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\Eluant.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="nunit.framework, Version=2.6.3.13283, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\nunit.framework.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<Compile Include="OpenRA.Game\MiniYamlTest.cs" />
<Compile Include="OpenRA.Game\ActorInfoTest.cs" />
<Compile Include="OpenRA.Game\OrderTest.cs" />
<Compile Include="OpenRA.Game\PlatformTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.Game\OpenRA.Game.csproj">
<Project>{0dfb103f-2962-400f-8c6d-e2c28ccba633}</Project>
<Name>OpenRA.Game</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -74,6 +74,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "System Lua scripts", "Syste
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRA.GameMonitor", "OpenRA.GameMonitor\OpenRA.GameMonitor.csproj", "{68295755-7902-4602-AC2C-9A8AC36D5EF7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenRA.Test", "OpenRA.Test\OpenRA.Test.csproj", "{6CB8E1B7-6B36-4D93-8633-7C573E194AC4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
@@ -105,6 +107,8 @@ Global
{33D03738-C154-4028-8EA8-63A3C488A651}.Debug|x86.Build.0 = Debug|x86
{68295755-7902-4602-AC2C-9A8AC36D5EF7}.Debug|x86.ActiveCfg = Debug|x86
{68295755-7902-4602-AC2C-9A8AC36D5EF7}.Debug|x86.Build.0 = Debug|x86
{6CB8E1B7-6B36-4D93-8633-7C573E194AC4}.Debug|x86.ActiveCfg = Debug|x86
{6CB8E1B7-6B36-4D93-8633-7C573E194AC4}.Debug|x86.Build.0 = Debug|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

BIN
thirdparty/nunit.framework.dll vendored Normal file

Binary file not shown.

10960
thirdparty/nunit.framework.xml vendored Normal file

File diff suppressed because it is too large Load Diff