package com.tinkerpop.gremlin.process;

import com.tinkerpop.gremlin.LoadGraphWith;
import com.tinkerpop.gremlin.process.util.TraverserIterator;
import com.tinkerpop.gremlin.structure.Contains;
import com.tinkerpop.gremlin.structure.Vertex;
import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

import static com.tinkerpop.gremlin.LoadGraphWith.GraphData.MODERN;
import static org.junit.Assert.*;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class CoreTraversalTest extends AbstractGremlinProcessTest {

    @Test
    @LoadGraphWith(MODERN)
    public void shouldAddStartsProperly() {
        final Traversal<Object, Vertex> traversal = g.of().out().out();
        assertFalse(traversal.hasNext());
        traversal.addStarts(new TraverserIterator(traversal.getSteps().get(0), false, g.V()));
        assertTrue(traversal.hasNext());
        int counter = 0;
        while (traversal.hasNext()) {
            traversal.next();
            counter++;
        }
        assertEquals(2, counter);

        traversal.addStarts(new TraverserIterator(traversal.getSteps().get(0), false, g.V()));
        traversal.addStarts(new TraverserIterator(traversal.getSteps().get(0), false, g.V()));
        counter = 0;
        while (traversal.hasNext()) {
            counter++;
            traversal.next();
        }
        assertEquals(4, counter);
        assertFalse(traversal.hasNext());
    }

    @Test
    @LoadGraphWith(MODERN)
    public void shouldTraversalResetProperly() {
        final Traversal<Object, Vertex> traversal = g.of().as("a").out().out().<Vertex>has("name", Contains.within, Arrays.asList("ripple", "lop")).as("b");
        if (new Random().nextBoolean()) traversal.reset();
        assertFalse(traversal.hasNext());
        traversal.addStarts(new TraverserIterator(traversal.getSteps().get(0), false, g.V()));
        assertTrue(traversal.hasNext());
        int counter = 0;
        while (traversal.hasNext()) {
            traversal.next();
            counter++;
        }
        assertEquals(2, counter);

        if (new Random().nextBoolean()) traversal.reset();
        traversal.addStarts(new TraverserIterator(traversal.getSteps().get(0), false, g.V()));
        assertTrue(traversal.hasNext());
        traversal.next();
        assertTrue(traversal.hasNext());
        traversal.reset();
        assertFalse(traversal.hasNext());

        traversal.addStarts(new TraverserIterator(traversal.getSteps().get(0), false, g.V()));
        counter = 0;
        while (traversal.hasNext()) {
            counter++;
            traversal.next();
        }
        assertEquals(2, counter);

        assertFalse(traversal.hasNext());
        if (new Random().nextBoolean()) traversal.reset();
        assertFalse(traversal.hasNext());
    }
}

@@ -77,16 +77,75 @@
 phWith;%0A
+import com.tinkerpop.gremlin.process.graph.GraphTraversal;%0A
 import c
@@ -197,16 +197,16 @@
 erator;%0A
-
 import c
@@ -293,16 +293,41 @@
 Vertex;%0A
+import org.junit.Ignore;%0A
 import o
@@ -562,16 +562,16 @@
 om)%0A */%0A
-
 public c
@@ -631,16 +631,329 @@
 Test %7B%0A%0A
+    @Test%0A    @Ignore%0A    public void shouldDetectNext() %7B%0A        g.addVertex();%0A        g.addVertex();%0A        g.addVertex();%0A        this.tryCommit(g);%0A        final GraphTraversal%3CVertex, Vertex%3E gt = this.g.V();%0A        assertTrue(gt.hasNext());%0A        assertEquals(3, gt.count().next().intValue());%0A    %7D%0A%0A
     @Tes

Add failing test - with ignore for now. #276