-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests, testrunner and gradle task
run-tests
- Loading branch information
Showing
16 changed files
with
949 additions
and
8 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 |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# krisp | ||
A Lisp in Kotlin | ||
|
||
A Lisp in Kotlin | ||
|
||
## Build | ||
|
||
$ gradle build | ||
|
||
## Start (REPL) | ||
|
||
$ bin/krisp-repl.sh | ||
|
||
## Run tests | ||
|
||
$ gradle run-tests |
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,4 @@ | ||
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" | ||
|
||
java -jar "${SCRIPT_DIR}/../build/libs/krisp-0.0.1-SNAPSHOT.jar" | ||
|
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.ninjacontrol.krisp | ||
|
||
class AllTests : TestSuite { | ||
|
||
override val name = "All tests" | ||
|
||
private val testSuites = listOf( | ||
EnvironmentTest(), | ||
NamespaceTest(), | ||
StringTest(), | ||
EvaluationTest(), | ||
FunctionsTest(), | ||
QuoteTest(), | ||
MacroTest(), | ||
MetadataTest() | ||
) | ||
|
||
override fun getTests() = testSuites.flatMap { testSuite -> testSuite.getTests() } | ||
|
||
override fun run(): Boolean { | ||
val only = getTests().filter { it.only } | ||
val suite = when { | ||
only.isNotEmpty() -> listOf(CustomSuite(testCases = only)) | ||
else -> testSuites | ||
} | ||
return suite.map { testSuite -> | ||
log(testSuite.name) | ||
testSuite.run() | ||
} | ||
.reduce { acc, result -> acc && 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,79 @@ | ||
package com.ninjacontrol.krisp | ||
|
||
class AssertionException(message: String, context: String? = null) : | ||
Throwable(context?.let { "$context: $message" } ?: message) | ||
|
||
fun assertNeverExecuted() { | ||
throw AssertionException("assertion failed, should never execute!") | ||
} | ||
|
||
fun fail(message: String) { | ||
throw AssertionException("test failed: $message") | ||
} | ||
|
||
fun assertTrue(a: Boolean, context: String? = null) { | ||
if (!a) throw AssertionException( | ||
"assertion failed, value is false", | ||
context | ||
) | ||
} | ||
|
||
fun assertFalse(a: Boolean, context: String? = null) { | ||
if (a) throw AssertionException( | ||
"assertion failed, value is true", | ||
context | ||
) | ||
} | ||
|
||
fun assertNotNull(a: Any?, context: String? = null) { | ||
if (a == null) throw AssertionException("assertion failed, value is null", context) | ||
} | ||
|
||
fun assertNull(a: Any?, context: String? = null) { | ||
if (a != null) throw AssertionException("assertion failed, value is not null", context) | ||
} | ||
|
||
fun assertNil(a: MalType, context: String? = null) { | ||
if (a !is MalNil) throw AssertionException("assertion failed, $a is not NIL", context) | ||
} | ||
|
||
fun assertError(a: MalType, context: String? = null) { | ||
if (a !is MalError) throw AssertionException("assertion failed, $a is not an error", context) | ||
} | ||
|
||
fun assertNonError(a: MalType, context: String? = null) { | ||
if (a is MalError) throw AssertionException("assertion failed, $a is an error", context) | ||
} | ||
|
||
fun assertEqual(a: String, b: String, context: String? = null) { | ||
if (a != b) throw AssertionException("assertion failed, \"$a\" is not equal to \"$b\"", context) | ||
} | ||
|
||
fun assertEqual(a: Int, b: Int, context: String? = null) { | ||
if (a != b) throw AssertionException("assertion failed, $a is not equal to $b", context) | ||
} | ||
|
||
fun assertEqual(a: Char, b: Char, context: String? = null) { | ||
if (a != b) throw AssertionException("assertion failed, '$a' is not equal to '$b'", context) | ||
} | ||
|
||
fun assertEqual(a: MalType, b: MalType, context: String? = null) { | ||
if (!isEqual(a, b)) throw AssertionException("assertion failed, $a is not equal to $b", context) | ||
} | ||
|
||
fun assertReadEval( | ||
input: String, | ||
result: MalType, | ||
env: Environment = replExecutionEnv, | ||
context: String? = null | ||
) { | ||
val ast = re(input, env) | ||
if (!isEqual( | ||
ast, | ||
result | ||
) | ||
) throw AssertionException( | ||
"assertion failed, expected '$input' to result in $result, but was $ast instead", | ||
context | ||
) | ||
} |
164 changes: 164 additions & 0 deletions
164
src/test/kotlin/com/ninjacontrol/krisp/EnvironmentTest.kt
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,164 @@ | ||
package com.ninjacontrol.krisp | ||
|
||
class EnvironmentTest : TestSuite { | ||
|
||
override val name = "Environment" | ||
|
||
private val tests = listOf( | ||
|
||
test { | ||
description = "Initialize environment" | ||
verify = { | ||
val env = Environment() | ||
assertNotNull(env) | ||
} | ||
}, | ||
test { | ||
description = "Initialize environment with bindings" | ||
verify = { | ||
val env = Environment.withBindings( | ||
outer = null, | ||
bindings = listOf( | ||
symbol("foo"), | ||
symbol("bar"), | ||
symbol("baz") | ||
), | ||
expressions = listOf( | ||
symbol("xux"), | ||
symbol("bug"), | ||
symbol("xug") | ||
) | ||
) | ||
assertEqual(env.get(symbol("foo")), symbol("xux")) | ||
assertEqual(env.get(symbol("bar")), symbol("bug")) | ||
assertEqual(env.get(symbol("baz")), symbol("xug")) | ||
} | ||
}, | ||
test { | ||
description = | ||
"Initialize environment with variadic parameters, 2 bindings, 3 expressions" | ||
verify = { | ||
val env = Environment.withBindings( | ||
outer = null, | ||
bindings = listOf( | ||
symbol("foo"), | ||
symbol("&"), | ||
symbol("baz") | ||
), | ||
expressions = listOf( | ||
symbol("xux"), | ||
symbol("bug"), | ||
symbol("xug") | ||
) | ||
) | ||
assertNotNull(env) | ||
assertEqual(env.get(symbol("foo")), symbol("xux")) | ||
assertEqual(env.get(symbol("baz")), list(symbol("bug"), symbol("xug"))) | ||
} | ||
}, | ||
test { | ||
description = | ||
"Initialize environment with variadic parameters, 2 bindings, 2 expressions" | ||
verify = { | ||
val env = Environment.withBindings( | ||
outer = null, | ||
bindings = listOf( | ||
symbol("foo"), | ||
symbol("&"), | ||
symbol("baz") | ||
), | ||
expressions = listOf( | ||
symbol("xux"), | ||
symbol("xug") | ||
) | ||
) | ||
assertNotNull(env) | ||
assertEqual(env.get(symbol("foo")), symbol("xux")) | ||
assertEqual(env.get(symbol("baz")), list(symbol("xug"))) | ||
} | ||
}, | ||
test { | ||
description = | ||
"Initialize environment with variadic parameters, 5 bindings, 12 expressions" | ||
verify = { | ||
val env = Environment.withBindings( | ||
outer = null, | ||
bindings = listOf( | ||
symbol("a"), | ||
symbol("b"), | ||
symbol("c"), | ||
symbol("d"), | ||
symbol("&"), | ||
symbol("e"), | ||
), | ||
expressions = listOf( | ||
symbol("a1"), | ||
symbol("b1"), | ||
symbol("c1"), | ||
symbol("d1"), | ||
symbol("e1"), | ||
symbol("e2"), | ||
symbol("e3"), | ||
symbol("e4"), | ||
symbol("e5"), | ||
symbol("e6"), | ||
symbol("e7"), | ||
symbol("e8"), | ||
) | ||
) | ||
assertNotNull(env) | ||
assertEqual(env.get(symbol("a")), symbol("a1")) | ||
assertEqual(env.get(symbol("b")), symbol("b1")) | ||
assertEqual(env.get(symbol("c")), symbol("c1")) | ||
assertEqual(env.get(symbol("d")), symbol("d1")) | ||
assertEqual( | ||
env.get(symbol("e")), | ||
list( | ||
symbol("e1"), | ||
symbol("e2"), | ||
symbol("e3"), | ||
symbol("e4"), | ||
symbol("e5"), | ||
symbol("e6"), | ||
symbol("e7"), | ||
symbol("e8"), | ||
) | ||
) | ||
} | ||
}, | ||
test { | ||
description = "Add symbol" | ||
verify = { | ||
val env = Environment() | ||
val foo = symbol("foo") | ||
val bar = symbol("bar") | ||
env.set(foo, bar) | ||
assertEqual(env.get(foo), bar) | ||
} | ||
}, | ||
test { | ||
description = "Find symbol in environment, symbol found" | ||
verify = { | ||
val env = Environment() | ||
val foo = symbol("foo") | ||
env.set(foo, foo) | ||
env.find(foo) | ||
assertTrue(env.find(foo) == env) | ||
} | ||
}, | ||
test { | ||
description = "Find symbol in environment, symbol not found" | ||
verify = { | ||
val env = Environment() | ||
val foo = symbol("foo") | ||
env.find(foo) | ||
assertNull(env.find(foo)) | ||
} | ||
}, | ||
|
||
) | ||
|
||
override fun getTests(): List<TestCase> = tests | ||
override fun run(): Boolean = | ||
verifyTests(tests) | ||
} |
Oops, something went wrong.