forked from Frege/frege-gradle-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from tricktron/f-test-task
Adds testFrege Task
- Loading branch information
Showing
8 changed files
with
256 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
plugins { | ||
id 'ch.fhnw.thga.frege' version '1.9.0-alpha' | ||
id 'ch.fhnw.thga.frege' version '2.0.0-alpha' | ||
} | ||
|
||
frege { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
group = ch.fhnw.thga | ||
version = 1.9.0-alpha | ||
version = 2.0.0-alpha |
144 changes: 144 additions & 0 deletions
144
src/functionalTest/java/ch/fhnw/thga/gradleplugins/TestFregeFunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package ch.fhnw.thga.gradleplugins; | ||
|
||
import static ch.fhnw.thga.gradleplugins.FregeExtension.DEFAULT_RELATIVE_SOURCE_DIR; | ||
import static ch.fhnw.thga.gradleplugins.FregePlugin.TEST_FREGE_TASK_NAME; | ||
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.createFregeSection; | ||
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runAndFailGradleTask; | ||
import static ch.fhnw.thga.gradleplugins.SharedFunctionalTestLogic.runGradleTask; | ||
import static ch.fhnw.thga.gradleplugins.SharedTaskLogic.EMPTY_LINE; | ||
import static ch.fhnw.thga.gradleplugins.SharedTaskLogic.NEW_LINE; | ||
import static org.gradle.testkit.runner.TaskOutcome.FAILED; | ||
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.File; | ||
import java.util.stream.Stream; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.testkit.runner.BuildResult; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.IndicativeSentencesGeneration; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import ch.fhnw.thga.gradleplugins.fregeproject.FregeProjectBuilder; | ||
import ch.fhnw.thga.gradleplugins.fregeproject.FregeSourceFile; | ||
|
||
public class TestFregeFunctionalTest | ||
{ | ||
private static final FregeSourceFile PROPERTY_TESTS_FR = new FregeSourceFile( | ||
String.format( | ||
"%s/%s", | ||
DEFAULT_RELATIVE_SOURCE_DIR, | ||
"ch/fhnw/thga/PropertyTests.fr" | ||
), | ||
String.join(NEW_LINE, | ||
"module ch.fhnw.thga.PropertyTests where", | ||
EMPTY_LINE, | ||
"import Test.QuickCheck", | ||
EMPTY_LINE, | ||
"p_pass = property $ \\(n::Integer) -> odd n ^^ even n", | ||
"p_fail = property $ \\(n::Integer) -> even n" | ||
) | ||
); | ||
|
||
@Nested | ||
@IndicativeSentencesGeneration( | ||
separator = " -> ", | ||
generator = DisplayNameGenerator.ReplaceUnderscores.class | ||
) | ||
class Test_frege_task_works | ||
{ | ||
@Test | ||
void given_frege_code_with_true_quick_check_property( | ||
@TempDir File testProjectDir) | ||
throws Exception | ||
{ | ||
String mainBuildConfig = createFregeSection( | ||
FregeDTOBuilder | ||
.builder() | ||
.version("'3.25.84'") | ||
.release("'3.25alpha'") | ||
.mainModule("'ch.fhnw.thga.PropertyTests'") | ||
.build() | ||
); | ||
|
||
Project project = FregeProjectBuilder | ||
.builder() | ||
.projectRoot(testProjectDir) | ||
.buildFile(mainBuildConfig) | ||
.fregeSourceFiles(() -> Stream.of(PROPERTY_TESTS_FR)) | ||
.build(); | ||
|
||
BuildResult result = runGradleTask( | ||
testProjectDir, | ||
TEST_FREGE_TASK_NAME, | ||
"--args=-v -p p_pass" | ||
); | ||
|
||
assertTrue( | ||
project | ||
.getTasks() | ||
.getByName(TEST_FREGE_TASK_NAME) | ||
instanceof TestFregeTask | ||
); | ||
assertEquals( | ||
SUCCESS, | ||
result.task(":" + TEST_FREGE_TASK_NAME).getOutcome() | ||
); | ||
assertTrue(result.getOutput().contains("OK")); | ||
assertTrue(result.getOutput().contains("Properties passed: 1, failed: 0")); | ||
} | ||
} | ||
|
||
@Nested | ||
@IndicativeSentencesGeneration( | ||
separator = " -> ", | ||
generator = DisplayNameGenerator.ReplaceUnderscores.class | ||
) | ||
class Test_frege_task_fails | ||
{ | ||
@Test | ||
void given_frege_code_with_false_quick_check_property( | ||
@TempDir File testProjectDir) | ||
throws Exception | ||
{ | ||
String mainBuildConfig = createFregeSection( | ||
FregeDTOBuilder | ||
.builder() | ||
.version("'3.25.84'") | ||
.release("'3.25alpha'") | ||
.mainModule("'ch.fhnw.thga.PropertyTests'") | ||
.build() | ||
); | ||
|
||
Project project = FregeProjectBuilder | ||
.builder() | ||
.projectRoot(testProjectDir) | ||
.buildFile(mainBuildConfig) | ||
.fregeSourceFiles(() -> Stream.of(PROPERTY_TESTS_FR)) | ||
.build(); | ||
|
||
BuildResult result = runAndFailGradleTask( | ||
testProjectDir, | ||
TEST_FREGE_TASK_NAME, | ||
"--args=-v -p p_fail" | ||
); | ||
|
||
assertTrue( | ||
project | ||
.getTasks() | ||
.getByName(TEST_FREGE_TASK_NAME) | ||
instanceof TestFregeTask | ||
); | ||
assertEquals( | ||
FAILED, | ||
result.task(":" + TEST_FREGE_TASK_NAME).getOutcome() | ||
); | ||
assertTrue(result.getOutput().contains("Failed")); | ||
assertTrue(result.getOutput().contains("Properties passed: 0, failed: 1")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/ch/fhnw/thga/gradleplugins/TestFregeTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ch.fhnw.thga.gradleplugins; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.file.DirectoryProperty; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.model.ObjectFactory; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.InputDirectory; | ||
import org.gradle.api.tasks.InputFile; | ||
import org.gradle.api.tasks.JavaExec; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.gradle.api.tasks.options.Option; | ||
import org.gradle.api.tasks.Internal; | ||
import org.gradle.api.provider.Provider; | ||
|
||
public abstract class TestFregeTask extends DefaultTask { | ||
private static final String TEST_MAIN_CLASS = "frege.tools.Quick"; | ||
private final JavaExec javaExec; | ||
|
||
@InputFile | ||
public abstract RegularFileProperty getFregeCompilerJar(); | ||
|
||
@InputDirectory | ||
public abstract DirectoryProperty getFregeOutputDir(); | ||
|
||
@Input | ||
@Option(option = "mainModule", | ||
description = "The full name of the Frege module with a main function, e.g. 'my.mod.Name'") | ||
public abstract Property<String> getMainModule(); | ||
|
||
@Input | ||
@Option(option = "args", | ||
description = "optional args passed to frege") | ||
public abstract Property<String> getFregeArgs(); | ||
|
||
@Internal | ||
final Provider<String> getAllArgs() | ||
{ | ||
return getFregeArgs() | ||
.map(args -> String.format("%s %s", args, getMainModule().get())); | ||
} | ||
|
||
@Input | ||
public abstract Property<String> getFregeDependencies(); | ||
|
||
@Inject | ||
public TestFregeTask(ObjectFactory objectFactory) { | ||
javaExec = objectFactory.newInstance(JavaExec.class); | ||
} | ||
|
||
@TaskAction | ||
public void runFrege() { | ||
javaExec.getMainClass().set(TEST_MAIN_CLASS); | ||
javaExec | ||
.setClasspath( | ||
SharedTaskLogic.setupClasspath( | ||
getProject(), | ||
getFregeDependencies(), | ||
getFregeCompilerJar(), | ||
getFregeOutputDir()) | ||
.get() | ||
) | ||
.setArgsString(getAllArgs().get()) | ||
.exec(); | ||
} | ||
} |