Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using DotSpatial.Data;
- using DotSpatial.Topology;
- using NUnit.Framework;
- using Moq;
- namespace DotSpatial.Data.Tests
- {
- [TestFixture]
- public class ShapeTest
- {
- #region Constructor Tests
- /// <summary>
- /// Empty parameter constructor test
- /// </summary>
- [Test]
- public void TestConstructShape()
- {
- var shape = new Shape();
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing FeatureType
- /// </summary>
- [Test]
- public void TestConstructShapeFromFeatureType()
- {
- var shape = new Shape(FeatureType.Point);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing IFeature
- /// Test Intended to Pass
- /// </summary>
- [Test]
- public void TestConstructShapeFromIFeature()
- {
- var feature = new Mock<IFeature>();
- var geometry = new Mock<IBasicGeometry>();
- var envelope = new Mock<IEnvelope>();
- var coordinate1 = new Coordinate(2, 30);
- var coordinate2 = new Coordinate(10, 40);
- var coordinateList = new List<Coordinate> {coordinate1, coordinate2};
- //setup geometry
- envelope.Setup(e => e.Minimum).Returns(coordinate1);
- envelope.Setup(e => e.Maximum).Returns(coordinate2);
- geometry.Setup(g => g.Envelope).Returns(envelope.Object);
- var dataTable = new System.Data.DataTable("FeatureAttributesTable");
- dataTable.Columns.Add("Column1");
- var row = dataTable.NewRow();
- row.ItemArray = new object[] {"attribute"};
- dataTable.Rows.Add(row);
- feature.Setup(f => f.DataRow).Returns(row);
- feature.Setup(f => f.NumPoints).Returns(50);
- feature.Setup(f => f.BasicGeometry).Returns(geometry.Object);
- feature.Setup(f => f.Coordinates).Returns(coordinateList);
- var shape = new Shape(feature.Object);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing IFeature where NumPoints is 0
- /// Fail: When NumPoints is 0, this constructor fails
- /// Maybe NumPoints is not really intended to be 0.
- /// Implementing IFeature and setting NumPoints to 0 will cause an exception
- /// NumPoints with value 0 needs to be checked or at least pass a proper exception message
- /// </summary>
- [Test]
- public void TestConstructShapeFromIFeatureWhereNumPointsIsZero()
- {
- var feature = new Mock<IFeature>();
- var geometry = new Mock<IBasicGeometry>();
- var envelope = new Mock<IEnvelope>();
- var coordinate1 = new Coordinate(2, 30);
- var coordinate2 = new Coordinate(10, 40);
- var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
- //setup geometry
- envelope.Setup(e => e.Minimum).Returns(coordinate1);
- envelope.Setup(e => e.Maximum).Returns(coordinate2);
- geometry.Setup(g => g.Envelope).Returns(envelope.Object);
- var dataTable = new System.Data.DataTable("FeatureAttributesTable");
- dataTable.Columns.Add("Column1");
- var row = dataTable.NewRow();
- row.ItemArray = new object[] { "attribute" };
- dataTable.Rows.Add(row);
- feature.Setup(f => f.DataRow).Returns(row);
- feature.Setup(f => f.NumPoints).Returns(0);
- feature.Setup(f => f.BasicGeometry).Returns(geometry.Object);
- feature.Setup(f => f.Coordinates).Returns(coordinateList);
- var shape = new Shape(feature.Object);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing IGeometry
- /// Test Intended to Pass
- /// </summary>
- [Test]
- public void TestConstructShapeFromIGeometry()
- {
- var geometry = new Mock<IGeometry>();
- //Coordinates
- var coordinate1 = new Coordinate(2, 30);
- var coordinate2 = new Coordinate(10, 40);
- var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
- //Envelope
- var envelope = new Mock<IEnvelope>();
- envelope.Setup(e => e.Minimum).Returns(coordinate1);
- envelope.Setup(e => e.Maximum).Returns(coordinate2);
- geometry.Setup(g => g.NumPoints).Returns(50);
- geometry.Setup(g => g.Coordinates).Returns(coordinateList);
- geometry.Setup(g => g.Envelope).Returns(envelope.Object);
- var shape = new Shape(geometry.Object);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing IGeometry where NumPoints is 0
- /// Fail: When NumPoints is 0, this constructor fails
- /// Maybe NumPoints is not really intended to be 0.
- /// Implementing IGeometry and setting NumPoints to 0 will cause an exception
- /// NumPoints with value 0 needs to be checked or at least pass a proper exception message
- /// </summary>
- [Test]
- public void TestConstructShapeFromIGeometryWhereNumPointsIsZero()
- {
- var geometry = new Mock<IGeometry>();
- //Coordinates
- var coordinate1 = new Coordinate(2, 30);
- var coordinate2 = new Coordinate(10, 40);
- var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
- //Envelope
- var envelope = new Mock<IEnvelope>();
- envelope.Setup(e => e.Minimum).Returns(coordinate1);
- envelope.Setup(e => e.Maximum).Returns(coordinate2);
- geometry.Setup(g => g.NumPoints).Returns(1);
- geometry.Setup(g => g.Coordinates).Returns(coordinateList);
- geometry.Setup(g => g.Envelope).Returns(envelope.Object);
- var shape = new Shape(geometry.Object);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing Coordinate as parameter
- /// </summary>
- [Test]
- public void TestConstructShapeFromCoordinate()
- {
- var coordinate = new Coordinate(2, 30);
- var shape = new Shape(coordinate);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing Vertex as parameter
- /// </summary>
- [Test]
- public void TestConstructShapeFromVertex()
- {
- var vertext = new Vertex(10, 40);
- var shape = new Shape(vertext);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing extent as parameter
- /// </summary>
- [Test]
- public void TestConstructShapeFromExtent()
- {
- var extent = new Extent(1, 1, 50, 50);
- var shape = new Shape(extent);
- Assert.IsNotNull(shape);
- }
- /// <summary>
- /// Constructor test passing envelope as parameter
- /// </summary>
- [Test]
- public void TestConstrucShapeFromEnvelope()
- {
- var coordinate1 = new Coordinate(1, 1);
- var coordinate2 = new Coordinate(10, 10);
- var envelope = new Envelope(coordinate1, coordinate2);
- var shape = new Shape(envelope);
- Assert.IsNotNull(shape);
- }
- #region ArgumentNullChecking
- //Argument Null Checking by throwing ArgumentNullException, unless if a null parameter is ok
- //This prevents throwing the NullReferenceException whose details are dull
- //By this users of the library will automatically know whats wrong with the instantiation
- //Below are constructor overloads which would require the argument not to be null
- /// <summary>
- /// Constructor test passing null IFeature
- /// </summary>
- [Test]
- public void TestConstructorPassingNullIFeature()
- {
- try
- {
- IFeature feature = null;
- var shape = new Shape(feature);
- }
- catch (Exception ex)
- {
- Assert.IsTrue(ex is ArgumentNullException);
- }
- }
- /// <summary>
- /// Constructor test passing null IGeometry
- /// </summary>
- [Test]
- public void TestConstructorPassingNullIGeometry()
- {
- try
- {
- IGeometry geometry = null;
- var shape = new Shape(geometry);
- }
- catch (Exception ex)
- {
- Assert.IsTrue(ex is ArgumentNullException);
- }
- }
- /// <summary>
- /// Constructor test passing null Coordinate
- /// </summary>
- [Test]
- public void TestContstructorPassingNullCoordinate()
- {
- try
- {
- Coordinate coordinate = null;
- var shape = new Shape(coordinate);
- }
- catch (Exception ex)
- {
- Assert.IsTrue(ex is ArgumentNullException);
- }
- }
- /// <summary>
- /// Constructor test passing null Extent
- /// </summary>
- [Test]
- public void TestConstructorPassingNullExtent()
- {
- try
- {
- Extent extent = null;
- var shape = new Shape(extent);
- }
- catch (Exception ex)
- {
- Assert.IsTrue(ex is ArgumentNullException);
- }
- }
- /// <summary>
- /// Consturctor test passing null IEnvelope
- /// </summary>
- [Test]
- public void TestConstuctorPassingNullEnvelope()
- {
- try
- {
- IEnvelope envelope = null;
- var shape = new Shape(envelope);
- }
- catch (Exception ex)
- {
- Assert.IsTrue(ex is ArgumentNullException);
- }
- }
- #endregion
- #endregion
- #region Method Tests
- /// <summary>
- /// Tests method ToGeometry()
- /// Fail: _shapeRange.FeatureType is not defined
- /// Creating an instance of shape without passing a parameter then calling ToGeometry method
- /// throws a NullReferenceException (member: _shapeRange.FeatureType)
- /// If the code content inside method ToGeometry(IGeometryFactory factory)
- /// is relly intended to check _shapeRange.FeatureType, we need to show at least
- /// an exception message saying it is not set
- /// </summary>
- [Test]
- public void TestToGeometry()
- {
- var shape = new Shape();
- var geometry = shape.ToGeometry();
- Assert.IsNotNull(geometry);
- }
- /// <summary>
- /// Tests method ToGeometry(IGeometryFactory factory)
- /// Fail: _shapeRange.FeatureType is not defined
- /// Creating an instance of shape without passing a parameter then calling ToGeometry method
- /// throws a NullReferenceException (member: _shapeRange.FeatureType)
- /// If the code content inside method ToGeometry(IGeometryFactory factory)
- /// is relly intended to check _shapeRange.FeatureType, we need to show at least
- /// an exception message saying it is not set
- /// </summary>
- [Test]
- public void TestToGeometryUsingIGeometryFactory()
- {
- var geometryFactory = new Mock<IGeometryFactory>();
- var shape = new Shape();
- var geometry = shape.ToGeometry(geometryFactory.Object);
- Assert.IsNotNull(geometry);
- }
- /// <summary>
- /// Test CopyAttributes method passing IFeature
- /// </summary>
- [Test]
- public void TestCopyAttributesUsingIFeature()
- {
- var feature = new Mock<IFeature>();
- var geometry = new Mock<IBasicGeometry>();
- var envelope = new Mock<IEnvelope>();
- var coordinate1 = new Coordinate(2, 30);
- var coordinate2 = new Coordinate(10, 40);
- var coordinateList = new List<Coordinate> { coordinate1, coordinate2 };
- //setup geometry
- envelope.Setup(e => e.Minimum).Returns(coordinate1);
- envelope.Setup(e => e.Maximum).Returns(coordinate2);
- geometry.Setup(g => g.Envelope).Returns(envelope.Object);
- var dataTable = new System.Data.DataTable("FeatureAttributesTable");
- dataTable.Columns.Add("Column1");
- var row = dataTable.NewRow();
- row.ItemArray = new object[] { "attribute" };
- dataTable.Rows.Add(row);
- feature.Setup(f => f.DataRow).Returns(row);
- feature.Setup(f => f.NumPoints).Returns(0);
- feature.Setup(f => f.BasicGeometry).Returns(geometry.Object);
- feature.Setup(f => f.Coordinates).Returns(coordinateList);
- var shape = new Shape();
- try
- {
- shape.CopyAttributes(feature.Object);
- Assert.IsTrue(true);
- }
- catch
- {
- Assert.IsTrue(false);
- throw;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement