-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 5 commits
dd5a8cd
7556495
f0579fc
765a07c
026c3ac
a9fb55f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^^ |
||
Assert.Fail(); | ||
} | ||
} | ||
} |
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(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, look better like that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) | ||
{ | ||
|
@@ -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> | ||
|
@@ -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()); | ||
|
@@ -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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.