package org.realityforge.arez;

import java.util.ArrayList;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import static org.testng.Assert.*;

final class TestSpyEventHandler
  implements SpyEventHandler
{
  private final ArrayList<Object> _events = new ArrayList<>();

  @Override
  public void onSpyEvent( @Nonnull final Object event )
  {
    _events.add( event );
  }

  /**
   * Assert Event exists in list and return it.
   */
  @Nonnull
  <T> T assertEvent( @Nonnull final Class<T> type )
  {
    for ( final Object event : _events )
    {
      if ( type.isInstance( event ) )
      {
        return type.cast( event );
      }
    }
    fail( "Unable to locate event of type " + type + " in event list " + _events );
    return null;
  }

  /**
   * Assert Event at index is of specific type and return it.
   */
  @Nonnull
  <T> T assertEvent( @Nonnull final Class<T> type, @Nonnegative final int index )
  {
    assertTrue( eventCount() > index );
    final Object event = _events.get( index );
    assertTrue( type.isInstance( event ),
                "Expected event at index " + index + " to be of type " + type + " but is " +
                " of type " + event.getClass() + " with value " + event );
    return type.cast( event );
  }

  void assertEventCount( final int count )
  {
    assertEquals( eventCount(), count, "Actual events: " + _events );
  }

  void assertEventCountAtLeast( final int count )
  {
    assertTrue( eventCount() >= count, "Expected more than " + count + ". Actual events: " + _events );
  }

  private int eventCount()
  {
    return _events.size();
  }
}

@@ -284,16 +284,137 @@
 ist%3C%3E();
+%0A  /**%0A   * When ussing assertNextEvent this tracks the index that we are up to.%0A   */%0A  private int _currentAssertIndex;
 %0A%0A  @Ove
@@ -1674,16 +1674,290 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Assert %22next%22 Event is of specific type.%0A   * Increment the next counter.%0A   */%0A  @Nonnull%0A  %3CT%3E T assertNextEvent( @Nonnull final Class%3CT%3E type )%0A  %7B%0A    final T event = assertEvent( type, _currentAssertIndex );%0A    _currentAssertIndex++;%0A    return event;%0A  %7D%0A%0A
   privat

Enhance test spy so the test code can worry about event sequencing rather than event indexes