public void test_forNull() throws Exception {
   this.addNodeById("2906110183022219846");
   this.addNodeById("2906110183022219807");
   this.addNodeById("2906110183022219843");
   this.addNodeById("2906110183022354865");
   this.addNodeById("2906110183022432276");
   SNode nullNode = null;
   Assert.assertTrue(Sequence.fromIterable(SNodeOperations.getReferences(nullNode)).isEmpty());
   SReference nullReference = null;
   Assert.assertNull(SLinkOperations.findLinkDeclaration(nullReference));
   Assert.assertNull(SLinkOperations.getRole(nullReference));
   Assert.assertNull(SLinkOperations.getTargetNode(nullReference));
   Assert.assertNull(SLinkOperations.getResolveInfo(nullReference));
 }
Пример #2
0
  @Override
  public List<SearchResult<ModelCheckerIssue>> checkModel(
      SModel model, ProgressMonitor monitor, final IOperationContext operationContext) {
    List<SearchResult<ModelCheckerIssue>> results =
        ListSequence.fromList(new ArrayList<SearchResult<ModelCheckerIssue>>());
    String title =
        "Checking " + SModelOperations.getModelName(model) + " for wrong aspect dependencies...";
    monitor.start(title, 1);

    int modelKind = getModelKind(model, null);
    if (modelKind == OTHER) {
      monitor.done();
      return results;
    }

    for (SNode node : ListSequence.fromList(SModelOperations.getNodes(model, null))) {
      if (monitor.isCanceled()) {
        break;
      }
      // Check for unresolved references
      for (final SReference ref : Sequence.fromIterable(SNodeOperations.getReferences(node))) {
        if ((AttributeOperations.getAttribute(
                node,
                new IAttributeDescriptor.LinkAttribute(
                    SConceptOperations.findConceptDeclaration(
                        "jetbrains.mps.lang.generator.structure.ReferenceMacro"),
                    SLinkOperations.getRole(ref)))
            != null)) {
          continue;
        }
        SNode targetNode = jetbrains.mps.util.SNodeOperations.getTargetNodeSilently(ref);
        if (targetNode == null) {
          addIssue(
              results,
              node,
              "Unresolved reference: " + SLinkOperations.getResolveInfo(ref),
              ModelChecker.SEVERITY_ERROR,
              "unresolved reference",
              new IModelCheckerFix() {
                public boolean doFix() {
                  return ResolverComponent.getInstance().resolve(ref, operationContext);
                }
              });
          continue;
        }

        SModel targetModel = SNodeOperations.getModel(targetNode);
        int targetKind = getModelKind(targetModel, ref);
        if (targetKind > modelKind) {
          addIssue(
              results,
              node,
              "Wrong reference: "
                  + SLinkOperations.getResolveInfo(ref)
                  + ", reference from "
                  + kindToString(modelKind)
                  + " to "
                  + kindToString(targetKind),
              ModelChecker.SEVERITY_ERROR,
              "wrong aspect dependency (" + kindToString(modelKind) + ")",
              null);
        }
      }
    }
    monitor.done();
    return results;
  }
    public void test_unresolvedReference() throws Exception {
      this.addNodeById("2906110183022219846");
      this.addNodeById("2906110183022219807");
      this.addNodeById("2906110183022219843");
      this.addNodeById("2906110183022354865");
      this.addNodeById("2906110183022432276");
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022219844"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
          "root",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022219844"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
          "leftChild",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022219844"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
          "rightChild",
          false);

      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022354866"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainerSubConcept"),
          "root",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022354866"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainerSubConcept"),
          "leftChild",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022354866"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainerSubConcept"),
          "rightChild",
          false);

      int initialSize =
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .count();
      Assert.assertTrue(
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .where(
                  new IWhereFilter<SReference>() {
                    public boolean accept(SReference it) {
                      return (SLinkOperations.getTargetNode(it) == null);
                    }
                  })
              .isEmpty());
      SLinkOperations.getTarget(
              SNodeOperations.cast(
                  this.getNodeById("2906110183022432277"),
                  "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
              "root",
              false)
          .delete();
      UnregisteredNodes.instance().clear();
      Assert.assertEquals(
          initialSize,
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .count());
      Iterable<SReference> brokenReferences =
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .where(
                  new IWhereFilter<SReference>() {
                    public boolean accept(SReference it) {
                      return jetbrains.mps.util.SNodeOperations.getTargetNodeSilently(it) == null;
                    }
                  });
      Assert.assertEquals(1, Sequence.fromIterable(brokenReferences).count());
      SReference theReference = Sequence.fromIterable(brokenReferences).first();
      Assert.assertEquals(
          SPropertyOperations.getString(
              SLinkOperations.findLinkDeclaration(
                  "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer", "root"),
              "role"),
          SLinkOperations.getRole(theReference));
      Assert.assertEquals(
          SLinkOperations.findLinkDeclaration(
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer", "root"),
          SLinkOperations.findLinkDeclaration(theReference));
      Assert.assertNotNull(SLinkOperations.getResolveInfo(theReference));
    }