moved to kotest assertions in compilerAst module tests

This commit is contained in:
Irmen de Jong 2021-11-07 21:18:18 +01:00
parent 1fbbed7e23
commit 7e8db16e18
10 changed files with 194 additions and 174 deletions

View File

@ -23,6 +23,7 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-cli:0.3.3'
implementation "com.michael-bull.kotlin-result:kotlin-result-jvm:1.1.12"
testImplementation 'io.kotest:kotest-runner-junit5-jvm:4.6.3'
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testImplementation 'org.hamcrest:hamcrest:2.2'

View File

@ -25,5 +25,8 @@
<orderEntry type="module" module-name="codeOptimizers" />
<orderEntry type="module" module-name="compilerInterfaces" />
<orderEntry type="module" module-name="codeGeneration" />
<orderEntry type="library" name="io.kotest.assertions.core.jvm" level="project" />
<orderEntry type="library" name="io.kotest.property.jvm" level="project" />
<orderEntry type="library" name="io.kotest.runner.junit5.jvm" level="project" />
</component>
</module>

View File

@ -16,8 +16,6 @@ dependencies {
implementation project(':parser')
testImplementation 'io.kotest:kotest-runner-junit5-jvm:4.6.3'
testImplementation "org.jetbrains.kotlin:kotlin-test-junit5"
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
}
configurations.all {

View File

@ -14,7 +14,8 @@
<orderEntry type="module" module-name="parser" />
<orderEntry type="library" name="antlr.antlr4" level="project" />
<orderEntry type="library" name="michael.bull.kotlin.result.jvm" level="project" />
<orderEntry type="library" name="io.kotest.runner.junit5.jvm" level="project" />
<orderEntry type="library" name="io.kotest.assertions.core.jvm" level="project" />
<orderEntry type="library" name="io.kotest.property.jvm" level="project" />
<orderEntry type="library" name="io.kotest.runner.junit5.jvm" level="project" />
</component>
</module>

View File

@ -1,6 +1,13 @@
package prog8tests.ast
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldStartWith
import io.kotest.matchers.types.instanceOf
import prog8.ast.IFunctionCall
import prog8.ast.Module
import prog8.ast.Node
@ -22,7 +29,6 @@ import kotlin.io.path.Path
import kotlin.io.path.isRegularFile
import kotlin.io.path.name
import kotlin.io.path.nameWithoutExtension
import kotlin.test.*
class TestProg8Parser: FunSpec( {
@ -34,14 +40,14 @@ class TestProg8Parser: FunSpec( {
// #40: Prog8ANTLRParser would report (throw) "missing <EOL> at '<EOF>'"
val module = parseModule(src)
assertEquals(1, module.statements.size)
module.statements.size shouldBe 1
}
test("is still accepted - #40, fixed by #45") {
val nl = "\n" // say, Unix-style (different flavours tested elsewhere)
val srcText = "foo {" + nl + "}" + nl // source does end with a newline (issue #40)
val module = parseModule(SourceCode.Text(srcText))
assertEquals(1, module.statements.size)
module.statements.size shouldBe 1
}
}
@ -55,21 +61,21 @@ class TestProg8Parser: FunSpec( {
// GOOD: 2nd block `bar` does start on a new line; however, a nl at the very end ain't needed
val srcGood = "foo {" + nl + "}" + nl + "bar {" + nl + "}"
assertFailsWith<ParseError> { parseModule(SourceCode.Text(srcBad)) }
shouldThrow<ParseError> { parseModule(SourceCode.Text(srcBad)) }
val module = parseModule(SourceCode.Text(srcGood))
assertEquals(2, module.statements.size)
module.statements.size shouldBe 2
}
test("is required between two Blocks or Directives - #47") {
// block and block
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
shouldThrow<ParseError>{ parseModule(SourceCode.Text("""
blockA {
} blockB {
}
""")) }
// block and directive
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
shouldThrow<ParseError>{ parseModule(SourceCode.Text("""
blockB {
} %import textio
""")) }
@ -78,12 +84,12 @@ class TestProg8Parser: FunSpec( {
// Leaving them in anyways.
// dir and block
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
shouldThrow<ParseError>{ parseModule(SourceCode.Text("""
%import textio blockB {
}
""")) }
assertFailsWith<ParseError>{ parseModule(SourceCode.Text("""
shouldThrow<ParseError>{ parseModule(SourceCode.Text("""
%import textio %import syslib
""")) }
}
@ -112,7 +118,7 @@ class TestProg8Parser: FunSpec( {
nlUnix // end with newline (see testModuleSourceNeedNotEndWithNewline)
val module = parseModule(SourceCode.Text(srcText))
assertEquals(2, module.statements.size)
module.statements.size shouldBe 2
}
}
@ -129,7 +135,7 @@ class TestProg8Parser: FunSpec( {
}
"""
val module = parseModule(SourceCode.Text(srcText))
assertEquals(1, module.statements.size)
module.statements.size shouldBe 1
}
test("are ok between blocks - #47") {
@ -145,7 +151,7 @@ class TestProg8Parser: FunSpec( {
}
"""
val module = parseModule(SourceCode.Text(srcText))
assertEquals(2, module.statements.size)
module.statements.size shouldBe 2
}
test("are ok after last block - #47") {
@ -159,7 +165,7 @@ class TestProg8Parser: FunSpec( {
"""
val module = parseModule(SourceCode.Text(srcText))
assertEquals(1, module.statements.size)
module.statements.size shouldBe 1
}
}
@ -170,20 +176,20 @@ class TestProg8Parser: FunSpec( {
val text = "%import ${importedNoExt.name}"
val module = parseModule(SourceCode.Text(text))
assertEquals(1, module.statements.size)
module.statements.size shouldBe 1
}
}
context("EmptySourcecode") {
test("from an empty string should result in empty Module") {
val module = parseModule(SourceCode.Text(""))
assertEquals(0, module.statements.size)
module.statements.size shouldBe 0
}
test("from an empty file should result in empty Module") {
val path = assumeReadableFile(fixturesDir, "empty.p8")
val module = parseModule(SourceCode.File(path))
assertEquals(0, module.statements.size)
module.statements.size shouldBe 0
}
}
@ -196,13 +202,13 @@ class TestProg8Parser: FunSpec( {
val module = parseModule(SourceCode.Text(srcText))
// Note: assertContains has *actual* as first param
assertContains(module.name, Regex("^<String@[0-9a-f\\-]+>$"))
module.name shouldContain Regex("^<String@[0-9a-f\\-]+>$")
}
test("parsed from a file") {
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
val module = parseModule(SourceCode.File(path))
assertEquals(path.nameWithoutExtension, module.name)
module.name shouldBe path.nameWithoutExtension
}
}
@ -216,10 +222,10 @@ class TestProg8Parser: FunSpec( {
expEndCol: Int? = null
) {
require(!listOf(expLine, expStartCol, expEndCol).all { it == null })
if (expLine != null) assertEquals(expLine, actual.line, ".position.line (1-based)")
if (expStartCol != null) assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)")
if (expEndCol != null) assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
if (expFile != null) assertEquals(expFile, actual.file, ".position.file")
if (expLine != null) actual.line shouldBe expLine
if (expStartCol != null) actual.startCol shouldBe expStartCol
if (expEndCol != null) actual.endCol shouldBe expEndCol
if (expFile != null) actual.file shouldBe expFile
}
fun assertPosition(
@ -230,11 +236,10 @@ class TestProg8Parser: FunSpec( {
expEndCol: Int? = null
) {
require(!listOf(expLine, expStartCol, expEndCol).all { it == null })
if (expLine != null) assertEquals(expLine, actual.line, ".position.line (1-based)")
if (expStartCol != null) assertEquals(expStartCol, actual.startCol, ".position.startCol (0-based)")
if (expEndCol != null) assertEquals(expEndCol, actual.endCol, ".position.endCol (0-based)")
// Note: assertContains expects *actual* value first
if (expFile != null) assertContains(actual.file, expFile, ".position.file")
if (expLine != null) actual.line shouldBe expLine
if (expStartCol != null) actual.startCol shouldBe expStartCol
if (expEndCol != null) actual.endCol shouldBe expEndCol
if (expFile != null) actual.file shouldContain expFile
}
fun assertPositionOf(
@ -259,14 +264,14 @@ class TestProg8Parser: FunSpec( {
test("in ParseError from bad string source code") {
val srcText = "bad * { }\n"
val e = assertFailsWith<ParseError> { parseModule(SourceCode.Text(srcText)) }
val e = shouldThrow<ParseError> { parseModule(SourceCode.Text(srcText)) }
assertPosition(e.position, Regex("^<String@[0-9a-f\\-]+>$"), 1, 4, 4)
}
test("in ParseError from bad file source code") {
val path = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8")
val e = assertFailsWith<ParseError> { parseModule(SourceCode.File(path)) }
val e = shouldThrow<ParseError> { parseModule(SourceCode.File(path)) }
assertPosition(e.position, SourceCode.relative(path).toString(), 2, 6)
}
@ -353,8 +358,8 @@ class TestProg8Parser: FunSpec( {
val path = assumeReadableFile(fixturesDir, "simple_main.p8")
val module = parseModule(SourceCode.File(path))
assertSomethingForAllNodes(module) {
assertFalse(Path(it.position.file).isAbsolute)
assertTrue(Path(it.position.file).isRegularFile())
Path(it.position.file).isAbsolute shouldBe false
Path(it.position.file).isRegularFile() shouldBe true
}
}
@ -371,7 +376,7 @@ class TestProg8Parser: FunSpec( {
""".trimIndent()
val module = parseModule(SourceCode.Text(srcText))
assertSomethingForAllNodes(module) {
assertTrue(it.position.file.startsWith(SourceCode.stringSourcePrefix))
it.position.file shouldStartWith SourceCode.stringSourcePrefix
}
}
@ -380,7 +385,7 @@ class TestProg8Parser: FunSpec( {
val resource = SourceCode.Resource("prog8lib/math.p8")
val module = parseModule(resource)
assertSomethingForAllNodes(module) {
assertTrue(it.position.file.startsWith(SourceCode.libraryFilePrefix))
it.position.file shouldStartWith SourceCode.libraryFilePrefix
}
}
}
@ -402,9 +407,9 @@ class TestProg8Parser: FunSpec( {
.statements.filterIsInstance<Subroutine>()[0]
val funCall = startSub.statements.filterIsInstance<IFunctionCall>().first()
assertIs<CharLiteral>(funCall.args[0])
funCall.args[0] shouldBe(instanceOf<CharLiteral>())
val char = funCall.args[0] as CharLiteral
assertEquals('\n', char.value)
char.value shouldBe '\n'
}
test("on rhs of block-level var decl, no AltEnc") {
@ -419,8 +424,8 @@ class TestProg8Parser: FunSpec( {
.statements.filterIsInstance<VarDecl>()[0]
val rhs = decl.value as CharLiteral
assertEquals('x', rhs.value, "char literal's .value")
assertEquals(false, rhs.altEncoding, "char literal's .altEncoding")
rhs.value shouldBe 'x'
rhs.altEncoding shouldBe false
}
test("on rhs of block-level const decl, with AltEnc") {
@ -435,8 +440,8 @@ class TestProg8Parser: FunSpec( {
.statements.filterIsInstance<VarDecl>()[0]
val rhs = decl.value as CharLiteral
assertEquals('x', rhs.value, "char literal's .value")
assertEquals(true, rhs.altEncoding, "char literal's .altEncoding")
rhs.value shouldBe 'x'
rhs.altEncoding shouldBe true
}
test("on rhs of subroutine-level var decl, no AltEnc") {
@ -454,8 +459,8 @@ class TestProg8Parser: FunSpec( {
.statements.filterIsInstance<VarDecl>()[0]
val rhs = decl.value as CharLiteral
assertEquals('x', rhs.value, "char literal's .value")
assertEquals(false, rhs.altEncoding, "char literal's .altEncoding")
rhs.value shouldBe 'x'
rhs.altEncoding shouldBe false
}
test("on rhs of subroutine-level const decl, with AltEnc") {
@ -473,8 +478,8 @@ class TestProg8Parser: FunSpec( {
.statements.filterIsInstance<VarDecl>()[0]
val rhs = decl.value as CharLiteral
assertEquals('x', rhs.value, "char literal's .value")
assertEquals(true, rhs.altEncoding, "char literal's .altEncoding")
rhs.value shouldBe 'x'
rhs.altEncoding shouldBe true
}
}
@ -504,26 +509,26 @@ class TestProg8Parser: FunSpec( {
.statements.filterIsInstance<ForLoop>()
.map { it.iterable }
assertEquals(5, iterables.size)
iterables.size shouldBe 5
val it0 = iterables[0] as RangeExpr
assertIs<StringLiteralValue>(it0.from, "parser should leave it as is")
assertIs<StringLiteralValue>(it0.to, "parser should leave it as is")
it0.from shouldBe instanceOf<StringLiteralValue>()
it0.to shouldBe instanceOf<StringLiteralValue>()
val it1 = iterables[1] as StringLiteralValue
assertEquals("something", it1.value, "parser should leave it as is")
it1.value shouldBe "something"
val it2 = iterables[2] as RangeExpr
assertIs<CharLiteral>(it2.from, "parser should leave it as is")
assertIs<CharLiteral>(it2.to, "parser should leave it as is")
it2.from shouldBe instanceOf<CharLiteral>()
it2.to shouldBe instanceOf<CharLiteral>()
val it3 = iterables[3] as RangeExpr
assertIs<NumericLiteralValue>(it3.from, "parser should leave it as is")
assertIs<NumericLiteralValue>(it3.to, "parser should leave it as is")
it3.from shouldBe instanceOf<NumericLiteralValue>()
it3.to shouldBe instanceOf<NumericLiteralValue>()
val it4 = iterables[4] as RangeExpr
assertIs<NumericLiteralValue>(it4.from, "parser should leave it as is")
assertIs<NumericLiteralValue>(it4.to, "parser should leave it as is")
it4.from shouldBe instanceOf<NumericLiteralValue>()
it4.to shouldBe instanceOf<NumericLiteralValue>()
}
}
@ -532,33 +537,33 @@ class TestProg8Parser: FunSpec( {
val char2 = CharLiteral('z', true, Position.DUMMY)
val program = Program("test", DummyFunctions, DummyMemsizer, AsciiStringEncoder)
assertEquals(65, char1.constValue(program).number.toInt())
assertEquals(122, char2.constValue(program).number.toInt())
char1.constValue(program).number.toInt() shouldBe 65
char2.constValue(program).number.toInt() shouldBe 122
}
test("testLiteralValueComparisons") {
val ten = NumericLiteralValue(DataType.UWORD, 10, Position.DUMMY)
val nine = NumericLiteralValue(DataType.UBYTE, 9, Position.DUMMY)
assertEquals(ten, ten)
assertNotEquals(ten, nine)
assertFalse(ten != ten)
assertTrue(ten != nine)
ten shouldBe ten
nine shouldNotBe ten
(ten != ten) shouldBe false
(ten != nine) shouldBe true
assertTrue(ten > nine)
assertTrue(ten >= nine)
assertTrue(ten >= ten)
assertFalse(ten > ten)
(ten > nine) shouldBe true
(ten >= nine) shouldBe true
(ten >= ten) shouldBe true
(ten > ten) shouldBe false
assertFalse(ten < nine)
assertFalse(ten <= nine)
assertTrue(ten <= ten)
assertFalse(ten < ten)
(ten < nine) shouldBe false
(ten <= nine) shouldBe false
(ten <= ten) shouldBe true
(ten < ten) shouldBe false
val abc = StringLiteralValue("abc", false, Position.DUMMY)
val abd = StringLiteralValue("abd", false, Position.DUMMY)
assertEquals(abc, abc)
assertTrue(abc!=abd)
assertFalse(abc!=abc)
abc shouldBe abc
(abc!=abd) shouldBe true
(abc!=abc) shouldBe false
}
test("testAnonScopeStillContainsVarsDirectlyAfterParse") {
@ -576,12 +581,18 @@ class TestProg8Parser: FunSpec( {
val mainBlock = module.statements.single() as Block
val start = mainBlock.statements.single() as Subroutine
val repeatbody = (start.statements.single() as RepeatLoop).body
assertFalse(mainBlock.statements.any { it is VarDecl }, "no vars moved to main block")
assertFalse(start.statements.any { it is VarDecl }, "no vars moved to start sub")
assertTrue(repeatbody.statements[0] is VarDecl, "var is still in repeat block (anonymousscope)")
withClue("no vars moved to main block") {
mainBlock.statements.any { it is VarDecl } shouldBe false
}
withClue("no vars moved to start sub") {
start.statements.any { it is VarDecl } shouldBe false
}
withClue("\"var is still in repeat block (anonymousscope") {
repeatbody.statements[0] shouldBe instanceOf<VarDecl>()
}
val initvalue = (repeatbody.statements[0] as VarDecl).value as? NumericLiteralValue
assertEquals(99, initvalue?.number?.toInt())
assertTrue(repeatbody.statements[1] is PostIncrDecr)
initvalue?.number?.toInt() shouldBe 99
repeatbody.statements[1] shouldBe instanceOf<PostIncrDecr>()
// the ast processing steps used in the compiler, will eventually move the var up to the containing scope (subroutine).
}
@ -612,6 +623,6 @@ class TestProg8Parser: FunSpec( {
val mainBlock = module.statements.single() as Block
val start = mainBlock.statements.single() as Subroutine
val labels = start.statements.filterIsInstance<Label>()
assertEquals(1, labels.size, "only one label in subroutine scope")
labels.size shouldBe 1
}
})

View File

@ -1,6 +1,9 @@
package prog8tests.ast
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.AnnotationSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import prog8.parser.SourceCode
import prog8.parser.SourceCode.Companion.libraryFilePrefix
import prog8tests.ast.helpers.assumeNotExists
@ -8,7 +11,6 @@ import prog8tests.ast.helpers.assumeReadableFile
import prog8tests.ast.helpers.fixturesDir
import prog8tests.ast.helpers.resourcesDir
import kotlin.io.path.Path
import kotlin.test.*
class TestSourceCode: AnnotationSpec() {
@ -21,30 +23,30 @@ class TestSourceCode: AnnotationSpec() {
val src = SourceCode.Text(text)
val actualText = src.getCharStream().toString()
assertContains(src.origin, Regex("^<String@[0-9a-f\\-]+>$"))
assertEquals(text, actualText)
assertFalse(src.isFromResources)
assertFalse(src.isFromFilesystem)
assertTrue(src.toString().startsWith("prog8.parser.SourceCode"))
src.origin shouldContain Regex("^<String@[0-9a-f\\-]+>$")
actualText shouldBe text
src.isFromResources shouldBe false
src.isFromFilesystem shouldBe false
src.toString().startsWith("prog8.parser.SourceCode") shouldBe true
}
@Test
fun testFromPathWithNonExistingPath() {
val filename = "i_do_not_exist.p8"
val path = assumeNotExists(fixturesDir, filename)
assertFailsWith<NoSuchFileException> { SourceCode.File(path) }
shouldThrow<NoSuchFileException> { SourceCode.File(path) }
}
@Test
fun testFromPathWithMissingExtension_p8() {
val pathWithoutExt = assumeNotExists(fixturesDir,"simple_main")
assumeReadableFile(fixturesDir,"simple_main.p8")
assertFailsWith<NoSuchFileException> { SourceCode.File(pathWithoutExt) }
shouldThrow<NoSuchFileException> { SourceCode.File(pathWithoutExt) }
}
@Test
fun testFromPathWithDirectory() {
assertFailsWith<AccessDeniedException> { SourceCode.File(fixturesDir) }
shouldThrow<AccessDeniedException> { SourceCode.File(fixturesDir) }
}
@Test
@ -53,10 +55,10 @@ class TestSourceCode: AnnotationSpec() {
val path = assumeReadableFile(fixturesDir, filename)
val src = SourceCode.File(path)
val expectedOrigin = SourceCode.relative(path).toString()
assertEquals(expectedOrigin, src.origin)
assertEquals(path.toFile().readText(), src.readText())
assertFalse(src.isFromResources)
assertTrue(src.isFromFilesystem)
src.origin shouldBe expectedOrigin
src.readText() shouldBe path.toFile().readText()
src.isFromResources shouldBe false
src.isFromFilesystem shouldBe true
}
@Test
@ -66,8 +68,8 @@ class TestSourceCode: AnnotationSpec() {
val srcFile = assumeReadableFile(path).toFile()
val src = SourceCode.File(path)
val expectedOrigin = SourceCode.relative(path).toString()
assertEquals(expectedOrigin, src.origin)
assertEquals(srcFile.readText(), src.readText())
src.origin shouldBe expectedOrigin
src.readText() shouldBe srcFile.readText()
}
@Test
@ -76,10 +78,10 @@ class TestSourceCode: AnnotationSpec() {
val srcFile = assumeReadableFile(resourcesDir, pathString).toFile()
val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix/$pathString", src.origin)
assertEquals(srcFile.readText(), src.readText())
assertTrue(src.isFromResources)
assertFalse(src.isFromFilesystem)
src.origin shouldBe "$libraryFilePrefix/$pathString"
src.readText() shouldBe srcFile.readText()
src.isFromResources shouldBe true
src.isFromFilesystem shouldBe false
}
@Test
@ -88,8 +90,8 @@ class TestSourceCode: AnnotationSpec() {
val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile()
val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix$pathString", src.origin)
assertEquals(srcFile.readText(), src.readText())
src.origin shouldBe "$libraryFilePrefix$pathString"
src.readText() shouldBe srcFile.readText()
}
@Test
@ -98,9 +100,9 @@ class TestSourceCode: AnnotationSpec() {
val srcFile = assumeReadableFile(resourcesDir, pathString).toFile()
val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix/$pathString", src.origin)
assertEquals(srcFile.readText(), src.readText())
assertTrue(src.isFromResources, ".isFromResources")
src.origin shouldBe "$libraryFilePrefix/$pathString"
src.readText() shouldBe srcFile.readText()
src.isFromResources shouldBe true
}
@Test
@ -109,8 +111,8 @@ class TestSourceCode: AnnotationSpec() {
val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile()
val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix$pathString", src.origin)
assertEquals(srcFile.readText(), src.readText())
src.origin shouldBe "$libraryFilePrefix$pathString"
src.readText() shouldBe srcFile.readText()
}
@Test
@ -119,9 +121,9 @@ class TestSourceCode: AnnotationSpec() {
val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile()
val src = SourceCode.Resource(pathString)
assertEquals("$libraryFilePrefix/prog8lib/math.p8", src.origin)
assertEquals(srcFile.readText(), src.readText())
assertTrue(src.isFromResources, ".isFromResources")
src.origin shouldBe "$libraryFilePrefix/prog8lib/math.p8"
src.readText() shouldBe srcFile.readText()
src.isFromResources shouldBe true
}
@ -130,13 +132,13 @@ class TestSourceCode: AnnotationSpec() {
val pathString = "/prog8lib/i_do_not_exist"
assumeNotExists(resourcesDir, pathString.substring(1))
assertFailsWith<NoSuchFileException> { SourceCode.Resource(pathString) }
shouldThrow<NoSuchFileException> { SourceCode.Resource(pathString) }
}
@Test
fun testFromResourcesWithNonExistingFile_withoutLeadingSlash() {
val pathString = "prog8lib/i_do_not_exist"
assumeNotExists(resourcesDir, pathString)
assertFailsWith<NoSuchFileException> { SourceCode.Resource(pathString) }
shouldThrow<NoSuchFileException> { SourceCode.Resource(pathString) }
}
}

View File

@ -1,14 +1,12 @@
package prog8tests.ast
import io.kotest.core.spec.style.AnnotationSpec
import io.kotest.matchers.shouldBe
import prog8.ast.base.DataType
import prog8.ast.statements.Block
import prog8.ast.statements.Subroutine
import prog8.parser.Prog8Parser.parseModule
import prog8.parser.SourceCode
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class TestSubroutines: AnnotationSpec() {
@ -31,12 +29,12 @@ class TestSubroutines: AnnotationSpec() {
val mainBlock = module.statements.single() as Block
val asmfunc = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="asmfunc"}
val func = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="func"}
assertTrue(asmfunc.isAsmSubroutine)
assertEquals(DataType.STR, asmfunc.parameters.single().type)
assertTrue(asmfunc.statements.isEmpty())
assertFalse(func.isAsmSubroutine)
assertEquals(DataType.STR, func.parameters.single().type)
assertTrue(func.statements.isEmpty())
asmfunc.isAsmSubroutine shouldBe true
asmfunc.parameters.single().type shouldBe DataType.STR
asmfunc.statements.isEmpty() shouldBe true
func.isAsmSubroutine shouldBe false
func.parameters.single().type shouldBe DataType.STR
func.statements.isEmpty() shouldBe true
}
@Test
@ -57,11 +55,11 @@ class TestSubroutines: AnnotationSpec() {
val mainBlock = module.statements.single() as Block
val asmfunc = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="asmfunc"}
val func = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="func"}
assertTrue(asmfunc.isAsmSubroutine)
assertEquals(DataType.ARRAY_UB, asmfunc.parameters.single().type)
assertTrue(asmfunc.statements.isEmpty())
assertFalse(func.isAsmSubroutine)
assertEquals(DataType.ARRAY_UB, func.parameters.single().type)
assertTrue(func.statements.isEmpty())
asmfunc.isAsmSubroutine shouldBe true
asmfunc.parameters.single().type shouldBe DataType.ARRAY_UB
asmfunc.statements.isEmpty() shouldBe true
func.isAsmSubroutine shouldBe false
func.parameters.single().type shouldBe DataType.ARRAY_UB
func.statements.isEmpty() shouldBe true
}
}

View File

@ -1,10 +1,10 @@
package prog8tests.ast.helpers
import io.kotest.assertions.withClue
import io.kotest.matchers.shouldBe
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.*
import kotlin.test.assertFalse
import kotlin.test.assertTrue
val workingDir = assumeDirectory("").absolute() // Note: "." does NOT work..!
@ -20,7 +20,9 @@ fun createIfNotExists(workingDir: Path, path: String): Path {
}
fun assumeNotExists(path: Path): Path {
assertFalse(path.exists(), "sanity check: should not exist: ${path.absolute()}")
withClue("sanity check: should not exist: ${path.absolute()}") {
path.exists() shouldBe false
}
return path
}
@ -28,12 +30,16 @@ fun assumeNotExists(pathStr: String): Path = assumeNotExists(Path(pathStr))
fun assumeNotExists(path: Path, other: String): Path = assumeNotExists(path / other)
fun assumeReadable(path: Path): Path {
assertTrue(path.isReadable(), "sanity check: should be readable: ${path.absolute()}")
withClue("sanity check: should be readable: ${path.absolute()}") {
path.isReadable() shouldBe true
}
return path
}
fun assumeReadableFile(path: Path): Path {
assertTrue(path.isRegularFile(), "sanity check: should be normal file: ${path.absolute()}")
withClue("sanity check: should be normal file: ${path.absolute()}") {
path.isRegularFile() shouldBe true
}
return assumeReadable(path)
}
@ -44,7 +50,9 @@ fun assumeReadableFile(path: Path, other: String): Path = assumeReadableFile(pat
fun assumeReadableFile(path: Path, other: Path): Path = assumeReadableFile(path / other)
fun assumeDirectory(path: Path): Path {
assertTrue(path.isDirectory(), "sanity check; should be directory: $path")
withClue("sanity check; should be directory: $path") {
path.isDirectory() shouldBe true
}
return path
}

View File

@ -1,12 +1,12 @@
package prog8tests.ast
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import prog8tests.ast.helpers.*
import kotlin.io.path.Path
import kotlin.io.path.div
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
// Do not move into folder helpers/!
@ -22,18 +22,18 @@ class PathsHelpersTests: FunSpec({
test("on non-existing path") {
val path = fixturesDir / "i_do_not_exist"
withClue("should return the path") {
assertEquals(path, assumeNotExists(path))
assumeNotExists(path) shouldBe path
}
}
test("on existing file") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeNotExists(fixturesDir / "simple_main.p8")
}
}
test("on existing directory") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeNotExists(fixturesDir)
}
}
@ -44,19 +44,19 @@ class PathsHelpersTests: FunSpec({
test("on non-existing path") {
val path = fixturesDir / "i_do_not_exist"
withClue("should return the path") {
assertEquals(path, assumeNotExists("$path"))
assumeNotExists("$path") shouldBe path
}
}
test("on existing file") {
val path = fixturesDir / "simple_main.p8"
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeNotExists("$path")
}
}
test("on existing directory") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeNotExists("$fixturesDir")
}
}
@ -67,18 +67,18 @@ class PathsHelpersTests: FunSpec({
test("on non-existing path") {
val path = fixturesDir / "i_do_not_exist"
withClue("should return the path") {
assertEquals(path, assumeNotExists(fixturesDir, "i_do_not_exist"))
assumeNotExists(fixturesDir / "i_do_not_exist") shouldBe path
}
}
test("on existing file") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeNotExists(fixturesDir, "simple_main.p8")
}
}
test("on existing directory") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeNotExists(fixturesDir, "..")
}
}
@ -90,14 +90,14 @@ class PathsHelpersTests: FunSpec({
context("WithOnePathArg") {
test("on non-existing path") {
val path = fixturesDir / "i_do_not_exist"
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory(path)
}
}
test("on existing file") {
val path = fixturesDir / "simple_main.p8"
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory(path)
}
}
@ -105,7 +105,7 @@ class PathsHelpersTests: FunSpec({
test("on existing directory") {
val path = workingDir
withClue("should return the path") {
assertEquals(path, assumeDirectory(path))
assumeDirectory(path) shouldBe path
}
}
}
@ -113,14 +113,14 @@ class PathsHelpersTests: FunSpec({
context("WithOneStringArg") {
test("on non-existing path") {
val path = fixturesDir / "i_do_not_exist"
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory("$path")
}
}
test("on existing file") {
val path = fixturesDir / "simple_main.p8"
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory("$path")
}
}
@ -128,20 +128,20 @@ class PathsHelpersTests: FunSpec({
test("on existing directory") {
val path = workingDir
withClue("should return the path") {
assertEquals(path, assumeDirectory("$path"))
assumeDirectory("$path") shouldBe path
}
}
}
context("WithPathAndStringArgs") {
test("on non-existing path") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory(fixturesDir, "i_do_not_exist")
}
}
test("on existing file") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory(fixturesDir, "simple_main.p8")
}
}
@ -149,20 +149,20 @@ class PathsHelpersTests: FunSpec({
test("on existing directory") {
val path = workingDir / ".."
withClue("should return resulting path") {
assertEquals(path, assumeDirectory(workingDir, ".."))
assumeDirectory(workingDir / "..") shouldBe path
}
}
}
context("WithStringAndStringArgs") {
test("on non-existing path") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory("$fixturesDir", "i_do_not_exist")
}
}
test("on existing file") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory("$fixturesDir", "simple_main.p8")
}
}
@ -170,20 +170,20 @@ class PathsHelpersTests: FunSpec({
test("on existing directory") {
val path = workingDir / ".."
withClue("should return resulting path") {
assertEquals(path, assumeDirectory("$workingDir", ".."))
assumeDirectory(workingDir / "..") shouldBe path
}
}
}
context("WithStringAndPathArgs") {
test("on non-existing path") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory("$fixturesDir", Path("i_do_not_exist"))
}
}
test("on existing file") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeDirectory("$fixturesDir", Path("simple_main.p8"))
}
}
@ -191,7 +191,7 @@ class PathsHelpersTests: FunSpec({
test("on existing directory") {
val path = workingDir / ".."
withClue("should return resulting path") {
assertEquals(path, assumeDirectory("$workingDir", Path("..")))
assumeDirectory(workingDir / Path("..")) shouldBe path
}
}
}
@ -203,7 +203,7 @@ class PathsHelpersTests: FunSpec({
test("on non-existing path") {
val path = fixturesDir / "i_do_not_exist"
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeReadableFile(path)
}
}
@ -211,12 +211,12 @@ class PathsHelpersTests: FunSpec({
test("on readable file") {
val path = fixturesDir / "simple_main.p8"
withClue("should return the path") {
assertEquals(path, assumeReadableFile(path))
assumeReadableFile(path) shouldBe path
}
}
test("on directory") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeReadableFile(fixturesDir)
}
}
@ -226,7 +226,7 @@ class PathsHelpersTests: FunSpec({
test("on non-existing path") {
val path = fixturesDir / "i_do_not_exist"
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeReadableFile("$path")
}
}
@ -234,12 +234,12 @@ class PathsHelpersTests: FunSpec({
test("on readable file") {
val path = fixturesDir / "simple_main.p8"
withClue("should return the resulting path") {
assertEquals(path, assumeReadableFile("$path"))
assumeReadableFile("$path") shouldBe path
}
}
test("on directory") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeReadableFile("$fixturesDir")
}
}
@ -247,7 +247,7 @@ class PathsHelpersTests: FunSpec({
context("WithPathAndStringArgs") {
test("on non-existing path") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeReadableFile(fixturesDir, "i_do_not_exist")
}
}
@ -255,12 +255,12 @@ class PathsHelpersTests: FunSpec({
test("on readable file") {
val path = fixturesDir / "simple_main.p8"
withClue("should return the resulting path") {
assertEquals(path, assumeReadableFile(fixturesDir, "simple_main.p8"))
assumeReadableFile(fixturesDir / "simple_main.p8") shouldBe path
}
}
test("on directory") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeReadableFile(fixturesDir, "..")
}
}
@ -268,19 +268,19 @@ class PathsHelpersTests: FunSpec({
context("WithPathAndPathArgs") {
test("on non-existing path") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeReadableFile(fixturesDir, Path("i_do_not_exist"))
}
}
test("on readable file") {
withClue("should return the resulting path") {
assertEquals(fixturesDir / "simple_main.p8", assumeReadableFile(fixturesDir, Path("simple_main.p8")))
assumeReadableFile(fixturesDir / Path("simple_main.p8")) shouldBe fixturesDir / "simple_main.p8"
}
}
test("on directory") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeReadableFile(fixturesDir, Path(".."))
}
}
@ -288,19 +288,19 @@ class PathsHelpersTests: FunSpec({
context("WithStringAndStringArgs") {
test("on non-existing path") {
assertFailsWith<java.lang.AssertionError> {
shouldThrow<java.lang.AssertionError> {
assumeReadableFile("$fixturesDir", "i_do_not_exist")
}
}
test("on readable file") {
withClue("should return the resulting path") {
assertEquals( fixturesDir / "simple_main.p8", assumeReadableFile(fixturesDir.toString(), "simple_main.p8"))
assumeReadableFile(fixturesDir / "simple_main.p8") shouldBe fixturesDir / "simple_main.p8"
}
}
test("on directory") {
assertFailsWith<AssertionError> {
shouldThrow<AssertionError> {
assumeReadableFile("$fixturesDir", "..")
}
}

View File

@ -3,8 +3,6 @@ TODO
For next compiler release (7.3)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- replace all old asserts in compilerAst tests by kotest equivalents
- remove kotlin.test and junit dependencies in compilerAst module
- migrate Compiler tests to KoTest library (this will close github issue #70)
...