package jetbrains.mps.tool.gentest.plugin; /*Generated by MPS */ import org.jetbrains.mps.openapi.model.SModel; import java.util.Map; import jetbrains.mps.baseLanguage.tuples.runtime.Tuples; import org.jetbrains.mps.openapi.model.SNode; import jetbrains.mps.internal.collections.runtime.MapSequence; import java.util.HashMap; import jetbrains.mps.internal.collections.runtime.Sequence; import jetbrains.mps.internal.collections.runtime.ITranslator2; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations; import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory; import jetbrains.mps.internal.collections.runtime.IWhereFilter; import jetbrains.mps.internal.collections.runtime.ISelector; import jetbrains.mps.lang.core.behavior.INamedConcept__BehaviorDescriptor; import jetbrains.mps.baseLanguage.behavior.Classifier__BehaviorDescriptor; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations; import jetbrains.mps.internal.collections.runtime.ListSequence; import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes; import jetbrains.mps.baseLanguage.tuples.runtime.MultiTuple; import java.util.List; import jetbrains.mps.baseLanguage.scopes.ClassifiersScope; import org.jetbrains.mps.openapi.persistence.PersistenceFacade; import jetbrains.mps.smodel.SModelUtil_new; import jetbrains.mps.smodel.SReference; public class TestCollector { private static final String TEST_CASE = "TEST_CASE"; private static final String TEST_ANN = "TEST_ANN"; private static final String MPS_LAUNCH_ANN = "MPS_LAUNCH_ANN"; private static final String IGNORE_ANN = "IGNORE_ANN"; private Iterable<SModel> models; private Map<Tuples._2<String, String>, SNode> nodeCache = MapSequence.fromMap(new HashMap<Tuples._2<String, String>, SNode>()); public TestCollector(Iterable<SModel> models) { this.models = models; } public Iterable<String> collectTests() { return Sequence.fromIterable(models).translate(new ITranslator2<SModel, SNode>() { public Iterable<SNode> translate(SModel model) { return SModelOperations.roots(model, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c108ca66L, "jetbrains.mps.baseLanguage.structure.ClassConcept")); } }).where(new IWhereFilter<SNode>() { public boolean accept(SNode cls) { return isJunit38Test(cls) || isJUnit4Test(cls); } }).select(new ISelector<SNode, String>() { public String select(SNode cls) { return (String) INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(cls); } }); } private boolean isJunit38Test(SNode maybetest) { boolean isdesc = (boolean) Classifier__BehaviorDescriptor.isDescendant_id6dL7A1DpKo1.invoke(maybetest, getTestCaseClass(maybetest)); boolean hasann = hasAnnotation(maybetest, getIgnoreAnnotation(maybetest), getMPSLaunchAnnotation(maybetest)); return isdesc && !(hasann) && hasJUnit38TestMethods(maybetest); } private boolean hasJUnit38TestMethods(final SNode maybetest) { return Sequence.fromIterable(Classifier__BehaviorDescriptor.methods_id4_LVZ3pBKCn.invoke(maybetest)).where(new IWhereFilter<SNode>() { public boolean accept(SNode meth) { return "public".equals(SConceptOperations.conceptAlias(SNodeOperations.getConcept(SLinkOperations.getTarget(meth, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x112670d273fL, 0x112670d886aL, "visibility"))))) && SPropertyOperations.getString(meth, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name")).startsWith("test"); } }).any(new IWhereFilter<SNode>() { public boolean accept(SNode meth) { return !(hasAnnotation(meth, getIgnoreAnnotation(maybetest))); } }); } private boolean isJUnit4Test(SNode maybetest) { return !(hasAnnotation(getIgnoreAnnotation(maybetest), getMPSLaunchAnnotation(maybetest))) && hasJUnit4TestMethods(maybetest); } private boolean hasJUnit4TestMethods(final SNode maybetest) { return Sequence.fromIterable(Classifier__BehaviorDescriptor.methods_id4_LVZ3pBKCn.invoke(maybetest)).where(new IWhereFilter<SNode>() { public boolean accept(SNode meth) { return "public".equals(SConceptOperations.conceptAlias(SNodeOperations.getConcept(SLinkOperations.getTarget(meth, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x112670d273fL, 0x112670d886aL, "visibility"))))) && hasAnnotation(meth, getTestAnnotation(maybetest)); } }).any(new IWhereFilter<SNode>() { public boolean accept(SNode meth) { return !(hasAnnotation(meth, getIgnoreAnnotation(maybetest))); } }); } private boolean hasAnnotation(final SNode hasann, SNode... annotation) { return Sequence.fromIterable(Sequence.fromArray(annotation)).any(new IWhereFilter<SNode>() { public boolean accept(final SNode ann) { return ListSequence.fromList(SLinkOperations.getChildren(hasann, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6be947aL, 0x114a6beb0bdL, "annotation"))).any(new IWhereFilter<SNode>() { public boolean accept(SNode anninst) { return SLinkOperations.getTarget(anninst, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a6b4ccabL, 0x114a6b85d40L, "annotation")) == ann; } }); } }); } private SNode getTestCaseClass(final SNode maybetest) { return SNodeOperations.cast(getNode(SNodeOperations.getModel(maybetest), TEST_CASE, new _FunctionTypes._return_P0_E0<SNode>() { public SNode invoke() { final String testCase = INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(SLinkOperations.getTarget(_quotation_createNode_rpw6il_a0a0a0c0a0a31(), MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"))); return SNodeOperations.cast(ListSequence.fromList(TestCollector.this.allClassifiers(maybetest)).findFirst(new IWhereFilter<SNode>() { public boolean accept(SNode cls) { return INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(cls).equals(testCase); } }), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c108ca66L, "jetbrains.mps.baseLanguage.structure.ClassConcept")); } }), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c108ca66L, "jetbrains.mps.baseLanguage.structure.ClassConcept")); } private SNode getTestAnnotation(final SNode maybetest) { return SNodeOperations.cast(getNode(SNodeOperations.getModel(maybetest), TEST_ANN, new _FunctionTypes._return_P0_E0<SNode>() { public SNode invoke() { final String testAnn = INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(SLinkOperations.getTarget(_quotation_createNode_rpw6il_a0a0a0c0a0a41(), MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"))); return ListSequence.fromList(TestCollector.this.allAnnotations(maybetest)).findFirst(new IWhereFilter<SNode>() { public boolean accept(SNode ann) { return INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(ann).equals(testAnn); } }); } }), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a69dc80cL, "jetbrains.mps.baseLanguage.structure.Annotation")); } private SNode getIgnoreAnnotation(final SNode maybetest) { return SNodeOperations.cast(getNode(SNodeOperations.getModel(maybetest), IGNORE_ANN, new _FunctionTypes._return_P0_E0<SNode>() { public SNode invoke() { final String ignoreAnn = INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(SLinkOperations.getTarget(_quotation_createNode_rpw6il_a0a0a0c0a0a51(), MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"))); return ListSequence.fromList(TestCollector.this.allAnnotations(maybetest)).findFirst(new IWhereFilter<SNode>() { public boolean accept(SNode ann) { return INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(ann).equals(ignoreAnn); } }); } }), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a69dc80cL, "jetbrains.mps.baseLanguage.structure.Annotation")); } private SNode getMPSLaunchAnnotation(final SNode maybetest) { return SNodeOperations.cast(getNode(SNodeOperations.getModel(maybetest), MPS_LAUNCH_ANN, new _FunctionTypes._return_P0_E0<SNode>() { public SNode invoke() { final String mpslaunchAnn = INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(SLinkOperations.getTarget(_quotation_createNode_rpw6il_a0a0a0c0a0a61(), MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"))); return ListSequence.fromList(TestCollector.this.allAnnotations(maybetest)).findFirst(new IWhereFilter<SNode>() { public boolean accept(SNode ann) { return INamedConcept__BehaviorDescriptor.getFqName_idhEwIO9y.invoke(ann).equals(mpslaunchAnn); } }); } }), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a69dc80cL, "jetbrains.mps.baseLanguage.structure.Annotation")); } private SNode getNode(SModel context, String kind, _FunctionTypes._return_P0_E0<? extends SNode> getter) { SNode node = MapSequence.fromMap(nodeCache).get(MultiTuple.<String,String>from(context.getReference().getModelName(), kind)); if (node == null) { node = getter.invoke(); MapSequence.fromMap(nodeCache).put(MultiTuple.<String,String>from(context.getReference().getModelName(), kind), node); } return node; } protected List<SNode> allClassifiers(SNode maybetest) { Iterable<SNode> result = Sequence.fromIterable(new ClassifiersScope(SNodeOperations.getModel(maybetest), maybetest, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, "jetbrains.mps.baseLanguage.structure.Classifier"), true).getAvailableElements("")).select(new ISelector<SNode, SNode>() { public SNode select(SNode it) { return SNodeOperations.cast(it, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, "jetbrains.mps.baseLanguage.structure.Classifier")); } }); return Sequence.fromIterable(result).toListSequence(); } protected List<SNode> allAnnotations(SNode maybetest) { Iterable<SNode> result = Sequence.fromIterable(new ClassifiersScope(SNodeOperations.getModel(maybetest), maybetest, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a69dc80cL, "jetbrains.mps.baseLanguage.structure.Annotation"), true).getAvailableElements("")).select(new ISelector<SNode, SNode>() { public SNode select(SNode it) { return SNodeOperations.cast(it, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x114a69dc80cL, "jetbrains.mps.baseLanguage.structure.Annotation")); } }); return Sequence.fromIterable(result).toListSequence(); } private static SNode _quotation_createNode_rpw6il_a0a0a0c0a0a31() { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode quotedNode_1 = null; quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x101de48bf9eL, "ClassifierType"), null, null, false); quotedNode_1.setReference(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), SReference.create(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), quotedNode_1, facade.createModelReference("83f155ff-422c-4b5a-a2f2-b459302dd215/java:junit.framework(jetbrains.mps.baseLanguage.unitTest.libs/)"), facade.createNodeId("~TestCase"))); return quotedNode_1; } private static SNode _quotation_createNode_rpw6il_a0a0a0c0a0a41() { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode quotedNode_1 = null; quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x101de48bf9eL, "ClassifierType"), null, null, false); quotedNode_1.setReference(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), SReference.create(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), quotedNode_1, facade.createModelReference("83f155ff-422c-4b5a-a2f2-b459302dd215/java:org.junit(jetbrains.mps.baseLanguage.unitTest.libs/)"), facade.createNodeId("~Test"))); return quotedNode_1; } private static SNode _quotation_createNode_rpw6il_a0a0a0c0a0a51() { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode quotedNode_1 = null; quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x101de48bf9eL, "ClassifierType"), null, null, false); quotedNode_1.setReference(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), SReference.create(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), quotedNode_1, facade.createModelReference("83f155ff-422c-4b5a-a2f2-b459302dd215/java:org.junit(jetbrains.mps.baseLanguage.unitTest.libs/)"), facade.createNodeId("~Ignore"))); return quotedNode_1; } private static SNode _quotation_createNode_rpw6il_a0a0a0c0a0a61() { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode quotedNode_1 = null; quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x101de48bf9eL, "ClassifierType"), null, null, false); quotedNode_1.setReference(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), SReference.create(MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"), quotedNode_1, facade.createModelReference("920eaa0e-ecca-46bc-bee7-4e5c59213dd6/java:jetbrains.mps(Testbench/)"), facade.createNodeId("~MPSLaunch"))); return quotedNode_1; } }