diff --git a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/HostedFileContent.cs b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/HostedFileContent.cs index 5f0f13d8b24..cc9d8fd4e97 100644 --- a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/HostedFileContent.cs +++ b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/HostedFileContent.cs @@ -25,7 +25,7 @@ public sealed class HostedFileContent : AIContent /// is empty or composed entirely of whitespace. public HostedFileContent(string fileId) { - FileId = fileId; + FileId = Throw.IfNullOrWhitespace(fileId); } /// diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedFileContentTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedFileContentTests.cs index 44750ae396c..58768d32553 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedFileContentTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedFileContentTests.cs @@ -12,9 +12,9 @@ public class HostedFileContentTests [Fact] public void Constructor_InvalidInput_Throws() { - Assert.Throws(() => new HostedFileContent(null!)); - Assert.Throws(() => new HostedFileContent(string.Empty)); - Assert.Throws(() => new HostedFileContent(" ")); + Assert.Throws("fileId", () => new HostedFileContent(null!)); + Assert.Throws("fileId", () => new HostedFileContent(string.Empty)); + Assert.Throws("fileId", () => new HostedFileContent(" ")); } [Fact] @@ -36,9 +36,9 @@ public void Constructor_PropsRoundtrip() c.FileId = "id456"; Assert.Equal("id456", c.FileId); - Assert.Throws(() => c.FileId = null!); - Assert.Throws(() => c.FileId = string.Empty); - Assert.Throws(() => c.FileId = " "); + Assert.Throws("value", () => c.FileId = null!); + Assert.Throws("value", () => c.FileId = string.Empty); + Assert.Throws("value", () => c.FileId = " "); Assert.Null(c.RawRepresentation); object raw = new(); @@ -89,7 +89,7 @@ public void MediaType_Roundtrips() public void MediaType_InvalidValue_Throws(string invalidMediaType) { HostedFileContent c = new("id123"); - Assert.Throws(() => c.MediaType = invalidMediaType); + Assert.Throws("value", () => c.MediaType = invalidMediaType); } [Theory] diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedVectorStoreContentTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedVectorStoreContentTests.cs index d709dc3ac87..105d8f2efd9 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedVectorStoreContentTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/HostedVectorStoreContentTests.cs @@ -12,9 +12,9 @@ public class HostedVectorStoreContentTests [Fact] public void Constructor_InvalidInput_Throws() { - Assert.Throws(() => new HostedVectorStoreContent(null!)); - Assert.Throws(() => new HostedVectorStoreContent(string.Empty)); - Assert.Throws(() => new HostedVectorStoreContent(" ")); + Assert.Throws("vectorStoreId", () => new HostedVectorStoreContent(null!)); + Assert.Throws("vectorStoreId", () => new HostedVectorStoreContent(string.Empty)); + Assert.Throws("vectorStoreId", () => new HostedVectorStoreContent(" ")); } [Fact] @@ -35,9 +35,9 @@ public void Constructor_PropsRoundtrip() c.VectorStoreId = "id456"; Assert.Equal("id456", c.VectorStoreId); - Assert.Throws(() => c.VectorStoreId = null!); - Assert.Throws(() => c.VectorStoreId = string.Empty); - Assert.Throws(() => c.VectorStoreId = " "); + Assert.Throws("value", () => c.VectorStoreId = null!); + Assert.Throws("value", () => c.VectorStoreId = string.Empty); + Assert.Throws("value", () => c.VectorStoreId = " "); Assert.Equal("id456", c.VectorStoreId); Assert.Null(c.RawRepresentation); diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonSchemaTransformCacheTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonSchemaTransformCacheTests.cs index 4233e5cdbe1..8f514f7a15e 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonSchemaTransformCacheTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonSchemaTransformCacheTests.cs @@ -12,13 +12,13 @@ public static class AIJsonSchemaTransformCacheTests [Fact] public static void NullOptions_ThrowsArgumentNullException() { - Assert.Throws(() => new AIJsonSchemaTransformCache(transformOptions: null!)); + Assert.Throws("transformOptions", () => new AIJsonSchemaTransformCache(transformOptions: null!)); } [Fact] public static void EmptyOptions_ThrowsArgumentException() { - Assert.Throws(() => new AIJsonSchemaTransformCache(transformOptions: new())); + Assert.Throws("transformOptions", () => new AIJsonSchemaTransformCache(transformOptions: new())); } [Fact] @@ -33,14 +33,14 @@ public static void TransformOptions_ReturnsExpectedValue() public static void NullFunction_ThrowsArgumentNullException() { AIJsonSchemaTransformCache cache = new(new() { ConvertBooleanSchemas = true }); - Assert.Throws(() => cache.GetOrCreateTransformedSchema(function: null!)); + Assert.Throws("function", () => cache.GetOrCreateTransformedSchema(function: null!)); } [Fact] public static void NullResponseFormat_ThrowsArgumentNullException() { AIJsonSchemaTransformCache cache = new(new() { ConvertBooleanSchemas = true }); - Assert.Throws(() => cache.GetOrCreateTransformedSchema(responseFormat: null!)); + Assert.Throws("responseFormat", () => cache.GetOrCreateTransformedSchema(responseFormat: null!)); } [Fact] diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs index cb61fcf7086..f27b35e847c 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs @@ -1074,8 +1074,8 @@ public static void AddAIContentType_NonAIContent_ThrowsArgumentException() public static void AddAIContentType_BuiltInAIContent_ThrowsArgumentException() { JsonSerializerOptions options = new(); - Assert.Throws(() => options.AddAIContentType("discriminator")); - Assert.Throws(() => options.AddAIContentType("discriminator")); + Assert.Throws("contentType", () => options.AddAIContentType("discriminator")); + Assert.Throws("contentType", () => options.AddAIContentType("discriminator")); } [Fact] @@ -1398,8 +1398,8 @@ public static void TransformJsonSchema_ValidateWithTestData(ITestData testData) public static void TransformJsonSchema_InvalidOptions_ThrowsArgumentException() { JsonElement schema = JsonDocument.Parse("{}").RootElement; - Assert.Throws(() => AIJsonUtilities.TransformSchema(schema, transformOptions: null!)); - Assert.Throws(() => AIJsonUtilities.TransformSchema(schema, transformOptions: new())); + Assert.Throws("transformOptions", () => AIJsonUtilities.TransformSchema(schema, transformOptions: null!)); + Assert.Throws("transformOptions", () => AIJsonUtilities.TransformSchema(schema, transformOptions: new())); } [Theory] @@ -1412,7 +1412,7 @@ public static void TransformJsonSchema_InvalidInput_ThrowsArgumentException(stri { JsonElement schema = JsonDocument.Parse(invalidSchema).RootElement; AIJsonSchemaTransformOptions transformOptions = new() { ConvertBooleanSchemas = true }; - Assert.Throws(() => AIJsonUtilities.TransformSchema(schema, transformOptions)); + Assert.Throws("schema", () => AIJsonUtilities.TransformSchema(schema, transformOptions)); } private class DerivedAIContent : AIContent diff --git a/test/Libraries/Microsoft.Extensions.AI.Evaluation.NLP.Tests/NGramTests.cs b/test/Libraries/Microsoft.Extensions.AI.Evaluation.NLP.Tests/NGramTests.cs index 6c0aefbb02a..f6401cabe08 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Evaluation.NLP.Tests/NGramTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Evaluation.NLP.Tests/NGramTests.cs @@ -21,7 +21,7 @@ public void Constructor_ValuesAndLength() [Fact] public void Constructor_ThrowsOnEmpty() { - Assert.Throws(() => new NGram(Array.Empty())); + Assert.Throws("values", () => new NGram(Array.Empty())); } [Fact] @@ -61,7 +61,7 @@ public void NGramBuilder_Create_Works() [Fact] public void CreateNGrams() { - Assert.Throws(() => new int[0].CreateNGrams(-1).ToList()); + Assert.Throws("n", () => new int[0].CreateNGrams(-1).ToList()); ReadOnlySpan data = [1, 2, 3]; @@ -81,11 +81,11 @@ public void CreateNGrams() [Fact] public void CreateAllNGrams() { - Assert.Throws(() => new int[0].CreateAllNGrams(-1).ToList()); + Assert.Throws("minN", () => new int[0].CreateAllNGrams(-1).ToList()); - Assert.Throws(() => new int[0].CreateAllNGrams(0).ToList()); + Assert.Throws("minN", () => new int[0].CreateAllNGrams(0).ToList()); - Assert.Throws(() => new int[0].CreateAllNGrams(1, 0).ToList()); + Assert.Throws("maxN", () => new int[0].CreateAllNGrams(1, 0).ToList()); ReadOnlySpan arr = [1, 2, 3]; diff --git a/test/Libraries/Microsoft.Extensions.AI.Integration.Tests/ToolReductionTests.cs b/test/Libraries/Microsoft.Extensions.AI.Integration.Tests/ToolReductionTests.cs index 02a045d9308..436d657acfa 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Integration.Tests/ToolReductionTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Integration.Tests/ToolReductionTests.cs @@ -16,7 +16,7 @@ public class ToolReductionTests public void EmbeddingToolReductionStrategy_Constructor_ThrowsWhenToolLimitIsLessThanOrEqualToZero() { using var gen = new DeterministicTestEmbeddingGenerator(); - Assert.Throws(() => new EmbeddingToolReductionStrategy(gen, toolLimit: 0)); + Assert.Throws("toolLimit", () => new EmbeddingToolReductionStrategy(gen, toolLimit: 0)); } [Fact] diff --git a/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/MessageCountingChatReducerTests.cs b/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/MessageCountingChatReducerTests.cs index 000f4889bf3..b2b74c711d7 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/MessageCountingChatReducerTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/MessageCountingChatReducerTests.cs @@ -18,7 +18,7 @@ public class MessageCountingChatReducerTests [InlineData(-10)] public void Constructor_ThrowsOnInvalidTargetCount(int targetCount) { - Assert.Throws(() => new MessageCountingChatReducer(targetCount)); + Assert.Throws(nameof(targetCount), () => new MessageCountingChatReducer(targetCount)); } [Fact] @@ -126,8 +126,8 @@ public async Task ReduceAsync_IgnoresFunctionCallsAndResults() [ new ChatMessage(ChatRole.User, "What's the weather?"), new ChatMessage(ChatRole.Assistant, [new FunctionCallContent("call1", "get_weather", new Dictionary { ["location"] = "Seattle" })]), - new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call1", "Sunny, 72°F")]), - new ChatMessage(ChatRole.Assistant, "The weather in Seattle is sunny and 72°F."), + new ChatMessage(ChatRole.Tool, [new FunctionResultContent("call1", "Sunny, 72°F")]), + new ChatMessage(ChatRole.Assistant, "The weather in Seattle is sunny and 72°F."), new ChatMessage(ChatRole.User, "Thanks!"), new ChatMessage(ChatRole.Assistant, "You're welcome!"), ]; diff --git a/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/SummarizingChatReducerTests.cs b/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/SummarizingChatReducerTests.cs index 8d5901996aa..8fa801c4811 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/SummarizingChatReducerTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Tests/ChatReduction/SummarizingChatReducerTests.cs @@ -17,7 +17,7 @@ public class SummarizingChatReducerTests [Fact] public void Constructor_ThrowsOnNullChatClient() { - Assert.Throws(() => new SummarizingChatReducer(null!, targetCount: 5, threshold: 2)); + Assert.Throws("chatClient", () => new SummarizingChatReducer(null!, targetCount: 5, threshold: 2)); } [Theory] @@ -27,7 +27,7 @@ public void Constructor_ThrowsOnNullChatClient() public void Constructor_ThrowsOnInvalidTargetCount(int targetCount) { using var chatClient = new TestChatClient(); - Assert.Throws(() => new SummarizingChatReducer(chatClient, targetCount, threshold: 2)); + Assert.Throws(nameof(targetCount), () => new SummarizingChatReducer(chatClient, targetCount, threshold: 2)); } [Theory] @@ -36,7 +36,7 @@ public void Constructor_ThrowsOnInvalidTargetCount(int targetCount) public void Constructor_ThrowsOnInvalidThresholdCount(int thresholdCount) { using var chatClient = new TestChatClient(); - Assert.Throws(() => new SummarizingChatReducer(chatClient, targetCount: 5, thresholdCount)); + Assert.Throws("threshold", () => new SummarizingChatReducer(chatClient, targetCount: 5, thresholdCount)); } [Fact]