mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
fb513c38a6
* Checkpoint * Update workflows/DeepResearch.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Comment * Fix comment * Update package version * Fix nuget haxx * Checkpoint * Code complete * Testing * Message content workaround * Add sequential flow * Checkpoint * Integration test project * Checkpoint * Checkpoint cleanup * Complete * Checkpoint * Fix tests * Comment cleanup * Namespace * Formatting * Analyzer updates * Workflow update * Fixed! * Update dotnet/tests/Microsoft.Agents.Workflows.Declarative.UnitTests/DeclarativeWorkflowTest.cs Co-authored-by: Tao Chen <taochen@microsoft.com> * Fix build error * Purge "immutable" set and dictionary * Fix as task * Collection expression * Another * Frozen => Readonly (perf) * Fix * Namespace --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Tao Chen <taochen@microsoft.com>
210 lines
6.4 KiB
C#
210 lines
6.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Agents.Workflows.Declarative.ObjectModel;
|
|
using Microsoft.Bot.ObjectModel;
|
|
using Microsoft.PowerFx.Types;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace Microsoft.Agents.Workflows.Declarative.UnitTests.ObjectModel;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="SetVariableExecutor"/>.
|
|
/// </summary>
|
|
public sealed class SetVariableExecutorTest(ITestOutputHelper output) : WorkflowActionExecutorTest(output)
|
|
{
|
|
[Fact]
|
|
public void InvalidModel()
|
|
{
|
|
// Arrange, Act, Assert
|
|
Assert.Throws<DeclarativeModelException>(() => new SetVariableExecutor(new SetVariable(), this.State));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetNumericValue()
|
|
{
|
|
// Arrange, Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetNumericValue),
|
|
variableName: "TestVariable",
|
|
variableValue: new NumberDataValue(42),
|
|
expectedValue: FormulaValue.New(42));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetStringValue()
|
|
{
|
|
// Arrange, Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetStringValue),
|
|
variableName: "TestVariable",
|
|
variableValue: new StringDataValue("Text"),
|
|
expectedValue: FormulaValue.New("Text"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetBooleanValue()
|
|
{
|
|
// Arrange, Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetBooleanValue),
|
|
variableName: "TestVariable",
|
|
variableValue: new BooleanDataValue(true),
|
|
expectedValue: FormulaValue.New(true));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetBooleanExpression()
|
|
{
|
|
// Arrange
|
|
ValueExpression.Builder expressionBuilder = new(ValueExpression.Expression("true || false"));
|
|
|
|
// Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetBooleanExpression),
|
|
variableName: "TestVariable",
|
|
valueExpression: expressionBuilder,
|
|
expectedValue: FormulaValue.New(true));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetNumberExpression()
|
|
{
|
|
// Arrange
|
|
ValueExpression.Builder expressionBuilder = new(ValueExpression.Expression("9 - 3"));
|
|
|
|
// Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetBooleanExpression),
|
|
variableName: "TestVariable",
|
|
valueExpression: expressionBuilder,
|
|
expectedValue: FormulaValue.New(6));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetStringExpression()
|
|
{
|
|
// Arrange
|
|
ValueExpression.Builder expressionBuilder = new(ValueExpression.Expression(@"Concatenate(""A"", ""B"", ""C"")"));
|
|
|
|
// Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetBooleanExpression),
|
|
variableName: "TestVariable",
|
|
valueExpression: expressionBuilder,
|
|
expectedValue: FormulaValue.New("ABC"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetBooleanVariable()
|
|
{
|
|
// Arrange
|
|
this.State.Set("Source", FormulaValue.New(true));
|
|
ValueExpression.Builder expressionBuilder = new(ValueExpression.Variable(PropertyPath.TopicVariable("Source")));
|
|
|
|
// Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetBooleanExpression),
|
|
variableName: "TestVariable",
|
|
valueExpression: expressionBuilder,
|
|
expectedValue: FormulaValue.New(true));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetNumberVariable()
|
|
{
|
|
// Arrange
|
|
this.State.Set("Source", FormulaValue.New(321));
|
|
ValueExpression.Builder expressionBuilder = new(ValueExpression.Variable(PropertyPath.TopicVariable("Source")));
|
|
|
|
// Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetBooleanExpression),
|
|
variableName: "TestVariable",
|
|
valueExpression: expressionBuilder,
|
|
expectedValue: FormulaValue.New(321));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetStringVariable()
|
|
{
|
|
// Arrange
|
|
this.State.Set("Source", FormulaValue.New("Test"));
|
|
ValueExpression.Builder expressionBuilder = new(ValueExpression.Variable(PropertyPath.TopicVariable("Source")));
|
|
|
|
// Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(SetBooleanExpression),
|
|
variableName: "TestVariable",
|
|
valueExpression: expressionBuilder,
|
|
expectedValue: FormulaValue.New("Test"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateExistingValue()
|
|
{
|
|
// Arrange
|
|
this.State.Set("VarA", FormulaValue.New(33));
|
|
|
|
// Act, Assert
|
|
await this.ExecuteTest(
|
|
displayName: nameof(UpdateExistingValue),
|
|
variableName: "VarA",
|
|
variableValue: new NumberDataValue(42),
|
|
expectedValue: FormulaValue.New(42));
|
|
}
|
|
|
|
private Task ExecuteTest(
|
|
string displayName,
|
|
string variableName,
|
|
DataValue variableValue,
|
|
FormulaValue expectedValue)
|
|
{
|
|
// Arrange
|
|
ValueExpression.Builder expressionBuilder = new(ValueExpression.Literal(variableValue));
|
|
|
|
// Act & Assert
|
|
return this.ExecuteTest(displayName, variableName, expressionBuilder, expectedValue);
|
|
}
|
|
|
|
private async Task ExecuteTest(
|
|
string displayName,
|
|
string variableName,
|
|
ValueExpression.Builder valueExpression,
|
|
FormulaValue expectedValue)
|
|
{
|
|
// Arrange
|
|
SetVariable model =
|
|
this.CreateModel(
|
|
displayName,
|
|
FormatVariablePath(variableName),
|
|
valueExpression);
|
|
|
|
this.State.Set(variableName, FormulaValue.New(33));
|
|
|
|
// Act
|
|
SetVariableExecutor action = new(model, this.State);
|
|
await this.Execute(action);
|
|
|
|
// Assert
|
|
this.VerifyModel(model, action);
|
|
this.VerifyState(variableName, expectedValue);
|
|
}
|
|
|
|
private SetVariable CreateModel(string displayName, string variablePath, ValueExpression.Builder valueExpression)
|
|
{
|
|
SetVariable.Builder actionBuilder =
|
|
new()
|
|
{
|
|
Id = this.CreateActionId(),
|
|
DisplayName = this.FormatDisplayName(displayName),
|
|
Variable = InitializablePropertyPath.Create(variablePath),
|
|
Value = valueExpression,
|
|
};
|
|
|
|
SetVariable model = this.AssignParent<SetVariable>(actionBuilder);
|
|
|
|
return model;
|
|
}
|
|
}
|