diff --git a/Streamistry.Core/Fluent/BasePipeBuilder.cs b/Streamistry.Core/Fluent/BasePipeBuilder.cs index 4b57333..762cf71 100644 --- a/Streamistry.Core/Fluent/BasePipeBuilder.cs +++ b/Streamistry.Core/Fluent/BasePipeBuilder.cs @@ -39,6 +39,8 @@ public PluckerBuilder Pluck(Expression new(this, expr); public CasterBuilder Cast() => new(this); + public ConstantBuilder Constant(TNext value) + => new(this, value); public SplitterBuilder Split(Func? function) => new(this, function); diff --git a/Streamistry.Core/Fluent/ConstantBuilder.cs b/Streamistry.Core/Fluent/ConstantBuilder.cs new file mode 100644 index 0000000..e1a297a --- /dev/null +++ b/Streamistry.Core/Fluent/ConstantBuilder.cs @@ -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 : PipeElementBuilder +{ + private TOutput Value { get; set; } + + public ConstantBuilder(IPipeBuilder upstream, TOutput value) + : base(upstream) + => (Value) = (value); + + public override IChainablePort OnBuildPipeElement() + => new Constant( + Upstream.BuildPipeElement() + , Value + ); +} diff --git a/Streamistry.Core/Pipes/Mappers/Constant.cs b/Streamistry.Core/Pipes/Mappers/Constant.cs new file mode 100644 index 0000000..44f90ef --- /dev/null +++ b/Streamistry.Core/Pipes/Mappers/Constant.cs @@ -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 : Mapper +{ + protected Constant(TOutput value, IChainablePort? upstream) + : base((x) => value, upstream) + { } + + public Constant(IChainablePort upstream, TOutput value) + : this(value, upstream) + { } + + public Constant(TOutput value) + : this(value, null) + { } +} diff --git a/Streamistry.Testing/Fluent/PipelineBuilderTests.cs b/Streamistry.Testing/Fluent/PipelineBuilderTests.cs index edf15f0..f11b970 100644 --- a/Streamistry.Testing/Fluent/PipelineBuilderTests.cs +++ b/Streamistry.Testing/Fluent/PipelineBuilderTests.cs @@ -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() { diff --git a/Streamistry.Testing/Pipes/Mappers/ConstantTests.cs b/Streamistry.Testing/Pipes/Mappers/ConstantTests.cs new file mode 100644 index 0000000..3bddd94 --- /dev/null +++ b/Streamistry.Testing/Pipes/Mappers/ConstantTests.cs @@ -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(1); + var output = caster.EmitAndGetOutput("foo"); + + Assert.That(output, Is.EqualTo(1)); + } +}