示例#1
0
 static void reportUnknownVariablesInSettingWithoutExeRows(
     final FileValidationContext validationContext,
     final ProblemsReportingStrategy reporter,
     final List<VariableDeclaration> variablesDeclarations,
     final Set<String> variables) {
   final Set<String> definedVariables = newHashSet(variables);
   for (final VariableDeclaration variableDeclaration : variablesDeclarations) {
     if (TestCaseTableValidator.isInvalidVariableDeclaration(
         definedVariables, variableDeclaration)) {
       final String variableName = variableDeclaration.getVariableName().getText();
       final RobotProblem problem =
           RobotProblem.causedBy(VariablesProblem.UNDECLARED_VARIABLE_USE)
               .formatMessageWith(variableName);
       final int variableOffset = variableDeclaration.getStartFromFile().getOffset();
       final ProblemPosition position =
           new ProblemPosition(
               variableDeclaration.getStartFromFile().getLine(),
               Range.closed(
                   variableOffset,
                   variableOffset
                       + ((variableDeclaration.getEndFromFile().getOffset() + 1)
                           - variableOffset)));
       final Map<String, Object> additionalArguments =
           ImmutableMap.<String, Object>of(
               AdditionalMarkerAttributes.NAME, variableDeclaration.asToken().getText());
       reporter.handleProblem(problem, validationContext.getFile(), position, additionalArguments);
     }
   }
 }
示例#2
0
  private void reportDuplicatedCases(final List<TestCase> cases) {
    final Set<String> duplicatedNames = newHashSet();

    for (final TestCase case1 : cases) {
      for (final TestCase case2 : cases) {
        if (case1 != case2) {
          final String case1Name = case1.getTestName().getText();
          final String case2Name = case2.getTestName().getText();

          if (case1Name.equalsIgnoreCase(case2Name)) {
            duplicatedNames.add(case1Name.toLowerCase());
          }
        }
      }
    }

    for (final TestCase testCase : cases) {
      final RobotToken caseName = testCase.getTestName();
      final String name = caseName.getText();

      if (duplicatedNames.contains(name.toLowerCase())) {
        final RobotProblem problem =
            RobotProblem.causedBy(TestCasesProblem.DUPLICATED_CASE).formatMessageWith(name);
        final Map<String, Object> additionalArguments =
            ImmutableMap.<String, Object>of("name", name);
        reporter.handleProblem(problem, validationContext.getFile(), caseName, additionalArguments);
      }
    }
  }
示例#3
0
  static void reportKeywordUsageProblems(
      final FileValidationContext validationContext,
      final ProblemsReportingStrategy reporter,
      final List<? extends RobotExecutableRow<?>> executables,
      final Optional<String> templateKeyword) {

    for (final RobotExecutableRow<?> executable : executables) {
      if (!executable.isExecutable() || templateKeyword.isPresent()) {
        continue;
      }

      final IExecutableRowDescriptor<?> executableRowDescriptor = executable.buildLineDescription();
      RobotToken keywordName = executableRowDescriptor.getAction().getToken();

      final IFile file = validationContext.getFile();
      if (executableRowDescriptor.getRowType() == ERowType.FOR) {
        final List<BuildMessage> messages = executableRowDescriptor.getMessages();
        for (final BuildMessage buildMessage : messages) {
          final RobotProblem problem =
              RobotProblem.causedBy(KeywordsProblem.UNKNOWN_KEYWORD)
                  .formatMessageWith(buildMessage.getMessage());
          reporter.handleProblem(problem, file, keywordName);
        }
        continue;
      }

      if (executableRowDescriptor.getRowType() == ERowType.FOR_CONTINUE) {
        final ForLoopContinueRowDescriptor<?> loopContinueRowDescriptor =
            (ForLoopContinueRowDescriptor<?>) executable.buildLineDescription();
        keywordName = loopContinueRowDescriptor.getKeywordAction().getToken();
      }

      if (!keywordName.getFilePosition().isNotSet()) {
        validateExistingKeywordCall(
            validationContext,
            reporter,
            keywordName,
            Optional.of(executableRowDescriptor.getKeywordArguments()));
      } else {
        reporter.handleProblem(
            RobotProblem.causedBy(KeywordsProblem.MISSING_KEYWORD)
                .formatMessageWith(executable.getAction().getText()),
            file,
            executable.getAction());
      }
    }
  }
示例#4
0
 @Override
 public void validate(final IProgressMonitor monitor) throws CoreException {
   for (final RobotToken docToken : getDeclaration()) {
     String raw = docToken.getRaw();
     final String rawWihtoutWithspaces = raw.replaceAll("\\s", "");
     if (rawWihtoutWithspaces.toLowerCase().contains(representation)) {
       reporter.handleProblem(
           RobotProblem.causedBy(getProblemId()).formatMessageWith(raw), file, docToken);
     }
   }
 }
示例#5
0
  private void reportEmptyCases(final List<TestCase> cases) {
    for (final TestCase testCase : cases) {
      final RobotToken caseName = testCase.getTestName();

      if (!hasAnythingToExecute(testCase)) {
        final String name = caseName.getText();
        final RobotProblem problem =
            RobotProblem.causedBy(TestCasesProblem.EMPTY_CASE).formatMessageWith(name);
        final Map<String, Object> arguments =
            ImmutableMap.<String, Object>of(AdditionalMarkerAttributes.NAME, name);
        reporter.handleProblem(problem, validationContext.getFile(), caseName, arguments);
      }
    }
  }
示例#6
0
  private void reportEmptyNamesOfCases(final List<TestCase> cases) {
    for (final TestCase testCase : cases) {
      final RobotToken caseName = testCase.getName();
      if (caseName.getText().trim().isEmpty()) {
        final RobotProblem problem = RobotProblem.causedBy(TestCasesProblem.EMPTY_CASE_NAME);
        final int startOffset = caseName.getStartOffset();
        final int endOffset = caseName.getEndOffset();

        final ProblemPosition problemPosition =
            new ProblemPosition(
                caseName.getFilePosition().getLine(), Range.closed(startOffset, endOffset));
        reporter.handleProblem(problem, validationContext.getFile(), problemPosition);
      }
    }
  }
示例#7
0
  static void reportUnknownVariables(
      final FileValidationContext validationContext,
      final ProblemsReportingStrategy reporter,
      final List<? extends RobotExecutableRow<?>> executables,
      final Set<String> variables) {

    final Set<String> definedVariables = newHashSet(variables);

    for (final RobotExecutableRow<?> row : executables) {
      if (row.isExecutable()) {
        final IExecutableRowDescriptor<?> lineDescription = row.buildLineDescription();

        for (final VariableDeclaration variableDeclaration : lineDescription.getUsedVariables()) {
          if (isInvalidVariableDeclaration(
              validationContext, definedVariables, lineDescription, variableDeclaration)) {
            String variableName = variableDeclaration.getVariableName().getText();
            final Optional<TextPosition> extractVariableName =
                new VariableComputationHelper().extractVariableName(variableDeclaration);
            if (extractVariableName.isPresent()) {
              variableName = extractVariableName.get().getText();
            }
            final RobotProblem problem =
                RobotProblem.causedBy(VariablesProblem.UNDECLARED_VARIABLE_USE)
                    .formatMessageWith(variableName);
            final int variableOffset = variableDeclaration.getStartFromFile().getOffset();
            final ProblemPosition position =
                new ProblemPosition(
                    variableDeclaration.getStartFromFile().getLine(),
                    Range.closed(
                        variableOffset,
                        variableOffset
                            + ((variableDeclaration.getEndFromFile().getOffset() + 1)
                                - variableOffset)));
            final Map<String, Object> additionalArguments =
                ImmutableMap.<String, Object>of(
                    AdditionalMarkerAttributes.NAME, variableDeclaration.asToken().getText());
            reporter.handleProblem(
                problem, validationContext.getFile(), position, additionalArguments);
          }
        }
        definedVariables.addAll(
            VariableNamesSupport.extractUnifiedVariableNames(
                lineDescription.getCreatedVariables()));
      }
    }
  }
示例#8
0
 static void reportKeywordUsageProblemsInSetupAndTeardownSetting(
     final FileValidationContext validationContext,
     final ProblemsReportingStrategy reporter,
     final RobotToken keywordNameToken,
     final Optional<List<RobotToken>> arguments) {
   final MappingResult variablesExtraction =
       new VariableExtractor().extract(keywordNameToken, validationContext.getFile().getName());
   final List<VariableDeclaration> variablesDeclarations =
       variablesExtraction.getCorrectVariables();
   if (variablesExtraction.getMappedElements().size() == 1 && variablesDeclarations.size() == 1) {
     final RobotProblem problem =
         RobotProblem.causedBy(GeneralSettingsProblem.VARIABLE_AS_KEYWORD_USAGE_IN_SETTING)
             .formatMessageWith(variablesDeclarations.get(0).getVariableName().getText());
     reporter.handleProblem(problem, validationContext.getFile(), keywordNameToken);
   } else {
     validateExistingKeywordCall(validationContext, reporter, keywordNameToken, arguments);
   }
 }
示例#9
0
 static void validateTimeoutSetting(
     final FileValidationContext validationContext,
     final ProblemsReportingStrategy reporter,
     final Set<String> variables,
     final RobotToken timeoutToken) {
   final String timeout = timeoutToken.getText();
   if (!RobotTimeFormat.isValidRobotTimeArgument(timeout.trim())) {
     final List<VariableDeclaration> variablesDeclarations =
         new VariableExtractor()
             .extract(timeoutToken, validationContext.getFile().getName())
             .getCorrectVariables();
     if (!variablesDeclarations.isEmpty()) {
       reportUnknownVariablesInSettingWithoutExeRows(
           validationContext, reporter, variablesDeclarations, variables);
     } else {
       final RobotProblem problem =
           RobotProblem.causedBy(ArgumentProblem.INVALID_TIME_FORMAT).formatMessageWith(timeout);
       reporter.handleProblem(problem, validationContext.getFile(), timeoutToken);
     }
   }
 }
示例#10
0
  static void validateExistingKeywordCall(
      final FileValidationContext validationContext,
      final ProblemsReportingStrategy reporter,
      final RobotToken keywordName,
      final Optional<List<RobotToken>> arguments) {
    final ListMultimap<String, KeywordEntity> keywordProposal =
        validationContext.findPossibleKeywords(keywordName.getText());

    final Optional<String> nameToUse =
        GherkinStyleSupport.firstNameTransformationResult(
            keywordName.getText(),
            new NameTransformation<String>() {

              @Override
              public Optional<String> transform(final String gherkinNameVariant) {
                return validationContext.isKeywordAccessible(keywordProposal, gherkinNameVariant)
                    ? Optional.of(gherkinNameVariant)
                    : Optional.<String>absent();
              }
            });
    final String name =
        !nameToUse.isPresent() || nameToUse.get().isEmpty()
            ? keywordName.getText()
            : nameToUse.get();
    final int offset =
        keywordName.getStartOffset() + (keywordName.getText().length() - name.length());
    final ProblemPosition position =
        new ProblemPosition(
            keywordName.getLineNumber(), Range.closed(offset, offset + name.length()));

    if (!nameToUse.isPresent()) {
      reporter.handleProblem(
          RobotProblem.causedBy(KeywordsProblem.UNKNOWN_KEYWORD).formatMessageWith(name),
          validationContext.getFile(),
          position,
          ImmutableMap.<String, Object>of(
              AdditionalMarkerAttributes.NAME,
              name,
              AdditionalMarkerAttributes.ORIGINAL_NAME,
              keywordName.getText()));
      return;
    }

    final ListMultimap<KeywordScope, KeywordEntity> keywords =
        validationContext.getPossibleKeywords(keywordProposal, name);

    for (final KeywordScope scope : KeywordScope.defaultOrder()) {
      final List<KeywordEntity> keywordEntities = keywords.get(scope);
      if (keywordEntities.size() == 1) {
        final ValidationKeywordEntity keyword = (ValidationKeywordEntity) keywordEntities.get(0);
        if (keyword.isDeprecated()) {
          reporter.handleProblem(
              RobotProblem.causedBy(KeywordsProblem.DEPRECATED_KEYWORD).formatMessageWith(name),
              validationContext.getFile(),
              position);
        }
        if (keyword.isFromNestedLibrary(validationContext.getFile())) {
          reporter.handleProblem(
              RobotProblem.causedBy(KeywordsProblem.KEYWORD_FROM_NESTED_LIBRARY)
                  .formatMessageWith(name),
              validationContext.getFile(),
              position);
        }
        if (keyword.hasInconsistentName(name)) {
          reporter.handleProblem(
              RobotProblem.causedBy(
                      KeywordsProblem.KEYWORD_OCCURRENCE_NOT_CONSISTENT_WITH_DEFINITION)
                  .formatMessageWith(name, keyword.getNameFromDefinition()),
              validationContext.getFile(),
              position,
              ImmutableMap.<String, Object>of(
                  AdditionalMarkerAttributes.NAME,
                  name,
                  AdditionalMarkerAttributes.ORIGINAL_NAME,
                  keyword.getNameFromDefinition(),
                  AdditionalMarkerAttributes.SOURCES,
                  keyword.getSourceNameInUse()));
        }
        if (arguments.isPresent()) {
          new KeywordCallArgumentsValidator(
                  validationContext.getFile(),
                  keywordName,
                  reporter,
                  keyword.getArgumentsDescriptor(),
                  arguments.get())
              .validate(new NullProgressMonitor());
        }
        break;
      } else if (keywordEntities.size() > 1) {
        final Iterable<?> sources =
            transform(
                keywordEntities,
                new Function<KeywordEntity, String>() {

                  @Override
                  public String apply(final KeywordEntity kw) {
                    return kw.getSourceNameInUse();
                  }
                });
        reporter.handleProblem(
            RobotProblem.causedBy(KeywordsProblem.AMBIGUOUS_KEYWORD)
                .formatMessageWith(name, "[" + Joiner.on(", ").join(sources) + "]"),
            validationContext.getFile(),
            position,
            ImmutableMap.<String, Object>of(
                AdditionalMarkerAttributes.NAME,
                name,
                AdditionalMarkerAttributes.ORIGINAL_NAME,
                keywordName.getText(),
                AdditionalMarkerAttributes.SOURCES,
                Joiner.on(';').join(sources)));
        break;
      }
    }
  }