package it.halfone.test;
import it.halfone.parser.Parser;
import it.halfone.regex.Regex;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Regex - 13/ott/2011
*
* @author Andrea La Rosa
*/
public class RegexTest {
private final String literalValue = "ere4j";
private Regex literal;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
literal = Regex.literal(literalValue);
}
@Test
public void test() {
Assert.assertTrue(literal.compile().match(literalValue));
literal.then(literal);
Assert.assertTrue(literal.compile().match(literalValue));
literal.or(literal);
Assert.assertTrue(literal.compile().match(literalValue));
literal.atLeast(3);
Assert.assertTrue(literal.compile().match(literalValue));
literal.atLeastOnce();
Assert.assertTrue(literal.compile().match(literalValue));
literal.repeat(2, 5);
Assert.assertTrue(literal.compile().match(literalValue));
literal.repeat(2);
Assert.assertTrue(literal.compile().match(literalValue));
literal.optional();
Assert.assertTrue(literal.compile().match(literalValue));
literal.star();
Assert.assertTrue(literal.compile().match(literalValue));
literal.mark("value");
Assert.assertTrue(literal.compile().match(literalValue));
literal.boost(5);
Assert.assertTrue(literal.compile().match(literalValue));
Parser parser = literal.then(literal).compile();
Assert.assertTrue(parser.match(literalValue + literalValue));
Assert.assertFalse(parser.match(""));
Assert.assertFalse(parser.match(literalValue));
Assert.assertFalse(parser.match(literalValue + literalValue + literalValue));
parser = literal.or("").compile();
Assert.assertTrue(parser.match(literalValue));
Assert.assertTrue(parser.match(""));
Assert.assertFalse(parser.match(literalValue + literalValue));
parser = literal.atLeast(3).compile();
Assert.assertTrue(parser.match(literalValue + literalValue + literalValue));
Assert.assertTrue(parser.match(literalValue + literalValue + literalValue + literalValue));
Assert.assertTrue(parser.match(literalValue + literalValue + literalValue + literalValue + literalValue + literalValue));
Assert.assertFalse(parser.match(""));
Assert.assertFalse(parser.match(literalValue));
Assert.assertFalse(parser.match(literalValue + literalValue));
parser = literal.mark("value").compile();
Assert.assertTrue(parser.parseBest(literalValue).get("value").contains(literalValue));
// Boost Test
Regex anyStar = Regex.any().star().mark("any");
Regex aStar = Regex.literal("a").star().boost(1).mark("a");
parser = anyStar.then(aStar).compile();
Assert.assertTrue(parser.parseBest("aaa").get("a").contains("aaa"));
}
}