Skip to content
This repository was archived by the owner on Nov 6, 2019. It is now read-only.

Commit 37e16b5

Browse files
epabstSchahen
authored andcommitted
Test all of testData via Kotlin
1 parent 63b2e7a commit 37e16b5

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ tasks {
9090
}
9191
into("$projectDir/node_modules")
9292
}
93-
val npmModules = arrayOf("jest")
93+
val npmModules = arrayOf("jest", "fs", "path", "process", "typescript")
9494
val installTestNpmModules = task<NpmTask>("installTestNpmModules") {
9595
setNpmCommand("install", *npmModules)
9696
}

src/main/kotlin/node/node.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ external object fs {
3535
fun argv(index: Int): String
3636
fun writeFileSync(path: String, text: String): Unit
3737
fun readFileSync(path: String): String
38+
fun readFileSync(path: String, encodingOptions: EncodingOptions): String
39+
fun readdirSync(path: String): Array<String>
3840
fun existsSync(path: String): Boolean
3941
fun mkdirSync(path: String): Unit
4042
}
@@ -43,5 +45,7 @@ external object module {
4345
val parent: Any?
4446
}
4547

48+
class EncodingOptions(val encoding: String = "utf8")
49+
4650
external val __dirname: String
4751
external val __filename: String
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ts2kt
2+
3+
import node.EncodingOptions
4+
import node.fs
5+
import kotlin.test.Test
6+
import kotlin.test.assertEquals
7+
8+
class RunnerTest {
9+
val TS_EXT = ".ts"
10+
val KT_EXT = ".kt"
11+
val UNVERIFIED_FILE_PREFIX = "// OUT:"
12+
13+
@Test
14+
fun translateToFile() {
15+
translateAndVerifyDirectory("testData")
16+
}
17+
18+
private fun translateAndVerifyDirectory(path: String) {
19+
console.info("Looking in $path")
20+
fs.readdirSync(path).forEach { file ->
21+
if (file.endsWith(".ts")) {
22+
translateToFileAndVerify(path + "/" + file)
23+
} else if (!file.contains(".")) {
24+
translateAndVerifyDirectory(path + "/" + file)
25+
}
26+
}
27+
}
28+
29+
fun translateToFileAndVerify(srcPath: String, expectedPath: String = srcPath.replace(TS_EXT, KT_EXT)) {
30+
val outPath = expectedPath + ".out"
31+
console.log("\n--------\nsrcPath = " + srcPath +
32+
", outPath = " + outPath +
33+
", expectedPath = " + expectedPath + "\n")
34+
translateToFile(srcPath, outPath)
35+
val encodingOptions = EncodingOptions(encoding = "utf8")
36+
val actual = fs.readFileSync(outPath, encodingOptions)
37+
38+
val expected: String?
39+
if (!fs.existsSync(expectedPath)) {
40+
expected = UNVERIFIED_FILE_PREFIX + "\n" + actual
41+
fs.writeFileSync(expectedPath, expected)
42+
} else {
43+
expected = fs.readFileSync(expectedPath, encodingOptions)
44+
}
45+
assertEquals(expected, actual, "expectedPath: $expectedPath, outPath=$outPath")
46+
}
47+
}

0 commit comments

Comments
 (0)