import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

/**
 * User: Johannes Krampf <johkra@gmail.com>
 * Date: 27.02.11
 */
public class QueryTest {
    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

    @Before
    public void setUp() throws Exception {
        System.setOut(new PrintStream(outContent));
    }

    private void verifyOutput(String commands, String exceptedOutput) throws Exception {
        byte[] bytes = commands.getBytes("utf-8");
        ByteArrayInputStream input = new ByteArrayInputStream(bytes);
        Prolog.procFile(input, "");
        assertEquals(exceptedOutput, outContent.toString());
    }

    @Test
    public void testSimpleQuery() throws Exception {
        verifyOutput("boy(bill)\n" +
                "boy(bill)?",
                "Yes\n");
    }

    @Test
    public void testVariableQuery() throws Exception {
        verifyOutput("boy(X)?", "{X=bill}\n");
    }

    @Test
    public void testIndirectQuery() throws Exception {
        verifyOutput("mother(alice,bill)\n" +
                "child(J,K) :- mother(K,J)\n" +
                "son(X,Y) :- child(X,Y),boy(X)\n" +
                "son(X,alice)?", "{X=bill}\n");
    }

    @Test
    public void testMember() throws Exception {
        verifyOutput("member(X,[X|T])\n" +
                "member(X,[H|T]) :- member(X,T)\n" +
                "member(a,[a,b,c])?\n" +
                "member(b,[a,b,c])?\n" +
                "member(c,[a,b,c])?", "Yes\nYes\nYes\n");
    }

    @Test
    public void testAppend() throws Exception {
        verifyOutput("append([],L,L)\n" +
                "append([X|A],B,[X|C]) :- append(A,B,C)\n" +
                "append([a,b],[c,d],X)?", "{X=[a,b,c,d]}\n");
    }

    @Test
    public void testLength() throws Exception {
        verifyOutput("length([],0)\n" +
                "length([H|T],N) :- length(T,Nt), N is Nt+1\n" +
                "length([],X)?\n" +
                "length([1],X)?\n" +
                "length([1,2,3],X)?", "{X=0}\n{X=1}\n{X=3}\n");
    }

    @After
    public void tearDown() throws Exception {
        System.setOut(null);
    }
}

@@ -2204,24 +2204,526 @@
 n%22);%0A    %7D%0A%0A
+    @Test%0A    public void testWithoutCut() throws Exception %7B%0A        verifyOutput(%22childOf(X,Y) :- parent(Y,X)%5Cn%22 +%0A                %22parent(chris,jon)%5Cn%22 +%0A                %22parent(maryann,jon)%5Cn%22 +%0A                %22childOf(A,B)?%22,%22%7BA=jon, B=chris%7D%5Cn%22 +%0A                %22%7BA=jon, B=maryann%7D%5Cn%22);%0A    %7D%0A%0A    @Test%0A    public void testFail() throws Exception %7B%0A        verifyOutput(%22childOfCut(X,Y) :- parent(Y,X),cut%5Cn%22 +%0A                %22childOfCut(A,B)?%22,%0A                %22%7BA=jon, B=chris%7D%5Cn%22);%0A    %7D%0A%0A
     @After%0A 

Add tests for cut