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), TestState.Excluded);
    pendingChanges.itemChanged(createPendingChange(30), TestState.Excluded);

    pendingChanges.itemChanged(createPendingChange(20), TestState.Excluded);
    pendingChanges.itemChanged(createPendingChange(40), TestState.Excluded);

    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(), TestState.Excluded);
    pendingChanges.itemChanged(createExcluded(), TestState.Included);

    // 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();
    TestFindResult notModified = createExcluded();
    notModified.setTestState(TestState.NotModified);

    pendingChanges.itemChanged(included, TestState.Excluded);
    pendingChanges.itemChanged(excluded, TestState.Included);
    pendingChanges.itemChanged(notModified, TestState.Included);

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

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

  public void testShouldSetRollbackStateWhenRemovingFromPendingChanges() {
    // 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, TestState.Included);
    pendingChanges.itemChanged(excluded, TestState.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) {
    FindResultImpl findResult = new FindResultImpl(startOffset, startOffset + 10);
    TestFindResult testFindResult = new TestFindResult(new MockDocument(""), findResult);
    testFindResult.setTestState(TestState.NotModified);
    return testFindResult;
  }
}

@@ -1332,30 +1332,52 @@
 Changed(
-cre
+getTestFindResult(TestSt
 ate
+.
 Included
 (), Test
@@ -1364,25 +1364,24 @@
 ate.Included
-(
 ), TestState
@@ -1423,30 +1423,52 @@
 Changed(
-cre
+getTestFindResult(TestSt
 ate
+.
 Excluded
 (), Test
@@ -1459,17 +1459,16 @@
 Excluded
-(
 ), TestS
@@ -1722,38 +1722,60 @@
  included = 
-cre
+getTestFindResult(TestSt
 ate
+.
 Included
 ();%0A    Test
@@ -1754,33 +1754,32 @@
 stState.Included
-(
 );%0A    TestFindR
@@ -1791,38 +1791,60 @@
  excluded = 
-cre
+getTestFindResult(TestSt
 ate
+.
 Excluded
 ();%0A    Test
@@ -1827,25 +1827,24 @@
 ate.Excluded
-(
 );%0A    TestF
@@ -1871,54 +1871,25 @@
 d = 
-createExcluded();%0A    notModified.setTestState
+getTestFindResult
 (Tes
@@ -2621,30 +2621,52 @@
 luded = 
-cre
+getTestFindResult(TestSt
 ate
+.
 Included
 ();%0A    
@@ -2657,17 +2657,16 @@
 Included
-(
 );%0A    T
@@ -2690,30 +2690,52 @@
 luded = 
-cre
+getTestFindResult(TestSt
 ate
+.
 Excluded
 ();%0A%0A   
@@ -2726,17 +2726,16 @@
 Excluded
-(
 );%0A%0A    
@@ -3241,209 +3241,45 @@
 ult 
-createIncluded() %7B%0A    TestFindResult pendingChange = createPendingChange(1);%0A    pendingChange.setTestState(TestState.Included);%0A    return pendingChange;%0A  %7D%0A%0A  private TestFindResult createExcluded(
+getTestFindResult(TestState testState
 ) %7B%0A
@@ -3364,33 +3364,33 @@
 etTestState(
-T
+t
 estState
 .Excluded);%0A
@@ -3373,33 +3373,24 @@
 te(testState
-.Excluded
 );%0A    retur

improve test