package tora;

import gw.config.CommonServices;
import gw.lang.parser.*;
import gw.util.GosuExceptionUtil;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.script.ScriptException;

import java.util.HashMap;

import static junit.framework.TestCase.assertEquals;

public class ImportingJavaClassTest
{

  public static class JavaClass {
    public static int staticFoo() {
      return 42;
    }

    public String returnHello() {
      return "hello";
    }
  }

  @BeforeClass
  public static void beforeClass() {
    Utils.maybeInit();
  }

  @Test
  public void testJavaClassImport() throws ScriptException {
    assertEquals(42, eval("return ImportClass.javaFoo()"));
    assertEquals("hello", eval("var imported = new ImportClass(); return imported.javaBar()"));
  }

  @Test
  public void testGosuClassImport() throws ScriptException {
    assertEquals(43, eval("return ImportClass.gosuFoo()"));
    assertEquals(70, eval("var imported = new ImportClass(); return imported.gosuDouble (35)"));
  }

  @Test
  public void testImportsInJavascriptProgram() throws ScriptException {
    assertEquals(43, eval("return ImportProgram.gosuFoo()"));
    assertEquals(42, eval("return ImportProgram.javaFoo()"));
  }


  private Object eval( String program )
  {
    IGosuProgramParser programParser = CommonServices.getGosuParserFactory().createProgramParser();
    try
    {
      IParseResult iParseResult = programParser.parseExpressionOrProgram( program, new StandardSymbolTable( true ), new ParserOptions() );
      return iParseResult.getProgram().evaluate( new ExternalSymbolMapForMap( new HashMap<>() ) );
    }
    catch( Exception e )
    {
      throw GosuExceptionUtil.forceThrow( e );
    }
  }

}

@@ -67,9 +67,189 @@
 ser.
-*
+ExternalSymbolMapForMap;%0Aimport gw.lang.parser.IGosuProgramParser;%0Aimport gw.lang.parser.IParseResult;%0Aimport gw.lang.parser.ParserOptions;%0Aimport gw.lang.parser.StandardSymbolTable
 ;%0Aim
@@ -284,39 +284,8 @@
 il;%0A
-import junit.framework.Assert;%0A
 impo
@@ -371,17 +371,16 @@
 eption;%0A
-%0A
 import j
@@ -724,32 +724,146 @@
 ybeInit();%0A  %7D%0A%0A
+  @Test%0A  public void testEval() throws ScriptException %7B%0A    assertEquals(%22foo%22, eval(%22return %5C%22foo%5C%22%22));%0A  %7D%0A  %0A
   @Test%0A  public
@@ -1515,14 +1515,13 @@
 ));%0A
+
   %7D%0A%0A
-%0A
   pr

Fix test(?!)