package com.karateca.ddescriber.dialog;

import com.intellij.find.impl.FindResultImpl;
import com.intellij.mock.MockDocument;
import com.karateca.ddescriber.model.TestFindResult;
import com.karateca.ddescriber.model.TestState;
import junit.framework.TestCase;

import java.util.List;

/**
 * @author Andres Dominguez.
 */
public class PendingChangesTest extends TestCase {

  private PendingChanges pendingChanges;

  public void setUp() throws Exception {
    pendingChanges = new PendingChanges();
  }

  public void testGetTestsToChange() throws Exception {
    pendingChanges.itemChanged(createPendingChange(10));
    pendingChanges.itemChanged(createPendingChange(30));

    pendingChanges.itemChanged(createPendingChange(20));
    pendingChanges.itemChanged(createPendingChange(40));

    List<TestFindResult> testsToChange = pendingChanges.getTestsToChange();

    assertEquals(4, testsToChange.size());
    assertEquals(10, testsToChange.get(0).getStartOffset());
    assertEquals(20, testsToChange.get(1).getStartOffset());
    assertEquals(30, testsToChange.get(2).getStartOffset());
    assertEquals(40, testsToChange.get(3).getStartOffset());
  }

  public void testShouldAddItems() {
    // When you add two items.
    pendingChanges.itemChanged(createIncluded());
    pendingChanges.itemChanged(createExcluded());

    // Then ensure there are two items.
    assertEquals(2, pendingChanges.getTestsToChange().size());
  }

  public void testShouldRemoveTestResultThatDidNotChangeStatus() {
    // Given that you add two items.
    TestFindResult included = createIncluded();
    TestFindResult excluded = createExcluded();

    pendingChanges.itemChanged(included);
    pendingChanges.itemChanged(excluded);

    // When you remove the change of the included.
    included.setPendingChangeState(TestState.Included);
    pendingChanges.itemChanged(included);

    // Then ensure there is on item.
    assertEquals(1, pendingChanges.getTestsToChange().size());
    assertEquals(excluded, pendingChanges.getTestsToChange().get(0));
  }

  public void testShouldClearPendingStateWhenRemovingFromPendingChanges() {
    // Given that you have an included and an excluded tests.
    TestFindResult included = createIncluded();
    TestFindResult excluded = createExcluded();

    // When you remove the changed state.
    included.setPendingChangeState(TestState.Included);
    excluded.setPendingChangeState(TestState.Excluded);

    pendingChanges.itemChanged(included);
    pendingChanges.itemChanged(excluded);

    // Then ensure the new state is rolled back.
    assertEquals(TestState.RolledBack, included.getPendingChangeState());
    assertEquals(TestState.RolledBack, excluded.getPendingChangeState());
  }

  private TestFindResult createIncluded() {
    TestFindResult pendingChange = createPendingChange(1);
    pendingChange.setTestState(TestState.Included);
    return pendingChange;
  }

  private TestFindResult createExcluded() {
    TestFindResult pendingChange = createPendingChange(1);
    pendingChange.setTestState(TestState.Excluded);
    return pendingChange;
  }

  private TestFindResult createPendingChange(int startOffset) {
    return new TestFindResult(new MockDocument(""), new FindResultImpl(startOffset, startOffset + 10));
  }
}

@@ -608,16 +608,36 @@
 ange(10)
+, TestState.Excluded
 );%0A    p
@@ -685,16 +685,36 @@
 ange(30)
+, TestState.Excluded
 );%0A%0A    
@@ -763,16 +763,36 @@
 ange(20)
+, TestState.Excluded
 );%0A    p
@@ -840,16 +840,36 @@
 ange(40)
+, TestState.Excluded
 );%0A%0A    
@@ -1348,16 +1348,36 @@
 cluded()
+, TestState.Excluded
 );%0A    p
@@ -1418,16 +1418,36 @@
 cluded()
+, TestState.Included
 );%0A%0A    
@@ -1745,24 +1745,128 @@
 eExcluded();
+%0A    TestFindResult notModified = createExcluded();%0A    notModified.setTestState(TestState.NotModified);
 %0A%0A    pendin
@@ -1886,32 +1886,52 @@
 Changed(included
+, TestState.Excluded
 );%0A    pendingCh
@@ -1948,32 +1948,117 @@
 Changed(excluded
+, TestState.Included);%0A    pendingChanges.itemChanged(notModified, TestState.Included
 );%0A%0A    // When 
@@ -2104,39 +2104,45 @@
     
-included.setPendingChangeState(
+pendingChanges.itemChanged(included, 
 Test
@@ -2181,33 +2181,56 @@
 ges.itemChanged(
-i
+notModified, TestState.I
 ncluded);%0A%0A    /
@@ -2427,20 +2427,19 @@
 ould
-ClearPending
+SetRollback
 Stat
@@ -2823,24 +2823,44 @@
 ged(included
+, TestState.Included
 );%0A    pendi
@@ -2885,24 +2885,44 @@
 ged(excluded
+, TestState.Excluded
 );%0A%0A    // T
@@ -3559,106 +3559,258 @@
     
-return new TestFindResult(new MockDocument(%22%22), new FindResultImpl(startOffset, startOffset + 10))
+FindResultImpl findResult = new FindResultImpl(startOffset, startOffset + 10);%0A    TestFindResult testFindResult = new TestFindResult(new MockDocument(%22%22), findResult);%0A    testFindResult.setTestState(TestState.NotModified);%0A    return testFindResult
 ;%0A  

improve the test