Skip to content

Commit 9c8999c

Browse files
committed
Improved test coverage and argument validation in: SingleStepSolution, SudokuPuzzle and SudokuFilePuzzleProvider classes.
1 parent 01a9e0d commit 9c8999c

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

SimpleSudokuSolver.Tests/PuzzleProviders/SudokuFilePuzzleProviderTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ public void SudokuFilePuzzleProvider_ThrowsExceptionForEmptyPath_Test()
1717
}
1818

1919
[Test]
20-
public void SudokuFilePuzzleProvider_WorksWithAllFormats_Test()
20+
public void SudokuFilePuzzleProvider_IgnoresCommentsAndEmptyLines_WorksWithAllFormats_Test()
2121
{
2222
var filePath = Path.GetTempFileName();
23+
File.WriteAllText(filePath, $"#comment{Environment.NewLine} #comment{Environment.NewLine} {Environment.NewLine}");
24+
2325
var separators = new[] { ",", " ", "\t", "" };
2426
var sudoku = new int[9, 9];
2527
sudoku[1, 2] = 5;
@@ -39,7 +41,7 @@ private void SaveSudokuUsingSeparator(int[,] sudoku, string separator, string fi
3941
{
4042
var sudokuToString = new SudokuPuzzle(sudoku).ToString();
4143
var sudokuToStringWithSeparator = sudokuToString.Replace(" ", separator);
42-
File.WriteAllText(filePath, sudokuToStringWithSeparator);
44+
File.AppendAllText(filePath, sudokuToStringWithSeparator);
4345
}
4446
}
4547
}

SimpleSudokuSolver.Tests/SingleStepSolutionTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ namespace SimpleSudokuSolver.Tests
66
[TestFixture]
77
public class SingleStepSolutionTests
88
{
9+
[Test]
10+
public void ConstructorArgumentValidationTest()
11+
{
12+
var eliminations = new SingleStepSolution.Candidate[0];
13+
Assert.That(() => new SingleStepSolution(null, "test"), Throws.InstanceOf<ArgumentNullException>());
14+
Assert.That(() => new SingleStepSolution(eliminations, null), Throws.InstanceOf<ArgumentNullException>());
15+
Assert.That(() => new SingleStepSolution(eliminations, string.Empty), Throws.InstanceOf<ArgumentException>());
16+
17+
Assert.That(() => new SingleStepSolution(0, 0, 0, null), Throws.InstanceOf<ArgumentNullException>());
18+
Assert.That(() => new SingleStepSolution(0, 0, 0, string.Empty), Throws.InstanceOf<ArgumentException>());
19+
}
20+
921
[Test]
1022
public void CandidateEqualityTest()
1123
{

SimpleSudokuSolver/Model/SudokuPuzzle.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public void ApplySingleStepSolution(SingleStepSolution singleStepSolution)
7979
if (singleStepSolution == null)
8080
return;
8181

82-
if (singleStepSolution.Result == null &&
83-
(singleStepSolution.Eliminations == null || singleStepSolution.Eliminations.Length == 0))
82+
if (singleStepSolution.Result == null && singleStepSolution.Eliminations.Length == 0)
8483
return;
8584

8685
int[] oldCanBe = new int[] { };

SimpleSudokuSolver/SingleStepSolution.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public override string ToString()
115115

116116
public SingleStepSolution(int indexOfRow, int indexOfColumn, int value, string strategyName)
117117
{
118+
ValidateStrategyName(strategyName);
119+
118120
Eliminations = null;
119121
Result = new Candidate(indexOfRow, indexOfColumn, value);
120122
Strategy = strategyName;
@@ -123,6 +125,11 @@ public SingleStepSolution(int indexOfRow, int indexOfColumn, int value, string s
123125

124126
public SingleStepSolution(Candidate[] eliminations, string strategyName)
125127
{
128+
if (eliminations == null)
129+
throw new ArgumentNullException(nameof(eliminations));
130+
131+
ValidateStrategyName(strategyName);
132+
126133
Eliminations = eliminations;
127134
Result = null;
128135

@@ -132,7 +139,7 @@ public SingleStepSolution(Candidate[] eliminations, string strategyName)
132139
var stringBuilder = new StringBuilder();
133140
stringBuilder.AppendLine($"Eliminated candidates [{strategyName}]:");
134141

135-
foreach(var groupedElimination in groupedEliminations)
142+
foreach (var groupedElimination in groupedEliminations)
136143
{
137144
stringBuilder.AppendLine($"- Row {groupedElimination.IndexOfRow + 1} Column {groupedElimination.IndexOfColumn + 1} " +
138145
$"Values {string.Join(",", groupedElimination.Values)}");
@@ -146,5 +153,14 @@ public override string ToString()
146153
{
147154
return SolutionDescription;
148155
}
156+
157+
private void ValidateStrategyName(string strategyName)
158+
{
159+
if (strategyName == null)
160+
throw new ArgumentNullException(nameof(strategyName));
161+
162+
if (string.IsNullOrEmpty(strategyName))
163+
throw new ArgumentException("Must not be an empty string", nameof(strategyName));
164+
}
149165
}
150166
}

0 commit comments

Comments
 (0)