Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level code and updated interfaces to accomodate #14

Merged
merged 6 commits into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
<Compile Include="Management\TileChangeManagerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TwoDimension\AreaTests.cs" />
<Compile Include="TwoDimension\CreationTests.cs" />
<Compile Include="TwoDimension\EntityTests.cs" />
<Compile Include="TwoDimension\LevelTests.cs" />
<Compile Include="TwoDimension\PositionTests.cs" />
<Compile Include="TwoDimension\TileTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Tests/TwoDimension/AreaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public void PositionSet()
Assert.That(() => area.SetPosition(null, mockPosition.Object), Throws.ArgumentNullException);
Assert.That(() => area.SetPosition(mockLevel.Object, null), Throws.ArgumentNullException);

// TODO: Issue 13 (https://github.com/Wizcorp/TileSystem/issues/13)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issues directly in the code :D we have to be careful to remove those kind of comments when we close the issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I hope that each one will be a pull request when it's fixed, also the task manager in visual studio is pretty nice for this kind of thing.


// Test Set Position Works
Assert.That(() => area.SetPosition(mockLevel.Object, mockPosition.Object), Throws.Nothing);

Expand Down
138 changes: 138 additions & 0 deletions Tests/TwoDimension/CreationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using Moq;

using NUnit.Framework;

using TileSystem.Interfaces.Base;
using TileSystem.Interfaces.TwoDimension;
using TileSystem.Implementation.TwoDimension;
using TileSystem.Interfaces.Creation;

namespace Tests.TwoDimension
{
[TestFixture]
[Category("TwoDimension")]
public class CreationTests
{
[Test]
public void CreateArea()
{
// Create mock factories
var mockAreaFactory = new Mock<IAreaFactory>();
var mockTileFactory = new Mock<ITileFactory>();
var mockEntityFactory = new Mock<IEntityFactory>();

// Create a mock position that will be used with the area set position
var mockPosition2D = new Mock<IPosition2D>();

// Set up area factory so it will have a mock area to invoke without throwing in the create area
mockAreaFactory.Setup(factory => factory.CreateArea(null, null)).Returns(new Mock<IArea>().Object);

// Create level with mock factories
Level level = new Level(mockAreaFactory.Object, mockTileFactory.Object, mockEntityFactory.Object);

bool createdCalled = false;

// Register added event and make sure it is called
level.AreaCreated += (sender, args) =>
{
createdCalled = true;
};

// Test Null
Assert.That(() => level.CreateArea(null, mockPosition2D.Object, null, null), Throws.ArgumentNullException);

// Test Null
Assert.That(() => level.CreateArea(level, null, null, null), Throws.ArgumentNullException);

// Assert add event was not called
Assert.IsFalse(createdCalled);

// Test Create Works
Assert.That(() => level.CreateArea(level, mockPosition2D.Object, null, null), Throws.Nothing);

// Assert add event was called
Assert.IsTrue(createdCalled);
}

[Test]
public void CreateTile()
{
// Create mock factories
var mockAreaFactory = new Mock<IAreaFactory>();
var mockTileFactory = new Mock<ITileFactory>();
var mockEntityFactory = new Mock<IEntityFactory>();

// Create a mock position that will be used with the tile set position
var mockPosition2D = new Mock<IPosition2D>();
// Create a mock area that will be used with tile set position
var mockArea = new Mock<IArea>();

// Set up tile factory so it will have a mock tile to invoke without throwing in the create tile
mockTileFactory.Setup(factory => factory.CreateTile(null, null)).Returns(new Mock<ITile>().Object);

// Create level with mock factories
Level level = new Level(mockAreaFactory.Object, mockTileFactory.Object, mockEntityFactory.Object);

bool createdCalled = false;

// Register added event and make sure it is called
level.TileCreated += (sender, args) =>
{
createdCalled = true;
};

// Test Null
Assert.That(() => level.CreateTile(null, mockPosition2D.Object, null, null), Throws.ArgumentNullException);

// Test Null
Assert.That(() => level.CreateTile(mockArea.Object, null, null, null), Throws.ArgumentNullException);

// Assert add event was not called
Assert.IsFalse(createdCalled);

// Test Create Works
Assert.That(() => level.CreateTile(mockArea.Object, mockPosition2D.Object, null, null), Throws.Nothing);

// Assert add event was called
Assert.IsTrue(createdCalled);
}

[Test]
public void CreateEntity()
{
// Create mock factories
var mockAreaFactory = new Mock<IAreaFactory>();
var mockTileFactory = new Mock<ITileFactory>();
var mockEntityFactory = new Mock<IEntityFactory>();

// Create a mock tile that will be used with entity set parent
var mockTile = new Mock<ITile>();

// Set up entity factory so it will have a mock entity to invoke without throwing in the create entity
mockEntityFactory.Setup(factory => factory.CreateEntity(null, null)).Returns(new Mock<IEntity>().Object);

// Create level with mock factories
Level level = new Level(mockAreaFactory.Object, mockTileFactory.Object, mockEntityFactory.Object);

bool createdCalled = false;

// Register added event and make sure it is called
level.EntityCreated += (sender, args) =>
{
createdCalled = true;
};

// Test Null
Assert.That(() => level.CreateEntity(null, null, null), Throws.ArgumentNullException);

// Assert add event was not called
Assert.IsFalse(createdCalled);

// Test Create Works
Assert.That(() => level.CreateEntity(mockTile.Object, null, null), Throws.Nothing);

// Assert add event was called
Assert.IsTrue(createdCalled);
}
}
}
126 changes: 126 additions & 0 deletions Tests/TwoDimension/LevelTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using Moq;

using NUnit.Framework;

using TileSystem.Interfaces.Base;
using TileSystem.Interfaces.TwoDimension;
using TileSystem.Implementation.TwoDimension;
using TileSystem.Interfaces.Creation;

namespace Tests.TwoDimension
{
[TestFixture]
[Category("TwoDimension")]
public class LevelTests
{
[Test]
public void LevelFactoryConstructor()
{
// Create mock factories
var mockAreaFactory = new Mock<IAreaFactory>();
var mockTileFactory = new Mock<ITileFactory>();
var mockEntityFactory = new Mock<IEntityFactory>();

// Test Area Null
Assert.That(() => new Level(null, mockTileFactory.Object, mockEntityFactory.Object), Throws.ArgumentNullException);

// Test Tile Null
Assert.That(() => new Level(mockAreaFactory.Object, null, mockEntityFactory.Object), Throws.ArgumentNullException);

// Test Entity Null
Assert.That(() => new Level(mockAreaFactory.Object, mockTileFactory.Object, null), Throws.ArgumentNullException);

// Test Create Works
Assert.That(() => new Level(mockAreaFactory.Object, mockTileFactory.Object, mockEntityFactory.Object), Throws.Nothing);
}

[Test]
public void AreaAdd()
{
Level level = new Level();
var mockArea = new Mock<IArea>();

bool addCalled = false;

// Register added event and make sure it is called
level.AreaAdded += (sender, args) =>
{
addCalled = true;
};

// Test Null
Assert.That(() => level.Add(null), Throws.ArgumentNullException);

// Assert add event was not called
Assert.IsFalse(addCalled);

// Test Add Works
Assert.That(() => level.Add(mockArea.Object), Throws.Nothing);

// Assert add event was called
Assert.IsTrue(addCalled);

// Reset before next test
addCalled = false;

// Test duplicate fails
Assert.That(() => level.Add(mockArea.Object), Throws.ArgumentException);

// Assert add event was not called
Assert.IsFalse(addCalled);
}

[Test]
public void AreaRemove()
{
Level level = new Level();
var mockArea = new Mock<IArea>();

bool removeCalled = false;

// Register removed event and make sure it is called
level.AreaRemoved += (sender, args) =>
{
removeCalled = true;
};

// Add Area
level.Add(mockArea.Object);

// Test Null
Assert.That(() => level.Remove(null), Throws.ArgumentNullException);

// Assert remove event was not called
Assert.IsFalse(removeCalled);

// Test Remove (true removing the object)
Assert.That(level.Remove(mockArea.Object), Is.True);

// Assert remove event was called
Assert.IsTrue(removeCalled);

// Reset before next test
removeCalled = false;

// Test Remove (false not removing the object)
Assert.That(level.Remove(mockArea.Object), Is.False);

// Assert remove event was not called
Assert.IsFalse(removeCalled);
}

[Test]
public void PositionGet()
{
// TODO: Add issue
Assert.Fail();
}

[Test]
public void PositionGetNeighbours()
{
// TODO: Add issue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^

Assert.Fail();
}
}
}
34 changes: 34 additions & 0 deletions Tests/TwoDimension/PositionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using NUnit.Framework;

using TileSystem.Interfaces.TwoDimension;
using TileSystem.Implementation.TwoDimension;

namespace Tests.TwoDimension
{
[TestFixture]
[Category("TwoDimension")]
public class PositionTests
{
[Test]
public void PositionConstructor()
{
int x = 0;
int y = 0;

// Test Area Null
Assert.That(() => new Position2D(x, y), Throws.Nothing);

// Create position and check x,y
IPosition2D pos = new Position2D(x, y);
Assert.AreEqual(x, pos.X);
Assert.AreEqual(y, pos.Y);
}

[Test]
public void CompareTo()
{
// TODO: Issue 11 (https://github.com/Wizcorp/TileSystem/issues/11)
Assert.Fail();
}
}
}
31 changes: 25 additions & 6 deletions TileSystem/Implementation/TwoDimension/Area.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace TileSystem.Implementation.TwoDimension
/// </summary>
public class Area : IArea
{
// Position in 2d
private IPosition2D position2d;

// List of tiles this area contains
private List<ITile> tiles;

// Destroyed event from IArea
Expand All @@ -26,12 +30,15 @@ public class Area : IArea
public event EventHandler<TileRemovedArgs> TileRemoved;

// Representation in the system
public string Type { get; private set; }
public string Variation { get; private set; }
public string Type { get; protected set; }
public string Variation { get; protected set; }

// Location in the system
public ILevel Level { get; private set; }
public IPosition2D Position { get; private set; }
public IPosition Position
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, look better like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem is where it's used you will have to cast, but to keep the internals clean it is actually better in my opinion, but when you put samples together we will really know.

{
get { return position2d; }
}

/// <summary>
/// Default constructor sets up a list of ITile
Expand All @@ -58,7 +65,7 @@ public Area(string type, string variation) : this()
/// </summary>
/// <param name="level">Parent level</param>
/// <param name="position">position in 2d</param>
public void SetPosition(ILevel level, IPosition2D position)
public void SetPosition(ILevel level, IPosition position)
{
if (level == null)
{
Expand All @@ -70,8 +77,15 @@ public void SetPosition(ILevel level, IPosition2D position)
throw new ArgumentNullException("position", "Position can not be null");
}

IPosition2D pos = position as IPosition2D;

if (pos == null)
{
throw new ArgumentException("position must be of type IPosition2D", "position");
}

Level = level;
Position = position;
position2d = pos;
}

/// <summary>
Expand Down Expand Up @@ -155,6 +169,11 @@ public virtual void Destroy(bool propagate = false)
}
}

if (Level != null)
{
Level.Remove(this);
}

if (Destroyed != null)
{
Destroyed.Invoke(this, new AreaDestroyedArgs());
Expand All @@ -167,7 +186,7 @@ public virtual void Destroy(bool propagate = false)
/// <returns>Formatted string representation of the Area(X,Y, Tile Count)</returns>
public override string ToString()
{
return string.Format("[Area X:{0} Y:{1}, Tiles Count:{2}]", Position.X, Position.Y, tiles.Count);
return string.Format("[Area X:{0} Y:{1}, Tiles Count:{2}]", position2d.X, position2d.Y, tiles.Count);
}
}
}
Loading