JS lib: introduced different output strategies, and chose them depending of environment.

#KT-2994 Fixed
This commit is contained in:
Zalim Bashorov
2014-10-13 18:04:18 +04:00
parent 40e1292e9a
commit 55c19a632a
6 changed files with 168 additions and 33 deletions
@@ -0,0 +1,63 @@
package foo
val EXPECTED = """Hello, World
***
####
"""
val EXPECTED_NEWLINE_FOR_EACH = """Hello
, World
***
##
##
"""
native
fun eval(code: String): Any? = noImpl
native
var buffer: String = noImpl
fun test(expected: String, initCode: String, getResult: () -> String) {
buffer = ""
eval("Kotlin.out = new $initCode")
print("Hello")
print(", World")
print("\n")
println()
println("***")
print("##")
print("##")
println()
val actual = getResult()
assertEquals(expected, actual, initCode)
}
fun box(): String {
test(EXPECTED, "Kotlin.NodeJsOutput(outputStream)") {
buffer
}
test(EXPECTED_NEWLINE_FOR_EACH, "Kotlin.OutputToConsoleLog()") {
buffer
}
test(EXPECTED, "Kotlin.BufferedOutput()") {
eval("Kotlin.out.buffer") as String
}
test(EXPECTED, "Kotlin.BufferedOutputToConsoleLog()") {
buffer
}
return "OK"
}
@@ -0,0 +1,24 @@
var buffer = "";
function writeToBuffer(a) {
var type = typeof a;
if (type !== "string") throw Error("Expected string argument type, but got: " + type);
buffer += a;
}
function writelnToBuffer(a) {
writeToBuffer(a);
writeToBuffer("\n");
}
var GLOBAL = (0, eval)("this");
GLOBAL.console = {
log: writelnToBuffer
};
GLOBAL.outputStream = {
write: writeToBuffer
};