Пример #1
0
  private static boolean isKeywordFromBuiltin(
      final FileValidationContext validationContext, final String keywordName) {

    for (final KeywordScope scope : KeywordScope.defaultOrder()) {
      final List<KeywordEntity> possible =
          validationContext.getPossibleKeywords(keywordName, true).get(scope);
      if (scope != KeywordScope.STD_LIBRARY && !possible.isEmpty()) {
        return false;
      } else if (scope == KeywordScope.STD_LIBRARY) {
        return possible.size() == 1 && possible.get(0).getSourceNameInUse().equals("BuiltIn");
      }
    }
    return false;
  }
Пример #2
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;
      }
    }
  }