package jetbrains.mps.make.generator;

/*Generated by MPS */

import jetbrains.mps.make.unittest.MockTestCase;
import jetbrains.mps.make.facet.IFacetManifest;
import jetbrains.mps.make.facet.FacetRegistry;
import org.junit.Test;
import jetbrains.mps.make.script.ScriptBuilder;
import jetbrains.mps.make.script.IScriptController;
import jetbrains.mps.make.unittest.Mockups;
import jetbrains.mps.make.script.IConfigMonitor;
import org.jmock.Expectations;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.jmock.api.Action;
import org.jmock.api.Invocation;
import jetbrains.mps.make.script.IPropertiesPool;
import jetbrains.mps.make.script.IFeedback;
import jetbrains.mps.make.script.IQuery;
import jetbrains.mps.make.script.IOption;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.make.script.IScript;
import jetbrains.mps.make.facet.IFacet;
import jetbrains.mps.make.facet.ITarget;
import org.junit.Assert;
import jetbrains.mps.make.script.IResult;
import jetbrains.mps.progress.EmptyProgressMonitor;
import org.junit.Before;
import java.lang.reflect.Constructor;
import org.junit.After;

public class Generator_Test extends MockTestCase {
  private IFacetManifest manifest;
  private FacetRegistry myFacetRegistry;
  @Test
  public void test_queryStop() throws Exception {
    ScriptBuilder scb = new ScriptBuilder(myFacetRegistry);
    final IScriptController mons = Mockups.monitors(context, "mons");
    final IConfigMonitor cmon = context.mock(IConfigMonitor.class);
    context.checking(new Expectations() {
      {
        final Object[] cfg = new Object[1];
        exactly(1).of(mons).runConfigWithMonitor((_FunctionTypes._void_P1_E0<? super IConfigMonitor>) with(new BaseMatcher<Object>() {
          @Override
          public boolean matches(Object o) {
            cfg[0] = o;
            return true;
          }
          @Override
          public void describeTo(Description p0) {
          }
        }));
        will(new Action() {
          @Override
          public Object invoke(Invocation invocation) throws Throwable {
            ((_FunctionTypes._void_P1_E0<? super IConfigMonitor>) cfg[0]).invoke(cmon);
            return null;
          }
          @Override
          public void describeTo(Description description) {
          }
        });
        exactly(1).of(mons).setup(with(Expectations.aNonNull(IPropertiesPool.class)), with(Expectations.aNonNull(Iterable.class)), with(Expectations.<Iterable>anything()));
        exactly(1).of(cmon).reportFeedback(with(Expectations.aNonNull(IFeedback.class)));

        final IQuery[] query = new IQuery[1];
        exactly(1).of(cmon).relayQuery(with(new BaseMatcher<IQuery<IOption>>() {
          @Override
          public boolean matches(Object o) {
            if (o instanceof IQuery) {
              query[0] = (IQuery) o;
              return true;
            }
            return false;
          }
          @Override
          public void describeTo(Description p0) {
          }
        }));
        will(new Action() {
          @Override
          public Object invoke(Invocation invocation) throws Throwable {
            return Sequence.fromIterable(query[0].options()).last();
          }
          @Override
          public void describeTo(Description description) {
          }
        });
      }
    });
    Mockups.allowing(context, mons);

    IScript scr = scb.withFacetName(new IFacet.Name("jetbrains.mps.make.tests.Maker_")).withFacetName(new IFacet.Name("jetbrains.mps.make.tests.Generator_")).withFacetName(new IFacet.Name("jetbrains.mps.make.tests.TextGen_")).withFinalTarget(new ITarget.Name("jetbrains.mps.make.tests.Maker_.Make")).toScript();

    Assert.assertTrue(scr.isValid());
    ITarget dt = scr.finalTarget();
    Assert.assertNotNull(dt);
    Assert.assertEquals(new ITarget.Name("jetbrains.mps.make.tests.Maker_.Make"), dt.getName());
    IResult res = scr.execute(mons, null, new EmptyProgressMonitor());
    Assert.assertNotNull(res);
    Assert.assertFalse(res.isSucessful());
    Assert.assertTrue(Sequence.fromIterable(res.output()).isEmpty());
  }
  @Before
  public void setUp() throws Exception {
    Class<?> mf = Class.forName(Generator_Test.class.getPackage().getName() + ".FacetManifest");
    Constructor<?> ctor = mf.getConstructor();
    Object inst = ctor.newInstance();
    this.manifest = (IFacetManifest) inst;
    myFacetRegistry = new FacetRegistry(null);
    myFacetRegistry.init();
    registerFacets(manifest);
  }
  @After
  public void tearDown() throws Exception {
    unregisterFacets(manifest);
    myFacetRegistry.dispose();
  }
  private void registerFacets(IFacetManifest fm) {
    for (IFacet fct : fm.facets()) {
      myFacetRegistry.register(fct);
    }
  }
  private void unregisterFacets(IFacetManifest fm) {
    for (IFacet fct : fm.facets()) {
      myFacetRegistry.unregister(fct);
    }
  }
}
