1 package io.github.reggert.reb4j; 2 3 import static org.hamcrest.CoreMatchers.is; 4 import static org.junit.Assert.assertThat; 5 6 import io.github.reggert.reb4j.Alternation; 7 import io.github.reggert.reb4j.CharLiteral; 8 import io.github.reggert.reb4j.Expression; 9 import io.github.reggert.reb4j.Literal; 10 11 import java.util.HashSet; 12 13 import org.junit.Test; 14 15 16 17 public class AlternationTest 18 { 19 20 @Test 21 public void testAlternation() 22 { 23 final CharLiteral a = Literal.literal('a'); 24 final CharLiteral b = Literal.literal('b'); 25 final CharLiteral c = Literal.literal('c'); 26 final CharLiteral d = Literal.literal('d'); 27 final CharLiteral e = Literal.literal('e'); 28 final CharLiteral f = Literal.literal('f'); 29 final Alternation aOrB = a.or(b); 30 final Alternation aOrBOrC = aOrB.or(c); 31 final Alternation eOrF = e.or(f); 32 final Alternation dOrEOrF = d.or(eOrF); 33 final Alternation aOrBOrCOrDOrEOrF = aOrBOrC.or(dOrEOrF); 34 35 assertThat(aOrB.toPattern().pattern(), is("a|b")); 36 assertThat(aOrBOrC.toPattern().pattern(), is("a|b|c")); 37 assertThat(eOrF.toPattern().pattern(), is("e|f")); 38 assertThat(dOrEOrF.toPattern().pattern(), is("d|e|f")); 39 assertThat(aOrBOrCOrDOrEOrF.toPattern().pattern(), is("a|b|c|d|e|f")); 40 } 41 42 @Test 43 public void testAlternationHashcodeEquals() 44 { 45 final CharLiteral a = Literal.literal('a'); 46 final CharLiteral b = Literal.literal('b'); 47 final CharLiteral c = Literal.literal('c'); 48 49 final Alternation aOrB = a.or(b); 50 final Alternation aOrBOrC1 = aOrB.or(c); 51 final Alternation aOrBOrC2 = Alternation.alternatives(a, b, c); 52 final HashSet<Expression> expressions = new HashSet<Expression>(); 53 expressions.add(a); 54 expressions.add(b); 55 expressions.add(c); 56 expressions.add(aOrBOrC1); 57 assertThat(expressions.size(), is(4)); 58 assertThat(expressions.contains(aOrBOrC1), is(true)); 59 assertThat(expressions.contains(aOrBOrC2), is(true)); 60 assertThat(expressions.contains(aOrB), is(false)); 61 assertThat(aOrBOrC1.equals(aOrBOrC1), is(true)); 62 assertThat(aOrBOrC1.equals(aOrBOrC2), is(true)); 63 assertThat(aOrBOrC1.equals(a), is(false)); 64 assertThat(aOrBOrC1.equals(null), is(false)); 65 } 66 67 }