package com.karateca.jstoolbox.generatemethod;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vfs.VirtualFile;

import java.util.List;

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

  private ClassFinder classFinder;

  public void testGetClassNameClassFound() {
    // Given a file with a constructor.
    givenAFile("child.js");

    // When you get the class name.
    String className = classFinder.getClassName();

    // Then ensure the class name was found.
    assertEquals("Yo.child", className);
  }

  public void testGetClassNameClassNotFound() {
    // Given a file without constructor annotation.
    givenAFile("noConstructor.js");

    // When you get the class name.
    String className = classFinder.getClassName();

    // Then ensure the class name was not found.
    assertNull(className);
  }

  public void testGetParentClassName() {
    // Given a file with extends annotation.
    givenAFile("child.js");

    // When you get the parent class name.
    String parentClassName = classFinder.getParentClassName();

    // Then ensure the name was found.
    assertEquals("Yo.parent", parentClassName);
  }

  public void testGetParentClassNameNotFound() {
    // Given a class without a parent.
    givenAFile("grandpa.js");

    // When you get the parent class name.
    String parentClassName = classFinder.getParentClassName();

    // Then ensure it is null.
    assertNull(parentClassName);
  }

  public void testFindMethods() {
    // Given a file with functions.
    givenAFile("child.js");

    // When you get the functions.
    List<Function> methods = classFinder.getMethods();

    // Then ensure all the functions were found.
    assertEquals(4, methods.size());
    assertEquals("plainMethod", methods.get(0).getName());
    assertEquals("", methods.get(0).getArguments());

    assertEquals("withArguments", methods.get(1).getName());
    assertEquals("one, two", methods.get(1).getArguments());

    assertEquals("withManyLines", methods.get(2).getName());
    assertEquals("one, two,\n              three", methods.get(2).getArguments());

    assertEquals("withJsDoc", methods.get(3).getName());
    assertEquals("one, two, three", methods.get(3).getArguments());
  }

  private void givenAFile(String fileName) {
    prepareScenarioWithTestFile(fileName);
    classFinder = new ClassFinder(getDocument(virtualFile));
  }

  private Document getDocument(VirtualFile virtualFile) {
    return FileDocumentManager.getInstance().getDocument(virtualFile);
  }
}


@@ -1822,24 +1822,67 @@
 ds.size());%0A
+    Function plainMethod = methods.get(0);%0A
     assertEq
@@ -1901,30 +1901,27 @@
 ethod%22, 
-methods.get(0)
+plainMethod
 .getName
@@ -1950,26 +1950,23 @@
 %22%22, 
-m
+plainM
 ethod
-s
 .get
-(0).get
 Argu
@@ -1964,36 +1964,121 @@
 d.getArguments()
+);%0A    assertNull(plainMethod.getJsDoc());%0A%0A    Function withArguments = methods.get(1
 );%0A
-%0A
     assertEquals
@@ -2095,30 +2095,29 @@
 ments%22, 
-methods.get(1)
+withArguments
 .getName
@@ -2150,30 +2150,29 @@
 , two%22, 
-methods.get(1)
+withArguments
 .getArgu
@@ -2173,33 +2173,32 @@
 etArguments());%0A
-%0A
     assertEquals
@@ -2187,32 +2187,70 @@
 ;%0A    assert
-Equals(%22
+Null(withArguments.getJsDoc());%0A%0A    Function 
 withManyLine
@@ -2250,18 +2250,18 @@
 anyLines
-%22,
+ =
  methods
@@ -2263,24 +2263,73 @@
 thods.get(2)
+;%0A    assertEquals(%22withManyLines%22, withManyLines
 .getName());
@@ -2358,16 +2358,29 @@
 , two,%5Cn
+%22 +%0A        %22
         
@@ -2393,30 +2393,29 @@
 three%22, 
-methods.get(2)
+withManyLines
 .getArgu
@@ -2420,25 +2420,24 @@
 guments());%0A
-%0A
     assertEq
@@ -2430,32 +2430,70 @@
 ;%0A    assert
-Equals(%22
+Null(withManyLines.getJsDoc());%0A%0A    Function 
 withJsDoc%22, 
@@ -2489,18 +2489,18 @@
 ithJsDoc
-%22,
+ =
  methods
@@ -2502,24 +2502,65 @@
 thods.get(3)
+;%0A    assertEquals(%22withJsDoc%22, withJsDoc
 .getName());
@@ -2600,35 +2600,198 @@
 e%22, 
-methods.get(3).getArguments
+withJsDoc.getArguments());%0A    assertEquals(%22/**%5Cn%22 +%0A        %22 *%5Cn%22 +%0A        %22 * @param one%5Cn%22 +%0A        %22 * @param two%5Cn%22 +%0A        %22 * @param three%5Cn%22 +%0A        %22 */%22, withJsDoc.getJsDoc
 ());

add tests for jsdoc