package jetbrains.mps.testbench.junit.suites; /*Generated by MPS */ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.List; import org.jetbrains.mps.openapi.model.SNode; import java.lang.reflect.InvocationTargetException; import jetbrains.mps.project.Project; import jetbrains.mps.smodel.ModelAccess; import jetbrains.mps.util.Computable; import jetbrains.mps.internal.collections.runtime.ListSequence; import java.util.ArrayList; import org.jetbrains.mps.openapi.model.SModel; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations; import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory; import org.jetbrains.mps.openapi.language.SAbstractConcept; import jetbrains.mps.internal.collections.runtime.ISelector; import jetbrains.mps.tool.environment.Environment; import jetbrains.mps.tool.environment.MpsEnvironment; import jetbrains.mps.tool.environment.EnvironmentConfig; import jetbrains.mps.testbench.junit.runners.FromDirWithModulesProjectStrategy; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations; import jetbrains.mps.scope.Scope; import org.junit.Test; import org.jetbrains.mps.openapi.model.SReference; import jetbrains.mps.smodel.constraints.ModelConstraints; import jetbrains.mps.util.CollectionUtil; import junit.framework.Assert; /** * todo: extract common part with BaseCheckModulesTest */ @RunWith(Parameterized.class) public class ScopesTest { @Parameterized.Parameters public static List<SNode[]> getNodesToCheck() throws InvocationTargetException, InterruptedException { final Project project = initTestEnvironmentAndLoadContextProject(); return ModelAccess.instance().runReadAction(new Computable<List<SNode[]>>() { public List<SNode[]> compute() { List<SNode[]> nodesToCheck = ListSequence.fromList(new ArrayList<SNode[]>()); for (SModel model : project.getProjectModels()) { for (SNode root : model.getRootNodes()) { // todo: use fast nodes finder here ListSequence.fromList(nodesToCheck).addSequence(ListSequence.fromList(SNodeOperations.getNodeDescendants(root, MetaAdapterFactory.getConcept(0x8585453e6bfb4d80L, 0x98deb16074f1d86cL, 0x7181d929c720809L, "jetbrains.mps.lang.test.structure.ScopesTest"), true, new SAbstractConcept[]{})).select(new ISelector<SNode, SNode[]>() { public SNode[] select(SNode it) { return new SNode[]{it}; } })); } } return nodesToCheck; } }); } public static Project initTestEnvironmentAndLoadContextProject() throws InvocationTargetException, InterruptedException { Environment env = MpsEnvironment.getOrCreate(EnvironmentConfig.defaultConfig()); return env.createProject(new FromDirWithModulesProjectStrategy()); } private SNode myNode; public ScopesTest(SNode node) { myNode = node; } public List<SNode> getExpectedNodes(SNode forNode) { List<SNode> expected = new ArrayList<SNode>(); for (SNode child : SLinkOperations.getChildren(forNode, MetaAdapterFactory.getContainmentLink(0x8585453e6bfb4d80L, 0x98deb16074f1d86cL, 0x7181d929c720809L, 0x32ba5b0ec25fea03L, "nodes"))) { expected.add(SLinkOperations.getTarget(child, MetaAdapterFactory.getReferenceLink(0x8585453e6bfb4d80L, 0x98deb16074f1d86cL, 0x32ba5b0ec25fe9f3L, 0x383e5e55de89bc1fL, "ref"))); } return expected; } public List<SNode> getScopeNodes(Scope scope) { List<SNode> scopeSet = new ArrayList<SNode>(); for (SNode node : scope.getAvailableElements(null)) { scopeSet.add(node); } return scopeSet; } public StringBuilder getFailMessage(List<SNode> unExpected, List<SNode> notFounded) { StringBuilder builder = new StringBuilder(System.getProperty("line.separator")); builder.append("\tIn node " + SLinkOperations.getTarget(myNode, MetaAdapterFactory.getReferenceLink(0x8585453e6bfb4d80L, 0x98deb16074f1d86cL, 0x7181d929c720809L, 0x4b9f88d62c795596L, "checkingReference"))); builder.append(System.getProperty("line.separator")); if (!(unExpected.isEmpty())) { builder.append("\t\tUnexpected scope elements:"); builder.append(System.getProperty("line.separator")); for (SNode node : unExpected) { builder.append("\t\t\t"); builder.append(node); builder.append(System.getProperty("line.separator")); } } if (!(notFounded.isEmpty())) { builder.append("\t\tNot founded scope elements:"); builder.append(System.getProperty("line.separator")); for (SNode node : notFounded) { builder.append("\t\t\t"); builder.append(node); builder.append(System.getProperty("line.separator")); } } builder.append(System.getProperty("line.separator")); return builder; } @Test public void test() { ModelAccess.instance().runReadAction(new Runnable() { @Override public void run() { SReference reference = null; for (SReference ref : SNodeOperations.getReferences(SNodeOperations.getParent(ScopesTest.this.myNode))) { if (SLinkOperations.getTargetNode(ref) == SLinkOperations.getTarget(ScopesTest.this.myNode, MetaAdapterFactory.getReferenceLink(0x8585453e6bfb4d80L, 0x98deb16074f1d86cL, 0x7181d929c720809L, 0x4b9f88d62c795596L, "checkingReference"))) { reference = ref; break; } } Scope scope = ModelConstraints.getScope(reference); List<SNode> expected = ScopesTest.this.getExpectedNodes(myNode); List<SNode> scopeSet = ScopesTest.this.getScopeNodes(scope); List<SNode> unExpected = CollectionUtil.subtract(expected, scopeSet); List<SNode> notFounded = CollectionUtil.subtract(scopeSet, expected); Assert.assertTrue(ScopesTest.this.getFailMessage(unExpected, notFounded).toString(), unExpected.isEmpty() && notFounded.isEmpty()); } }); } }