-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dhruv Rajvanshi
authored and
Dhruv Rajvanshi
committed
Mar 26, 2023
1 parent
a2a316a
commit cb855dd
Showing
6 changed files
with
129 additions
and
9 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
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,28 @@ | ||
package hadesc | ||
|
||
import hadesc.location.SourcePath | ||
import hadesc.parser.Lexer | ||
import hadesc.text.Text | ||
import kotlin.io.path.Path | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class LexerTests { | ||
@Test | ||
fun `lexer offset should be correct`() { | ||
val text = "def foo bar (" | ||
val lexer = makeLexer(text) | ||
assertEquals(0, lexer.offset) | ||
|
||
lexer.nextToken() | ||
assertEquals(3, lexer.offset) | ||
|
||
lexer.nextToken() | ||
assertEquals(7, lexer.offset) | ||
|
||
} | ||
|
||
|
||
} | ||
private fun makeLexer(text: String) = | ||
Lexer(SourcePath(Path("test.hds")), Text.from(text)) |
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,75 @@ | ||
package hadesc | ||
|
||
import hadesc.context.Context | ||
import hadesc.location.SourcePath | ||
import hadesc.parser.Parser | ||
import hadesc.qualifiedname.QualifiedName | ||
import hadesc.text.Text | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import org.junit.jupiter.api.Test | ||
import kotlin.io.path.Path | ||
import kotlin.test.assertEquals | ||
|
||
class ParserTests { | ||
private val ctx = mockk<Context> { | ||
every { resolver } returns mockk { | ||
every { onParseBlock(any()) } returns unit | ||
every { onParseSourceFile(any()) } returns unit | ||
every { onParseDeclaration(any()) } returns unit | ||
every { onParseClosure(any()) } returns unit | ||
every { onParseMatchArm(any()) } returns unit | ||
every { onParseMatchExpression(any()) } returns unit | ||
every { onParseScopeNode(any()) } returns unit | ||
} | ||
every { makeName(any()) } answers { Name(invocation.args[0] as String) } | ||
} | ||
|
||
@Test | ||
fun `sourceFiles have correct length`() { | ||
val text = """ | ||
def foo(): Void {} | ||
""".trimIndent() | ||
val parser = makeParser(text) | ||
val sourceFile = parser.parseSourceFile() | ||
|
||
assertEquals( | ||
text.length, | ||
sourceFile.length, | ||
) | ||
} | ||
|
||
@Test | ||
fun `Source files with leading comments have correct length`() { | ||
val text = """ | ||
// this is a comment | ||
def foo(): Void {} | ||
""".trimIndent() | ||
val parser = makeParser(text) | ||
val sourceFile = parser.parseSourceFile() | ||
|
||
assertEquals( | ||
text.length, | ||
sourceFile.length | ||
) | ||
} | ||
|
||
@Test | ||
fun `Source files with trailing comments have the correct length`() { | ||
val text = """ | ||
// this is a comment | ||
def foo(): Void {} | ||
// a trailing comment | ||
""".trimIndent() | ||
val parser = makeParser(text) | ||
val sourceFile = parser.parseSourceFile() | ||
|
||
assertEquals( | ||
text.length + 1, // 1 for EOF character | ||
sourceFile.length | ||
) | ||
} | ||
|
||
private fun makeParser(source: String) = | ||
Parser(ctx, QualifiedName(emptyList()), SourcePath(Path("test.hds")), Text.from(source)) | ||
} |