Skip to content

Commit

Permalink
[Gradle] Add few unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage committed Aug 31, 2023
1 parent c34a233 commit 8489e81
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
1 change: 1 addition & 0 deletions gradle-twirl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ repositories {

dependencies {
compileOnly("com.typesafe.play:twirl-compiler_2.13:$compilerVersion")
testImplementation("org.assertj:assertj-core:3.24.2")
}

tasks.jar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private TwirlSourceDirectorySet getTwirlSourceDirectorySet(SourceSet sourceSet)
return twirlSourceDirectorySet;
}

private static JavaPluginExtension javaPluginExtension(Project project) {
static JavaPluginExtension javaPluginExtension(Project project) {
return extensionOf(project, JavaPluginExtension.class);
}

Expand Down
84 changes: 73 additions & 11 deletions gradle-twirl/src/test/java/play/twirl/gradle/TwirlPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,85 @@
*/
package play.twirl.gradle;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import static play.twirl.gradle.TwirlPlugin.javaPluginExtension;

import java.nio.charset.StandardCharsets;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.internal.artifacts.configurations.DefaultConfiguration;
import org.gradle.api.internal.project.DefaultProject;
import org.gradle.api.tasks.SourceSet;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/** A simple unit test for the 'com.playframework.twirl' plugin. */
/** A simple unit test to check a Twirl Gradle Plugin */
class TwirlPluginTest {

private Project project;

@BeforeEach
void init() {
project = ProjectBuilder.builder().build();
project.getPluginManager().apply("application");
project.getPluginManager().apply("com.typesafe.play.twirl");
((DefaultProject) project).evaluate();
}

@Test
@DisplayName("Twirl extension should be registered")
void extensionShouldBeRegistered() {
TwirlExtension ext = (TwirlExtension) project.getExtensions().findByName("twirl");
assertThat(ext).isNotNull();
assertThat((ext).getScalaVersion().getOrNull()).isEqualTo("2.13");
}

@Test
@DisplayName("Twirl configuration should be registered")
void configurationShouldBeRegistered() {
Configuration conf = project.getConfigurations().findByName("twirl");
assertThat(conf).isNotNull();
assertThat(conf.isTransitive()).isTrue();
assertThat(conf.isVisible()).isFalse();
((DefaultConfiguration) conf).runDependencyActions();
assertThat(conf.getDependencies())
.anyMatch(
dependency ->
"com.typesafe.play".equals(dependency.getGroup())
&& dependency.getName().startsWith("twirl-compiler"));
}

@Test
void pluginRegistersATask() {
// Create a test project and apply the plugin
Project project = ProjectBuilder.builder().build();
project.getPlugins().apply("application");
project.getPlugins().apply("com.typesafe.play.twirl");

// Verify the result
assertNotNull(project.getTasks().findByName("compileTwirl"));
assertNotNull(project.getTasks().findByName("compileTestTwirl"));
@DisplayName("Twirl source directory set should be registered for main source set")
void sourceDirectorySetShouldBeRegisteredForMainSourceSet() {
checkSourceDirectorySet(javaPluginExtension(project).getSourceSets().getByName("main"));
}

@Test
@DisplayName("Twirl source directory set should be registered for test source set")
void sourceDirectorySetShouldBeRegisteredForTestSourceSet() {
checkSourceDirectorySet(javaPluginExtension(project).getSourceSets().getByName("test"));
}

@Test
@DisplayName("Twirl compile task should be registered for main/test source sets")
void compileTasksShouldBeRegistered() {
assertThat(project.getTasks().findByName("compileTwirl")).isNotNull();
assertThat(project.getTasks().findByName("compileTestTwirl")).isNotNull();
}

private void checkSourceDirectorySet(SourceSet sourceSet) {
assertThat(sourceSet).isNotNull();
TwirlSourceDirectorySet twirlSourceSet =
sourceSet.getExtensions().findByType(TwirlSourceDirectorySet.class);
assertThat(twirlSourceSet).isNotNull();
assertThat(twirlSourceSet.getName()).isEqualTo("twirl");
assertThat(twirlSourceSet.getSourceEncoding().getOrNull())
.isEqualTo(StandardCharsets.UTF_8.name());
assertThat(twirlSourceSet.getTemplateImports().get()).isEmpty();
assertThat(twirlSourceSet.getConstructorAnnotations().get()).isEmpty();
assertThat(twirlSourceSet.getTemplateFormats().get()).containsKeys("html", "txt", "js", "xml");
}
}

0 comments on commit 8489e81

Please sign in to comment.