package com.chrisfolger.needsmoredojo.core.refactoring;

import com.chrisfolger.needsmoredojo.testutil.BasicPsiElements;
import com.chrisfolger.needsmoredojo.testutil.MockJSArrayLiteralExpression;
import com.chrisfolger.needsmoredojo.testutil.MockJSCallExpression;
import com.chrisfolger.needsmoredojo.testutil.MockJSObjectLiteralExpression;
import com.intellij.lang.javascript.psi.JSCallExpression;
import com.intellij.lang.javascript.psi.JSExpression;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class TestUtilConverter
{
    private UtilConverter converter;

    @Before
    public void setup()
    {
        converter = new UtilConverter();
    }

    @Test
    public void testTheBasicHappyBath()
    {
        Map<String, String> propertyMap = new HashMap<String, String>();
        propertyMap.put("property 1", "value");

        JSExpression[] arguments = new JSExpression[] {
                new MockJSArrayLiteralExpression(new String[] { "mixin1", "mixin2"}),
                new MockJSObjectLiteralExpression(propertyMap)
        };

        JSCallExpression callExpression = new MockJSCallExpression(arguments);
        Object[] statements = new Object[] {callExpression, null};

        UtilConverter.UtilItem result = converter.getDeclareStatementFromParsedStatement(statements);
        assertEquals(2, result.getExpressionsToMixin().length);
        assertEquals(1, result.getMethodsToConvert().length);
    }

    @Test
    public void testWhenFirstArgumentIsNull()
    {
        Map<String, String> propertyMap = new HashMap<String, String>();
        propertyMap.put("property 1", "value");

        JSExpression[] arguments = new JSExpression[] {
                BasicPsiElements.Null(),
                new MockJSObjectLiteralExpression(propertyMap)
        };

        JSCallExpression callExpression = new MockJSCallExpression(arguments);
        Object[] statements = new Object[] {callExpression, null};

        UtilConverter.UtilItem result = converter.getDeclareStatementFromParsedStatement(statements);
        assertEquals(0, result.getExpressionsToMixin().length);
        assertEquals(1, result.getMethodsToConvert().length);
    }
}

@@ -100,245 +100,9 @@
 til.
-BasicPsiElements;%0Aimport com.chrisfolger.needsmoredojo.testutil.MockJSArrayLiteralExpression;%0Aimport com.chrisfolger.needsmoredojo.testutil.MockJSCallExpression;%0Aimport com.chrisfolger.needsmoredojo.testutil.MockJSObjectLiteralExpression
+*
 ;%0Aim
@@ -2029,11 +2029,863 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testWhenFirstArgumentIsAClassName()%0A    %7B%0A        Map%3CString, String%3E propertyMap = new HashMap%3CString, String%3E();%0A        propertyMap.put(%22property 1%22, %22value%22);%0A%0A        JSExpression%5B%5D arguments = new JSExpression%5B%5D %7B%0A                new MockJSLiteralExpression(%22test class%22),%0A                new MockJSArrayLiteralExpression(new String%5B%5D %7B %22define 1%22, %22define 2%22%7D),%0A                new MockJSObjectLiteralExpression(propertyMap)%0A        %7D;%0A%0A        JSCallExpression callExpression = new MockJSCallExpression(arguments);%0A        Object%5B%5D statements = new Object%5B%5D %7BcallExpression, null%7D;%0A%0A        UtilConverter.UtilItem result = converter.getDeclareStatementFromParsedStatement(statements);%0A        assertEquals(2, result.getExpressionsToMixin().length);%0A        assertEquals(1, result.getMethodsToConvert().length);%0A    %7D
 %0A%7D%0A

add test to catch issue when using declare with a declared class