fix: throw exception when accessing result props incorrectly
This commit is contained in:
@@ -154,7 +154,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
|
||||
"Get Weather",
|
||||
"Get the weather for a location in the specified units",
|
||||
typeof(GetWeatherTool),
|
||||
nameof(GetWeatherTool.GetWeather)
|
||||
nameof(GetWeatherTool.GetWeatherStatically)
|
||||
);
|
||||
|
||||
var response = await _client.CreateMessageAsync(new MessageRequest(
|
||||
|
||||
@@ -69,7 +69,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeOfType<MessageResponse>();
|
||||
result.Error.Should().BeNull();
|
||||
|
||||
var message = result.Value;
|
||||
message.Id.Should().Be("msg_013Zva2CMHLNnXjNJJKqJ2EF");
|
||||
@@ -135,7 +134,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeOfType<MessageResponse>();
|
||||
result.Error.Should().BeNull();
|
||||
|
||||
var message = result.Value;
|
||||
message.Id.Should().Be("msg_01D7FLrfh4GYq7yT1ULFeyMV");
|
||||
@@ -163,7 +161,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
var toolCallResult = await message.ToolCall!.InvokeAsync();
|
||||
toolCallResult.IsSuccess.Should().BeTrue();
|
||||
toolCallResult.Value!.ToString().Should().Be("^GSPC");
|
||||
toolCallResult.Error.Should().BeNull();
|
||||
}
|
||||
|
||||
|
||||
@@ -208,7 +205,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
|
||||
result.IsSuccess.Should().BeTrue();
|
||||
result.Value.Should().BeOfType<MessageResponse>();
|
||||
result.Error.Should().BeNull();
|
||||
|
||||
var message = result.Value;
|
||||
message.Id.Should().Be("msg_01D7FLrfh4GYq7yT1ULFeyMV");
|
||||
@@ -323,7 +319,6 @@ public class AnthropicApiClientTests : IntegrationTest
|
||||
var toolCallResult = await toolCall!.InvokeAsync<string>();
|
||||
|
||||
toolCallResult.IsSuccess.Should().BeTrue();
|
||||
toolCallResult.Error.Should().BeNull();
|
||||
toolCallResult.Value.Should().Be(getWeather("San Francisco, CA","fahrenheit"));
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user