Skip to content

Commit 0fece21

Browse files
authored
Merge pull request #300 from SamSmithNZ-dotcom/copilot/fix-exception-type-checks
Improve test assertions and exception types for null unit validation
2 parents c5eca32 + 51e917a commit 0fece21

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
lines changed

src/MandMCounter.Core/Calculator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private static float GetCubicCmForRectangle(string unit, float height, float wid
124124
{
125125
if (string.IsNullOrEmpty(unit))
126126
{
127-
unit = "";
127+
throw new ArgumentNullException(nameof(unit), "Unit cannot be null or empty.");
128128
}
129129

130130
float baseCalculation = height * width * length;
@@ -152,7 +152,7 @@ private static float GetCubicCmForCylinder(string unit, float height, float radi
152152
{
153153
if (string.IsNullOrEmpty(unit))
154154
{
155-
unit = "";
155+
throw new ArgumentNullException(nameof(unit), "Unit cannot be null or empty.");
156156
}
157157

158158
// radius ^ 2 * (pi) * height

src/MandMCounter.Tests/SkittleTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public void CountSkittlesInAUSGallonTest()
1818
float quantity = 1f;
1919

2020
//Act
21-
2221
float result = Calculator.CountSkittles(unit, quantity);
2322

2423
//Assert
@@ -33,7 +32,6 @@ public void CountSkittlesInAUSQuartTest()
3332
float quantity = 1f;
3433

3534
//Act
36-
3735
float result = Calculator.CountSkittles(unit, quantity);
3836

3937
//Assert
@@ -48,7 +46,6 @@ public void CountSkittlesInAUSCupTest()
4846
float quantity = 1f;
4947

5048
//Act
51-
5249
float result = Calculator.CountSkittles(unit, quantity);
5350

5451
//Assert
@@ -178,7 +175,7 @@ public void CountSkittlesInA1CubicNullUnitTest()
178175
//Act
179176

180177
//Assert
181-
Assert.Throws<Exception>(() => Calculator.CountSkittles(unit, height, width, length));
178+
Assert.Throws<ArgumentNullException>(() => Calculator.CountSkittles(unit, height, width, length));
182179
}
183180

184181
#endregion
@@ -226,7 +223,7 @@ public void CountSkittlesInACylinderWithNullUnitTest()
226223
float radius = 5;
227224

228225
//Act & Assert
229-
Assert.Throws<Exception>(() => Calculator.CountSkittles(unit, height, radius));
226+
Assert.Throws<ArgumentNullException>(() => Calculator.CountSkittles(unit, height, radius));
230227
}
231228

232229
#endregion

src/MandMCounter.Tests/UnitsTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace MandMCounter.Tests
77
{
88
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
99
[TestClass]
10-
public class UnitsTests
10+
public class UnitTests
1111
{
1212
[TestMethod]
1313
public void GetUnitsForVolumeTest()
@@ -18,9 +18,9 @@ public void GetUnitsForVolumeTest()
1818
List<string> results = Units.GetUnitsForVolume();
1919

2020
//Assert
21-
Assert.IsTrue(results != null);
21+
Assert.IsNotNull(results);
2222
Assert.IsTrue(results.Count > 0);
23-
Assert.IsTrue(string.IsNullOrEmpty(results[0]) == false);
23+
Assert.IsFalse(string.IsNullOrEmpty(results[0]));
2424
}
2525

2626
[TestMethod]
@@ -32,9 +32,9 @@ public void GetUnitsForContainerTest()
3232
List<string> results = Units.GetUnitsForContainer();
3333

3434
//Assert
35-
Assert.IsTrue(results != null);
35+
Assert.IsNotNull(results);
3636
Assert.IsTrue(results.Count > 0);
37-
Assert.IsTrue(string.IsNullOrEmpty(results[0]) == false);
37+
Assert.IsFalse(string.IsNullOrEmpty(results[0]));
3838
}
3939

4040
[TestMethod]

0 commit comments

Comments
 (0)