Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lprakashv committed May 25, 2021
1 parent fb8f3dc commit 48eb1d8
Showing 1 changed file with 77 additions and 14 deletions.
91 changes: 77 additions & 14 deletions src/test/java/testpackage/PMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import io.github.lprakashv.patternmatcher4j.exceptions.PMatcherException;
import io.github.lprakashv.patternmatcher4j.match.DestructuredMatch.MField;
import io.github.lprakashv.patternmatcher4j.matcher.PMatcher;
import lombok.Data;

import java.util.Optional;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -15,34 +19,28 @@ public class PMatcherTest {
Person veryOld = new Person("Gary", 101, true);

private PMatcher<Object, String> createPatternMatchForPerson(Object person) {
return new PMatcher<Object, String>(person)
.matchRef(lalit)
.thenReturn("Original one and only Lalit found!")
return new PMatcher<Object, String>(person).matchRef(lalit).thenReturn("Original one and only Lalit found!")
.matchCase( // any number of field matches
// this is a field with predicate match with Function<Object, Boolean> passed
MField.with(
"name", name -> name != null && ((String) name).toLowerCase().equals("lalit")),
MField.with("name", name -> name != null && ((String) name).toLowerCase().equals("lalit")),
MField.with("age", age -> age != null && (Integer) age < 60),
// this is field with value match with .withValue method
MField.withValue("eligible", true))
// every matchCase has to be followed by an action. matchCase() returns an instance of
// every matchCase has to be followed by an action. matchCase() returns an
// instance of
// CaseActionAppender
.thenReturn("Young Lalit found")
// .action of CaseActionAppender will return a new Matcher with appended action
.matchCase(MField.withValue("age", null))
.thenReturn("God found")
.matchCase(MField.withValue("age", null)).thenReturn("God found")
// this is a value match
.matchValue(new Person("Nitin", 26, false))
.thenTransform(p -> "Uneligible Nitin with age=" + ((Person) p).getAge() + " found")
// this is field with type match
.matchCase(MField.with("extra", String.class))
.thenTransform(
p -> "testpackage.Person with String extra found with extra value=" + ((Person) p).getExtra())
.thenTransform(p -> "testpackage.Person with String extra found with extra value=" + ((Person) p).getExtra())
// this is type match
.matchCase(NonPerson.class)
.thenReturn("This is not a person")
.matchCase(p -> p != null && ((Person) p).getAge() > 100)
.thenReturn("Very old person");
.matchCase(NonPerson.class).thenReturn("This is not a person")
.matchCase(p -> p != null && ((Person) p).getAge() > 100).thenReturn("Very old person");
// we can use get() which will return Optional<R>
}

Expand Down Expand Up @@ -101,4 +99,69 @@ public void testMatchRef() throws PMatcherException {

Assert.assertEquals("Original one and only Lalit found!", result);
}

interface Entity {

}

@Data
class P implements Entity {
private final String name;
private final Integer age;
}

class NP implements Entity {

}

@Data
class RetVal {
private final String message;
private final int messageLength;
private final boolean isPrivileged;
}

private Optional<RetVal> matchResult(Entity entity) throws PMatcherException {
return new PMatcher<Entity, RetVal>(entity)
.matchCase(MField.with("name", name -> ((String) name).startsWith("John")), MField.withValue("age", 18))
.thenSupply(() -> {
String msg = "18 year old John";
return new RetVal(msg, msg.length(), false);
}).matchRef(new P("Lalit Vatsal", 29)).thenSupply(() -> {
String msg = "Reference matched!";
return new RetVal(msg, msg.length(), true);
}).matchCase(MField.with("name", name -> ((String) name).startsWith("Lalit")),
MField.with("age", age -> ((Integer) age) > 18))
.thenTransform(p -> {
String msg = "" + ((P) p).getAge() + " years old " + ((P) p).getName();
return new RetVal(msg, msg.length(), ((P) p).getAge() > 18);
}).matchCase(NP.class).thenReturn(new RetVal("", 0, false)).get();
}

@Test
public void basicTest() throws PMatcherException {
P person = new P("Lalit Vatsal", 29);
NP np = new NP();

Optional<RetVal> personRetVal = matchResult(person);
Optional<RetVal> npRetVal = matchResult(np);

Assert.assertTrue("Person should be found", personRetVal.isPresent());
Assert.assertEquals(new RetVal("29 years old Lalit Vatsal", 25, true), personRetVal.get());
Assert.assertEquals(new RetVal("", 0, false), npRetVal.get());
}

enum Animal {
CAT, DOG, ELEPHANT, FISH, GIRAFFE, HORSE;
}

@Test
public void basicEnumTest() throws PMatcherException {

String message = new PMatcher<Animal, String>(Animal.DOG).matchValue(Animal.CAT).thenReturn("Found a Cat!")
.matchValue(Animal.DOG).thenReturn("Found a Dog!").matchValue(Animal.ELEPHANT).thenReturn("Found an Elephant!")
.getOrElse("Unnkown Animal");

Assert.assertEquals("Found a Dog!", message);
}
}

0 comments on commit 48eb1d8

Please sign in to comment.