package com.dwarfartisan.parsec;

import static com.dwarfartisan.parsec.Txt.space;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * Created by Mars Liu on 16/9/15.
 *
 * Tests About look behind.
 */
public class BehindTest extends Base {

    @Test
    public void simple() throws Exception{
        State<Character, Integer, Integer> state = newState("this is a string data.");
        Parsec<String, Character, Integer, Integer> parser =
                new Text<Integer, Integer>("this").over(new Behind<>(new Text<>(" is")));

        String re = parser.parse(state);

        assertEquals(re, "this");
        assertThat("Expect status stop after this but is. ", state.status(), IsEqual.equalTo(4));
    }

    @Test
    public void result() throws Exception{
        State<Character, Integer, Integer> state = newState("this is a string data.");
        Parsec<String, Character, Integer, Integer> parser =
                new Text<Integer, Integer>("this").then(space()).then(new Behind<>(new Text<>("is")));

        String re = parser.parse(state);

        assertEquals(re, "is");
        assertThat("Expect status stop after this (5) but is. ", state.status(), IsEqual.equalTo(5));
    }
}

@@ -110,16 +110,55 @@
 sEqual;%0A
+import org.hamcrest.core.IsInstanceOf;%0A
 import o
@@ -172,16 +172,17 @@
 t.Test;%0A
+%0A
 import s
@@ -1281,11 +1281,668 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void fail() throws Exception%7B%0A        State%3CCharacter, Integer, Integer%3E state = newState(%22this is a string.%22);%0A        Parsec%3CString, Character, Integer, Integer%3E parser =%0A                new Text%3CInteger, Integer%3E(%22this%22).then(space()).then(new Behind%3C%3E(new Text%3C%3E(%22 is%22)));%0A%0A        try %7B%0A            String re = parser.parse(state);%0A        %7D catch (Exception e) %7B%0A            assertThat(%22Expect status stop after this (5) but is. %22, state.status(), IsEqual.equalTo(5));%0A            assertThat(%22Expect the parser fail when try match %5C%22 is%5C%22%22,%0A                    e, IsInstanceOf.instanceOf(ParsecException.class));%0A        %7D%0A    %7D
 %0A%7D%0A

add a failed test