package expectj;

import junit.framework.TestCase;
import expectj.test.StagedSpawnable;

/**
 * Verify that the different expect() methods of {@link SpawnedProcess} work as expected.
 * @author johan.walles@gmail.com
 */
public class TestExpect extends TestCase
{
    /**
     * Generate a SpawnedProcess producing the indicated output on stdout.
     * @param strings The strings to print to stdout.  Strings will be produced with
     * 500ms between each.
     * @return A new SpawnedProcess.
     * @throws Exception when things go wrong.
     */
    private SpawnedProcess getSpawnedProcess(final String ... strings)
    throws Exception
    {
        return new ExpectJ("/dev/null", -1).spawn(new StagedSpawnable(strings));
    }
    
    /**
     * Test that we can find a simple string.
     * @throws Exception if things go wrong.
     */
    public void testExpectString()
    throws Exception
    {
        SpawnedProcess testMe = getSpawnedProcess("flaska");
        testMe.expect("flaska");
    }
}

@@ -11,16 +11,86 @@
 pectj;%0A%0A
+import java.util.Date;%0Aimport java.util.concurrent.TimeoutException;%0A%0A
 import j
@@ -841,18 +841,16 @@
 an find 
-a 
 simple s
@@ -854,16 +854,363 @@
 e string
+s.%0A     * @throws Exception if things go wrong.%0A     */%0A    public void testExpectStrings()%0A    throws Exception%0A    %7B%0A        SpawnedProcess testMe = getSpawnedProcess(%22flaska%22, %22gris%22);%0A        testMe.expect(%22flaska%22);%0A        testMe.expect(%22gris%22);%0A    %7D%0A%0A    /**%0A     * Test that we can find simple strings with an unexpected string in between
 .%0A     *
@@ -1288,16 +1288,30 @@
 ctString
+sWithExtraData
 ()%0A    t
@@ -1382,32 +1382,50 @@
 Process(%22flaska%22
+, %22nyckel%22, %22gris%22
 );%0A        testM
@@ -1440,20 +1440,914 @@
 (%22flaska%22);%0A
+        testMe.expect(%22gris%22);%0A    %7D%0A    %0A    /**%0A     * Test that we time out properly when we don't find what we're looking for.%0A     * @throws Exception if things go wrong.%0A     */%0A    public void testTimeout()%0A    throws Exception %0A    %7B%0A        // Test longer duration output than timeout%0A        SpawnedProcess testMe = getSpawnedProcess(%22flaska%22, %22nyckel%22, %22gris%22, %22hink%22);%0A        Date beforeTimeout = new Date();%0A        try %7B%0A            testMe.expect(%22klubba%22, 1);%0A            fail(%22Test should have timed out%22);%0A        %7D catch (TimeoutException expected) %7B%0A            // Ignoring expected exception%0A        %7D%0A        Date afterTimeout = new Date();%0A        %0A        long msElapsed = afterTimeout.getTime() - beforeTimeout.getTime();%0A        if (msElapsed %3C 900 %7C%7C msElapsed %3E 1100) %7B%0A            fail(%22Should have timed out after 1s, timed out in %22 + msElapsed + %22ms%22);%0A        %7D%0A
     %7D%0A%7D%0A

Add tests for ignoring unexpected output, and a timeout test.