Skip to content

Commit dabf7a9

Browse files
authored
Tweak VectorOperation/SpanTest exception validation (microsoft#533)
### Motivation and Context Fix some unit tests to validate what they intended to validate. ### Description The validation is ensuring if an exception is thrown that it's of the right type, but it's not actually ensuring that an exception is thrown. For the array-based validation, we can just use the Assert.Throws helper. For the ref struct-based validation, we can't capture the ref structs into a lambda, but we can at least assert that the operation throws.
1 parent 2ffda1a commit dabf7a9

2 files changed

Lines changed: 6 additions & 16 deletions

File tree

dotnet/src/SemanticKernel.UnitTests/VectorOperations/VectorOperationTests.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ public void ItThrowsOnCosineSimilarityWithDifferentLengthVectorsFP()
5656
var shortVector = new float[] { -1.0F, 4.0F };
5757

5858
// Assert
59-
try
60-
{
61-
shortVector.CosineSimilarity(this._floatV2);
62-
}
63-
catch (ArgumentException target)
64-
{
65-
Assert.IsType<ArgumentException>(target);
66-
}
59+
Assert.Throws<ArgumentException>(() => shortVector.CosineSimilarity(this._floatV2));
6760
}
6861

6962
[Fact]
@@ -73,14 +66,7 @@ public void ItThrowsOnCosineSimilarityWithDifferentLengthVectorsDouble()
7366
var shortVector = new double[] { -1.0, 4.0 };
7467

7568
// Assert
76-
try
77-
{
78-
shortVector.CosineSimilarity(this._doubleV2);
79-
}
80-
catch (ArgumentException target)
81-
{
82-
Assert.IsType<ArgumentException>(target);
83-
}
69+
Assert.Throws<ArgumentException>(() => shortVector.CosineSimilarity(this._doubleV2));
8470
}
8571

8672
[Fact]

dotnet/src/SemanticKernel.UnitTests/VectorOperations/VectorSpanTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void ItThrowsOnCosineSimilarityWithDifferentLengthVectorsFloat()
7171
try
7272
{
7373
vSpan1.CosineSimilarity(vSpan2);
74+
Assert.True(false, "No exception thrown");
7475
}
7576
catch (ArgumentException target)
7677
{
@@ -89,6 +90,7 @@ public void ItThrowsOnCosineSimilarityWithDifferentLengthVectorsDouble()
8990
try
9091
{
9192
vSpan1.CosineSimilarity(vSpan2);
93+
Assert.True(false, "No exception thrown");
9294
}
9395
catch (ArgumentException target)
9496
{
@@ -161,6 +163,7 @@ public void ItThrowsOnDotProductWithDifferentLengthVectorsFP()
161163
try
162164
{
163165
vSpan1.Dot(vSpan2);
166+
Assert.True(false, "No exception thrown");
164167
}
165168
catch (ArgumentException target)
166169
{
@@ -179,6 +182,7 @@ public void ItThrowsOnDotProductWithDifferentLengthVectorsDouble()
179182
try
180183
{
181184
vSpan1.Dot(vSpan2);
185+
Assert.True(false, "No exception thrown");
182186
}
183187
catch (ArgumentException target)
184188
{

0 commit comments

Comments
 (0)