Skip to content

Commit

Permalink
Fix M.E.AI argument tests to validate argument names (#5653)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Nov 18, 2024
1 parent e0f354a commit f085689
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public int? Dimensions
{
if (value is not null)
{
_ = Throw.IfLessThan(value.Value, 1);
_ = Throw.IfLessThan(value.Value, 1, nameof(value));
}

_dimensions = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public void Constructor_Value_Roundtrips()
[Fact]
public void Constructor_NullOrWhiteSpace_Throws()
{
Assert.Throws<ArgumentNullException>(() => new ChatFinishReason(null!));
Assert.Throws<ArgumentException>(() => new ChatFinishReason(" "));
Assert.Throws<ArgumentNullException>("value", () => new ChatFinishReason(null!));
Assert.Throws<ArgumentException>("value", () => new ChatFinishReason(" "));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public void Singletons_Idempotent()
[Fact]
public void Constructor_InvalidArgs_Throws()
{
Assert.Throws<ArgumentException>(() => new ChatResponseFormatJson(null, "name"));
Assert.Throws<ArgumentException>(() => new ChatResponseFormatJson(null, null, "description"));
Assert.Throws<ArgumentException>(() => new ChatResponseFormatJson(null, "name", "description"));
Assert.Throws<ArgumentException>("schemaName", () => new ChatResponseFormatJson(null, "name"));
Assert.Throws<ArgumentException>("schemaDescription", () => new ChatResponseFormatJson(null, null, "description"));
Assert.Throws<ArgumentException>("schemaName", () => new ChatResponseFormatJson(null, "name", "description"));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public void Constructor_Value_Roundtrips()
[Fact]
public void Constructor_NullOrWhiteSpace_Throws()
{
Assert.Throws<ArgumentNullException>(() => new ChatRole(null!));
Assert.Throws<ArgumentException>(() => new ChatRole(" "));
Assert.Throws<ArgumentNullException>("value", () => new ChatRole(null!));
Assert.Throws<ArgumentException>("value", () => new ChatRole(" "));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DelegatingChatClientTests
[Fact]
public void RequiresInnerChatClient()
{
Assert.Throws<ArgumentNullException>(() => new NoOpDelegatingChatClient(null!));
Assert.Throws<ArgumentNullException>("innerClient", () => new NoOpDelegatingChatClient(null!));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public void Ctor_InvalidUri_Throws(string path, Type exception)
[InlineData("type/subtype;key=")]
[InlineData("type/subtype;=value")]
[InlineData("type/subtype;key=value;another=")]
public void Ctor_InvalidMediaType_Throws(string mediaType)
public void Ctor_InvalidMediaType_Throws(string type)
{
Assert.Throws<ArgumentException>(() => CreateDataContent("http://localhost/test", mediaType));
Assert.Throws<ArgumentException>("mediaType", () => CreateDataContent("http://localhost/test", type));
}

[Theory]
Expand Down Expand Up @@ -151,7 +151,7 @@ public void Serialize_MatchesExpectedJson()
[InlineData("""{ "mediaType":"text/plain" }""")]
public void Deserialize_MissingUriString_Throws(string json)
{
Assert.Throws<ArgumentNullException>(() => JsonSerializer.Deserialize<DataContent>(json, TestJsonSerializerContext.Default.Options)!);
Assert.Throws<ArgumentNullException>("uri", () => JsonSerializer.Deserialize<DataContent>(json, TestJsonSerializerContext.Default.Options)!);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,9 @@ public static void CreateFromParsedArguments_ParseException_HasExpectedHandling(
[Fact]
public static void CreateFromParsedArguments_NullInput_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => FunctionCallContent.CreateFromParsedArguments((string)null!, "callId", "functionName", _ => null));
Assert.Throws<ArgumentNullException>(() => FunctionCallContent.CreateFromParsedArguments("{}", null!, "functionName", _ => null));
Assert.Throws<ArgumentNullException>(() => FunctionCallContent.CreateFromParsedArguments("{}", "callId", null!, _ => null));
Assert.Throws<ArgumentNullException>(() => FunctionCallContent.CreateFromParsedArguments("{}", "callId", "functionName", null!));
Assert.Throws<ArgumentNullException>("encodedArguments", () => FunctionCallContent.CreateFromParsedArguments((string)null!, "callId", "functionName", _ => null));
Assert.Throws<ArgumentNullException>("callId", () => FunctionCallContent.CreateFromParsedArguments("{}", null!, "functionName", _ => null));
Assert.Throws<ArgumentNullException>("name", () => FunctionCallContent.CreateFromParsedArguments("{}", "callId", null!, _ => null));
Assert.Throws<ArgumentNullException>("argumentParser", () => FunctionCallContent.CreateFromParsedArguments("{}", "callId", "functionName", null!));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DelegatingEmbeddingGeneratorTests
[Fact]
public void RequiresInnerService()
{
Assert.Throws<ArgumentNullException>(() => new NoOpDelegatingEmbeddingGenerator(null!));
Assert.Throws<ArgumentNullException>("innerGenerator", () => new NoOpDelegatingEmbeddingGenerator(null!));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void Constructor_Parameterless_PropsDefaulted()
public void InvalidArgs_Throws()
{
EmbeddingGenerationOptions options = new();
Assert.Throws<ArgumentOutOfRangeException>(() => options.Dimensions = 0);
Assert.Throws<ArgumentOutOfRangeException>(() => options.Dimensions = -1);
Assert.Throws<ArgumentOutOfRangeException>("value", () => options.Dimensions = 0);
Assert.Throws<ArgumentOutOfRangeException>("value", () => options.Dimensions = -1);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public void Ctor_ValidArgs_NoExceptions()

instance.CopyTo(Array.Empty<Embedding<float>>(), 0);

Assert.Throws<ArgumentOutOfRangeException>(() => instance[0]);
Assert.Throws<ArgumentOutOfRangeException>(() => instance[-1]);
Assert.Throws<ArgumentOutOfRangeException>("index", () => instance[0]);
Assert.Throws<ArgumentOutOfRangeException>("index", () => instance[-1]);
}
}

Expand Down Expand Up @@ -77,8 +77,8 @@ public void Ctor_RoundtripsEnumerable()
Assert.False(generatedEmbeddings.Contains(null!));
Assert.Equal(-1, generatedEmbeddings.IndexOf(null!));

Assert.Throws<ArgumentOutOfRangeException>(() => generatedEmbeddings[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => generatedEmbeddings[2]);
Assert.Throws<ArgumentOutOfRangeException>("index", () => generatedEmbeddings[-1]);
Assert.Throws<ArgumentOutOfRangeException>("index", () => generatedEmbeddings[2]);

Assert.True(embeddings.SequenceEqual(generatedEmbeddings));

Expand Down Expand Up @@ -240,7 +240,7 @@ public void Indexer_InvalidIndex_Throws()
embeddings.AddRange(new[] { e1, e2 });
Assert.Equal(2, embeddings.Count);

Assert.Throws<ArgumentOutOfRangeException>(() => embeddings[-1]);
Assert.Throws<ArgumentOutOfRangeException>(() => embeddings[2]);
Assert.Throws<ArgumentOutOfRangeException>("index", () => embeddings[-1]);
Assert.Throws<ArgumentOutOfRangeException>("index", () => embeddings[2]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public void BuildsPipelineInOrderAdded()
[Fact]
public void DoesNotAcceptNullInnerService()
{
Assert.Throws<ArgumentNullException>(() => new ChatClientBuilder((IChatClient)null!));
Assert.Throws<ArgumentNullException>("innerClient", () => new ChatClientBuilder((IChatClient)null!));
}

[Fact]
public void DoesNotAcceptNullFactories()
{
Assert.Throws<ArgumentNullException>(() => new ChatClientBuilder((Func<IServiceProvider, IChatClient>)null!));
Assert.Throws<ArgumentNullException>("innerClientFactory", () => new ChatClientBuilder((Func<IServiceProvider, IChatClient>)null!));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ public void BuildsPipelineInOrderAdded()
[Fact]
public void DoesNotAcceptNullInnerService()
{
Assert.Throws<ArgumentNullException>(() => new EmbeddingGeneratorBuilder<string, Embedding<float>>((IEmbeddingGenerator<string, Embedding<float>>)null!));
Assert.Throws<ArgumentNullException>("innerGenerator", () => new EmbeddingGeneratorBuilder<string, Embedding<float>>((IEmbeddingGenerator<string, Embedding<float>>)null!));
}

[Fact]
public void DoesNotAcceptNullFactories()
{
Assert.Throws<ArgumentNullException>(() => new EmbeddingGeneratorBuilder<string, Embedding<float>>((Func<IServiceProvider, IEmbeddingGenerator<string, Embedding<float>>>)null!));
Assert.Throws<ArgumentNullException>("innerGeneratorFactory",
() => new EmbeddingGeneratorBuilder<string, Embedding<float>>((Func<IServiceProvider, IEmbeddingGenerator<string, Embedding<float>>>)null!));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class AIFunctionFactoryTest
[Fact]
public void InvalidArguments_Throw()
{
Assert.Throws<ArgumentNullException>(() => AIFunctionFactory.Create(method: null!));
Assert.Throws<ArgumentNullException>(() => AIFunctionFactory.Create(method: null!, target: new object()));
Assert.Throws<ArgumentNullException>(() => AIFunctionFactory.Create(method: null!, target: new object(), name: "myAiFunk"));
Assert.Throws<ArgumentNullException>(() => AIFunctionFactory.Create(typeof(AIFunctionFactoryTest).GetMethod(nameof(InvalidArguments_Throw))!, null));
Assert.Throws<ArgumentException>(() => AIFunctionFactory.Create(typeof(List<>).GetMethod("Add")!, new List<int>()));
Assert.Throws<ArgumentNullException>("method", () => AIFunctionFactory.Create(method: null!));
Assert.Throws<ArgumentNullException>("method", () => AIFunctionFactory.Create(method: null!, target: new object()));
Assert.Throws<ArgumentNullException>("method", () => AIFunctionFactory.Create(method: null!, target: new object(), name: "myAiFunk"));
Assert.Throws<ArgumentNullException>("target", () => AIFunctionFactory.Create(typeof(AIFunctionFactoryTest).GetMethod(nameof(InvalidArguments_Throw))!, null));
Assert.Throws<ArgumentException>("method", () => AIFunctionFactory.Create(typeof(List<>).GetMethod("Add")!, new List<int>()));
}

[Fact]
Expand Down

0 comments on commit f085689

Please sign in to comment.