Skip to content

Commit bc5d681

Browse files
committed
Expression.equalTo for MetricSpaces
1 parent 50fe0ea commit bc5d681

File tree

6 files changed

+84
-11
lines changed

6 files changed

+84
-11
lines changed
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
package net.itarray.automotion.internal.properties;
22

33
import net.itarray.automotion.internal.geometry.Direction;
4-
import net.itarray.automotion.internal.properties.Context;
4+
import net.itarray.automotion.internal.geometry.Scalar;
55
import net.itarray.automotion.validation.properties.Expression;
66

77
import java.util.function.BiFunction;
88

9+
import static net.itarray.automotion.internal.geometry.Scalar.scalar;
10+
911
public class BinaryExpression<L, R, T> implements Expression<T> {
1012

1113
private final Expression<L> left;
1214
private final Expression<R> right;
13-
private final BiFunction<L, R, T> operation;
15+
private ContextBiFunction<L, R, T> contextBiFunction;
1416
private String descriptionFormat;
1517

16-
public BinaryExpression(Expression<L> left, Expression<R> right, BiFunction<L, R, T> operation, String descriptionFormat) {
18+
public BinaryExpression(Expression<L> left, Expression<R> right, ContextBiFunction<L, R, T> contextBiFunction, String descriptionFormat) {
1719
this.left = left;
1820
this.right = right;
19-
this.operation = operation;
2021
this.descriptionFormat = descriptionFormat;
22+
this.contextBiFunction = contextBiFunction;
23+
}
24+
25+
public BinaryExpression(Expression<L> left, Expression<R> right, BiFunction<L, R, T> operation, String descriptionFormat) {
26+
this(left, right, (l, r, context) -> operation.apply(l, r), descriptionFormat);
2127
}
2228

2329
@Override
2430
public T evaluateIn(Context context, Direction direction) {
25-
return operation.apply(left.evaluateIn(context, direction), right.evaluateIn(context, direction));
31+
return contextBiFunction.apply(left.evaluateIn(context, direction), right.evaluateIn(context, direction), context);
2632
}
2733

2834
@Override
2935
public String getDescription(Context context, Direction direction) {
36+
String toleranceDescription = context.getTolerance().equals(scalar(0)) ? "" : String.format(" with tolerance %s", context.getTolerance());
3037
return String.format(
3138
descriptionFormat,
3239
left.getDescription(context, direction),
33-
right.getDescription(context, direction));
40+
right.getDescription(context, direction)) + toleranceDescription;
3441
}
3542
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.itarray.automotion.internal.properties;
2+
3+
@FunctionalInterface
4+
public interface ContextBiFunction<L, R, T> {
5+
T apply(L l, R r, Context context);
6+
}

src/main/java/net/itarray/automotion/validation/properties/Expression.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package net.itarray.automotion.validation.properties;
22

33
import net.itarray.automotion.internal.geometry.Direction;
4+
import net.itarray.automotion.internal.geometry.MetricSpace;
45
import net.itarray.automotion.internal.geometry.Scalar;
6+
import net.itarray.automotion.internal.properties.BinaryExpression;
57
import net.itarray.automotion.internal.properties.Context;
68
import net.itarray.automotion.internal.properties.PagePercentage;
79
import net.itarray.automotion.internal.properties.PagePercentageOrPixels;
810
import net.itarray.automotion.internal.properties.PercentReference;
11+
import net.itarray.automotion.internal.properties.PixelConstant;
912

1013
import static net.itarray.automotion.internal.geometry.Scalar.scalar;
1114
import static net.itarray.automotion.internal.properties.PercentReference.PAGE;
@@ -22,7 +25,6 @@ static PagePercentageOrPixels percentOrPixels(int constant) {
2225

2326
static Expression<Scalar> percent(int percentage, PercentReference reference) {
2427
return percent(scalar(percentage), reference);
25-
2628
}
2729

2830
static Expression<Scalar> percent(Scalar percentage, PercentReference reference) {
@@ -33,6 +35,14 @@ static Expression<Scalar> percent(Scalar percentage, PercentReference reference)
3335
}
3436
}
3537

38+
static <V extends MetricSpace<V>> Expression<Boolean> equalTo(Expression<V> left, Expression<V> right) {
39+
return new BinaryExpression<>(
40+
left,
41+
right,
42+
(scalar, other, context) -> scalar.minus(other).norm().isLessOrEqualTo(context.getTolerance()),
43+
"%s to be equal to %s");
44+
}
45+
3646
T evaluateIn(Context context, Direction direction);
3747

3848
String getDescription(Context context, Direction direction);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package net.itarray.automotion.tests.properties;
2+
3+
import net.itarray.automotion.internal.geometry.Direction;
4+
import net.itarray.automotion.internal.geometry.Scalar;
5+
import net.itarray.automotion.internal.properties.BinaryExpression;
6+
import net.itarray.automotion.internal.properties.PixelConstant;
7+
import net.itarray.automotion.validation.properties.Expression;
8+
import org.junit.Before;
9+
import org.junit.Ignore;
10+
import org.junit.Test;
11+
12+
import static net.itarray.automotion.internal.geometry.Scalar.scalar;
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
public class EqualToTest {
16+
17+
private Expression<Boolean> expression;
18+
19+
@Before
20+
public void setUp() {
21+
expression = Expression.equalTo(
22+
new PixelConstant(scalar(17)),
23+
new PixelConstant(scalar(16)));
24+
}
25+
26+
@Test
27+
public void evaluatesToTheValueOfTheOperationAppliedToTheEvaluatedLeftAndRightExpressions() {
28+
Boolean result = expression.evaluateIn(new TestContext(), Direction.RIGHT);
29+
assertThat(result).isFalse();
30+
}
31+
32+
@Test
33+
public void evaluatesToTheValueOfTheOperationAppliedToTheEvaluatedLeftAndRightExpressionsTakingTheToleranceOfTheContextIntoAccount() {
34+
Boolean result = expression.evaluateIn(new TestContext().withTolerance(scalar(1)), Direction.RIGHT);
35+
assertThat(result).isTrue();
36+
}
37+
38+
@Test
39+
public void describesItself() {
40+
String description = expression.getDescription(new TestContext(), Direction.RIGHT);
41+
assertThat(description).isEqualTo("17px to be equal to 16px");
42+
}
43+
44+
@Test
45+
public void describesItselfWithTolerance() {
46+
String description = expression.getDescription(new TestContext().withTolerance(scalar(1)), Direction.RIGHT);
47+
assertThat(description).isEqualTo("17px to be equal to 16px with tolerance 1");
48+
}
49+
}

src/test/java/net/itarray/automotion/tests/properties/PagePercentageOrPixelsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void evaluatesToItsValueInAnyPixelContext() {
2626

2727
@Test
2828
public void evaluatesToItsValueInAnyPercentageContext() {
29-
TestContext context = new TestContext();
30-
context.setPixels(false);
29+
TestContext context = new TestContext().withPixels(false);
3130
assertThat(condition.evaluateIn(context, Direction.RIGHT)).isEqualTo(scalar(104));
3231
}
3332

src/test/java/net/itarray/automotion/tests/properties/TestContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ public boolean isPixels() {
3131
return pixels;
3232
}
3333

34-
public void setPixels(boolean pixels) {
34+
public TestContext withPixels(boolean pixels) {
3535
this.pixels = pixels;
36+
return this;
3637
}
3738

3839
@Override
3940
public Scalar getTolerance() {
4041
return tolerance;
4142
}
4243

43-
public void setTolerance(Scalar tolerance) {
44+
public TestContext withTolerance(Scalar tolerance) {
4445
this.tolerance = tolerance;
46+
return this;
4547
}
4648
}

0 commit comments

Comments
 (0)