+ Added .Children() method to OSCADObjects

+ Added some tests for children
+ Fixed an issue where cloned objects for Transforms would not have cloned children
This commit is contained in:
Mike Smith 2016-02-14 00:19:29 -08:00
parent 8fe459a5ba
commit e8c42deb30
11 changed files with 96 additions and 9 deletions

View File

@ -0,0 +1,46 @@
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 OSCADObjectTests
{
[TestMethod]
public void OSCADObject_ChildrenForSimpleStructureYieldsAllChildren()
{
var cube = new Cube();
var translatedCube = cube.Translate(1, 2, 5);
//Should contain both translation and Cube
var coloredTranslatedCube = translatedCube.Color("Red");
List<OSCADObject> expectedChildren = new List<OSCADObject>() { cube, translatedCube };
var children = coloredTranslatedCube.Children();
Assert.IsTrue(children.Contains(cube));
Assert.IsTrue(children.Contains(translatedCube));
Assert.IsFalse(children.Contains(coloredTranslatedCube));
}
[TestMethod]
public void OSCADObject_ClonesContainChildren()
{
var text = new Text3D("Hi").Rotate(90, 0, 0);
var clone = text.Clone();
//Clone has a child, and it should be the same thing
Assert.IsTrue(clone.Children().Count() == 1);
Assert.IsTrue(clone.Children().FirstOrDefault().GetType() == text.Children().FirstOrDefault().GetType());
//But the child should be a different instance
Assert.IsFalse(clone.Children().FirstOrDefault() == text.Children().FirstOrDefault());
}
}
}

View File

@ -53,6 +53,7 @@
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="CubeTests.cs" /> <Compile Include="CubeTests.cs" />
<Compile Include="OSCADObjectTests.cs" />
<Compile Include="SphereTests.cs" /> <Compile Include="SphereTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>

View File

@ -203,5 +203,32 @@ namespace OSCADSharp
{ {
return this.ToString() == other.ToString(); return this.ToString() == other.ToString();
} }
protected List<OSCADObject> children = new List<OSCADObject>();
/// <summary>
/// Returns all children of this OSCADObject
/// </summary>
/// <returns></returns>
public IEnumerable<OSCADObject> Children()
{
Stack<OSCADObject> toTraverse = new Stack<OSCADObject>(this.children);
List<OSCADObject> allChildren = new List<OSCADObject>();
OSCADObject child = null;
while(toTraverse.Count > 0)
{
child = toTraverse.Pop();
allChildren.Add(child);
foreach (var subChild in child.Children())
{
toTraverse.Push(subChild);
}
}
return allChildren;
}
} }
} }

View File

@ -14,12 +14,11 @@ namespace OSCADSharp.Scripting
internal class BlockStatementObject : OSCADObject internal class BlockStatementObject : OSCADObject
{ {
private string outerStatement; private string outerStatement;
private IEnumerable<OSCADObject> children;
internal BlockStatementObject(string outerStatement, IEnumerable<OSCADObject> children) internal BlockStatementObject(string outerStatement, IEnumerable<OSCADObject> children)
{ {
this.outerStatement = outerStatement; this.outerStatement = outerStatement;
this.children = children; this.children = children.ToList();
} }
public override string ToString() public override string ToString()

View File

@ -30,6 +30,8 @@ namespace OSCADSharp.Transforms
this.obj = obj; this.obj = obj;
this.ColorName = color; this.ColorName = color;
this.Opacity = opacity; this.Opacity = opacity;
this.children.Add(obj);
} }
public override string ToString() public override string ToString()
@ -41,7 +43,7 @@ namespace OSCADSharp.Transforms
public override OSCADObject Clone() public override OSCADObject Clone()
{ {
return new ColoredObject(this.obj, this.ColorName, this.Opacity); return new ColoredObject(this.obj.Clone(), this.ColorName, this.Opacity);
} }
} }
} }

View File

@ -32,12 +32,14 @@ namespace OSCADSharp.Transforms
{ {
this.obj = obj; this.obj = obj;
this.Height = height; this.Height = height;
this.children.Add(obj);
} }
public override OSCADObject Clone() public override OSCADObject Clone()
{ {
return new LinearExtrudedObject(this.obj, this.Height); return new LinearExtrudedObject(this.obj.Clone(), this.Height);
} }
public override string ToString() public override string ToString()

View File

@ -29,6 +29,8 @@ namespace OSCADSharp.Transforms
{ {
this.obj = obj; this.obj = obj;
this.Normal = normal; this.Normal = normal;
this.children.Add(obj);
} }
public override string ToString() public override string ToString()
@ -40,7 +42,7 @@ namespace OSCADSharp.Transforms
public override OSCADObject Clone() public override OSCADObject Clone()
{ {
return new MirroredObject(this.obj, this.Normal); return new MirroredObject(this.obj.Clone(), this.Normal);
} }
} }
} }

View File

@ -27,6 +27,8 @@ namespace OSCADSharp.Transforms
{ {
this.obj = obj; this.obj = obj;
this.Size = size; this.Size = size;
this.children.Add(obj);
} }
public override string ToString() public override string ToString()
@ -39,7 +41,7 @@ namespace OSCADSharp.Transforms
public override OSCADObject Clone() public override OSCADObject Clone()
{ {
return new ResizedObject(this.obj, this.Size); return new ResizedObject(this.obj.Clone(), this.Size);
} }
} }
} }

View File

@ -27,6 +27,8 @@ namespace OSCADSharp.Transforms
{ {
this.obj = obj; this.obj = obj;
this.Angle = angle; this.Angle = angle;
this.children.Add(obj);
} }
public override string ToString() public override string ToString()
@ -39,7 +41,7 @@ namespace OSCADSharp.Transforms
public override OSCADObject Clone() public override OSCADObject Clone()
{ {
return new RotatedObject(this.obj, this.Angle); return new RotatedObject(this.obj.Clone(), this.Angle);
} }
} }
} }

View File

@ -27,6 +27,8 @@ namespace OSCADSharp.Transforms
{ {
this.obj = obj; this.obj = obj;
this.ScaleFactor = scale; this.ScaleFactor = scale;
this.children.Add(obj);
} }
public override string ToString() public override string ToString()
@ -39,7 +41,7 @@ namespace OSCADSharp.Transforms
public override OSCADObject Clone() public override OSCADObject Clone()
{ {
return new ScaledObject(this.obj, this.ScaleFactor); return new ScaledObject(this.obj.Clone(), this.ScaleFactor);
} }
} }
} }

View File

@ -24,6 +24,8 @@ namespace OSCADSharp.Transforms
{ {
this.obj = obj; this.obj = obj;
this.Vector = vector; this.Vector = vector;
this.children.Add(obj);
} }
public override string ToString() public override string ToString()
@ -36,7 +38,7 @@ namespace OSCADSharp.Transforms
public override OSCADObject Clone() public override OSCADObject Clone()
{ {
return new TranslatedObject(this.obj, this.Vector); return new TranslatedObject(this.obj.Clone(), this.Vector);
} }
} }
} }