/**
  * Method that searches for testMethod keyword in a method given a node.
  *
  * @param astNode parent node for method.
  * @return a boolean value, returns true if there is a testMethod keyword or false otherwise.
  */
 public static boolean hasTestMethodKeyword(AstNode astNode) {
   boolean hasAnnotation = false;
   List<AstNode> modifiersChildren = astNode.getChildren(MODIFIERS);
   for (AstNode modifier : modifiersChildren) {
     for (AstNode modifierChild : modifier.getChildren()) {
       if ((modifierChild.getTokenOriginalValue().matches(TEST_METHOD_PATTERN))) {
         hasAnnotation = true;
         break;
       }
     }
   }
   return hasAnnotation;
 }
  private boolean isFriendDeclaration(AstNode declaratorList) {
    AstNode simpleDeclNode = declaratorList.getFirstAncestor(CxxGrammarImpl.simpleDeclaration);

    if (simpleDeclNode == null) {
      LOG.warn(
          "No simple declaration found for declarator list at {}", declaratorList.getTokenLine());
      return false;
    }

    AstNode simpleDeclSpecifierSeq =
        simpleDeclNode.getFirstChild(CxxGrammarImpl.simpleDeclSpecifierSeq);

    if (simpleDeclSpecifierSeq == null) {
      return false;
    }

    List<AstNode> declSpecifiers = simpleDeclSpecifierSeq.getChildren(CxxGrammarImpl.declSpecifier);

    for (AstNode declSpecifier : declSpecifiers) {
      AstNode friendNode = declSpecifier.getFirstChild(CxxKeyword.FRIEND);
      if (friendNode != null) {
        return true;
      }
    }

    return false;
  }
  private void visitDeclaratorList(AstNode declaratorList) {

    // do not handle declaration in function body
    AstNode functionBody = declaratorList.getFirstAncestor(CxxGrammarImpl.functionBody);

    if (functionBody != null) {
      return;
    }

    // ignore friend declarations
    if (isFriendDeclaration(declaratorList)) {
      return;
    }

    AstNode declaration = declaratorList.getFirstAncestor(CxxGrammarImpl.declaration);

    List<AstNode> declarators = declaratorList.getChildren(CxxGrammarImpl.initDeclarator);

    if (declarators.size() == 1) {
      // a special handling is needed in case of single declarator
      // because documentation may be located on different places
      // depending on the declaration
      visitSingleDeclarator(declaration, declarators.get(0));
    } else {
      // with several declarators, documentation should be located
      // on each declarator
      for (AstNode declarator : declarators) {
        visitDeclarator(declarator, declarator);
      }
    }
  }
Пример #4
0
  @Test
  public void test_children_when_one_child() {
    AstNodeType type1 = mock(AstNodeType.class);
    AstNodeType type2 = mock(AstNodeType.class);
    AstNode child = mock(AstNode.class);
    when(node.getNumberOfChildren()).thenReturn(1);

    when(node.getFirstChild()).thenReturn(child);
    AstSelect children = select.children();
    assertThat((Object) children).isInstanceOf(SingleAstSelect.class);
    assertThat(children).containsOnly(child);

    when(node.getChildren()).thenReturn(ImmutableList.of(child));

    children = select.children(type1);
    assertThat((Object) children).isSameAs(AstSelectFactory.empty());

    when(child.getType()).thenReturn(type1);
    children = select.children(type1);
    assertThat((Object) children).isInstanceOf(SingleAstSelect.class);
    assertThat(children).containsOnly(child);

    children = select.children(type1, type2);
    assertThat((Object) children).isSameAs(AstSelectFactory.empty());

    when(child.is(type1, type2)).thenReturn(true);
    children = select.children(type1, type2);
    assertThat((Object) children).isInstanceOf(SingleAstSelect.class);
    assertThat(children).containsOnly(child);
  }
Пример #5
0
 private static String getValue(AstNode astNode) {
   StringBuilder sb = new StringBuilder();
   for (AstNode child : astNode.getChildren()) {
     sb.append(child.getTokenValue());
   }
   return sb.toString();
 }
Пример #6
0
 @Test
 public void getSecondDeclarationTest() {
   AstNodeXPathQuery<AstNode> xpath1 =
       AstNodeXPathQuery.create("/COMPILATION_UNIT/DEFINITION[@tokenLine=4]");
   AstNodeXPathQuery<AstNode> xpath2 = AstNodeXPathQuery.create("/COMPILATION_UNIT/DEFINITION[2]");
   AstNode declarationAtLineFour = fileNode.getChildren().get(1);
   assertThat(declarationAtLineFour.is(MiniCGrammar.DEFINITION)).isTrue();
   assertThat(declarationAtLineFour.getTokenLine()).isEqualTo(4);
   assertThat(xpath1.selectSingleNode(fileNode)).isEqualTo(declarationAtLineFour);
   assertThat(xpath1.selectSingleNode(fileNode)).isEqualTo(xpath2.selectSingleNode(fileNode));
 }
  @Override
  protected void retrievePrivateClassMember(AstNode classDeclaration) {
    for (AstNode classStmt : classDeclaration.getChildren(PHPGrammar.CLASS_STATEMENT)) {
      AstNode stmtChild = classStmt.getFirstChild();

      if (stmtChild.is(PHPGrammar.CLASS_VARIABLE_DECLARATION)) {
        List<AstNode> modifiers =
            stmtChild
                .getFirstChild(PHPGrammar.VARIABLE_MODIFIERS)
                .getChildren(PHPGrammar.MEMBER_MODIFIER);

        if (isPrivate(modifiers)) {
          for (AstNode varDeclaration : stmtChild.getChildren(PHPGrammar.VARIABLE_DECLARATION)) {
            AstNode varIdentifier = varDeclaration.getFirstChild(PHPGrammar.VAR_IDENTIFIER);

            addPrivateMember(getCalledName(varIdentifier, modifiers), varIdentifier);
          }
        }
      }
    }
  }
 /**
  * Looks for an Assertion inside method given a list of name nodes.
  *
  * @param nameNodes list of Name nodes
  * @param pattern a pattern to define if it is assert only or assertEquals or assertNotEquals
  * @return a boolean value true if there is an assertion, false otherwise.
  */
 public static boolean hasAssertion(List<AstNode> nameNodes, String pattern) {
   StringBuilder validcall = new StringBuilder();
   for (AstNode nameNodeChild : nameNodes) {
     if (nameNodeChild.getTokenOriginalValue().matches(SYSTEM_PATTERN)) {
       nameNodeChild
           .getChildren()
           .stream()
           .forEach(
               (AstNode nameNodeGrandChild) -> {
                 validcall.append(nameNodeGrandChild.getTokenOriginalValue());
               });
       break;
     }
   }
   return validcall.toString().matches(pattern);
 }
  private void visitEnumSpecifier(AstNode enumSpecifierNode) {
    AstNode enumIdNode = enumSpecifierNode.getFirstDescendant(GenericTokenType.IDENTIFIER);

    String enumId = (enumIdNode == null) ? UNNAMED_ENUM_ID : enumIdNode.getTokenValue();

    if (!isPublicApiMember(enumSpecifierNode)) {
      logDebug(enumId + " not in public API");
      return;
    }

    visitPublicApi(enumSpecifierNode, enumId, getBlockDocumentation(enumSpecifierNode));

    // deal with enumeration values
    AstNode enumeratorList = enumSpecifierNode.getFirstDescendant(CxxGrammarImpl.enumeratorList);

    if (enumeratorList != null) {
      for (AstNode definition : enumeratorList.getChildren(CxxGrammarImpl.enumeratorDefinition)) {

        // look for block documentation
        List<Token> comments = getBlockDocumentation(definition);

        // look for inlined doc
        if (comments.isEmpty()) {
          AstNode next = definition.getNextAstNode();

          // inline documentation may be on the next definition token
          // or next curly brace
          if (next != null) {
            // discard COMMA
            if (next.getToken().getType() == CxxPunctuator.COMMA) {
              next = next.getNextAstNode();
            }

            comments = getInlineDocumentation(next.getToken(), definition.getTokenLine());
          }
        }

        visitPublicApi(
            definition,
            definition.getFirstDescendant(GenericTokenType.IDENTIFIER).getTokenValue(),
            comments);
      }
    }
  }
  private boolean isDefaultOrDeleteFunctionBody(AstNode functionBodyNode) {
    boolean defaultOrDelete = false;
    List<AstNode> functionBody = functionBodyNode.getChildren();

    // look for exact sub AST
    if (functionBody.size() == 3) {
      if (functionBody.get(0).is(CxxPunctuator.ASSIGN)
          && functionBody.get(2).is(CxxPunctuator.SEMICOLON)) {

        AstNode bodyType = functionBody.get(1);

        if (bodyType.is(CxxKeyword.DELETE) || bodyType.is(CxxKeyword.DEFAULT)) {
          defaultOrDelete = true;
        }
      }
    }

    return defaultOrDelete;
  }
  private void visitMemberDeclaration(AstNode memberDeclaration) {

    AstNode declaratorList =
        memberDeclaration.getFirstDescendant(CxxGrammarImpl.memberDeclaratorList);

    if (!isPublicApiMember(memberDeclaration)) {
      // if not part of the API, nothing to measure
      return;
    }

    AstNode subclassSpecifier = memberDeclaration.getFirstDescendant(CxxGrammarImpl.classSpecifier);

    if (subclassSpecifier != null) {
      // sub classes are handled by subscription
      return;
    }

    AstNode functionDef = memberDeclaration.getFirstDescendant(CxxGrammarImpl.functionDefinition);

    if (functionDef != null) {
      // functionDef are handled by subscription
      return;
    }

    if (declaratorList != null) {
      List<AstNode> declarators = declaratorList.getChildren(CxxGrammarImpl.memberDeclarator);

      // if only one declarator, the doc should be placed before the
      // memberDeclaration, or inlined
      if (declarators.size() == 1) {
        visitMemberDeclarator(memberDeclaration);
      } // if several declarators, doc should be placed before each
      // declarator, or inlined
      else {
        for (AstNode declarator : declarators) {
          visitMemberDeclarator(declarator);
        }
      }
    }
  }