package com.karateca.ddescriber.model;

import com.karateca.ddescriber.BaseTestCase;

import java.util.List;

/**
 * @author Andres Dominguez.
 */
public class HierarchyTest extends BaseTestCase {

  private Hierarchy getHierarchyForTestFile(String testFile) {
    prepareScenarioWithTestFile(testFile);
    jasmineFinder.findAll();

    return new Hierarchy(document, jasmineFinder.findResults, myFixture.getCaretOffset());
  }

  public void testGetClosest() throws Exception {
    // Given that the caret is under "inner it 2".
    Hierarchy hierarchy = getHierarchyForTestFile("jasmineTestBefore.js");

    // When you find the closest test or fixture.
    TestFindResult closest = hierarchy.getClosest();

    // Then ensure the closest test is "inner it 2".
    assertEquals(33, closest.getLineNumber());
    assertEquals("inner it 2", closest.getTestText());
  }

  public void testGetMarkedElements() {
    // Given that you have the caret at the top.
    Hierarchy hierarchy = getHierarchyForTestFile("jasmineTestCaretTop.js");

    // When you get the marked elements.
    List<TestFindResult> elements = hierarchy.getMarkedElements();

    // Then ensure two elements are returned.
    assertEquals(2, elements.size());
    assertEquals(23, elements.get(0).getLineNumber());
    assertEquals(33, elements.get(1).getLineNumber());
  }

  public void testShouldFindExcludedElements() {
    // Given a jasmine file with excluded xdescribe and xit.
    Hierarchy hierarchy = getHierarchyForTestFile("testWihManyLevels.js");

    // When you get the elements.
    List<TestFindResult> tests = hierarchy.getAllUnitTests();

    // Then ensure there are excluded elements.
    TestFindResult itElement = tests.get(11);
    assertEquals("should be excluded 1", itElement.getTestText());
    assertTrue(itElement.getTestState() == TestState.Excluded);

    TestFindResult describeElement = tests.get(12);
    assertEquals("excluded 2", describeElement.getTestText());
    assertTrue(describeElement.getTestState() == TestState.Excluded);

    // And ensure it found all the tests.
    assertEquals(16, tests.size());
  }

  public void testShouldFindTestsWithTrickyText() {
    // Load a file with tricky text.
    Hierarchy hierarchy = getHierarchyForTestFile("jasmineWithWeirdRegularExpressions.js");

    // When you get the elements.
    List<TestFindResult> tests = hierarchy.getAllUnitTests();

    // Then ensure the test count is correct.
    assertEquals(11, tests.size());

    int i = 0;
    assertDescribe(tests.get(i++), "top describe", TestState.NotModified);
    assertIt(tests.get(i++), "should not be included iit", TestState.NotModified);
    assertIt(tests.get(i++), "should not be excluded exit", TestState.NotModified);
    assertIt(tests.get(i++), "should not be a describe()", TestState.NotModified);
    assertIt(tests.get(i++), "should not be a ddescribe", TestState.NotModified);
    assertIt(tests.get(i++), "should be excluded it( iit( describe(", TestState.Excluded);
    assertIt(tests.get(i++), "should be included it( xit( describe( xdescribe(", TestState.Included);
    assertDescribe(tests.get(i++), "suite not excluded xdescribe(", TestState.NotModified);
    assertDescribe(tests.get(i++), "suite not included ddescribe(", TestState.NotModified);
    assertDescribe(tests.get(i++), "suite excluded describe( it(", TestState.Excluded);
    assertDescribe(tests.get(i++), "suite included describe( xdescribe( iit(", TestState.Included);
  }

  private void assertDescribe(TestFindResult findResult, String expectedText, TestState expectedState) {
    assertEquals(expectedText, findResult.getTestText());
    assertEquals(expectedState, findResult.getTestState());
    assertTrue(findResult.isDescribe());
  }

  private void assertIt(TestFindResult findResult, String expectedText, TestState expectedState) {
    assertEquals(expectedText, findResult.getTestText());
    assertEquals(expectedState, findResult.getTestState());
    assertFalse(findResult.isDescribe());
  }
}

@@ -107,45 +107,8 @@
 t;%0A%0A
-/**%0A * @author Andres Dominguez.%0A */%0A
 publ
@@ -436,16 +436,164 @@
 ption %7B%0A
+    getClosest(%22jasmine1/jasmineTestBefore.js%22);%0A    getClosest(%22jasmine2/jasmineTestBefore.js%22);%0A  %7D%0A%0A  private void getClosest(String testFile) %7B%0A
     // G
@@ -689,30 +689,16 @@
 ile(
-%22jasmineTestBefore.js%22
+testFile
 );%0A%0A
@@ -988,37 +988,218 @@
 etMarkedElements
+Jasmine1
 () %7B%0A
+    getMarkedElements(%22jasmine1/jasmineTestCaretTop.js%22);%0A    getMarkedElements(%22jasmine2/jasmineTestCaretTop.js%22);%0A  %7D%0A%0A  private void getMarkedElements(String testFile) %7B%0A
     // Given tha
@@ -1285,32 +1285,16 @@
 ile(
-%22jasmineTestCaretTop.js%22
+testFile
 );%0A%0A
@@ -2427,24 +2427,263 @@
 ckyText() %7B%0A
+    shouldFindTestsWithTrickyText(%22jasmine1/jasmineWithWeirdRegularExpressions.js%22);%0A    shouldFindTestsWithTrickyText(%22jasmine2/jasmineWithWeirdRegularExpressions.js%22);%0A  %7D%0A%0A  private void shouldFindTestsWithTrickyText(String testFile) %7B%0A
     // Load 
@@ -2761,47 +2761,16 @@
 ile(
-%22jasmineWithWeirdRegularExpressions.js%22
+testFile
 );%0A%0A

Add scenarios for jasmine 1, 2