Skip to content

Commit

Permalink
feat: new pipe to return a constant for each input (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Sep 23, 2024
1 parent 65d0bc4 commit 5257797
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Streamistry.Core/Fluent/BasePipeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public PluckerBuilder<TOutput, TNext> Pluck<TNext>(Expression<Func<TOutput, TNex
=> new(this, expr);
public CasterBuilder<TOutput, TNext> Cast<TNext>()
=> new(this);
public ConstantBuilder<TOutput, TNext> Constant<TNext>(TNext value)
=> new(this, value);
public SplitterBuilder<TOutput, TNext> Split<TNext>(Func<TOutput?, TNext[]?>? function)
=> new(this, function);

Expand Down
22 changes: 22 additions & 0 deletions Streamistry.Core/Fluent/ConstantBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Streamistry.Pipes.Mappers;

namespace Streamistry.Fluent;
public class ConstantBuilder<TInput, TOutput> : PipeElementBuilder<TInput, TOutput>
{
private TOutput Value { get; set; }

public ConstantBuilder(IPipeBuilder<TInput> upstream, TOutput value)
: base(upstream)
=> (Value) = (value);

public override IChainablePort<TOutput> OnBuildPipeElement()
=> new Constant<TInput, TOutput>(
Upstream.BuildPipeElement()
, Value
);
}
21 changes: 21 additions & 0 deletions Streamistry.Core/Pipes/Mappers/Constant.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Streamistry.Pipes.Mappers;
public class Constant<TInput, TOutput> : Mapper<TInput, TOutput>
{
protected Constant(TOutput value, IChainablePort<TInput>? upstream)
: base((x) => value, upstream)
{ }

public Constant(IChainablePort<TInput> upstream, TOutput value)
: this(value, upstream)
{ }

public Constant(TOutput value)
: this(value, null)
{ }
}
20 changes: 20 additions & 0 deletions Streamistry.Testing/Fluent/PipelineBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ public void Build_PluckerCheckpoint_Success()
});
}

[Test]
public void Build_ConstantCheckpoint_Success()
{
var pipeline = new PipelineBuilder()
.Source([1, 2, 3])
.Constant(0).Checkpoint(out var constant)
.Build();

Assert.That(pipeline, Is.Not.Null);
Assert.That(constant, Is.Not.Null);

var output = constant.GetOutputs(pipeline.Start);
Assert.Multiple(() =>
{
Assert.That(output[0], Is.EqualTo(0));
Assert.That(output[1], Is.EqualTo(0));
Assert.That(output[2], Is.EqualTo(0));
});
}

[Test]
public void Build_SplitterCheckpoint_Success()
{
Expand Down
22 changes: 22 additions & 0 deletions Streamistry.Testing/Pipes/Mappers/ConstantTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Streamistry.Pipes.Mappers;
using Streamistry.Testability;

namespace Streamistry.Testing.Pipes.Mappers;
public class ConstantTests
{
[Test]
public void Emit_ConstantInt_Success()
{
var caster = new Constant<string?, int>(1);
var output = caster.EmitAndGetOutput("foo");

Assert.That(output, Is.EqualTo(1));
}
}

0 comments on commit 5257797

Please sign in to comment.