+ Added UnionTests

+ Fixed an issue where the children of Union as per Children() appeared in the reverse of the expected order
This commit is contained in:
Mike Smith 2016-02-14 11:50:32 -08:00
parent ae3f9e4d2f
commit 750937a25e
3 changed files with 56 additions and 1 deletions

View File

@ -56,6 +56,7 @@
<Compile Include="OSCADObjectTests.cs" />
<Compile Include="SphereTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnionTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OSCADSharp\OSCADSharp.csproj">

View File

@ -0,0 +1,52 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OSCADSharp.Solids;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.UnitTests
{
[TestClass]
public class UnionTests
{
[TestMethod]
public void Union_AffectedObjectsAreChildren()
{
var union = new Cube().Union(new Cylinder());
var children = union.Children();
Assert.IsTrue(children.ElementAt(0).GetType() == typeof(Cube));
Assert.IsTrue(children.ElementAt(1).GetType() == typeof(Cylinder));
}
[TestMethod]
public void Union_AffectedObjectsAreInOutputScript()
{
var union = new Cube().Union(new Cylinder());
string script = union.ToString();
Assert.IsTrue(script.Contains("cube("));
Assert.IsTrue(script.Contains("cylinder("));
}
[TestMethod]
public void Union_FirstStatementInOutputScriptIsUnionMethodCall()
{
var union = new Cube().Union(new Cylinder());
string script = union.ToString();
Assert.IsTrue(script.StartsWith("union()"));
}
[TestMethod]
public void Union_FirstElementAppearsFirstInOutputScript()
{
var union = new Cube().Union(new Cylinder());
string script = union.ToString();
Assert.IsTrue(script.IndexOf("cube(") < script.IndexOf("cylinder("));
}
}
}

View File

@ -233,7 +233,9 @@ namespace OSCADSharp
/// <returns></returns>
public IEnumerable<OSCADObject> Children()
{
Stack<OSCADObject> toTraverse = new Stack<OSCADObject>(this.children);
// Initial children are reversed here because for objects with multiple children (such as boolean operations)
// the natural collection order would yield opposite the expected order in a stack (first child would be the last popped)
Stack<OSCADObject> toTraverse = new Stack<OSCADObject>(this.children.Reverse<OSCADObject>());
List<OSCADObject> allChildren = new List<OSCADObject>();
OSCADObject child = null;