fix: throw exception when accessing result props incorrectly

This commit is contained in:
Stevan Freeborn
2024-07-06 11:55:47 -05:00
parent 1d352a6f95
commit c538a3eb8c
8 changed files with 116 additions and 26 deletions
@@ -11,7 +11,12 @@ public class AnthropicResultTests
var actual = AnthropicResult<string>.Success(value, headers);
actual.IsSuccess.Should().BeTrue();
actual.IsFailure.Should().BeFalse();
actual.Value.Should().Be(value);
var action = () => { var _ = actual.Error; };
action.Should().Throw<InvalidOperationException>();
actual.Headers.Should().Be(headers);
}
@@ -24,7 +29,12 @@ public class AnthropicResultTests
var actual = AnthropicResult<string>.Failure(error, headers);
actual.IsSuccess.Should().BeFalse();
actual.IsFailure.Should().BeTrue();
actual.Error.Should().Be(error);
var action = () => { var _ = actual.Value; };
action.Should().Throw<InvalidOperationException>();
actual.Headers.Should().Be(headers);
}
}
@@ -0,0 +1,34 @@
namespace AnthropicClient.Tests.Unit.Models;
public class ToolCallResultTests
{
[Fact]
public void Success_WhenCalled_ItShouldReturnSuccessResult()
{
var value = "success";
var actual = ToolCallResult<string>.Success(value);
actual.IsSuccess.Should().BeTrue();
actual.IsFailure.Should().BeFalse();
actual.Value.Should().Be(value);
var action = () => { var _ = actual.Error; };
action.Should().Throw<InvalidOperationException>();
}
[Fact]
public void Failure_WhenCalled_ItShouldReturnFailureResult()
{
var error = new Exception();
var actual = ToolCallResult<string>.Failure(error);
actual.IsSuccess.Should().BeFalse();
actual.IsFailure.Should().BeTrue();
actual.Error.Should().Be(error);
var action = () => { var _ = actual.Value; };
action.Should().Throw<InvalidOperationException>();
}
}
@@ -16,7 +16,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("Hello, World!");
result.Error.Should().BeNull();
}
[Fact]
@@ -30,7 +29,6 @@ public class ToolCallTests : SerializationTest
var result = await toolCall.InvokeAsync();
result.IsSuccess.Should().BeFalse();
result.Value.Should().BeNull();
result.Error.Should().NotBeNull();
}
@@ -46,7 +44,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("Hello, World!");
result.Error.Should().BeNull();
}
[Fact]
@@ -60,7 +57,6 @@ public class ToolCallTests : SerializationTest
var result = await toolCall.InvokeAsync();
result.IsSuccess.Should().BeFalse();
result.Value.Should().BeNull();
result.Error.Should().NotBeNull();
}
@@ -78,7 +74,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("42");
result.Error.Should().BeNull();
}
[Fact]
@@ -94,7 +89,6 @@ public class ToolCallTests : SerializationTest
var result = await toolCall.InvokeAsync();
result.IsSuccess.Should().BeFalse();
result.Value.Should().BeNull();
result.Error.Should().NotBeNull();
}
@@ -112,7 +106,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("42");
result.Error.Should().BeNull();
}
[Fact]
@@ -128,7 +121,6 @@ public class ToolCallTests : SerializationTest
var result = await toolCall.InvokeAsync();
result.IsSuccess.Should().BeFalse();
result.Value.Should().BeNull();
result.Error.Should().NotBeNull();
}
@@ -146,7 +138,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("42");
result.Error.Should().BeNull();
}
[Fact]
@@ -162,7 +153,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("42");
result.Error.Should().BeNull();
}
[Fact]
@@ -177,7 +167,6 @@ public class ToolCallTests : SerializationTest
var result = await toolCall.InvokeAsync();
result.IsSuccess.Should().BeFalse();
result.Value.Should().BeNull();
result.Error.Should().NotBeNull();
}
@@ -195,7 +184,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("Monday");
result.Error.Should().BeNull();
}
[Fact]
@@ -219,7 +207,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().Be("John");
result.Error.Should().BeNull();
}
[Fact]
@@ -236,7 +223,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().BeNull();
result.Error.Should().BeNull();
}
[Fact]
@@ -253,7 +239,6 @@ public class ToolCallTests : SerializationTest
result.IsSuccess.Should().BeTrue();
result.Value.Should().BeNull();
result.Error.Should().BeNull();
}
}
@@ -301,7 +301,7 @@ public class ToolTests : SerializationTest
tool.DisplayName.Should().Be("Name");
tool.Description.Should().Be("Description");
tool.Function.Method.Name.Should().Be(nameof(ProperTool.GetWeather));
tool.Function.Instance.Should().BeNull();
tool.Function.Instance.Should().BeOfType<ProperTool>();
tool.InputSchema.Should().BeEquivalentTo(
expectedSchema,
t => t.IgnoringCyclicReferences()