package com.karateca.ddescriber;

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(28, closest.getLineNumber());
    assertEquals("        it('inner it 2', function () {", closest.lineText);
  }

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

    // When you the unit tests for the current describe.
    List<TestFindResult> elements = hierarchy.getUnitTestsForCurrentDescribe();

    // Then ensure all the 'its' in the current describe and the parents
    // are returned.
    assertEquals(5, elements.size());
    assertEquals("describe('top describe', function () {", elements.get(0).lineText);
    assertEquals("    ddescribe('inner describe', function () {", elements.get(1).lineText);
    assertEquals("        it('inner it 1', function () {", elements.get(2).lineText);
    assertEquals("        it('inner it 2', function () {", elements.get(3).lineText);
    assertEquals("        iit('inner it 3', function () {", elements.get(4).lineText);
  }

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

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

    // Then ensure the first level inside the top describe is returned.
    assertEquals(4, elements.size());
    int i = 0;
    assertEquals("describe('top describe', function () {", elements.get(i++).lineText);
    assertEquals("    it('first it', function () {", elements.get(i++).lineText);
    assertEquals("    it('second it', function () {", elements.get(i++).lineText);
    assertEquals("    ddescribe('inner describe', function () {", elements.get(i++).lineText);
  }

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

@@ -793,31 +793,16 @@
 er it 2'
-, function () %7B
 %22, close
@@ -804,24 +804,29 @@
 closest.
-line
+getTest
 Text
+()
 );%0A  %7D%0A%0A
@@ -1329,39 +1329,24 @@
 op describe'
-, function () %7B
 %22, elements.
@@ -1352,24 +1352,29 @@
 .get(0).
-line
+getTest
 Text
+()
 );%0A    a
@@ -1412,39 +1412,24 @@
 er describe'
-, function () %7B
 %22, elements.
@@ -1435,24 +1435,29 @@
 .get(1).
-line
+getTest
 Text
+()
 );%0A    a
@@ -1492,31 +1492,16 @@
 er it 1'
-, function () %7B
 %22, eleme
@@ -1511,24 +1511,29 @@
 .get(2).
-line
+getTest
 Text
+()
 );%0A    a
@@ -1568,31 +1568,16 @@
 er it 2'
-, function () %7B
 %22, eleme
@@ -1587,24 +1587,29 @@
 .get(3).
-line
+getTest
 Text
+()
 );%0A    a
@@ -1645,31 +1645,16 @@
 er it 3'
-, function () %7B
 %22, eleme
@@ -1664,24 +1664,29 @@
 .get(4).
-line
+getTest
 Text
+()
 );%0A  %7D%0A%0A
@@ -2157,39 +2157,24 @@
 op describe'
-, function () %7B
 %22, elements.
@@ -2178,32 +2178,37 @@
 ts.get(i++).
-line
+getTest
 Text
+()
 );%0A    asser
@@ -2229,39 +2229,24 @@
 t('first it'
-, function () %7B
 %22, elements.
@@ -2250,32 +2250,37 @@
 ts.get(i++).
-line
+getTest
 Text
+()
 );%0A    asser
@@ -2306,31 +2306,16 @@
 cond it'
-, function () %7B
 %22, eleme
@@ -2323,32 +2323,37 @@
 ts.get(i++).
-line
+getTest
 Text
+()
 );%0A    asser
@@ -2395,23 +2395,8 @@
 ibe'
-, function () %7B
 %22, e
@@ -2416,16 +2416,21 @@
 ++).
-line
+getTest
 Text
+()
 );%0A 

update tests for the new tostring