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());
  }
}

@@ -2115,11 +2115,1653 @@
 ());%0A  %7D
+%0A%0A  public void testShouldFindTestsWithTrickyText() %7B%0A    // Load a file with tricky text.%0A    Hierarchy hierarchy = getHierarchyForTestFile(%22jasmineWithWeirdRegularExpressions.js%22);%0A%0A    // When you get the elements.%0A    List%3CTestFindResult%3E tests = hierarchy.getAllUnitTests();%0A%0A    // Then ensure the test count is correct.%0A    assertEquals(11, tests.size());%0A%0A    int i = 0;%0A    TestFindResult findResult;%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22top describe%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22should not be included iit%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22should not be excluded exit%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22should not be a describe()%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22should not be a ddescribe%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22should be excluded it( iit( describe(%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22should be included it( xit( describe( xdescribe(%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22suite not excluded xdescribe(%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22suite not included ddescribe(%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22suite excluded describe( it(%22, findResult.getTestText());%0A%0A    findResult = tests.get(i++);%0A    assertEquals(%22suite included describe( xdescribe( iit(%22, findResult.getTestText());%0A  %7D
 %0A%7D%0A

test passing