Implement UndefinedPattern

This commit is contained in:
Riccardo Azzolini 2018-10-06 19:14:24 +02:00
parent 957e85b4d0
commit 95560e12b7
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package it.cavallium.warppi.math.rules.dsl.patterns;
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.Undefined;
import it.cavallium.warppi.math.rules.dsl.VisitorPattern;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* Matches and generates <code>Undefined</code>.
*/
public class UndefinedPattern extends VisitorPattern {
@Override
public Optional<Map<String, Function>> visit(Undefined undefined) {
return Optional.of(new HashMap<>());
}
@Override
public Function replace(MathContext mathContext, Map<String, Function> subFunctions) {
return new Undefined(mathContext);
}
}

View File

@ -115,6 +115,19 @@ public class PatternTest {
assertEquals(shouldMatch, pattern.replace(mathContext, subFunctions.get()));
}
@Test
public void undefinedPattern() {
final Pattern pattern = new UndefinedPattern();
final Function shouldNotMatch = new Number(mathContext, 0);
assertFalse(pattern.match(shouldNotMatch).isPresent());
final Function shouldMatch = new Undefined(mathContext);
final Optional<Map<String, Function>> subFunctions = pattern.match(shouldMatch);
assertTrue(subFunctions.isPresent());
assertTrue(pattern.replace(mathContext, subFunctions.get()) instanceof Undefined);
}
@Test
public void otherBinaryPatterns() {
final Number one = new Number(mathContext, 1);