Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "generatesSourcesExactly" method? #89

Open
edenman opened this issue Feb 25, 2016 · 3 comments
Open

Add "generatesSourcesExactly" method? #89

edenman opened this issue Feb 25, 2016 · 3 comments
Labels
P3 type=enhancement Make an existing feature better

Comments

@edenman
Copy link

edenman commented Feb 25, 2016

Currently if you call GeneratedPredicateClause.generatesSources(foo) but the code generates two files instead of just foo, this test will pass. If I were to add a generateSourcesExactly that requires all generated files be specified, would that be accepted?

@0x3333
Copy link

0x3333 commented Aug 30, 2016

I'm looking for this same thing, or at least know if a file has _not_ been generated.

@blipinsk
Copy link

A little workaround to achieve that (example using google/truth):

Compilation compilation = /* prepare your Compilation object */;

final ImmutableList<JavaFileObject> generatedSourceFiles = compilation.generatedSourceFiles();
final ImmutableList.Builder<String> builder = ImmutableList.builder();
for (JavaFileObject generatedSourceFile : generatedSourceFiles) {
    final String qualifiedName = generatedSourceFile.getName()
            .substring(StandardLocation.SOURCE_OUTPUT.getName().length() + 2)
            .replace(JavaFileObject.Kind.SOURCE.extension, "")
            .replace("/", ".");
    builder.add(qualifiedName);
}
final ImmutableList<String> generatedQualifiedNames = builder.build();

// Checking if expected files have been generated
Truth.assertThat(generatedQualifiedNames)
        .containsExactly("com.example.FileOne", "com.example.FileTwo", "com.example.FileThree");

// Checking if a specific file hasn't been generated
Truth.assertThat(generatedQualifiedNames)
        .doesNotContain("com.example.FileThatShouldntBeGenerated");

Alternatively you could dump all the logic (of extracting a qualified name data from JavaFileObject name) into a Correspondence object, and use:

Truth.assertThat(generatedQualifiedNames)
        .comparingElementsUsing(SOURCE_FILE_CORRESPONDS_TO_QUALIFIED_NAME)
        .doesNotContain("com.example.FileThatShouldntBeGenerated");

But you're loosing some of the Truth flexibility. E.g. you'd have to call containsExactlyElementsIn instead of containsExactly.

@raghsriniv raghsriniv added the P3 label Jun 26, 2019
@cgdecker cgdecker added the type=enhancement Make an existing feature better label Jul 29, 2019
@manan1979
Copy link

hi @edenman how can i solve this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 type=enhancement Make an existing feature better
Projects
None yet
Development

No branches or pull requests

6 participants