Skip to content

Commit

Permalink
Refactor test infrastructure as a readonly struct
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jul 9, 2024
1 parent 0b30c61 commit 22460a4
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 140 deletions.
27 changes: 17 additions & 10 deletions Bonsai.Core.Tests/InspectBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ public void Build_CombinatorTransformError_ThrowsRuntimeException()
[TestMethod]
public void Build_GroupInspectBuilder_ReturnNestedVisualizerElement()
{
ExpressionBuilder target = default;
var workflow = Workflow
ExpressionBuilder target = null;
var workflow = TestWorkflow
.New()
.AppendUnit()
.AppendNested(
input => input.AppendUnit().Do(builder => target = builder).AppendOutput(),
input => input
.AppendUnit()
.Capture(out target)
.AppendOutput(),
workflow => new GroupWorkflowBuilder(workflow))
.ToInspectableGraph();
workflow.Build();
Expand All @@ -101,13 +104,13 @@ public void Build_GroupInspectBuilder_ReturnNestedVisualizerElement()
[TestMethod]
public void Build_PropertyMappedInspectBuilderToWorkflowOutput_ReturnVisualizerElement()
{
ExpressionBuilder target = default;
var workflow = Workflow
ExpressionBuilder target;
var workflow = TestWorkflow
.New()
.AppendValue(0)
.AppendPropertyMapping(nameof(Reactive.Range.Count))
.AppendCombinator(new Reactive.Range())
.Do(builder => target = builder)
.Capture(out target)
.AppendOutput()
.ToInspectableGraph();
workflow.Build();
Expand All @@ -120,11 +123,13 @@ public void Build_PropertyMappedInspectBuilderToWorkflowOutput_ReturnVisualizerE
[TestMethod]
public void Build_SinkInspectBuilder_ReturnSourceVisualizerElement()
{
var workflow = Workflow
var workflow = TestWorkflow
.New()
.AppendValue(1)
.AppendNested(
input => input.AppendValue(string.Empty).AppendOutput(),
input => input
.AppendValue(string.Empty)
.AppendOutput(),
workflow => new Reactive.Sink(workflow))
.AppendOutput()
.ToInspectableGraph();
Expand All @@ -138,11 +143,13 @@ public void Build_SinkInspectBuilder_ReturnSourceVisualizerElement()
[TestMethod]
public void Build_VisualizerInspectBuilder_ReplaceSourceVisualizerElement()
{
var workflow = Workflow
var workflow = TestWorkflow
.New()
.AppendValue(1)
.AppendNested(
input => input.AppendValue(string.Empty).AppendOutput(),
input => input
.AppendValue(string.Empty)
.AppendOutput(),
workflow => new Reactive.Visualizer(workflow))
.AppendOutput()
.ToInspectableGraph();
Expand Down
112 changes: 112 additions & 0 deletions Bonsai.Core.Tests/TestWorkflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using Bonsai.Dag;
using Bonsai.Expressions;

namespace Bonsai.Core.Tests
{
public readonly struct TestWorkflow
{
private TestWorkflow(ExpressionBuilderGraph workflow, Node<ExpressionBuilder, ExpressionBuilderArgument> cursor)
{
Workflow = workflow ?? throw new ArgumentNullException(nameof(workflow));
Cursor = cursor;
}

public ExpressionBuilderGraph Workflow { get; }

public Node<ExpressionBuilder, ExpressionBuilderArgument> Cursor { get; }

public static TestWorkflow New()
{
var workflow = new ExpressionBuilderGraph();
return FromGraph(workflow);
}

public static TestWorkflow FromGraph(ExpressionBuilderGraph workflow)
{
return new TestWorkflow(workflow, null);
}

public TestWorkflow ResetCursor()
{
if (Cursor != null)
return new TestWorkflow(Workflow, null);
else
return this;
}

public TestWorkflow Capture(out ExpressionBuilder builder)
{
builder = Cursor?.Value;
return this;
}

public TestWorkflow Append(ExpressionBuilder builder)
{
var node = Workflow.Add(builder);
if (Cursor != null)
Workflow.AddEdge(Cursor, node, new ExpressionBuilderArgument());
return new TestWorkflow(Workflow, node);
}

public TestWorkflow AppendCombinator<TCombinator>(TCombinator combinator) where TCombinator : new()
{
var combinatorBuilder = new CombinatorBuilder { Combinator = combinator };
return Append(combinatorBuilder);
}

public TestWorkflow AppendValue<TValue>(TValue value)
{
var workflowProperty = new WorkflowProperty<TValue> { Value = value };
return AppendCombinator(workflowProperty);
}

public TestWorkflow AppendUnit()
{
return Append(new UnitBuilder());
}

public TestWorkflow AppendInput(int index = 0)
{
return ResetCursor().Append(new WorkflowInputBuilder { Index = index });
}

public TestWorkflow AppendOutput()
{
return Append(new WorkflowOutputBuilder());
}

public TestWorkflow AppendPropertyMapping(params string[] propertyNames)
{
var mappingBuilder = new PropertyMappingBuilder();
foreach (var name in propertyNames)
{
mappingBuilder.PropertyMappings.Add(new PropertyMapping { Name = name });
}
return Append(mappingBuilder);
}

public TestWorkflow AppendNested<TWorkflowExpressionBuilder>(
Func<TestWorkflow, TestWorkflow> selector,
Func<ExpressionBuilderGraph, TWorkflowExpressionBuilder> constructor)
where TWorkflowExpressionBuilder : ExpressionBuilder, IWorkflowExpressionBuilder
{
var nestedCursor = New();
if (Cursor != null)
nestedCursor = nestedCursor.AppendInput();
nestedCursor = selector(nestedCursor);
var workflowBuilder = constructor(nestedCursor.Workflow);
return Append(workflowBuilder);
}

public ExpressionBuilderGraph ToGraph()
{
return Workflow;
}

public ExpressionBuilderGraph ToInspectableGraph()
{
return Workflow.ToInspectableGraph();
}
}
}
130 changes: 0 additions & 130 deletions Bonsai.Core.Tests/Workflow.cs

This file was deleted.

0 comments on commit 22460a4

Please sign in to comment.