package jetbrains.mps.make.unittest;
/*Generated by MPS */
import org.junit.runner.RunWith;
import org.jmock.integration.junit4.JMock;
import org.junit.Test;
import jetbrains.mps.make.facet.ITarget;
import jetbrains.mps.internal.make.runtime.script.TargetRange;
import jetbrains.mps.internal.make.runtime.script.Script;
import junit.framework.Assert;
import jetbrains.mps.make.script.IResult;
import jetbrains.mps.progress.EmptyProgressMonitor;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.make.resources.IResource;
import org.jmock.Expectations;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.make.script.IJob;
import jetbrains.mps.make.script.IJobMonitor;
import jetbrains.mps.make.resources.IPropertiesAccessor;
import org.jetbrains.mps.openapi.util.ProgressMonitor;
import jetbrains.mps.make.script.IScriptController;
import jetbrains.mps.make.script.IFeedback;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.jmock.api.Action;
import org.jmock.api.Invocation;
import jetbrains.mps.make.script.IPropertiesPool;
@RunWith(JMock.class)
public class Execute_Test extends MockTestCase {
@Test
public void test_single() throws Exception {
final ITarget make = Mockups.target(context, "make");
Mockups.allowing(context, make);
TargetRange tr = new TargetRange();
tr.addTarget(make);
Script sc = new Script(tr, new ITarget.Name("make"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult res = sc.execute(null, null, new EmptyProgressMonitor());
Assert.assertNotNull(res);
Assert.assertTrue(res.isSucessful());
Assert.assertNotNull(res.output());
Assert.assertTrue(Sequence.fromIterable(res.output()).isEmpty());
}
@Test
public void test_resources() throws Exception {
final ITarget make = Mockups.target(context, "make", new Class[]{IResource.class});
final ITarget res = Mockups.target(context, "res");
final IResource resA = Mockups.resource(context, "resA");
final IResource resB = Mockups.resource(context, "resB");
final IResult result = Mockups.result(context, "result", true);
context.checking(new Expectations() {
{
atLeast(1).of(res).before();
will(Expectations.returnValue(Sequence.fromArray(new ITarget.Name[]{new ITarget.Name("make")})));
atLeast(1).of(res).producesOutput();
will(Expectations.returnValue(true));
exactly(1).of(res).createJob();
will(Expectations.returnValue(Mockups.job(context, "resjob", new _FunctionTypes._return_P0_E0<IResult>() {
public IResult invoke() {
return result;
}
})));
atLeast(1).of(result).output();
will(Expectations.onConsecutiveCalls(Expectations.returnValue(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)), Expectations.returnValue(null)));
exactly(1).of(make).createJob();
IJob makejob = new IJob() {
public IResult execute(Iterable<IResource> input, IJobMonitor mon, IPropertiesAccessor pa, ProgressMonitor progressMonitor) {
Assert.assertTrue(ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)).disjunction(Sequence.fromIterable(input)).isEmpty());
return result;
}
};
will(Expectations.returnValue(makejob));
}
});
Mockups.allowing(context, res);
Mockups.allowing(context, make);
Mockups.allowing(context, result);
TargetRange tr = new TargetRange();
tr.addTarget(make);
tr.addRelated(ListSequence.fromListAndArray(new ArrayList<ITarget>(), res));
Script sc = new Script(tr, new ITarget.Name("make"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult r = sc.execute(null, null, new EmptyProgressMonitor());
Assert.assertNotNull(r);
Assert.assertTrue(r.isSucessful());
Assert.assertTrue(Sequence.fromIterable(r.output()).isEmpty());
}
@Test
public void test_inputResources() throws Exception {
final ITarget make = Mockups.target(context, "make", new Class[]{IResource.class});
final IResource resA = Mockups.resource(context, "resA");
final IResource resB = Mockups.resource(context, "resB");
final IResult result = Mockups.result(context, "result", true);
context.checking(new Expectations() {
{
exactly(1).of(make).createJob();
IJob makejob = new IJob() {
public IResult execute(Iterable<IResource> input, IJobMonitor mon, IPropertiesAccessor pa, ProgressMonitor progressMonitor) {
Assert.assertTrue(ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)).disjunction(Sequence.fromIterable(input)).isEmpty());
return result;
}
};
will(Expectations.returnValue(makejob));
}
});
Mockups.allowing(context, make);
Mockups.allowing(context, result);
TargetRange tr = new TargetRange();
tr.addTarget(make);
Script sc = new Script(tr, new ITarget.Name("make"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult r = sc.execute(null, ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB), new EmptyProgressMonitor());
Assert.assertNotNull(r);
Assert.assertTrue(r.isSucessful());
Assert.assertTrue(Sequence.fromIterable(r.output()).isEmpty());
}
@Test
public void test_transpResources() throws Exception {
final ITarget make = Mockups.target(context, "make", new Class[]{IResource.class});
final ITarget nop = Mockups.target(context, "nop");
final ITarget res = Mockups.target(context, "res");
final IResource resA = Mockups.resource(context, "resA");
final IResource resB = Mockups.resource(context, "resB");
final IResult result = Mockups.result(context, "result", true);
context.checking(new Expectations() {
{
atLeast(1).of(res).before();
will(Expectations.returnValue(Sequence.fromArray(new ITarget.Name[]{new ITarget.Name("make")})));
exactly(1).of(res).createJob();
will(Expectations.returnValue(Mockups.job(context, "resjob", new _FunctionTypes._return_P0_E0<IResult>() {
public IResult invoke() {
return result;
}
})));
atLeast(1).of(res).producesOutput();
will(Expectations.returnValue(true));
atLeast(1).of(result).output();
will(Expectations.returnValue(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)));
atLeast(1).of(nop).before();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("make"))));
atLeast(1).of(nop).after();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("res"))));
exactly(1).of(make).createJob();
IJob makejob = new IJob() {
public IResult execute(Iterable<IResource> input, IJobMonitor mon, IPropertiesAccessor pa, ProgressMonitor progressMonitor) {
Assert.assertTrue(ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)).disjunction(Sequence.fromIterable(input)).isEmpty());
return result;
}
};
will(Expectations.returnValue(makejob));
}
});
Mockups.allowing(context, res);
Mockups.allowing(context, nop);
Mockups.allowing(context, make);
Mockups.allowing(context, result);
TargetRange tr = new TargetRange();
tr.addTarget(make);
tr.addRelated(ListSequence.fromListAndArray(new ArrayList<ITarget>(), res, nop));
Script sc = new Script(tr, new ITarget.Name("make"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult r = sc.execute(null, null, new EmptyProgressMonitor());
Assert.assertNotNull(r);
Assert.assertTrue(r.isSucessful());
Assert.assertTrue(Sequence.fromIterable(r.output()).isEmpty());
}
@Test
public void test_noDuplicateResources() throws Exception {
final ITarget make = Mockups.target(context, "make", new Class[]{IResource.class});
final ITarget nop = Mockups.target(context, "nop");
final ITarget nop2 = Mockups.target(context, "nop2");
final ITarget dup = Mockups.target(context, "dup");
final ITarget res = Mockups.target(context, "res");
final IResource resA = Mockups.resource(context, "resA");
final IResource resB = Mockups.resource(context, "resB");
final IResult result = Mockups.result(context, "result", true);
context.checking(new Expectations() {
{
atLeast(1).of(res).before();
will(Expectations.returnValue(Sequence.fromArray(new ITarget.Name[]{new ITarget.Name("make")})));
exactly(1).of(res).createJob();
will(Expectations.returnValue(Mockups.job(context, "resjob", new _FunctionTypes._return_P0_E0<IResult>() {
public IResult invoke() {
return result;
}
})));
atLeast(1).of(res).producesOutput();
will(Expectations.returnValue(true));
atLeast(1).of(result).output();
will(Expectations.returnValue(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)));
atLeast(1).of(nop).before();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("make"))));
atLeast(1).of(nop).after();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("res"))));
atLeast(1).of(nop2).before();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("make"))));
atLeast(1).of(nop2).after();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("res"))));
atLeast(1).of(dup).before();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("make"))));
atLeast(1).of(dup).after();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("res"))));
exactly(1).of(dup).createJob();
will(Expectations.returnValue(Mockups.job(context, "resjob2", new _FunctionTypes._return_P0_E0<IResult>() {
public IResult invoke() {
return result;
}
})));
atLeast(1).of(dup).producesOutput();
will(Expectations.returnValue(true));
exactly(1).of(make).createJob();
IJob makejob = new IJob() {
public IResult execute(Iterable<IResource> input, IJobMonitor mon, IPropertiesAccessor pa, ProgressMonitor progressMonitor) {
Assert.assertTrue(ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)).disjunction(Sequence.fromIterable(input)).isEmpty());
return result;
}
};
will(Expectations.returnValue(makejob));
}
});
Mockups.allowing(context, res);
Mockups.allowing(context, nop);
Mockups.allowing(context, nop2);
Mockups.allowing(context, dup);
Mockups.allowing(context, make);
Mockups.allowing(context, result);
TargetRange tr = new TargetRange();
tr.addTarget(make);
tr.addRelated(ListSequence.fromListAndArray(new ArrayList<ITarget>(), res, nop, nop2, dup));
Script sc = new Script(tr, new ITarget.Name("make"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult r = sc.execute(null, null, new EmptyProgressMonitor());
Assert.assertNotNull(r);
Assert.assertTrue(r.isSucessful());
Assert.assertTrue(Sequence.fromIterable(r.output()).isEmpty());
}
@Test
public void test_resultResources() throws Exception {
final ITarget nop = Mockups.target(context, "nop");
final ITarget res = Mockups.target(context, "res");
final IResource resA = Mockups.resource(context, "resA");
final IResource resB = Mockups.resource(context, "resB");
final IResult result = Mockups.result(context, "result", true);
context.checking(new Expectations() {
{
exactly(1).of(res).createJob();
will(Expectations.returnValue(Mockups.job(context, "resjob", new _FunctionTypes._return_P0_E0<IResult>() {
public IResult invoke() {
return result;
}
})));
atLeast(1).of(res).producesOutput();
will(Expectations.returnValue(true));
atLeast(1).of(result).output();
will(Expectations.returnValue(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)));
atLeast(1).of(nop).after();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("res"))));
}
});
Mockups.allowing(context, res);
Mockups.allowing(context, nop);
Mockups.allowing(context, result);
TargetRange tr = new TargetRange();
tr.addTarget(nop);
tr.addRelated(ListSequence.fromListAndArray(new ArrayList<ITarget>(), res));
Script sc = new Script(tr, new ITarget.Name("nop"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult r = sc.execute(null, null, new EmptyProgressMonitor());
Assert.assertNotNull(r);
Assert.assertTrue(r.isSucessful());
Assert.assertTrue(ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)).disjunction(Sequence.fromIterable(r.output())).isEmpty());
}
@Test
public void test_fail() throws Exception {
final ITarget make = Mockups.target(context, "make");
final ITarget gen = Mockups.target(context, "gen", new Class[]{IResource.class});
final ITarget res = Mockups.target(context, "res");
final IResource resA = Mockups.resource(context, "resA");
final IResource resB = Mockups.resource(context, "resB");
final IResult okresult = Mockups.result(context, "okresult", true);
final IResult failresult = Mockups.result(context, "failresult", false);
final IScriptController mons = Mockups.monitors(context, "mons");
final IJobMonitor jmon = context.mock(IJobMonitor.class);
final IFeedback[] fbk = new IFeedback[1];
context.checking(new Expectations() {
{
final Object[] job = new Object[1];
exactly(1).of(mons).runJobWithMonitor((_FunctionTypes._void_P1_E0<? super IJobMonitor>) with(new BaseMatcher<Object>() {
@Override
public boolean matches(Object o) {
job[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 IJobMonitor>) job[0]).invoke(jmon);
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()));
atLeast(1).of(mons).useMonitor(with(Expectations.aNonNull(ProgressMonitor.class)));
exactly(1).of(jmon).reportFeedback(with(new BaseMatcher<IFeedback>() {
@Override
public boolean matches(Object o) {
if (o instanceof IFeedback.ERROR) {
fbk[0] = (IFeedback) o;
return true;
}
return false;
}
@Override
public void describeTo(Description p0) {
}
}));
exactly(1).of(mons).useMonitor(with(Expectations.aNull(ProgressMonitor.class)));
atLeast(1).of(res).before();
will(Expectations.returnValue(Sequence.fromArray(new ITarget.Name[]{new ITarget.Name("make")})));
atLeast(1).of(res).producesOutput();
will(Expectations.returnValue(true));
exactly(1).of(res).createJob();
will(Expectations.returnValue(Mockups.job(context, "resjob", new _FunctionTypes._return_P0_E0<IResult>() {
public IResult invoke() {
return okresult;
}
})));
atLeast(1).of(okresult).output();
will(Expectations.returnValue(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)));
atLeast(1).of(gen).before();
will(Expectations.returnValue(Sequence.fromArray(new ITarget.Name[]{new ITarget.Name("make")})));
atLeast(1).of(gen).after();
will(Expectations.returnValue(Sequence.fromArray(new ITarget.Name[]{new ITarget.Name("res")})));
IJob genjob = new IJob() {
public IResult execute(Iterable<IResource> input, IJobMonitor mon, IPropertiesAccessor pa, ProgressMonitor progressMonitor) {
Assert.assertTrue(ListSequence.fromList(ListSequence.fromListAndArray(new ArrayList<IResource>(), resA, resB)).disjunction(Sequence.fromIterable(input)).isEmpty());
return failresult;
}
};
exactly(1).of(gen).createJob();
will(Expectations.returnValue(genjob));
never(make).createJob();
}
});
Mockups.allowing(context, res);
Mockups.allowing(context, gen);
Mockups.allowing(context, make);
Mockups.allowing(context, okresult);
Mockups.allowing(context, failresult);
Mockups.allowing(context, mons);
Mockups.allowing(context, jmon);
TargetRange tr = new TargetRange();
tr.addTarget(make);
tr.addRelated(ListSequence.fromListAndArray(new ArrayList<ITarget>(), gen, res));
Script sc = new Script(tr, new ITarget.Name("make"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult r = sc.execute(mons, null, new EmptyProgressMonitor());
Assert.assertNotNull(r);
Assert.assertFalse(r.isSucessful());
Assert.assertTrue(Sequence.fromIterable(r.output()).isEmpty());
Assert.assertTrue(fbk[0] instanceof IFeedback.ERROR);
}
@Test
public void test_variables() throws Exception {
final ITarget make = Mockups.target(context, "make");
final ITarget config = Mockups.target(context, "config");
final IJobMonitor monit = Mockups.monitor(context, "mon");
final String[] vars = new String[1];
context.checking(new Expectations() {
{
atLeast(1).of(config).before();
will(Expectations.returnValue(Sequence.<ITarget.Name>singleton(new ITarget.Name("make"))));
exactly(1).of(config).createJob();
IJob cj = new IJob() {
public IResult execute(Iterable<IResource> res, IJobMonitor mon, IPropertiesAccessor pa, ProgressMonitor progressMonitor) {
String[] arr = pa.global().properties(new ITarget.Name("make"), (new String[0]).getClass());
arr[0] = "BARFOO";
return new IResult.SUCCESS(res);
}
};
will(Expectations.returnValue(cj));
exactly(1).of(make).createParameters((Class<?>) with(Expectations.aNonNull(Class.class)));
will(Expectations.returnValue(vars));
exactly(1).of(make).createJob();
IJob mj = new IJob() {
public IResult execute(Iterable<IResource> res, IJobMonitor mon, IPropertiesAccessor pa, ProgressMonitor progressMonitor) {
String[] arr = pa.global().properties(new ITarget.Name("make"), (new String[0]).getClass());
Assert.assertEquals("BARFOO", arr[0]);
arr[0] = "FUBAR";
return new IResult.SUCCESS(res);
}
};
will(Expectations.returnValue(mj));
}
});
Mockups.allowing(context, make);
Mockups.allowing(context, config);
TargetRange tr = new TargetRange();
tr.addTarget(make);
tr.addTarget(config);
Script sc = new Script(tr, new ITarget.Name("make"));
sc.validate();
Assert.assertTrue(sc.isValid());
IResult res = sc.execute(null, null, new EmptyProgressMonitor());
Assert.assertNotNull(res);
Assert.assertTrue(res.isSucessful());
Assert.assertTrue(Sequence.fromIterable(res.output()).isEmpty());
Assert.assertNotNull(res.output());
Assert.assertEquals("FUBAR", vars[0]);
}
public Execute_Test() {
}
}