mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-07-31 15:58:27 -04:00
101 lines
2.5 KiB
C#
101 lines
2.5 KiB
C#
using Facepunch.ActionGraphs;
|
|
using Sandbox.ActionGraphs;
|
|
using Sandbox.Internal;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using NodeLibrary = Facepunch.ActionGraphs.NodeLibrary;
|
|
|
|
namespace ActionGraphTests
|
|
{
|
|
[AssetType( Name = "Example", Extension = "example" )]
|
|
public class ExampleResource : GameResource
|
|
{
|
|
public Func<Task> DoSomething { get; set; }
|
|
}
|
|
|
|
[TestClass]
|
|
public class SerializationTest
|
|
{
|
|
private NodeLibrary _oldNodeLibrary;
|
|
|
|
public TypeLibrary TypeLibrary;
|
|
public NodeLibrary Nodes => Game.NodeLibrary;
|
|
|
|
[TestInitialize]
|
|
public void Initialize()
|
|
{
|
|
TypeLibrary = new TypeLibrary();
|
|
TypeLibrary.AddAssembly( typeof( LogNodes ).Assembly, false );
|
|
|
|
_oldNodeLibrary = Nodes;
|
|
|
|
Game.NodeLibrary = new NodeLibrary( new TypeLoader( () => TypeLibrary ), new GraphLoader() );
|
|
|
|
var result = Nodes.AddAssembly( typeof( LogNodes ).Assembly );
|
|
|
|
foreach ( var (method, e) in result.Errors )
|
|
{
|
|
Debug.WriteLine( $"{method.ToSimpleString()}: {e}" );
|
|
}
|
|
|
|
Assert.IsFalse( result.AlreadyAdded );
|
|
Assert.AreEqual( 0, result.Errors.Count );
|
|
}
|
|
|
|
[TestCleanup]
|
|
public void Cleanup()
|
|
{
|
|
Game.NodeLibrary = _oldNodeLibrary;
|
|
}
|
|
|
|
private Func<Task> CreateHelloWorldGraph()
|
|
{
|
|
var graph = ActionGraph.CreateDelegate<Func<Task>>( Nodes );
|
|
var start = graph.Graph.InputNode!;
|
|
var log = graph.Graph.AddNode( LogNodes.Info );
|
|
|
|
log.Inputs.Signal.SetLink( start.Outputs.Signal );
|
|
log.Inputs["format"].Value = "Hello, {0}!";
|
|
log.Inputs["args"].Value = new object[] { "World" };
|
|
|
|
return graph;
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task SerializeSimple()
|
|
{
|
|
var resource = new ExampleResource { DoSomething = CreateHelloWorldGraph() };
|
|
var json = Json.SerializeAsObject( resource ).ToJsonString( Json.options );
|
|
|
|
Console.WriteLine( json );
|
|
|
|
resource.DoSomething = null;
|
|
|
|
Json.DeserializeToObject( resource, json );
|
|
|
|
await resource.DoSomething!();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Control case to complement <see cref="ScriptDeserialization_Disabled"/>.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public void ScriptDeserialization_Enabled()
|
|
{
|
|
Json.Deserialize<Action>( "{}" );
|
|
}
|
|
|
|
/// <summary>
|
|
/// ActionGraph deserialization is rejected when inside a <see cref="Json.DisableScriptDeserialization"/> block.
|
|
/// </summary>
|
|
[TestMethod]
|
|
public void ScriptDeserialization_Disabled()
|
|
{
|
|
using ( Json.DisableScriptDeserialization() )
|
|
{
|
|
Assert.ThrowsException<Exception>( () => Json.Deserialize<Action>( "{}" ) );
|
|
}
|
|
}
|
|
}
|
|
}
|