Ant task - <run-kotlinc> wrapper + "compilation-fail-tests" task testing <kotlinc> fails if compilation fails

This commit is contained in:
Evgeny Goldin
2012-01-17 01:19:10 +02:00
parent c39f078e35
commit 292366b310
2 changed files with 109 additions and 10 deletions
+50 -10
View File
@@ -42,6 +42,27 @@
</macrodef>
<!-- Runs <kotlinc> task specifying either "srcdir" or "file" attribute -->
<macrodef name="run-kotlinc">
<attribute name="file" default=""/>
<attribute name="srcdir" default=""/>
<sequential>
<delete dir = "${tests-dir}" includes = "**/*"/>
<if>
<equals arg1="@{file}" arg2=""/>
<then>
<kotlinc destdir = "${tests-dir}" srcdir = "@{srcdir}"/>
</then>
<else>
<kotlinc destdir = "${tests-dir}" file = "@{file}"/>
</else>
</if>
</sequential>
</macrodef>
<!-- Runs <java> for compiled classes and verifies the output correctness -->
<macrodef name="run-java">
<attribute name="out"/>
@@ -84,13 +105,10 @@
<attribute name="args" default=""/>
<attribute name="out"/>
<sequential>
<delete dir = "${tests-dir}" includes = "**/*"/>
<kotlinc destdir = "${tests-dir}" file = "@{root}/Hello.kt"/>
<run-java args = "@{args}" out = "@{out}"/>
<delete dir = "${tests-dir}" includes = "**/*"/>
<kotlinc destdir = "${tests-dir}" srcdir = "@{root}"/>
<run-java args = "@{args}" out = "@{out}"/>
<run-kotlinc file = "@{root}/Hello.kt"/>
<run-java args = "@{args}" out = "@{out}"/>
<run-kotlinc srcdir = "@{root}"/>
<run-java args = "@{args}" out = "@{out}"/>
</sequential>
</macrodef>
@@ -111,9 +129,7 @@
<!-- Compiles, runs and verifies the output of web demo longer examples -->
<macrodef name="longer-examples-tests">
<sequential>
<delete dir = "${tests-dir}" includes = "**/*"/>
<kotlinc destdir = "${tests-dir}" srcdir = "${basedir}/build-tools/test/longer-examples" />
<run-kotlinc srcdir = "${basedir}/build-tools/test/longer-examples" />
<run-java classname = "bottles.namespace" equals="false" out="12 bottles of beer on the wall, 12 bottles of beer." />
<run-java classname = "maze.namespace" equals="false" out="O ~OOOOOOOOOOOOOO"/>
<run-java classname = "life.namespace" equals="false" out="*** ** ** ***"/>
@@ -122,6 +138,29 @@
</macrodef>
<!-- Verifies build fails if <kotlinc> fails -->
<macrodef name="compilation-fail-test">
<sequential>
<var name="failed" value="false"/>
<trycatch property="error-message" reference="bar">
<try>
<!-- Should fail -->
<run-kotlinc srcdir = "${basedir}/build-tools/test/compilation-fail"/>
</try>
<catch>
<var name="failed" value="true"/>
</catch>
</trycatch>
<if>
<isfalse value="${failed}"/>
<then>
<fail message="Compilation should have failed!"/>
</then>
</if>
</sequential>
</macrodef>
<!-- Creates build tools distribution jar -->
<target name="buildToolsJar" depends="compile">
<mkdir dir ="${output}/classes/buildTools"/>
@@ -152,5 +191,6 @@
<setup-kotlin-home/>
<hello-tests/>
<longer-examples-tests/>
<compilation-fail-test/>
</target>
</project>
@@ -0,0 +1,59 @@
/**
* "Bottles.kt" that doesn't compile, see line 6.
*/
package bottles
fun main(args : Array<String>
if (args.isEmpty) {
printBottles(99)
}
else {
try {
printBottles(Integer.parseInt(args[0]))
}
catch (e : NumberFormatException) {
System.err?.println("You have passed '${args[0]}' as a number of bottles, " +
"but it is not a valid integral number")
}
}
}
fun printBottles(bottleCount : Int) {
if (bottleCount <= 0) {
println("No bottles - no song")
return
}
println("The \"${bottlesOfBeer(bottleCount)}\" song\n")
var bottles = bottleCount
while (bottles > 0) {
val bottlesOfBeer = bottlesOfBeer(bottles)
print("$bottlesOfBeer on the wall, $bottlesOfBeer.\nTake one down, pass it around, ")
bottles--
println("${bottlesOfBeer(bottles)} on the wall.\n")
}
println("No more bottles of beer on the wall, no more bottles of beer.\n" +
"Go to the store and buy some more, ${bottlesOfBeer(bottleCount)} on the wall.")
}
fun bottlesOfBeer(count : Int) : String =
when (count) {
0 -> "no more bottles"
1 -> "1 bottle"
else -> "$count bottles"
} + " of beer"
/*
* An excerpt from the Standard Library
*/
// From the std.io package
// These are simple functions that wrap standard Java API calls
fun print(message : String) { System.out?.print(message) }
fun println(message : String) { System.out?.println(message) }
// From the std package
// This is an extension property, i.e. a property that is defined for the
// type Array<T>, but does not sit inside the class Array
val <T> Array<T>.isEmpty : Boolean get() = size == 0