Refactor SetVariable activity: add null safety checks, update variable property to nullable type, and enhance unit tests for edge cases.
This commit is contained in:
parent
e6df8ea954
commit
8eb2b11f6e
|
|
@ -89,7 +89,7 @@ public class SetVariable : CodeActivity
|
|||
/// The variable to assign the value to.
|
||||
/// </summary>
|
||||
[Input(Description = "The variable to assign the value to.")]
|
||||
public Variable Variable { get; set; } = null!;
|
||||
public Variable? Variable { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The value to assign.
|
||||
|
|
@ -101,9 +101,13 @@ public class SetVariable : CodeActivity
|
|||
protected override void Execute(ActivityExecutionContext context)
|
||||
{
|
||||
// Always refer to the variable by ID to ensure that the variable is resolved from the correct scope.
|
||||
var variableId = Variable.Id;
|
||||
var variableId = Variable?.Id;
|
||||
var variable = context.ExpressionExecutionContext.EnumerateVariablesInScope().FirstOrDefault(x => x.Id == variableId);
|
||||
|
||||
if (variable == null)
|
||||
throw new($"Variable '{variableId}' not found.");
|
||||
|
||||
var value = context.Get(Value);
|
||||
variable?.Set(context, value);
|
||||
variable.Set(context, value);
|
||||
}
|
||||
}
|
||||
|
|
@ -57,7 +57,6 @@ public class ActivityRegistry(IActivityDescriber activityDescriber, IEnumerable<
|
|||
|
||||
var activityDescriptor = await activityDescriber.DescribeActivityAsync(activityType, cancellationToken);
|
||||
|
||||
|
||||
Add(activityDescriptor, _activityDescriptors, _manualActivityDescriptors);
|
||||
_manualActivityDescriptors.Add(activityDescriptor);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ using Elsa.Workflows;
|
|||
using Elsa.Workflows.CommitStates;
|
||||
using Elsa.Workflows.Management.Providers;
|
||||
using Elsa.Workflows.Management.Services;
|
||||
using Elsa.Workflows.PortResolvers;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NSubstitute;
|
||||
|
||||
|
|
@ -26,16 +27,15 @@ public static class ActivityTestHelper
|
|||
/// </summary>
|
||||
/// <param name="activity">The activity to execute</param>
|
||||
/// <returns>The ActivityExecutionContext used for execution</returns>
|
||||
public static async Task<ActivityExecutionContext> ExecuteActivityAsync(IActivity activity)
|
||||
public static async Task<ActivityExecutionContext> ExecuteActivityAsync(IActivity activity, Action<IServiceCollection>? configureServices = null)
|
||||
{
|
||||
var context = CreateMinimalActivityExecutionContext(activity, out var serviceProvider);
|
||||
|
||||
var context = await CreateMinimalActivityExecutionContext(activity, configureServices);
|
||||
|
||||
// Set up variables and inputs, then execute the activity
|
||||
await SetupExistingVariablesAsync(activity, context);
|
||||
await SetupInputValuesInMemoryAsync(activity, context, serviceProvider);
|
||||
await context.EvaluateInputPropertiesAsync();
|
||||
await activity.ExecuteAsync(context);
|
||||
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
|
|
@ -44,82 +44,61 @@ public static class ActivityTestHelper
|
|||
/// This helper method creates a real WorkflowExecutionContext using the minimal workflow pattern
|
||||
/// to provide proper context for activities.
|
||||
/// </summary>
|
||||
private static ActivityExecutionContext CreateMinimalActivityExecutionContext(IActivity activity, out IServiceProvider serviceProvider)
|
||||
private static async Task<ActivityExecutionContext> CreateMinimalActivityExecutionContext(IActivity activity, Action<IServiceCollection>? configureServices)
|
||||
{
|
||||
// Create a minimal service provider with the required services for expression evaluation
|
||||
var services = new ServiceCollection();
|
||||
|
||||
|
||||
// Add core services
|
||||
services.AddLogging();
|
||||
services.AddSingleton<ISystemClock>(_ => Substitute.For<ISystemClock>());
|
||||
services.AddSingleton<INotificationSender>(_ => Substitute.For<INotificationSender>());
|
||||
services.AddSingleton<IActivityVisitor, ActivityVisitor>();
|
||||
|
||||
// Add real expression evaluation services instead of mocks
|
||||
services.AddScoped<IExpressionEvaluator, ExpressionEvaluator>();
|
||||
|
||||
// Add the well-known type registry required by expression handlers
|
||||
services.AddSingleton<IWellKnownTypeRegistry, WellKnownTypeRegistry>();
|
||||
|
||||
services.AddSingleton<IActivityDescriber, ActivityDescriber>();
|
||||
services.AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>();
|
||||
services.AddSingleton<IActivityFactory, ActivityFactory>();
|
||||
services.AddSingleton<IPropertyDefaultValueResolver, PropertyDefaultValueResolver>();
|
||||
services.AddSingleton<IPropertyUIHandlerResolver, PropertyUIHandlerResolver>();
|
||||
services.AddSingleton<IActivityRegistry, ActivityRegistry>();
|
||||
services.AddScoped<IActivityRegistryLookupService, ActivityRegistryLookupService>();
|
||||
services.AddScoped<IIdentityGraphService, IdentityGraphService>();
|
||||
services.AddScoped<IWorkflowGraphBuilder, WorkflowGraphBuilder>();
|
||||
services.AddScoped<IActivityResolver, PropertyBasedActivityResolver>();
|
||||
services.AddScoped<IActivityResolver, SwitchActivityResolver>();
|
||||
services.AddScoped<DefaultActivityInputEvaluator>();
|
||||
|
||||
// Add the default expression descriptor provider which includes Literal expressions
|
||||
services.AddSingleton<IExpressionDescriptorProvider, DefaultExpressionDescriptorProvider>();
|
||||
services.AddSingleton<IExpressionDescriptorRegistry, ExpressionDescriptorRegistry>();
|
||||
|
||||
|
||||
services.AddSingleton<IIdentityGenerator>(_ => Substitute.For<IIdentityGenerator>());
|
||||
|
||||
// Mock the complex workflow-level dependencies
|
||||
services.AddSingleton<IActivityRegistry>(_ => Substitute.For<IActivityRegistry>());
|
||||
|
||||
// Set up the activity registry lookup service to return proper descriptors
|
||||
var activityRegistryLookup = Substitute.For<IActivityRegistryLookupService>();
|
||||
activityRegistryLookup.FindAsync(Arg.Any<string>(), Arg.Any<int>()).Returns(callInfo =>
|
||||
{
|
||||
var activityType = callInfo.ArgAt<string>(0);
|
||||
return Task.FromResult<ActivityDescriptor?>(new ActivityDescriptor
|
||||
{
|
||||
TypeName = activityType,
|
||||
Kind = ActivityKind.Action,
|
||||
Category = "Test",
|
||||
Description = "Test activity for unit testing",
|
||||
Version = 1
|
||||
});
|
||||
});
|
||||
|
||||
services.AddSingleton<IActivityRegistryLookupService>(_ => activityRegistryLookup);
|
||||
|
||||
services.AddSingleton<IIdentityGraphService>(_ => Substitute.For<IIdentityGraphService>());
|
||||
services.AddSingleton<IWorkflowGraphBuilder>(_ => Substitute.For<IWorkflowGraphBuilder>());
|
||||
|
||||
services.AddSingleton<IHasher>(_ => Substitute.For<IHasher>());
|
||||
services.AddSingleton<ICommitStateHandler>(_ => Substitute.For<ICommitStateHandler>());
|
||||
services.AddSingleton<IActivitySchedulerFactory>(_ => Substitute.For<IActivitySchedulerFactory>());
|
||||
|
||||
serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
// Create a minimal workflow
|
||||
activity.Id ??= $"test-activity-{Guid.NewGuid()}";
|
||||
|
||||
var workflow = new Workflow
|
||||
{
|
||||
Root = activity
|
||||
};
|
||||
|
||||
// Create a simple workflow graph manually instead of using the builder
|
||||
var rootNode = new ActivityNode(activity, "Root");
|
||||
var nodes = new List<ActivityNode> { rootNode };
|
||||
var workflowGraph = new WorkflowGraph(workflow, rootNode, nodes);
|
||||
|
||||
// Call the configure services action if provided.
|
||||
configureServices?.Invoke(services);
|
||||
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
var activityRegistry = serviceProvider.GetRequiredService<IActivityRegistry>();
|
||||
var workflowGraphBuilder = serviceProvider.GetRequiredService<IWorkflowGraphBuilder>();
|
||||
await activityRegistry.RegisterAsync(activity.GetType());
|
||||
var workflow = Workflow.FromActivity(activity);
|
||||
var workflowGraph = await workflowGraphBuilder.BuildAsync(workflow);
|
||||
|
||||
// Create workflow execution context using the static factory method
|
||||
var workflowExecutionContext = WorkflowExecutionContext.CreateAsync(
|
||||
serviceProvider,
|
||||
workflowGraph,
|
||||
$"test-instance-{Guid.NewGuid()}",
|
||||
var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync(
|
||||
serviceProvider,
|
||||
workflowGraph,
|
||||
$"test-instance-{Guid.NewGuid()}",
|
||||
CancellationToken.None
|
||||
).GetAwaiter().GetResult();
|
||||
|
||||
);
|
||||
|
||||
// Create ActivityExecutionContext for the actual activity we want to test
|
||||
var activityExecutionContext = workflowExecutionContext.CreateActivityExecutionContextAsync(activity)
|
||||
.GetAwaiter().GetResult();
|
||||
|
||||
return activityExecutionContext;
|
||||
return await workflowExecutionContext.CreateActivityExecutionContextAsync(activity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -133,7 +112,7 @@ public static class ActivityTestHelper
|
|||
.Where(p => p.PropertyType.IsGenericType &&
|
||||
p.PropertyType.BaseType == typeof(Variable))
|
||||
.ToList();
|
||||
|
||||
|
||||
foreach (var variable in variableProperties.Select(property => (Variable)property.GetValue(activity)!))
|
||||
{
|
||||
variable.Set(context.ExpressionExecutionContext, variable.Value);
|
||||
|
|
@ -141,35 +120,4 @@ public static class ActivityTestHelper
|
|||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets up input values in memory blocks so that context.Get() can find them during activity execution.
|
||||
/// This mimics what the workflow engine does when evaluating inputs.
|
||||
/// </summary>
|
||||
private static async Task SetupInputValuesInMemoryAsync(IActivity activity, ActivityExecutionContext context, IServiceProvider serviceProvider)
|
||||
{
|
||||
var activityType = activity.GetType();
|
||||
var inputProperties = activityType.GetProperties()
|
||||
.Where(p => p.PropertyType.IsGenericType &&
|
||||
p.PropertyType.GetGenericTypeDefinition() == typeof(Input<>))
|
||||
.ToList();
|
||||
|
||||
foreach (var input in inputProperties.Select(property => property.GetValue(activity) as Input))
|
||||
{
|
||||
if (input?.Expression == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the memory block reference for this input
|
||||
var memoryBlockReference = input.MemoryBlockReference();
|
||||
|
||||
// Evaluate the input using the expression evaluator
|
||||
var expressionEvaluator = serviceProvider.GetService<IExpressionEvaluator>();
|
||||
var evaluatedValue = await expressionEvaluator!.EvaluateAsync(input, context.ExpressionExecutionContext);
|
||||
|
||||
// Set the value in the memory block
|
||||
memoryBlockReference.Set(context, evaluatedValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,455 +1,49 @@
|
|||
using Elsa.Activities.UnitTests.Helpers;
|
||||
using Elsa.Workflows;
|
||||
using Elsa.Expressions.Models;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Elsa.Activities.UnitTests.Primitives;
|
||||
|
||||
public class SetVariableTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Should_Set_Variable_Integer()
|
||||
public async Task Should_Set_Variable_Integer()
|
||||
{
|
||||
// Arrange
|
||||
const int expected = 42; // The answer to life, the universe and everything.
|
||||
var variable = new Variable<int>("myVar", 0, "myVar");
|
||||
var setVariable = new SetVariable<int>(variable, new Input<int>(42, "inputId"));
|
||||
|
||||
var setVariable = new SetVariable<int>(variable, new Input<int>(expected));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal(42, result);
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Set_Variable_String()
|
||||
public async Task Should_Not_Throw_When_Variable_Is_Null()
|
||||
{
|
||||
// Arrange
|
||||
var variable = new Variable<string>("myStringVar", "", "myStringVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>("Hello World", "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal("Hello World", result);
|
||||
var setVariable = new SetVariable<string>(null!, new Input<string>("test value"));
|
||||
|
||||
// Act & Assert
|
||||
var exception = await Record.ExceptionAsync(async () => await ActivityTestHelper.ExecuteActivityAsync(setVariable));
|
||||
|
||||
Assert.NotNull(exception);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Overwrite_Existing_Variable()
|
||||
public async Task Should_Set_Variable_To_Null_Value()
|
||||
{
|
||||
// Arrange
|
||||
var variable = new Variable<int>("existingVar", 100, "inputId");
|
||||
var setVariable = new SetVariable<int>(variable, new Input<int>(200, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert - verify the original value was overwritten
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal(200, result);
|
||||
}
|
||||
var variable = new Variable<string?>("myVar", "initial value", "myVar");
|
||||
var setVariable = new SetVariable<string?>(variable, new Input<string?>(default(string)));
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Assign_Null_Value()
|
||||
{
|
||||
// Arrange
|
||||
var variable = new Variable<string>("nullVar", "initial", "nullVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>((string)null!, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Set_Variable_With_Special_Characters()
|
||||
{
|
||||
// Arrange
|
||||
var variable = new Variable<string>("special_var-123", "", "specialVar");
|
||||
const string specialValue = "Special chars: !@#$%^&*()_+-=[]{}|;':\",./<>?";
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>(specialValue, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal(specialValue, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Serialize_Complex_Object()
|
||||
{
|
||||
// Arrange
|
||||
var complexObject = new ComplexTestObject
|
||||
{
|
||||
Id = 123,
|
||||
Name = "Test Object",
|
||||
Properties = new Dictionary<string, object>
|
||||
{
|
||||
{"prop1", "value1"},
|
||||
{"prop2", 456},
|
||||
{"prop3", true}
|
||||
},
|
||||
Items = ["item1", "item2", "item3"]
|
||||
};
|
||||
|
||||
var variable = new Variable<ComplexTestObject>("complexVar", null!, "complexVar");
|
||||
var setVariable = new SetVariable<ComplexTestObject>(variable, new Input<ComplexTestObject>(complexObject, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(complexObject.Id, result.Id);
|
||||
Assert.Equal(complexObject.Name, result.Name);
|
||||
Assert.Equal(complexObject.Properties.Count, result.Properties.Count);
|
||||
Assert.Equal(complexObject.Items.Length, result.Items.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Assign_Large_Payload()
|
||||
{
|
||||
// Arrange
|
||||
var largePayload = new string('A', 1024 * 1024);
|
||||
var variable = new Variable<string>("largeVar", "", "largeVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>(largePayload, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal(largePayload.Length, result.Length);
|
||||
Assert.Equal(largePayload, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Case_Sensitivity_On_Variable_Names()
|
||||
{
|
||||
// Arrange
|
||||
var variable1 = new Variable<string>("CaseSensitive", "value1", "var1");
|
||||
var variable2 = new Variable<string>("casesensitive", "value2", "var2");
|
||||
|
||||
var setVariable1 = new SetVariable<string>(variable1, new Input<string>("updated1", "input1"));
|
||||
var setVariable2 = new SetVariable<string>(variable2, new Input<string>("updated2", "input2"));
|
||||
|
||||
// Act
|
||||
var context1 = await ActivityTestHelper.ExecuteActivityAsync(setVariable1);
|
||||
var context2 = await ActivityTestHelper.ExecuteActivityAsync(setVariable2);
|
||||
|
||||
// Assert - Variables with different casing should be treated as separate
|
||||
var result1 = variable1.Get(context1);
|
||||
var result2 = variable2.Get(context2);
|
||||
|
||||
Assert.Equal("updated1", result1);
|
||||
Assert.Equal("updated2", result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Reassign_Variable_Multiple_Times()
|
||||
{
|
||||
// Arrange
|
||||
var variable = new Variable<int>("multiVar", 0, "multiVar");
|
||||
|
||||
// Act - Multiple assignments
|
||||
var setVariable1 = new SetVariable<int>(variable, new Input<int>(10, "input1"));
|
||||
var context1 = await ActivityTestHelper.ExecuteActivityAsync(setVariable1);
|
||||
var result1 = variable.Get(context1);
|
||||
|
||||
var setVariable2 = new SetVariable<int>(variable, new Input<int>(20, "input2"));
|
||||
var context2 = await ActivityTestHelper.ExecuteActivityAsync(setVariable2);
|
||||
var result2 = variable.Get(context2);
|
||||
|
||||
var setVariable3 = new SetVariable<int>(variable, new Input<int>(30, "input3"));
|
||||
var context3 = await ActivityTestHelper.ExecuteActivityAsync(setVariable3);
|
||||
var result3 = variable.Get(context3);
|
||||
|
||||
// Assert - Each execution should update the variable
|
||||
Assert.Equal(10, result1);
|
||||
Assert.Equal(20, result2);
|
||||
Assert.Equal(30, result3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Boolean_Values()
|
||||
{
|
||||
// Arrange
|
||||
var variable = new Variable<bool>("boolVar", false, "boolVar");
|
||||
var setVariable = new SetVariable<bool>(variable, new Input<bool>(true, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_DateTime_Values()
|
||||
{
|
||||
// Arrange
|
||||
var testDate = new DateTime(2025, 10, 8, 14, 30, 0);
|
||||
var variable = new Variable<DateTime>("dateVar", DateTime.MinValue, "dateVar");
|
||||
var setVariable = new SetVariable<DateTime>(variable, new Input<DateTime>(testDate, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal(testDate, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Decimal_Values()
|
||||
{
|
||||
// Arrange
|
||||
var decimalValue = 123.456789m;
|
||||
var variable = new Variable<decimal>("decimalVar", 0m, "decimalVar");
|
||||
var setVariable = new SetVariable<decimal>(variable, new Input<decimal>(decimalValue, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal(decimalValue, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Array_Values()
|
||||
{
|
||||
// Arrange
|
||||
var arrayValue = new[] { "item1", "item2", "item3" };
|
||||
var variable = new Variable<string[]>("arrayVar", null!, "arrayVar");
|
||||
var setVariable = new SetVariable<string[]>(variable, new Input<string[]>(arrayValue, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(arrayValue.Length, result.Length);
|
||||
Assert.Equal(arrayValue, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Dictionary_Values()
|
||||
{
|
||||
// Arrange
|
||||
var dictionaryValue = new Dictionary<string, object>
|
||||
{
|
||||
{"key1", "value1"},
|
||||
{"key2", 123},
|
||||
{"key3", true}
|
||||
};
|
||||
var variable = new Variable<Dictionary<string, object>>("dictVar", null!, "dictVar");
|
||||
var setVariable = new SetVariable<Dictionary<string, object>>(variable, new Input<Dictionary<string, object>>(dictionaryValue, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(dictionaryValue.Count, result.Count);
|
||||
Assert.Equal(dictionaryValue["key1"], result["key1"]);
|
||||
Assert.Equal(dictionaryValue["key2"], result["key2"]);
|
||||
Assert.Equal(dictionaryValue["key3"], result["key3"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Evaluate_Variable_Name_Via_Expression()
|
||||
{
|
||||
// Arrange - Use an expression for the variable name
|
||||
var dynamicVariableName = "dynamic_var_" + DateTime.Now.Ticks;
|
||||
var variable = new Variable<string>(dynamicVariableName, "", "dynamicVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>("Dynamic Value", "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal("Dynamic Value", result);
|
||||
Assert.Equal(dynamicVariableName, variable.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Evaluate_Value_Via_Expression()
|
||||
{
|
||||
// Arrange - Use a computed expression for the value
|
||||
var computedValue = $"Computed at {DateTime.Now:yyyy-MM-dd HH:mm:ss}";
|
||||
var variable = new Variable<string>("expressionVar", "", "expressionVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>(computedValue, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal(computedValue, result);
|
||||
Assert.Contains("Computed at", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Expression_That_Returns_Null()
|
||||
{
|
||||
// Arrange - Expression that evaluates to null
|
||||
var variable = new Variable<string>("nullExpressionVar", "initial", "nullExpressionVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>((string)null!, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Undefined_Expression_Result()
|
||||
{
|
||||
// Arrange - Test handling of undefined/default values
|
||||
var variable = new Variable<int?>("undefinedVar", null, "undefinedVar");
|
||||
var setVariable = new SetVariable<int?>(variable, new Input<int?>((int?)null, "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Zero_Length_Variable_Name()
|
||||
{
|
||||
// Arrange
|
||||
var variable = new Variable<string>(string.Empty, "test", "emptyNameVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>("value", "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal("value", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Unicode_Variable_Names()
|
||||
{
|
||||
// Arrange - Test with Unicode characters in variable names
|
||||
var unicodeVariableName = "变量名_متغیر_переменная_🚀";
|
||||
var variable = new Variable<string>(unicodeVariableName, "", "unicodeVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>("Unicode Value", "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal("Unicode Value", result);
|
||||
Assert.Equal(unicodeVariableName, variable.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Extremely_Long_Variable_Name()
|
||||
{
|
||||
// Arrange - Test with very long variable name
|
||||
var longVariableName = new string('a', 1000);
|
||||
var variable = new Variable<string>(longVariableName, "", "longNameVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>("Long Name Value", "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal("Long Name Value", result);
|
||||
Assert.Equal(1000, variable.Name.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Numeric_Variable_Names()
|
||||
{
|
||||
// Arrange - Test with numeric variable names
|
||||
var numericVariableName = "12345";
|
||||
var variable = new Variable<string>(numericVariableName, "", "numericVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>("Numeric Name Value", "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal("Numeric Name Value", result);
|
||||
Assert.Equal(numericVariableName, variable.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Variable_Name_With_Spaces()
|
||||
{
|
||||
// Arrange - Test with spaces in variable names
|
||||
var spacedVariableName = "variable with spaces";
|
||||
var variable = new Variable<string>(spacedVariableName, "", "spacedVar");
|
||||
var setVariable = new SetVariable<string>(variable, new Input<string>("Spaced Name Value", "inputId"));
|
||||
|
||||
// Act
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
|
||||
// Assert
|
||||
var result = variable.Get(context);
|
||||
Assert.Equal("Spaced Name Value", result);
|
||||
Assert.Equal(spacedVariableName, variable.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Should_Handle_Concurrent_Variable_Updates()
|
||||
{
|
||||
// Arrange - Test thread safety with concurrent updates
|
||||
var variable = new Variable<int>("concurrentVar", 0, "concurrentVar");
|
||||
var tasks = new List<Task<int>>();
|
||||
|
||||
// Act - Create multiple concurrent assignments
|
||||
for (int i = 1; i <= 10; i++)
|
||||
{
|
||||
var value = i;
|
||||
tasks.Add(Task.Run(async () =>
|
||||
{
|
||||
var setVariable = new SetVariable<int>(variable, new Input<int>(value, $"input{value}"));
|
||||
var context = await ActivityTestHelper.ExecuteActivityAsync(setVariable);
|
||||
return variable.Get(context);
|
||||
}));
|
||||
}
|
||||
|
||||
var results = await Task.WhenAll(tasks);
|
||||
|
||||
// Assert - All operations should complete successfully
|
||||
Assert.Equal(10, results.Length);
|
||||
Assert.All(results, result => Assert.True(result >= 1 && result <= 10));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Complex test object for serialization testing
|
||||
/// </summary>
|
||||
public class ComplexTestObject
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public Dictionary<string, object> Properties { get; set; } = new();
|
||||
public string[] Items { get; set; } = [];
|
||||
}
|
||||
Loading…
Reference in a new issue