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

feature: WIP: Subject code generator, fixes #612 #894

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
383add2
feature: WIP: Subject code generator
astubbs Jul 29, 2021
1a6c0be
Include test jar build and use released roaster
astubbs Jul 29, 2021
1199e02
add mising actual field
astubbs Jul 29, 2021
1d0ceb9
fix actual access and add convenience assertTruth method
astubbs Jul 29, 2021
13451cb
feature: START-WIP: squash up: boiler plate only, three layer and mer…
astubbs Jul 29, 2021
a2904f8
feature: START-WIP: squash up: boiler plate only, three layer and mer…
astubbs Jul 29, 2021
1d2012c
WIP: test generators
astubbs Jul 29, 2021
31b9906
WIP: test generators iterate
astubbs Jul 30, 2021
12648cb
WIP: test generators iterate - generates valid code
astubbs Jul 30, 2021
b014e0b
WIP: test generators iterate - generates valid code on big project - …
astubbs Jul 30, 2021
073603e
compiles
astubbs Jul 31, 2021
58161fb
iterate, custom package targets (UUID, ZonedDateTime)
astubbs Jul 31, 2021
ee0b396
squash-up - switching machines
astubbs Aug 2, 2021
b600a34
Switch to Lombok for now (AutoValue later?), boolean strategy
astubbs Aug 2, 2021
b11c935
Fix child extend middle, add auto shading, fix static chains, update …
astubbs Aug 2, 2021
8ef4230
Don't make another middle if it already exists, move chickens into so…
astubbs Aug 2, 2021
487bafc
Create and tag with a user managed annotation marker
astubbs Aug 2, 2021
9cf239c
Add test strategies for optional, map, collection
astubbs Aug 2, 2021
420f4c6
Don't do protected methods, add simple javadoc, fix multiple calls to…
astubbs Aug 3, 2021
352493e
base package shortcut
astubbs Aug 3, 2021
c51e46e
feature: legacy mode for non bean compatible classes
astubbs Aug 2, 2021
a28a2dd
WIP-START: maven plugin
astubbs Aug 3, 2021
145fcbc
feature: Working plugin, recursive generation, auto shading
astubbs Aug 12, 2021
f760439
fix optional chaining
astubbs Aug 12, 2021
a3d898c
Update and stabilise base code generation test, fix toers
astubbs Aug 12, 2021
4ec12b3
Updating tests
astubbs Aug 12, 2021
1a96b1f
feature: Use generic types to have strong assertions for map and iter…
astubbs Aug 13, 2021
efb8118
remove mojo params not yet implemented
astubbs Aug 13, 2021
b4e627c
update tests
astubbs Aug 13, 2021
8ed2d40
update tests
astubbs Aug 13, 2021
058691f
feature: base subject extension points (e.g. MyStringSubject)
astubbs Aug 13, 2021
d5461b3
Fix naming collisions with legacy mode, fail gracefully and log with …
astubbs Aug 13, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
</excludes>
</configuration>
</execution>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
102 changes: 102 additions & 0 deletions extensions/generator-assertions-tests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-extensions-parent</artifactId>
<version>HEAD-SNAPSHOT</version>
</parent>

<artifactId>generator-assertions-tests</artifactId>
<version>HEAD-SNAPSHOT</version>

<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <artifactId>truth-generator-maven-plugin</artifactId>-->
<!-- <groupId>com.google.truth.extensions</groupId>-->
<!-- <configuration>-->
<!-- <classes>-->
<!-- <param>com.google.common.truth.extension.generator.testModel.MyEmployee</param>-->
<!-- </classes>-->
<!-- <entryPointClassPackage>com.google.truth.extensions.tests.projectUnderTest</entryPointClassPackage>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<!-- <dependency>-->
<!-- <groupId>com.google.truth.extensions</groupId>-->
<!-- <artifactId>truth-generator-maven-plugin</artifactId>-->
<!-- <type>maven-plugin</type>-->
<!-- <version>${project.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-java8-extension</artifactId>
<scope>compile</scope>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-generator-extension</artifactId>
<type>jar</type>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-generator-extension</artifactId>
<type>test-jar</type>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>uk.co.jemos.podam</groupId>
<artifactId>podam</artifactId>
<version>7.2.7.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.18.0</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.google.common.truth.extension.generator;

import com.google.common.truth.Truth;
import com.google.common.truth.extension.generator.internal.TruthGeneratorTest;
import com.google.common.truth.extension.generator.internal.legacy.NonBeanLegacyChildSubject;
import com.google.common.truth.extension.generator.internal.legacy.NonBeanLegacySubject;
import com.google.common.truth.extension.generator.testModel.ManagedTruth;
import com.google.common.truth.extension.generator.testModel.MyEmployee;
import com.google.common.truth.extension.generator.testModel.MyEmployeeChildSubject;
import com.google.common.truth.extension.generator.testModel.MyEmployeeSubject;
import com.google.common.truth.extension.generator.testing.legacy.NonBeanLegacy;
import org.junit.Test;
import uk.co.jemos.podam.api.PodamFactoryImpl;

import static com.google.common.truth.extension.generator.InstanceUtils.createInstance;
import static com.google.common.truth.extension.generator.testModel.MyEmployee.State.NEVER_EMPLOYED;
import static com.google.common.truth.extension.generator.testModel.MyEmployeeChildSubject.assertTruth;


/**
* Uses output from packages completed tests run from the generator module.
*
* @see TruthGeneratorTest#generate_code
*/
public class GeneratedAssertionTests {

public static final PodamFactoryImpl PODAM_FACTORY = new PodamFactoryImpl();

@Test
public void try_out_assertions() {
// all asserts should be available
MyEmployee hi = createInstance(MyEmployee.class);
hi = hi.toBuilder()
.name("Zeynep")
.boss(createInstance(MyEmployee.class).toBuilder().name("Tony").build())
.build();

assertTruth(hi).hasBirthYear().isAtLeast(200);
Truth.assertThat(hi.getBirthYear()).isAtLeast(200);

Truth.assertThat(hi.getBoss().getName()).contains("Tony");
assertTruth(hi).hasBoss().hasName().contains("Tony");
// assertTruth(hi).hasCard().hasEpoch().isAtLeast(20);
assertTruth(hi).hasProjectList().hasSize(5);
MyEmployeeSubject myEmployeeSubject = assertTruth(hi);

MyEmployeeChildSubject.assertThat(TestModelUtils.createEmployee()).hasProjectMapWithKey("key");
}

/**
* @see TruthGeneratorTest#test_legacy_mode
*/
@Test
public void test_legacy_mode() {
NonBeanLegacy nonBeanLegacy = createInstance(NonBeanLegacy.class).toBuilder().name("lilan").build();
NonBeanLegacySubject nonBeanLegacySubject = NonBeanLegacyChildSubject.assertThat(nonBeanLegacy);
nonBeanLegacySubject.hasAge().isNotNull();
nonBeanLegacySubject.hasName().isEqualTo("lilan");
}

@Test
public void recursive() {
MyEmployee emp = createInstance(MyEmployee.class);
MyEmployeeSubject es = ManagedTruth.assertThat(emp);

es.hasAnniversary().hasToLocalDate().hasEra().hasValue().isNotNull();
es.hasAnniversary().hasToLocalDate().hasChronology().hasId().isNotNull();
}

@Test
public void as_type_chain_transformers() {
MyEmployee emp = createInstance(MyEmployee.class);
MyEmployeeSubject es = ManagedTruth.assertThat(emp);

es.hasAnniversary().hasToLocalDate().hasEra();
es.hasAnniversary().hasToLocalDateTime().hasToLocalDate().hasEra().isNotNull();
}

@Test
public void enums(){
MyEmployee emp = createInstance(MyEmployee.class).toBuilder().employmentState(NEVER_EMPLOYED).build();
MyEmployeeSubject es = ManagedTruth.assertThat(emp);
es.hasEmploymentState().isEqualTo(NEVER_EMPLOYED);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.google.common.truth.extension.generator;

public class InstanceUtils {
static <T> T createInstance(Class<T> clazz) {
return GeneratedAssertionTests.PODAM_FACTORY.manufacturePojo(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.google.common.truth.extension.generator;

import com.google.common.truth.extension.generator.internal.extensions.ManagedTruth;
import com.google.common.truth.extension.generator.internal.extensions.MyEmployeeSubject;
import com.google.common.truth.extension.generator.testModel.MyEmployee;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.truth.extension.generator.InstanceUtils.createInstance;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class NativeSubjectExtensions {

@Test
public void my_string() {
String nameWithSpace = "tony ";
MyEmployee emp = createInstance(MyEmployee.class).toBuilder().workNickName(nameWithSpace).build();
MyEmployeeSubject es = ManagedTruth.assertThat(emp);

// my strings
es.hasWorkNickName().ignoringTrailingWhiteSpace().equalTo("tony");

// my maps
assertThatThrownBy(() -> es.hasProjectMap().containsKeys("key1", "key2")).isInstanceOf(AssertionError.class);
List<String> keys = new ArrayList<>(emp.getProjectMap().keySet());
es.hasProjectMap().containsKeys(keys.get(0), keys.get(1));
}

}
138 changes: 138 additions & 0 deletions extensions/generator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-extensions-parent</artifactId>
<version>HEAD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<name>Truth Extension for generating Subjects</name>
<artifactId>truth-generator-extension</artifactId>

<properties>
<version.roaster>2.23.0.Final</version.roaster>
</properties>

<dependencies>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-api</artifactId>
<version>${version.roaster}</version>
</dependency>
<dependency>
<groupId>org.jboss.forge.roaster</groupId>
<artifactId>roaster-jdt</artifactId>
<version>${version.roaster}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<!-- Used to generate Java 8 chains for users that use them -->
<groupId>com.google.truth.extensions</groupId>
<artifactId>truth-java8-extension</artifactId>
<scope>compile</scope>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<type>test-jar</type>
<version>HEAD-SNAPSHOT</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

<scope>test</scope>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-compat-qual</artifactId>
<version>2.5.5</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need?

</dependency>
<dependency>
<groupId>org.atteo</groupId>
<artifactId>evo-inflector</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
<!-- reflections optional dependency -->
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.1</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need?

</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-android</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outsource version

</dependency>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger</artifactId>
<version>0.6</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope

</dependency>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger-log4j2-backend</artifactId>
<version>0.6</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope

</dependency>
<dependency>
<groupId>com.google.flogger</groupId>
<artifactId>flogger-system-backend</artifactId>
<version>0.6</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scope

</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with auto value?

</dependency>
<dependency>
<groupId>uk.co.jemos.podam</groupId>
<artifactId>podam</artifactId>
<version>7.2.7.RELEASE</version>
<scope>compile</scope>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test scope

</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 1.7?

</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>13</source>
<target>13</target>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outsource? Should be 7/8?

</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.google.common.truth.extension.generator;

import com.google.common.truth.Subject;
import com.google.common.truth.extension.generator.internal.MyStringSubject;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

/**
* Marks for the machines, extensions to base Truth {@link Subject}s - for example, {@link MyStringSubject}.
*/
// todo scan for these instead of registering manually
@Target({ElementType.TYPE})
public @interface BaseSubjectExtension {
}
Loading