Skip to content

Commit 2e43a09

Browse files
committed
Code cleanup, new tests, improving code coverage of SimpleSudokuSolver.Model namespace.
1 parent 1342907 commit 2e43a09

File tree

13 files changed

+558
-260
lines changed

13 files changed

+558
-260
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using NUnit.Framework;
2+
using SimpleSudokuSolver.Model;
3+
using System;
4+
using System.Linq;
5+
6+
namespace SimpleSudokuSolver.Tests.Model
7+
{
8+
[TestFixture]
9+
public class BlockTests
10+
{
11+
[Test]
12+
public void ValidBlockTest()
13+
{
14+
var block = new Block(1, 2, 3);
15+
16+
Assert.That(block, Is.Not.Null);
17+
Assert.That(block.BlockRowIndex, Is.EqualTo(1));
18+
Assert.That(block.BlockColumnIndex, Is.EqualTo(2));
19+
Assert.That(block.Cells.GetLength(0), Is.EqualTo(3));
20+
Assert.That(block.Cells.GetLength(1), Is.EqualTo(3));
21+
22+
var allCells = block.Cells.Cast<Cell>().ToArray();
23+
Assert.That(allCells.All(x => x == null));
24+
Assert.That(allCells.Length, Is.EqualTo(9));
25+
}
26+
27+
[Test]
28+
public void InvalidBlockTest()
29+
{
30+
Assert.That(() => new Block(-1, 2, 3), Throws.InstanceOf<ArgumentException>());
31+
Assert.That(() => new Block(3, 2, 3), Throws.InstanceOf<ArgumentException>());
32+
Assert.That(() => new Block(1, -1, 3), Throws.InstanceOf<ArgumentException>());
33+
Assert.That(() => new Block(1, 3, 3), Throws.InstanceOf<ArgumentException>());
34+
Assert.That(() => new Block(1, 2, 9), Throws.InstanceOf<ArgumentException>());
35+
}
36+
37+
[Test]
38+
public void ToStringTest()
39+
{
40+
string newLine = Environment.NewLine;
41+
42+
var block = new Block(0, 0, 3);
43+
var toString = block.ToString();
44+
Assert.That(toString, Is.EqualTo($" {newLine} {newLine} {newLine}"));
45+
46+
block.Cells[0, 0] = new Cell(1);
47+
block.Cells[1, 1] = new Cell(5);
48+
block.Cells[2, 2] = new Cell(0);
49+
toString = block.ToString();
50+
Assert.That(toString, Is.EqualTo($"1 {newLine} 5 {newLine} 0{newLine}"));
51+
}
52+
}
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using NUnit.Framework;
2+
using SimpleSudokuSolver.Model;
3+
using System;
4+
5+
namespace SimpleSudokuSolver.Tests.Model
6+
{
7+
[TestFixture]
8+
public class CellTests
9+
{
10+
[Test]
11+
public void EmptyCellTest()
12+
{
13+
var emptyCell = new Cell(0);
14+
Assert.That(emptyCell.Value, Is.EqualTo(0));
15+
Assert.That(emptyCell.HasValue, Is.False);
16+
Assert.That(emptyCell.CanBe, Is.Empty);
17+
}
18+
19+
[Test]
20+
public void ValidCellTest()
21+
{
22+
var cell = new Cell(9);
23+
Assert.That(cell.Value, Is.EqualTo(9));
24+
Assert.That(cell.HasValue, Is.True);
25+
Assert.That(cell.CanBe, Is.Empty);
26+
}
27+
28+
[Test]
29+
public void InvalidCellTest()
30+
{
31+
Assert.That(() => new Cell(-1), Throws.InstanceOf<ArgumentException>());
32+
Assert.That(() => new Cell(10), Throws.InstanceOf<ArgumentException>());
33+
}
34+
35+
[Test]
36+
public void ToStringTest()
37+
{
38+
var cell = new Cell(5);
39+
var toString = cell.ToString();
40+
Assert.That(toString, Is.EqualTo("5"));
41+
42+
cell = new Cell(0);
43+
toString = cell.ToString();
44+
Assert.That(toString, Is.EqualTo("0"));
45+
}
46+
}
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using NUnit.Framework;
2+
using SimpleSudokuSolver.Model;
3+
using System;
4+
using System.Linq;
5+
6+
namespace SimpleSudokuSolver.Tests.Model
7+
{
8+
[TestFixture]
9+
public class ColumnTests
10+
{
11+
[Test]
12+
public void ValidColumnTest()
13+
{
14+
var column = new Column(1, 9);
15+
16+
Assert.That(column, Is.Not.Null);
17+
Assert.That(column.ColumnIndex, Is.EqualTo(1));
18+
Assert.That(column.Cells.Length, Is.EqualTo(9));
19+
Assert.That(column.Cells.All(x => x == null));
20+
}
21+
22+
[Test]
23+
public void InvalidColumnTest()
24+
{
25+
Assert.That(() => new Column(-1, 9), Throws.InstanceOf<ArgumentException>());
26+
Assert.That(() => new Column(9, 9), Throws.InstanceOf<ArgumentException>());
27+
Assert.That(() => new Column(1, 8), Throws.InstanceOf<ArgumentException>());
28+
}
29+
30+
[Test]
31+
public void ToStringTest()
32+
{
33+
string newLine = Environment.NewLine;
34+
35+
var column = new Column(0, 9);
36+
var toString = column.ToString();
37+
Assert.That(toString, Is.EqualTo($" {newLine} {newLine} {newLine} {newLine} {newLine} {newLine} {newLine} {newLine} {newLine}"));
38+
39+
column.Cells[0] = new Cell(1);
40+
column.Cells[3] = new Cell(4);
41+
column.Cells[8] = new Cell(0);
42+
toString = column.ToString();
43+
Assert.That(toString, Is.EqualTo($"1{newLine} {newLine} {newLine}4{newLine} {newLine} {newLine} {newLine} {newLine}0{newLine}"));
44+
}
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using NUnit.Framework;
2+
using SimpleSudokuSolver.Model;
3+
using System;
4+
using System.Linq;
5+
6+
namespace SimpleSudokuSolver.Tests.Model
7+
{
8+
[TestFixture]
9+
public class RowTests
10+
{
11+
[Test]
12+
public void ValidRowTest()
13+
{
14+
var row = new Row(1, 9);
15+
16+
Assert.That(row, Is.Not.Null);
17+
Assert.That(row.RowIndex, Is.EqualTo(1));
18+
Assert.That(row.Cells.Length, Is.EqualTo(9));
19+
Assert.That(row.Cells.All(x => x == null));
20+
}
21+
22+
[Test]
23+
public void InvalidRowTest()
24+
{
25+
Assert.That(() => new Row(-1, 9), Throws.InstanceOf<ArgumentException>());
26+
Assert.That(() => new Row(9, 9), Throws.InstanceOf<ArgumentException>());
27+
Assert.That(() => new Row(1, 8), Throws.InstanceOf<ArgumentException>());
28+
}
29+
30+
[Test]
31+
public void ToStringTest()
32+
{
33+
var row = new Row(0, 9);
34+
var toString = row.ToString();
35+
Assert.That(toString, Is.EqualTo(new string(' ', 17)));
36+
37+
row.Cells[0] = new Cell(1);
38+
row.Cells[3] = new Cell(4);
39+
row.Cells[8] = new Cell(0);
40+
toString = row.ToString();
41+
Assert.That(toString, Is.EqualTo($"1 4 0"));
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)