Skip to content

Commit

Permalink
fix: interval builder as public and Contained accepting a Func<> (#247)
Browse files Browse the repository at this point in the history
* fix: re-enable building intervals from string representation
* fix: ContainedIn should accept a Func<>
  • Loading branch information
Seddryck committed Jan 6, 2024
1 parent 4552b41 commit ab062f7
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Expressif.Testing/Functions/ExpressionFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public void Instantiate_RoundExpressionParameter_Valid()
public void Instantiate_RoundMultipleExpressionParameter_Valid()
{
var context = new Context();
var subFunction1 = new InputExpressionParameter(new InputExpression(new VariableParameter("myVar1"), new[] { new Function("numeric-to-decrement", Array.Empty<IParameter>()) }));
var subFunction2 = new InputExpressionParameter(new InputExpression(new VariableParameter("myVar2"), new[] { new Function("numeric-to-increment", Array.Empty<IParameter>()) }));
var subFunction1 = new InputExpressionParameter(new InputExpression(new VariableParameter("myVar1"), new[] { new Function("numeric-to-decrement", []) }));
var subFunction2 = new InputExpressionParameter(new InputExpression(new VariableParameter("myVar2"), new[] { new Function("numeric-to-increment", []) }));
var subFunction3 = new InputExpressionParameter(new InputExpression(new VariableParameter("myVar1"), new[] { new Function("numeric-to-add", [subFunction1]), new Function("numeric-to-multiply", [subFunction2]) }));
var function = new ExpressionFactory().Instantiate(typeof(Round), new[] { subFunction3 }, context);
context.Variables.Add<int>("myVar1", 4);
Expand Down
6 changes: 3 additions & 3 deletions Expressif.Testing/Predicates/Temporal/IntervalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class IntervalTest
[TestCase("(null)", false)]
public void ContainedIn_DateTime_Expected(object? value, bool expected)
=> Assert.That(new ContainedIn(
new Interval<System.DateTime>(
new System.DateTime(2022, 11, 20)
, new System.DateTime(2022, 11, 24)
() => new Interval<DateTime>(
new DateTime(2022, 11, 20)
, new DateTime(2022, 11, 24)
, IntervalType.Open
, IntervalType.Closed)
).Evaluate(value), Is.EqualTo(expected));
Expand Down
20 changes: 20 additions & 0 deletions Expressif.Testing/Values/IntervalBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,24 @@ public void Create_TwoSameValidType_Valid(string lowerBound, string upperBound,
[TestCase("2020-12-16", "4.12355")]
public void Create_MixedValidType_Invalid(string lowerBound, string upperBound)
=> Assert.That(() => new IntervalBuilder().Create(']', lowerBound, upperBound, '['), Throws.InvalidOperationException);

[Test]
[TestCase("[2022-10-12 12:16:10;2023-10-11 00:00:00]")]
[TestCase("[2022-10-12;2023-10-11]")]
public void CreateDateTime_FromString_valid(string value)
=> Assert.Multiple(() =>
{
Assert.That(() => new IntervalBuilder().Create(value), Is.Not.Null);
Assert.That(() => new IntervalBuilder().Create(value), Is.TypeOf<Interval<DateTime>>());
});

[Test]
[TestCase("[5;15]")]
[TestCase("[-5.05;10.256]")]
public void CreateNumeric_FromString_valid(string value)
=> Assert.Multiple(() =>
{
Assert.That(() => new IntervalBuilder().Create(value), Is.Not.Null);
Assert.That(() => new IntervalBuilder().Create(value), Is.TypeOf<Interval<decimal>>());
});
}
6 changes: 3 additions & 3 deletions Expressif/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ExpressionBuilder Chain(Type type, params object?[] parameters)
return this;
}

private IParameter[] Parametrize(object?[] parameters)
protected virtual IParameter[] Parametrize(object?[] parameters)
{
var typedParameters = new List<IParameter>();
foreach (var parameter in parameters)
Expand All @@ -57,7 +57,7 @@ private IParameter[] Parametrize(object?[] parameters)
_ => new LiteralParameter(parameter?.ToString() ?? new Null().Keyword)
});
}
return typedParameters.ToArray();
return [.. typedParameters];
}

public ExpressionBuilder Chain(ExpressionBuilder builder)
Expand Down Expand Up @@ -99,6 +99,6 @@ public string Serialize()
if (Pile.Count == 0)
throw new InvalidOperationException();

return Serializer.Serialize(Pile.ToArray());
return Serializer.Serialize([.. Pile]);
}
}
2 changes: 1 addition & 1 deletion Expressif/Functions/BaseExpressionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected T Instantiate<T>(Type type, IParameter[] parameters, IContext context)
return (T)ctor.Invoke(typedFunctionParameters.ToArray());
}

protected internal ConstructorInfo GetMatchingConstructor(Type type, int paramCount)
protected internal virtual ConstructorInfo GetMatchingConstructor(Type type, int paramCount)
=> type.GetConstructors().SingleOrDefault(x => x.GetParameters().Length == paramCount)
?? throw new MissingOrUnexpectedParametersFunctionException(type.Name, paramCount);

Expand Down
6 changes: 3 additions & 3 deletions Expressif/Predicates/Temporal/Interval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace Expressif.Predicates.Temporal;
/// </summary>
public class ContainedIn : BaseDateTimePredicate
{
public Interval<DateTime> Interval { get; }
public Func<Interval<DateTime>> Interval { get; }

/// <param name="interval">A temporal interval to compare to the argument.</param>
public ContainedIn(Interval<DateTime> interval)
public ContainedIn(Func<Interval<DateTime>> interval)
=> Interval = interval;

protected override bool EvaluateDateTime(DateTime dt)
=> Interval.Contains(dt);
=> Interval.Invoke().Contains(dt);
}
17 changes: 14 additions & 3 deletions Expressif/Values/IntervalBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Expressif.Predicates.Text;
using Expressif.Parsers;
using Expressif.Predicates.Text;
using Expressif.Values;
using Expressif.Values.Casters;
using Sprache;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,9 +12,9 @@

namespace Expressif.Values;

internal class IntervalBuilder
public class IntervalBuilder
{
public IInterval Create(char lowerBoundChar, string lowerBound, string upperBound, char upperBoundChar)
public virtual IInterval Create(char lowerBoundChar, string lowerBound, string upperBound, char upperBoundChar)
{
var lowerBoundType = lowerBoundChar == ']' ? IntervalType.Open : IntervalType.Closed;
var upperBoundType = upperBoundChar == '[' ? IntervalType.Open : IntervalType.Closed;
Expand All @@ -32,4 +34,13 @@ public IInterval Create(char lowerBoundChar, string lowerBound, string upperBoun
}
throw new InvalidOperationException();
}

public virtual IInterval Create(string value)
{
var interval = Interval.Parser.Parse(value);
return Create(interval);
}

public virtual IInterval Create(Interval interval)
=> Create(interval.LowerBoundType, interval.LowerBound, interval.UpperBound, interval.UpperBoundType);
}

0 comments on commit ab062f7

Please sign in to comment.