This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
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 #9 from jaa127/master
Better Examples and changes for testing: 0.6.0
- Loading branch information
Showing
58 changed files
with
870 additions
and
135 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
/.idea/ | ||
*.swp | ||
*~ | ||
*.sc |
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
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,6 +1,39 @@ | ||
# DirSuite | ||
|
||
See: [DirSuiteDemo](../testing/src/test/scala/fi/sn127/utils/testing/DirSuiteDemo.scala) | ||
DirSuite is an add-on to ScalaTest, and it makes possible to run and test | ||
corpus of tests which are defined in as files on directory tree. | ||
|
||
Test interface is simple, your testFunction takes as an input | ||
an array of strings. Return values, exceptions etc. you can define | ||
normally by using ScalaTest's assertions. | ||
|
||
It is possible to run multiple execution steps per one test case, | ||
e.g. for testing git session, one test could be: | ||
|
||
`init`, `add file.txt`, `commit -m "msg"` | ||
|
||
if test function is git's main method. | ||
|
||
DirSuite also supports automatic validation of potential output. | ||
Currently supported formats are txt and xml-output. | ||
|
||
|
||
### Examples | ||
|
||
Under examples are three demo DirSuites, which demonstrates | ||
different aspects of DirSuite: | ||
|
||
* [DemoApp](../examples/src/main/scala/DemoApp.scala) which is used in tests | ||
|
||
* [Test corpus](../examples/tests/) contains test inputs | ||
|
||
* [DirSuiteDemo](../examples/src/test/scala/DirSuiteDemo.scala) | ||
Normal usage of DirSuite | ||
|
||
* [MapArgsDemo](../examples/src/test/scala/MapArgsDemo.scala) | ||
Howto map and change test's arguments (e.g. to provide conf file) | ||
|
||
* [FailureDemo](../examples/src/test/scala/FailureDemo.scala) | ||
Ignored tests which all will fail, and provide example output of failurecases. | ||
|
||
TODO | ||
|
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,3 +1,15 @@ | ||
# How to use DirSuite | ||
|
||
See: [DirSuiteDemo](../testing/src/test/scala/fi/sn127/utils/testing/DirSuiteDemo.scala) | ||
Under examples are three demo DirSuites, which demonstrates | ||
different aspects of DirSuite: | ||
|
||
* [DirSuiteDemo](../examples/src/test/scala/DirSuiteDemo.scala) | ||
Normal usage of DirSuite | ||
|
||
* [MapArgsDemo](../examples/src/test/scala/MapArgsDemo.scala) | ||
Howto map and change test's arguments (e.g. to provide conf file) | ||
|
||
* [FailureDemo](../examples/src/test/scala/FailureDemo.scala) | ||
Ignored tests which all will fail, and provide example output of failurecases. | ||
|
||
|
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,7 @@ | ||
/**/lib_managed/ | ||
/**/src_managed/ | ||
/**/target/ | ||
/tests/**/out.* | ||
/.idea/ | ||
*.swp | ||
*~ |
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,7 @@ | ||
|
||
name := "DirSuiteExample" | ||
|
||
scalaVersion := "2.12.1" | ||
|
||
libraryDependencies += "fi.sn127" %% "utils-testing" % "0.6.0-pre" % "test" | ||
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test" |
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 @@ | ||
sbt.version=0.13.13 |
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,62 @@ | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.{Files, Path, Paths} | ||
|
||
object DemoApp { | ||
// | ||
val SUCCESS = 127 | ||
val FAILURE = 255 | ||
} | ||
|
||
class DemoApp(val testdir: Path) { | ||
|
||
def doSuccess(args: Array[String]): Int = { | ||
DemoApp.SUCCESS | ||
} | ||
|
||
def doFailure(args: Array[String]): Int = { | ||
DemoApp.FAILURE | ||
} | ||
|
||
def doFlaky(args: Array[String]): Int = { | ||
if (args(0) == "bang") { | ||
throw new RuntimeException("BANG!") | ||
} | ||
else if (args(0) == "fail") { | ||
DemoApp.FAILURE | ||
} else { | ||
DemoApp.SUCCESS | ||
} | ||
} | ||
|
||
def doArgsCount(args: Array[String]): Int = { | ||
args.length | ||
} | ||
|
||
def doTxt(args: Array[String]): Int = { | ||
val output = Paths.get(testdir.toString, args(0)) | ||
Files.write(output, args | ||
.mkString("hello\n", "\n", "\nworld\n") | ||
.getBytes(StandardCharsets.UTF_8)) | ||
DemoApp.SUCCESS | ||
} | ||
|
||
def doXml(args: Array[String]): Int = { | ||
val output = Paths.get(testdir.toString, args(0)) | ||
Files.write(output, args | ||
.mkString("<hello><arg>", "</arg><arg>", "</arg></hello>\n") | ||
.getBytes(StandardCharsets.UTF_8)) | ||
DemoApp.SUCCESS | ||
} | ||
|
||
def doTxtXml(args: Array[String]): Int = { | ||
val result = | ||
if (args(1) == "txt") { | ||
doTxt(args) | ||
} else if (args(1) == "xml") { | ||
doXml(args) | ||
} else { | ||
DemoApp.FAILURE | ||
} | ||
result | ||
} | ||
} |
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,156 @@ | ||
|
||
import java.nio.file.Paths | ||
|
||
import fi.sn127.utils.fs.{Glob, Regex} | ||
import fi.sn127.utils.testing.DirSuite | ||
|
||
class DirSuiteDemo extends DirSuite { | ||
|
||
val testdir = Paths.get("tests").toAbsolutePath.normalize | ||
val app = new DemoApp(testdir) | ||
|
||
/** | ||
* Find all noargs-tests and execute them. | ||
* | ||
* Assert that App (doArgsCount) return correct arg count (0) | ||
* | ||
* Search method: Glob | ||
* https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob | ||
*/ | ||
runDirSuite(testdir, Glob("success/noargs[0-9]*.exec")) { args: Array[String] => | ||
assertResult(0) { | ||
app.doArgsCount(args) | ||
} | ||
} | ||
|
||
/** | ||
* Find all args3 and execute them. | ||
* | ||
* Assert that App (doArgsCount) return correct arg count (3) | ||
* | ||
* Search method: Glob | ||
* https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob | ||
*/ | ||
runDirSuite(testdir, Glob("success/args3-[0-9]*.exec")) { args: Array[String] => | ||
assertResult(3) { | ||
app.doArgsCount(args) | ||
} | ||
} | ||
|
||
/** | ||
* Find all txt-output tests and execute them. | ||
* | ||
* Assert that doTxt was successful and check | ||
* output based on reference vectors. | ||
* | ||
* Search method: Regex | ||
* https://docs.oracle.com/javase/tutorial/essential/regex/index.html | ||
*/ | ||
runDirSuite(testdir, Regex("success/txt[0-9]+\\.exec")) { args: Array[String] => | ||
assertResult(DemoApp.SUCCESS) { | ||
app.doTxt(args) | ||
} | ||
} | ||
|
||
/** | ||
* Find all xml-output tests and execute them. | ||
* | ||
* Assert that doXml was successful and check | ||
* output based on reference vectors. | ||
* | ||
* Search method: Regex | ||
* https://docs.oracle.com/javase/tutorial/essential/regex/index.html | ||
*/ | ||
runDirSuite(testdir, Regex("success/xml[0-9]+\\.exec")) { args: Array[String] => | ||
assertResult(DemoApp.SUCCESS) { | ||
app.doXml(args) | ||
} | ||
} | ||
|
||
/** | ||
* Find all mixed output tests and execute them. | ||
* Use XML Validator for XML-files, and TXT Validator | ||
* for txt-files. This choice is done by | ||
* DirSuite::selectValidator, which can be overloaded | ||
* test-by-test class basis. | ||
* | ||
* Assert that doTxtXml was successful and check | ||
* output based on reference vectors. | ||
* | ||
* Search method: Glob | ||
* https://docs.oracle.com/javase/tutorial/essential/io/fileOps.html#glob | ||
*/ | ||
runDirSuite(testdir, Glob("success/mixed[0-9]*.exec")) { args: Array[String] => | ||
assertResult(DemoApp.SUCCESS) { | ||
app.doTxtXml(args) | ||
} | ||
} | ||
|
||
/** | ||
* Test that an exception is thrown. | ||
* | ||
* This is internal assertThrows/intercept, so every execution step | ||
* must throw an exception if multiple steps are run. | ||
*/ | ||
runDirSuite(testdir, Glob("success/singleStepEx[0-9]*.exec")) { args: Array[String] => | ||
assertThrows[RuntimeException]{ | ||
app.doFlaky(args) | ||
} | ||
} | ||
|
||
/** | ||
* First execution steps must succeed, and then | ||
* Last execution step must throw up an exception | ||
* when multiple steps are run | ||
* | ||
* For example: | ||
* exec 0 => assertResult(SUCCESS) | ||
* exec 1 => assertResult(SUCCESS) | ||
* exec 2 => assertThrows[RuntimeException] | ||
*/ | ||
runMultiTestDirSuite(testdir, Glob("success/multiStepEx[0-9]*.exec"))( | ||
{ args: Array[String] => | ||
/* | ||
* All steps at the begin must succeed | ||
*/ | ||
assertResult(DemoApp.SUCCESS) { | ||
app.doFlaky(args) | ||
} | ||
}, | ||
{ args: Array[String] => | ||
/* | ||
* Last step must fail with exception | ||
*/ | ||
assertThrows[RuntimeException] { | ||
app.doFlaky(args) | ||
} | ||
} | ||
) | ||
/** | ||
* First execution steps must succeed, and then | ||
* Last execution step must fail | ||
* | ||
* For example: | ||
* exec 0 => assertResult(SUCCESS) | ||
* exec 1 => assertResult(SUCCESS) | ||
* exec 2 => assertResult(FAILURE) | ||
*/ | ||
runMultiTestDirSuite(testdir, Glob("success/multiStepFail[0-9]*.exec"))( | ||
{ args: Array[String] => | ||
/* | ||
* All steps at the begin must succeed | ||
*/ | ||
assertResult(DemoApp.SUCCESS) { | ||
app.doFlaky(args) | ||
} | ||
}, | ||
{ args: Array[String] => | ||
/* | ||
* Last step must fail | ||
*/ | ||
assertResult(DemoApp.FAILURE) { | ||
app.doFlaky(args) | ||
} | ||
} | ||
) | ||
} |
Oops, something went wrong.