-
Notifications
You must be signed in to change notification settings - Fork 260
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
Changes from all commits
383add2
1a6c0be
1199e02
1d0ceb9
13451cb
a2904f8
1d2012c
31b9906
12648cb
b014e0b
073603e
58161fb
ee0b396
b600a34
b11c935
8ef4230
487bafc
9cf239c
420f4c6
352493e
c51e46e
a28a2dd
145fcbc
f760439
a3d898c
4ec12b3
1a96b1f
efb8118
b4e627c
8ed2d40
058691f
d5461b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
} | ||
|
||
} |
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> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.checkerframework</groupId> | ||
<artifactId>checker-compat-qual</artifactId> | ||
<version>2.5.5</version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove