Skip to content

Commit 57004d9

Browse files
authored
Fix Assert.Throws to validate parameter names (#7007)
1 parent 607269f commit 57004d9

File tree

9 files changed

+35
-35
lines changed

9 files changed

+35
-35
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/HostedFileContent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class HostedFileContent : AIContent
2525
/// <exception cref="ArgumentException"><paramref name="fileId"/> is empty or composed entirely of whitespace.</exception>
2626
public HostedFileContent(string fileId)
2727
{
28-
FileId = fileId;
28+
FileId = Throw.IfNullOrWhitespace(fileId);
2929
}
3030

3131
/// <summary>

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedFileContentTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class HostedFileContentTests
1212
[Fact]
1313
public void Constructor_InvalidInput_Throws()
1414
{
15-
Assert.Throws<ArgumentNullException>(() => new HostedFileContent(null!));
16-
Assert.Throws<ArgumentException>(() => new HostedFileContent(string.Empty));
17-
Assert.Throws<ArgumentException>(() => new HostedFileContent(" "));
15+
Assert.Throws<ArgumentNullException>("fileId", () => new HostedFileContent(null!));
16+
Assert.Throws<ArgumentException>("fileId", () => new HostedFileContent(string.Empty));
17+
Assert.Throws<ArgumentException>("fileId", () => new HostedFileContent(" "));
1818
}
1919

2020
[Fact]
@@ -36,9 +36,9 @@ public void Constructor_PropsRoundtrip()
3636
c.FileId = "id456";
3737
Assert.Equal("id456", c.FileId);
3838

39-
Assert.Throws<ArgumentNullException>(() => c.FileId = null!);
40-
Assert.Throws<ArgumentException>(() => c.FileId = string.Empty);
41-
Assert.Throws<ArgumentException>(() => c.FileId = " ");
39+
Assert.Throws<ArgumentNullException>("value", () => c.FileId = null!);
40+
Assert.Throws<ArgumentException>("value", () => c.FileId = string.Empty);
41+
Assert.Throws<ArgumentException>("value", () => c.FileId = " ");
4242

4343
Assert.Null(c.RawRepresentation);
4444
object raw = new();
@@ -89,7 +89,7 @@ public void MediaType_Roundtrips()
8989
public void MediaType_InvalidValue_Throws(string invalidMediaType)
9090
{
9191
HostedFileContent c = new("id123");
92-
Assert.Throws<ArgumentException>(() => c.MediaType = invalidMediaType);
92+
Assert.Throws<ArgumentException>("value", () => c.MediaType = invalidMediaType);
9393
}
9494

9595
[Theory]

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedVectorStoreContentTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class HostedVectorStoreContentTests
1212
[Fact]
1313
public void Constructor_InvalidInput_Throws()
1414
{
15-
Assert.Throws<ArgumentNullException>(() => new HostedVectorStoreContent(null!));
16-
Assert.Throws<ArgumentException>(() => new HostedVectorStoreContent(string.Empty));
17-
Assert.Throws<ArgumentException>(() => new HostedVectorStoreContent(" "));
15+
Assert.Throws<ArgumentNullException>("vectorStoreId", () => new HostedVectorStoreContent(null!));
16+
Assert.Throws<ArgumentException>("vectorStoreId", () => new HostedVectorStoreContent(string.Empty));
17+
Assert.Throws<ArgumentException>("vectorStoreId", () => new HostedVectorStoreContent(" "));
1818
}
1919

2020
[Fact]
@@ -35,9 +35,9 @@ public void Constructor_PropsRoundtrip()
3535
c.VectorStoreId = "id456";
3636
Assert.Equal("id456", c.VectorStoreId);
3737

38-
Assert.Throws<ArgumentNullException>(() => c.VectorStoreId = null!);
39-
Assert.Throws<ArgumentException>(() => c.VectorStoreId = string.Empty);
40-
Assert.Throws<ArgumentException>(() => c.VectorStoreId = " ");
38+
Assert.Throws<ArgumentNullException>("value", () => c.VectorStoreId = null!);
39+
Assert.Throws<ArgumentException>("value", () => c.VectorStoreId = string.Empty);
40+
Assert.Throws<ArgumentException>("value", () => c.VectorStoreId = " ");
4141
Assert.Equal("id456", c.VectorStoreId);
4242

4343
Assert.Null(c.RawRepresentation);

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonSchemaTransformCacheTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ public static class AIJsonSchemaTransformCacheTests
1212
[Fact]
1313
public static void NullOptions_ThrowsArgumentNullException()
1414
{
15-
Assert.Throws<ArgumentNullException>(() => new AIJsonSchemaTransformCache(transformOptions: null!));
15+
Assert.Throws<ArgumentNullException>("transformOptions", () => new AIJsonSchemaTransformCache(transformOptions: null!));
1616
}
1717

1818
[Fact]
1919
public static void EmptyOptions_ThrowsArgumentException()
2020
{
21-
Assert.Throws<ArgumentException>(() => new AIJsonSchemaTransformCache(transformOptions: new()));
21+
Assert.Throws<ArgumentException>("transformOptions", () => new AIJsonSchemaTransformCache(transformOptions: new()));
2222
}
2323

2424
[Fact]
@@ -33,14 +33,14 @@ public static void TransformOptions_ReturnsExpectedValue()
3333
public static void NullFunction_ThrowsArgumentNullException()
3434
{
3535
AIJsonSchemaTransformCache cache = new(new() { ConvertBooleanSchemas = true });
36-
Assert.Throws<ArgumentNullException>(() => cache.GetOrCreateTransformedSchema(function: null!));
36+
Assert.Throws<ArgumentNullException>("function", () => cache.GetOrCreateTransformedSchema(function: null!));
3737
}
3838

3939
[Fact]
4040
public static void NullResponseFormat_ThrowsArgumentNullException()
4141
{
4242
AIJsonSchemaTransformCache cache = new(new() { ConvertBooleanSchemas = true });
43-
Assert.Throws<ArgumentNullException>(() => cache.GetOrCreateTransformedSchema(responseFormat: null!));
43+
Assert.Throws<ArgumentNullException>("responseFormat", () => cache.GetOrCreateTransformedSchema(responseFormat: null!));
4444
}
4545

4646
[Fact]

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,8 @@ public static void AddAIContentType_NonAIContent_ThrowsArgumentException()
10741074
public static void AddAIContentType_BuiltInAIContent_ThrowsArgumentException()
10751075
{
10761076
JsonSerializerOptions options = new();
1077-
Assert.Throws<ArgumentException>(() => options.AddAIContentType<AIContent>("discriminator"));
1078-
Assert.Throws<ArgumentException>(() => options.AddAIContentType<TextContent>("discriminator"));
1077+
Assert.Throws<ArgumentException>("contentType", () => options.AddAIContentType<AIContent>("discriminator"));
1078+
Assert.Throws<ArgumentException>("contentType", () => options.AddAIContentType<TextContent>("discriminator"));
10791079
}
10801080

10811081
[Fact]
@@ -1398,8 +1398,8 @@ public static void TransformJsonSchema_ValidateWithTestData(ITestData testData)
13981398
public static void TransformJsonSchema_InvalidOptions_ThrowsArgumentException()
13991399
{
14001400
JsonElement schema = JsonDocument.Parse("{}").RootElement;
1401-
Assert.Throws<ArgumentNullException>(() => AIJsonUtilities.TransformSchema(schema, transformOptions: null!));
1402-
Assert.Throws<ArgumentException>(() => AIJsonUtilities.TransformSchema(schema, transformOptions: new()));
1401+
Assert.Throws<ArgumentNullException>("transformOptions", () => AIJsonUtilities.TransformSchema(schema, transformOptions: null!));
1402+
Assert.Throws<ArgumentException>("transformOptions", () => AIJsonUtilities.TransformSchema(schema, transformOptions: new()));
14031403
}
14041404

14051405
[Theory]
@@ -1412,7 +1412,7 @@ public static void TransformJsonSchema_InvalidInput_ThrowsArgumentException(stri
14121412
{
14131413
JsonElement schema = JsonDocument.Parse(invalidSchema).RootElement;
14141414
AIJsonSchemaTransformOptions transformOptions = new() { ConvertBooleanSchemas = true };
1415-
Assert.Throws<ArgumentException>(() => AIJsonUtilities.TransformSchema(schema, transformOptions));
1415+
Assert.Throws<ArgumentException>("schema", () => AIJsonUtilities.TransformSchema(schema, transformOptions));
14161416
}
14171417

14181418
private class DerivedAIContent : AIContent

test/Libraries/Microsoft.Extensions.AI.Evaluation.NLP.Tests/NGramTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void Constructor_ValuesAndLength()
2121
[Fact]
2222
public void Constructor_ThrowsOnEmpty()
2323
{
24-
Assert.Throws<ArgumentOutOfRangeException>(() => new NGram<int>(Array.Empty<int>()));
24+
Assert.Throws<ArgumentOutOfRangeException>("values", () => new NGram<int>(Array.Empty<int>()));
2525
}
2626

2727
[Fact]
@@ -61,7 +61,7 @@ public void NGramBuilder_Create_Works()
6161
[Fact]
6262
public void CreateNGrams()
6363
{
64-
Assert.Throws<ArgumentOutOfRangeException>(() => new int[0].CreateNGrams(-1).ToList());
64+
Assert.Throws<ArgumentOutOfRangeException>("n", () => new int[0].CreateNGrams(-1).ToList());
6565

6666
ReadOnlySpan<int> data = [1, 2, 3];
6767

@@ -81,11 +81,11 @@ public void CreateNGrams()
8181
[Fact]
8282
public void CreateAllNGrams()
8383
{
84-
Assert.Throws<ArgumentOutOfRangeException>(() => new int[0].CreateAllNGrams(-1).ToList());
84+
Assert.Throws<ArgumentOutOfRangeException>("minN", () => new int[0].CreateAllNGrams(-1).ToList());
8585

86-
Assert.Throws<ArgumentOutOfRangeException>(() => new int[0].CreateAllNGrams(0).ToList());
86+
Assert.Throws<ArgumentOutOfRangeException>("minN", () => new int[0].CreateAllNGrams(0).ToList());
8787

88-
Assert.Throws<ArgumentOutOfRangeException>(() => new int[0].CreateAllNGrams(1, 0).ToList());
88+
Assert.Throws<ArgumentOutOfRangeException>("maxN", () => new int[0].CreateAllNGrams(1, 0).ToList());
8989

9090
ReadOnlySpan<int> arr = [1, 2, 3];
9191

test/Libraries/Microsoft.Extensions.AI.Integration.Tests/ToolReductionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ToolReductionTests
1616
public void EmbeddingToolReductionStrategy_Constructor_ThrowsWhenToolLimitIsLessThanOrEqualToZero()
1717
{
1818
using var gen = new DeterministicTestEmbeddingGenerator();
19-
Assert.Throws<ArgumentOutOfRangeException>(() => new EmbeddingToolReductionStrategy(gen, toolLimit: 0));
19+
Assert.Throws<ArgumentOutOfRangeException>("toolLimit", () => new EmbeddingToolReductionStrategy(gen, toolLimit: 0));
2020
}
2121

2222
[Fact]

test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/MessageCountingChatReducerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MessageCountingChatReducerTests
1818
[InlineData(-10)]
1919
public void Constructor_ThrowsOnInvalidTargetCount(int targetCount)
2020
{
21-
Assert.Throws<ArgumentOutOfRangeException>(() => new MessageCountingChatReducer(targetCount));
21+
Assert.Throws<ArgumentOutOfRangeException>(nameof(targetCount), () => new MessageCountingChatReducer(targetCount));
2222
}
2323

2424
[Fact]
@@ -126,8 +126,8 @@ public async Task ReduceAsync_IgnoresFunctionCallsAndResults()
126126
[
127127
new ChatMessage(ChatRole.User, "What's the weather?"),
128128
new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("call1", "get_weather", new Dictionary<string, object?> { ["location"] = "Seattle" })]),
129-
new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call1", "Sunny, 72°F")]),
130-
new ChatMessage(ChatRole.Assistant, "The weather in Seattle is sunny and 72°F."),
129+
new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call1", "Sunny, 72°F")]),
130+
new ChatMessage(ChatRole.Assistant, "The weather in Seattle is sunny and 72°F."),
131131
new ChatMessage(ChatRole.User, "Thanks!"),
132132
new ChatMessage(ChatRole.Assistant, "You're welcome!"),
133133
];

test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/SummarizingChatReducerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class SummarizingChatReducerTests
1717
[Fact]
1818
public void Constructor_ThrowsOnNullChatClient()
1919
{
20-
Assert.Throws<ArgumentNullException>(() => new SummarizingChatReducer(null!, targetCount: 5, threshold: 2));
20+
Assert.Throws<ArgumentNullException>("chatClient", () => new SummarizingChatReducer(null!, targetCount: 5, threshold: 2));
2121
}
2222

2323
[Theory]
@@ -27,7 +27,7 @@ public void Constructor_ThrowsOnNullChatClient()
2727
public void Constructor_ThrowsOnInvalidTargetCount(int targetCount)
2828
{
2929
using var chatClient = new TestChatClient();
30-
Assert.Throws<ArgumentOutOfRangeException>(() => new SummarizingChatReducer(chatClient, targetCount, threshold: 2));
30+
Assert.Throws<ArgumentOutOfRangeException>(nameof(targetCount), () => new SummarizingChatReducer(chatClient, targetCount, threshold: 2));
3131
}
3232

3333
[Theory]
@@ -36,7 +36,7 @@ public void Constructor_ThrowsOnInvalidTargetCount(int targetCount)
3636
public void Constructor_ThrowsOnInvalidThresholdCount(int thresholdCount)
3737
{
3838
using var chatClient = new TestChatClient();
39-
Assert.Throws<ArgumentOutOfRangeException>(() => new SummarizingChatReducer(chatClient, targetCount: 5, thresholdCount));
39+
Assert.Throws<ArgumentOutOfRangeException>("threshold", () => new SummarizingChatReducer(chatClient, targetCount: 5, thresholdCount));
4040
}
4141

4242
[Fact]

0 commit comments

Comments
 (0)