Search in sources :

Example 11 with Example

use of com.google.copybara.doc.annotations.Example 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)11 StarlarkMethod (net.starlark.java.annot.StarlarkMethod)7 Pattern (com.google.re2j.Pattern)4 ImmutableList (com.google.common.collect.ImmutableList)3 DocDefault (com.google.copybara.doc.annotations.DocDefault)3 UsesFlags (com.google.copybara.doc.annotations.UsesFlags)3 Joiner (com.google.common.base.Joiner)2 Strings (com.google.common.base.Strings)2 Examples (com.google.copybara.doc.annotations.Examples)2 ValidationException (com.google.copybara.exception.ValidationException)2 Set (java.util.Set)2 Ascii (com.google.common.base.Ascii)1 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 GeneralOptions (com.google.copybara.GeneralOptions)1 Checker (com.google.copybara.checks.Checker)1 ConfigFile (com.google.copybara.config.ConfigFile)1 DocExample (com.google.copybara.doc.DocBase.DocExample)1 DocField (com.google.copybara.doc.DocBase.DocField)1