Search in sources :

Example 1 with Examples

use of com.google.copybara.doc.annotations.Examples in project copybara by google.

the class MarkdownGenerator method generateFunctionDocumentation.

private CharSequence generateFunctionDocumentation(TypeElement module, SkylarkModule skyModule, Element member) throws ElementException {
    StringBuilder sb = new StringBuilder();
    AnnotationHelper<SkylarkSignature> signature = annotationHelper(member, SkylarkSignature.class);
    if (signature == null || !(member instanceof VariableElement)) {
        return sb;
    }
    if (!signature.ann.documented()) {
        return sb;
    }
    String functionName = signature.ann.name();
    DeclaredType objectType = signature.getClassValue("objectType");
    if (objectType.toString().equals(module.toString())) {
        functionName = skyModule.name() + "." + functionName;
    }
    sb.append("<a id=\"").append(functionName).append("\" aria-hidden=\"true\"></a>\n");
    sb.append("### ").append(functionName).append("\n\n");
    sb.append(signature.ann.doc());
    sb.append("\n\n");
    // Create a string like `Origin foo(param, param2=Default)`
    sb.append("`");
    String returnTypeStr = skylarkTypeName(signature.getClassValue("returnType"));
    if (!returnTypeStr.equals("")) {
        sb.append(returnTypeStr).append(" ");
    }
    sb.append(functionName).append("(");
    List<AnnotationValue> params = signature.getValue("parameters");
    StringBuilder longDescription = new StringBuilder();
    int startIndex = firstParamIsSelf(module, skyModule, objectType) ? 1 : 0;
    for (int i = startIndex; i < params.size(); i++) {
        AnnotationHelper<Param> param = new AnnotationHelper<>(signature.ann.parameters()[i], (AnnotationMirror) params.get(i).getValue(), member);
        if (i > startIndex) {
            sb.append(", ");
        }
        sb.append(param.ann.name());
        String defaultValue = param.ann.defaultValue();
        if (!defaultValue.isEmpty()) {
            sb.append("=").append(defaultValue);
        }
        longDescription.append(param.ann.name()).append("|");
        longDescription.append("`").append(skylarkTypeNameGeneric(param.getClassValue("type"), param.getClassValue("generic1"))).append("`").append("<br><p>");
        longDescription.append(param.ann.doc());
        longDescription.append("</p>");
        longDescription.append("\n");
    }
    sb.append(")`\n\n");
    if (longDescription.length() > 0) {
        sb.append("#### Parameters:\n\n");
        sb.append("Parameter | Description\n");
        sb.append("--------- | -----------\n");
        sb.append(longDescription);
        sb.append("\n\n");
    }
    // Generate flags associated with an specific method
    sb.append(generateFlagsInfo(member));
    AnnotationHelper<Examples> examples = annotationHelper(member, Examples.class);
    AnnotationHelper<Example> singleExample = annotationHelper(member, Example.class);
    if (examples != null) {
        sb.append("#### Examples:\n\n");
        for (Example example : examples.ann.value()) {
            printExample(sb, example);
        }
    } else if (singleExample != null) {
        sb.append("#### Example:\n\n");
        printExample(sb, singleExample.ann);
    }
    return sb;
}
Also used : VariableElement(javax.lang.model.element.VariableElement) SkylarkSignature(com.google.devtools.build.lib.skylarkinterface.SkylarkSignature) Example(com.google.copybara.doc.annotations.Example) Param(com.google.devtools.build.lib.skylarkinterface.Param) AnnotationValue(javax.lang.model.element.AnnotationValue) Examples(com.google.copybara.doc.annotations.Examples) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with Examples

use of com.google.copybara.doc.annotations.Examples in project copybara by google.

the class ExamplesTest method testExamples.

@Test
public void testExamples() {
    SkylarkTestExecutor executor = getExecutor();
    ImmutableList.Builder<Result> resBuilder = ImmutableList.builder();
    for (Class<?> module : executor.getModules()) {
        for (Method method : module.getMethods()) {
            Examples examples = method.getAnnotation(Examples.class);
            ImmutableList<Example> samples;
            if (examples == null) {
                Example singleSample = method.getAnnotation(Example.class);
                if (singleSample != null) {
                    samples = ImmutableList.of(singleSample);
                } else {
                    continue;
                }
            } else {
                samples = ImmutableList.copyOf(examples.value());
            }
            resBuilder.addAll(checkExamples(executor, module, samples, method.getName()));
        }
    }
    ImmutableList<Result> result = resBuilder.build();
    // Normally we won't remove examples or modules. This checks that we don't go down. This
    // is the number of modules in Apr 2019. We can update this from time to time. It is not
    // critical to have an accurate number, but that we don't lose at least these.
    assertWithMessage("Less examples than expected").that(result.size()).isAtLeast(48);
    Set<? extends Class<?>> modules = result.stream().map(e -> e.cls).collect(Collectors.toSet());
    assertWithMessage("Less classes than expected: " + modules).that(modules.size()).isAtLeast(5);
    List<Result> errors = result.stream().filter(Result::isError).collect(Collectors.toList());
    assertWithMessage("Errors in examples(" + errors.size() + "):\n\n" + Joiner.on("\n-----------------------------\n").join(errors)).that(errors).isEmpty();
}
Also used : SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Examples(com.google.copybara.doc.annotations.Examples) RunWith(org.junit.runner.RunWith) ValidationException(com.google.copybara.exception.ValidationException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) Set(java.util.Set) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Collectors(java.util.stream.Collectors) Strings(com.google.common.base.Strings) List(java.util.List) Example(com.google.copybara.doc.annotations.Example) ImmutableList(com.google.common.collect.ImmutableList) Method(java.lang.reflect.Method) Nullable(javax.annotation.Nullable) Joiner(com.google.common.base.Joiner) ImmutableList(com.google.common.collect.ImmutableList) Method(java.lang.reflect.Method) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Example(com.google.copybara.doc.annotations.Example) Examples(com.google.copybara.doc.annotations.Examples) Test(org.junit.Test)

Aggregations

Example (com.google.copybara.doc.annotations.Example)2 Examples (com.google.copybara.doc.annotations.Examples)2 Joiner (com.google.common.base.Joiner)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 ValidationException (com.google.copybara.exception.ValidationException)1 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)1 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)1 Param (com.google.devtools.build.lib.skylarkinterface.Param)1 SkylarkSignature (com.google.devtools.build.lib.skylarkinterface.SkylarkSignature)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 VariableElement (javax.lang.model.element.VariableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 Test (org.junit.Test)1