/*
 * Copyright Matt Palmer 2009-2011, All rights reserved.
 *
 */

package net.domesdaybook.compiler.sequence;

import net.domesdaybook.matcher.sequence.SequenceOfSequencesMatcher;
import net.domesdaybook.matcher.bytes.AllBitmaskMatcher;
import net.domesdaybook.matcher.bytes.ByteRangeMatcher;
import net.domesdaybook.matcher.bytes.SetBinarySearchMatcher;
import net.domesdaybook.matcher.bytes.OneByteMatcher;
import net.domesdaybook.compiler.CompileException;
import net.domesdaybook.matcher.sequence.CaseInsensitiveStringMatcher;
import net.domesdaybook.matcher.sequence.ByteSequenceMatcher;
import net.domesdaybook.matcher.sequence.FixedGapMatcher;
import net.domesdaybook.matcher.sequence.SequenceMatcher;
import net.domesdaybook.matcher.sequence.SingleByteSequenceMatcher;
import org.antlr.runtime.tree.CommonTree;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author matt
 */
public class SequenceMatcherCompilerTest {


    private SequenceMatcherCompiler compiler = new SequenceMatcherCompiler();

    /**
     * 
     */
    public SequenceMatcherCompilerTest() {
    }

    /**
     * 
     * @throws Exception
     */
    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    /**
     * 
     * @throws Exception
     */
    @AfterClass
    public static void tearDownClass() throws Exception {
    }


    /**
     * 
     * @throws CompileException
     */
    @Test(expected=CompileException.class)
    public void testCompileNullExpression() throws CompileException {
        compiler.compile((String) null);
    }

    /**
     * 
     * @throws CompileException
     */
    @Test(expected=CompileException.class)
    public void testCompileEmptyExpression() throws CompileException {
        compiler.compile("");
    }
    
    /**
     * 
     * @throws CompileException
     */
    @Test(expected=CompileException.class)
    public void testCompileNullAST() throws CompileException {
        compiler.compile((CommonTree) null);
    }


    /**
     * Test of compile method, of class SequenceMatcherCompiler.
     * @throws Exception 
     */
    @Test
    public void testBasicCompile() throws Exception {
        SequenceMatcher matcher;
        basicTests("00", 1, OneByteMatcher.class);
        basicTests("01", 1, OneByteMatcher.class);
        basicTests("fF", 1, OneByteMatcher.class);

        basicTests("[00ff]", 1, SetBinarySearchMatcher.class);
        basicTests("[ff00]", 1, SetBinarySearchMatcher.class);
        basicTests("[7f80]", 1, ByteRangeMatcher.class);
        basicTests("[807f]", 1, ByteRangeMatcher.class);
        basicTests(" [0102]", 1, ByteRangeMatcher.class);
        
        basicTests("'a string'", 8, ByteSequenceMatcher.class);
        basicTests("`a string`", 8, CaseInsensitiveStringMatcher.class);
        basicTests("01 'a string' 02", 10, ByteSequenceMatcher.class);

        basicTests("0102", 2, ByteSequenceMatcher.class);
        basicTests("01 02", 2, ByteSequenceMatcher.class);
        basicTests("01fd", 2, ByteSequenceMatcher.class);


        basicTests(" [0102] &01", 2, SingleByteSequenceMatcher.class);
        basicTests(" [0102] [^ffee]", 2, SingleByteSequenceMatcher.class);


        basicTests("01fd ef   de", 4, ByteSequenceMatcher.class);

        basicTests("01fd [ef]   de", 4, ByteSequenceMatcher.class);
        
        basicTests("01fd [ef fe]   de", 4, SequenceOfSequencesMatcher.class);

        basicTests("01{4}", 4, ByteSequenceMatcher.class);

        basicTests("010203{6}", 8, ByteSequenceMatcher.class);

        basicTests("[fffe]", 1, AllBitmaskMatcher.class);
        basicTests("[fffe]{5}", 5, SingleByteSequenceMatcher.class);
        
        basicTests("(0102){2}", 4, ByteSequenceMatcher.class);
        basicTests("(dd[ff03]){3}", 6, SequenceOfSequencesMatcher.class);
        basicTests("'start'(dd[ff03]){3}", 11, SequenceOfSequencesMatcher.class);

        basicTests(".{1000}", 1000, FixedGapMatcher.class);
    }


    private SequenceMatcher basicTests(String expression, int length, Class matcherClass) {
        SequenceMatcher matcher = null;
        try {
            matcher = compiler.compile(expression);
            assertEquals("length of " + expression, length, matcher.length());
            assertEquals("class of " + expression, matcherClass, matcher.getClass());
        } catch (CompileException ex) {
            fail(expression + " " + ex.getMessage());
        }
        return matcher;
    }


}
@@ -151,35 +151,36 @@
 nce.Sequence
-OfSequences
+MatcherArray
 Matcher;%0Aimp
@@ -509,29 +509,31 @@
 InsensitiveS
-tring
+equence
 Matcher;%0Aimp
@@ -570,32 +570,29 @@
 equence.Byte
-Sequence
+Array
 Matcher;%0Aimp
@@ -745,34 +745,32 @@
 equence.
-SingleByteSequence
+ByteMatcherArray
 Matcher;
@@ -2757,32 +2757,29 @@
 g'%22, 8, Byte
-Sequence
+Array
 Matcher.clas
@@ -2834,21 +2834,23 @@
 nsitiveS
-tring
+equence
 Matcher.
@@ -2900,32 +2900,29 @@
 2%22, 10, Byte
-Sequence
+Array
 Matcher.clas
@@ -2956,32 +2956,29 @@
 02%22, 2, Byte
-Sequence
+Array
 Matcher.clas
@@ -3012,32 +3012,29 @@
 02%22, 2, Byte
-Sequence
+Array
 Matcher.clas
@@ -3067,32 +3067,29 @@
 fd%22, 2, Byte
-Sequence
+Array
 Matcher.clas
@@ -3131,34 +3131,32 @@
 01%22, 2, 
-SingleByteSequence
+ByteMatcherArray
 Matcher.
@@ -3204,34 +3204,32 @@
 e%5D%22, 2, 
-SingleByteSequence
+ByteMatcherArray
 Matcher.
@@ -3276,32 +3276,29 @@
 de%22, 4, Byte
-Sequence
+Array
 Matcher.clas
@@ -3342,32 +3342,29 @@
 de%22, 4, Byte
-Sequence
+Array
 Matcher.clas
@@ -3423,35 +3423,36 @@
  4, Sequence
-OfSequences
+MatcherArray
 Matcher.clas
@@ -3487,32 +3487,29 @@
 4%7D%22, 4, Byte
-Sequence
+Array
 Matcher.clas
@@ -3548,32 +3548,29 @@
 6%7D%22, 8, Byte
-Sequence
+Array
 Matcher.clas
@@ -3671,26 +3671,24 @@
  5, 
-SingleByteSequence
+ByteMatcherArray
 Matc
@@ -3747,24 +3747,21 @@
  4, Byte
-Sequence
+Array
 Matcher.
@@ -3811,35 +3811,36 @@
  6, Sequence
-OfSequences
+MatcherArray
 Matcher.clas
@@ -3902,19 +3902,20 @@
 ence
-OfSequences
+MatcherArray
 Matc

Refactor to work with new class names.