Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Forge.Tests/Helpers/StatescriptTestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright © Gamesmiths Guild.
#pragma warning disable SA1649, SA1402 // File name should match first type name

using Gamesmiths.Forge.Abilities;
using Gamesmiths.Forge.Core;
using Gamesmiths.Forge.Effects;
using Gamesmiths.Forge.Effects.Components;
using Gamesmiths.Forge.Effects.Duration;
using Gamesmiths.Forge.Effects.Magnitudes;
using Gamesmiths.Forge.Statescript;
using Gamesmiths.Forge.Statescript.Nodes;
using Gamesmiths.Forge.Statescript.Nodes.Action;
Expand Down Expand Up @@ -42,6 +47,79 @@ public static SetVariableNode CreateSetVariableNode(
}
}

/// <summary>
/// Convenience helpers for creating ability-driven graph contexts in resolver tests.
/// </summary>
internal static class ResolverTestContextFactory
{
public static GraphContext CreateAbilityGraphContext(TestEntity entity, float magnitude = 0f)
{
var graph = new Graph();
var captureNode = new CaptureGraphContextNode();

graph.AddNode(captureNode);
graph.AddConnection(new Connection(
graph.EntryNode.OutputPorts[EntryNode.OutputPort],
captureNode.InputPorts[ActionNode.InputPort]));

var behavior = new GraphAbilityBehavior(graph);

AbilityData abilityData = CreateAbilityData("ResolverTest", () => behavior);
AbilityHandle? handle = Grant(entity, abilityData) ?? throw new InvalidOperationException(
"Failed to grant the resolver test ability and create an ability graph context.");

if (!handle.Activate(out _, magnitude: magnitude))
{
throw new InvalidOperationException(
"Failed to activate the resolver test ability while creating an ability graph context." +
$" Magnitude: {magnitude}.");
}

if (captureNode.CapturedGraphContext is null)
{
throw new InvalidOperationException(
"The resolver test ability activated, but no graph context was captured by CaptureGraphContextNode.");
}

return captureNode.CapturedGraphContext;
}

private static AbilityHandle? Grant(TestEntity target, AbilityData data)
{
var grantConfig = new GrantAbilityConfig(
data,
new ScalableInt(1),
AbilityDeactivationPolicy.CancelImmediately,
AbilityDeactivationPolicy.CancelImmediately,
false,
false,
LevelComparison.Higher);

var effectData = new EffectData(
"Grant",
new DurationData(DurationType.Infinite),
effectComponents: [new GrantAbilityEffectComponent([grantConfig])]);

var grantEffect = new Effect(effectData, new EffectOwnership(null, null));
_ = target.EffectsManager.ApplyEffect(grantEffect);

target.Abilities.TryGetAbility(data, out AbilityHandle? handle);

if (handle is null)
{
throw new InvalidOperationException(
"Failed to retrieve the granted ability handle after applying the grant effect.");
}

return handle;
}
Comment thread
lextatic marked this conversation as resolved.

private static AbilityData CreateAbilityData(string name, Func<IAbilityBehavior> behaviorFactory)
{
return new AbilityData(name, behaviorFactory: behaviorFactory);
}
}

internal sealed class TrackingActionNode(string? name = null, List<string>? executionLog = null) : ActionNode
{
private readonly string? _name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

using static Gamesmiths.Forge.Tests.Helpers.NodeBindings;

namespace Gamesmiths.Forge.Tests.Statescript;
namespace Gamesmiths.Forge.Tests.Statescript.Nodes.Action;

public class ActionNodeTests
public class SetVariableNodeTests
{
[Fact]
[Trait("Graph", "SetVariable")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

using static Gamesmiths.Forge.Tests.Helpers.NodeBindings;

namespace Gamesmiths.Forge.Tests.Statescript;
namespace Gamesmiths.Forge.Tests.Statescript.Nodes.Condition;

public class ExpressionResolverTests(TagsAndCuesFixture tagsAndCuesFixture) : IClassFixture<TagsAndCuesFixture>
public class ExpressionNodeTests(TagsAndCuesFixture tagsAndCuesFixture) : IClassFixture<TagsAndCuesFixture>
{
private readonly TagsManager _tagsManager = tagsAndCuesFixture.TagsManager;
private readonly CuesManager _cuesManager = tagsAndCuesFixture.CuesManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

using static Gamesmiths.Forge.Tests.Helpers.NodeBindings;

namespace Gamesmiths.Forge.Tests.Statescript;
namespace Gamesmiths.Forge.Tests.Statescript.Nodes.State;

public class StateNodeTests
public class TimerNodeTests
{
[Fact]
[Trait("Graph", "Timer")]
Expand Down
Loading
Loading