public void SetObject_TwoEqualObjects_ThrowsError() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "a".AsSpatialObject(1, 1); var thing2 = "a".AsSpatialObject(1, 1); grid.SetObject(thing1); Assert.Throws <InvalidOperationException>(() => grid.SetObject(thing2)); }
public void SetObject_DuplicateId_ThrowsError() { var grid = new UniversalGrid <int>(10, 20); var thing1 = (1).AsSpatialObject(1, 1, "a"); var thing2 = (2).AsSpatialObject(1, 2, "a"); grid.SetObject(thing1); Assert.Throws <InvalidOperationException>(() => grid.SetObject(thing2)); }
public void ToCsv_ReturnsValidData() { var grid = new UniversalGrid <string>(3, 3); grid.SetObject("X", 0, 0); grid.SetObject("O", 1, 1); grid.SetObject("X", 2, 2); var csv = grid.ToCsv(','); Console.WriteLine(csv); }
public void Move_RuleViolation_RaisesEvent() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "A".AsSpatialObject(1, 1); ISpatialRule rule = null; grid.RuleViolated += (s, e) => { rule = e.Rule; }; grid.AddConstraint((x, m) => m.Any(p => p.Y > 1), 1, 23); // Add a rule which prevents Y from exceeding 2 grid.SetObject(thing1); thing1.Move(Direction.Right); // 2, 1 Assert.That(rule == null); thing1.Move(Direction.Down); // try to move => 2, 2 Assert.That(thing1.TopLeft.Y, Is.EqualTo(1), "The Y value should remain unchanged"); Assert.That(thing1.TopLeft.X, Is.EqualTo(2), "The X value should remain unchanged"); Assert.That(rule.Id == 23); }
public void SetObject_AtZero_FiredItemAddedAndModifiedEvent() { var grid = new UniversalGrid <string>(10, 20); var thing1 = new Spatial2DThing <string>(new Point2D()); bool evFired = false; grid.ItemAdded += (s, e) => { Assert.That(e.Target, Is.SameAs(thing1)); evFired = true; }; var modified = false; grid.Modified += (s, e) => { modified = true; }; grid.SetObject(thing1); Assert.That(modified); Assert.That(evFired); }
public void GetObjectsWithin_ReturnsCorrectObject() { var grid = new UniversalGrid <string>(10, 20); var thing1 = "A".AsSpatialObject(1, 1); grid.SetObject(thing1); Assert.That(grid.GetObjectsWithin(new Rectangle(0, 0, 5, 5)).Single(), Is.EqualTo(thing1)); }
public void Clear_SingleItem_ReturnsTrue() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "a".AsSpatialObject(1, 1); grid.SetObject(thing1); var cleared = grid.Clear(); Assert.That(cleared); }
public void Render_ToConsole() { var grid = new UniversalGrid <int>(10, 10); var item = grid.SetObject(7, 5, 5); item.Label = "X"; var cellCount = RenderToConsole(grid); Assert.That(cellCount, Is.EqualTo(100)); }
public void Move_OutOfBounds_ThrowsError() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "A".AsSpatialObject(1, 1); grid.SetObject(thing1); thing1.Move(Direction.Down); // 1, 2 Assert.Throws <ObjectOutOfBoundsException>(() => thing1.Move(Direction.Down)); // 1, 3 }
public void Clear_TwoItems_UsingPredicate_ReturnsTrue() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "a".AsSpatialObject(1, 1); var thing2 = "b".AsSpatialObject(2, 1); grid.SetObject(thing1); grid.SetObject(thing2); bool removeFired = false; grid.ItemRemoved += (s, e) => { Assert.That(e.Target.Data, Is.EqualTo("a")); removeFired = true; }; var cleared = grid.Clear(x => x.Data == "a"); Assert.That(cleared); Assert.That(removeFired); }
public void ToXml_ReturnsValidXmlDoc() { var grid = new UniversalGrid <string>(10, 15); grid.SetObject("X", 5, 5); var xmlDoc = grid.ToXml(); Assert.That(xmlDoc.Root.Name.LocalName, Is.EqualTo("grid")); Assert.That(xmlDoc.Root.Attribute("width").Value, Is.EqualTo("10")); Assert.That(xmlDoc.Root.Attribute("height").Value, Is.EqualTo("15")); Assert.That(xmlDoc.Root.Attribute("viewport").Value, Is.EqualTo("0 0 9 14")); Assert.That(xmlDoc.Root.Elements().ElementAt(5).Elements().ElementAt(5).Value, Is.EqualTo("X")); Console.WriteLine(xmlDoc); }
public void ToHtml_ReturnsValidXhtmlTable() { var grid = new UniversalGrid <string>(10, 10); grid.SetObject("X", 5, 5); var html = grid.ToHtml(tableClass: "tbl"); var htmlDoc = XDocument.Parse(html); Assert.That(htmlDoc.Root.Name.LocalName, Is.EqualTo("table")); Assert.That(htmlDoc.Root.Attribute("class").Value, Is.EqualTo("tbl")); Assert.That(htmlDoc.Root.Elements().ElementAt(5).Elements().ElementAt(5).Value, Is.EqualTo("X")); Console.WriteLine(htmlDoc); }
public void ToHtml_SetsColour() { var grid = new UniversalGrid <string>(10, 10); var obj = grid.SetObject("X", 5, 5); obj.Colour = new Drawing.Colour() { R = 255, A = 255 }; var html = grid.ToHtml(); var htmlDoc = XDocument.Parse(html); Assert.That(htmlDoc.Root.Elements().ElementAt(5).Elements().ElementAt(5).Attribute("style").Value, Is.EqualTo("color: #ff0000")); Console.WriteLine(htmlDoc); }
public void Render_Rotation_ToConsole() { var grid = new UniversalGrid <int>(10, 10); var item = (1).AsSpatialObject(2, 5, new Point2D() { X = 3, Y = 5 }); item.Label = "X"; grid.SetObject(item); RenderToConsole(grid); item.Rotate(); var cellCount = RenderToConsole(grid); Assert.That(cellCount, Is.EqualTo(100)); }
public void ToSvg_ReturnsValidSvgDoc() { var grid = new UniversalGrid <string>(10, 15) { UnitHeight = 50, UnitWidth = 50 }; var obj = grid.SetObject("X", 5, 5); obj.Colour = new Colour() { R = 255, A = 255 }; var svgDoc = grid.ToSvg(); Assert.That(svgDoc.Root.Name.LocalName, Is.EqualTo("svg")); Console.WriteLine(svgDoc); }
public void Move_InvokesAction() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "A".AsSpatialObject(1, 1); bool actionInvoked = false; grid.AddAction((x, m) => m.Any(p => p.Y > 1), (g, x) => { actionInvoked = true; }); // Add a rule which prevents Y from exceeding 2 grid.SetObject(thing1); thing1.Move(Direction.Right); // 2, 1 Assert.That(actionInvoked, Is.False); thing1.Move(Direction.Down); // 2, 2 Assert.That(actionInvoked, Is.True); }
public void SetObjectThenMove_MovesWithinGrid() { var grid = new UniversalGrid <string>(10, 20); var thing1 = "A".AsSpatialObject(1, 1); var movedFired = false; grid.ItemMoved += (s, e) => { movedFired = true; }; grid.SetObject(thing1); Assert.That(grid.GetObjectsAt(1, 1).Single(), Is.SameAs(thing1)); thing1.Move(0, 1); Assert.That(grid.GetObjectsAt(1, 1).Any(), Is.False); Assert.That(grid.GetObjectsAt(1, 2).Single(), Is.SameAs(thing1)); Assert.That(movedFired); }
public void Move_NonTypes_RuleViolation_RaisesEvent() { var grid = new UniversalGrid <string>(3, 3); var thing1 = "A".AsSpatialObject(1, 1); var rule = grid.AddConstraint((x, m) => m.Any(p => p.Y > 1)); // Add a rule which prevents Y from exceeding 2 bool wasExecuted = false; grid.RuleViolated += (s, e) => { wasExecuted = true; Assert.That(e.Rule, Is.SameAs(rule)); }; grid.SetObject(thing1); thing1.Move(Direction.Down); // try to move => 1, 2 Assert.That(rule.Id == 1); Assert.That(wasExecuted); }