From f58ec5a12ca641feea12309bacf2323b47fa9921 Mon Sep 17 00:00:00 2001 From: Pavel Talanov Date: Tue, 21 Feb 2012 19:36:53 +0400 Subject: [PATCH] added some test files that dropped were not tracked by git by accident removed redundant ktSrc of module Translator --- jslib/src/helper/ip.kt | 7 ++++ jslib/src/html5/files.kt | 37 +++++++++++++++++++ .../webDemoExamples1/cases/forLoop.kt | 9 +++++ .../webDemoExamples1/cases/ifAsExpression.kt | 5 +++ .../webDemoExamples1/cases/isCheck.kt | 10 +++++ .../cases/multiLanguageHello.kt | 9 +++++ .../webDemoExamples1/cases/nullChecks.kt | 23 ++++++++++++ .../cases/objectOrientedHello.kt | 10 +++++ .../webDemoExamples1/cases/patternMatching.kt | 18 +++++++++ .../webDemoExamples1/cases/printArg.kt | 3 ++ .../webDemoExamples1/cases/ranges.kt | 35 ++++++++++++++++++ .../webDemoExamples1/cases/whileLoop.kt | 5 +++ 12 files changed, 171 insertions(+) create mode 100644 jslib/src/helper/ip.kt create mode 100644 jslib/src/html5/files.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/forLoop.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/ifAsExpression.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/isCheck.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/multiLanguageHello.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/nullChecks.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/objectOrientedHello.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/patternMatching.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/printArg.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/ranges.kt create mode 100644 translator/testFiles/webDemoExamples1/cases/whileLoop.kt diff --git a/jslib/src/helper/ip.kt b/jslib/src/helper/ip.kt new file mode 100644 index 00000000000..d6f47cc8296 --- /dev/null +++ b/jslib/src/helper/ip.kt @@ -0,0 +1,7 @@ +package ip.helper + +import html5.HTMLInputElement +import js.native + +native +fun getInputElement() : HTMLInputElement = js.noImpl \ No newline at end of file diff --git a/jslib/src/html5/files.kt b/jslib/src/html5/files.kt new file mode 100644 index 00000000000..8e05d2af4ef --- /dev/null +++ b/jslib/src/html5/files.kt @@ -0,0 +1,37 @@ +package html5.files + +import js.native + +native +class FileReader() { + var onloadend : ((FileReaderEvent)->Unit)? = js.noImpl // + fun readAsDataURL(blob : Blob) = js.noImpl + val result : File = js.noImpl +} + +native +class FileReaderEvent() { + val target : FileReader = js.noImpl +} + +native +class FileList() { + fun item(index : Int) : File? = js.noImpl + val length : Int = js.noImpl +} + +native +class File() : Blob() { + val name : String = js.noImpl + // readonly attribute Date lastModifiedDate; +} + +native +open class Blob() { + val size : Int = js.noImpl + val `type` : String = js.noImpl + //Blob slice(optional long long start, + //optional long long end, + //optional DOMString contentType); + // +} diff --git a/translator/testFiles/webDemoExamples1/cases/forLoop.kt b/translator/testFiles/webDemoExamples1/cases/forLoop.kt new file mode 100644 index 00000000000..cda1f388f83 --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/forLoop.kt @@ -0,0 +1,9 @@ +fun main(args : Array) { + for (arg in args) + println(arg) + + // or + println() + for (i in args.indices) + println(args[i]) +} diff --git a/translator/testFiles/webDemoExamples1/cases/ifAsExpression.kt b/translator/testFiles/webDemoExamples1/cases/ifAsExpression.kt new file mode 100644 index 00000000000..52982f586ee --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/ifAsExpression.kt @@ -0,0 +1,5 @@ +fun main(args : Array) { + println(max(parseInt(args[0]), parseInt(args[1]))) +} + +fun max(a : Int, b : Int) = if (a > b) a else b \ No newline at end of file diff --git a/translator/testFiles/webDemoExamples1/cases/isCheck.kt b/translator/testFiles/webDemoExamples1/cases/isCheck.kt new file mode 100644 index 00000000000..f12f411b660 --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/isCheck.kt @@ -0,0 +1,10 @@ +fun main(args : Array) { + println(getStringLength("aaa")) + println(getStringLength(1)) +} + +fun getStringLength(obj : Any) : Int? { + if (obj is String) + return obj.length // no cast to String is needed + return null +} \ No newline at end of file diff --git a/translator/testFiles/webDemoExamples1/cases/multiLanguageHello.kt b/translator/testFiles/webDemoExamples1/cases/multiLanguageHello.kt new file mode 100644 index 00000000000..d358e13556a --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/multiLanguageHello.kt @@ -0,0 +1,9 @@ +fun main(args : Array) { + val language = if (args.size == 0) "EN" else args[0] + println(when (language) { + "EN" -> "Hello!" + "FR" -> "Salut!" + "IT" -> "Ciao!" + else -> "Sorry, I can't greet you in $language yet" + }) +} diff --git a/translator/testFiles/webDemoExamples1/cases/nullChecks.kt b/translator/testFiles/webDemoExamples1/cases/nullChecks.kt new file mode 100644 index 00000000000..649445ff33a --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/nullChecks.kt @@ -0,0 +1,23 @@ +// Return null if str does not hold a number +fun myParseInt(str : String) : Int? { + try{ + return parseInt(str) + } catch (e: NumberFormatException) { + println("One of argument isn't Int") + } + return null +} + +fun main(args : Array) { + if (args.size < 2) { + print("No number supplied"); + } else { + val x = myParseInt(args[0]) + val y = myParseInt(args[1]) + + // We cannot say 'x * y' now because they may hold nulls + if (x != null && y != null) { + print(x * y) // Now we can + } + } +} diff --git a/translator/testFiles/webDemoExamples1/cases/objectOrientedHello.kt b/translator/testFiles/webDemoExamples1/cases/objectOrientedHello.kt new file mode 100644 index 00000000000..04f7e19b7be --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/objectOrientedHello.kt @@ -0,0 +1,10 @@ +class Greeter(name : String) { + val name = name + fun greet() { + println("Hello, ${name}!"); + } +} + +fun main(args : Array) { + Greeter(args[0]).greet() +} \ No newline at end of file diff --git a/translator/testFiles/webDemoExamples1/cases/patternMatching.kt b/translator/testFiles/webDemoExamples1/cases/patternMatching.kt new file mode 100644 index 00000000000..7882247f3ef --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/patternMatching.kt @@ -0,0 +1,18 @@ +fun main(args : Array) { + cases("Hello") + cases(1) + cases(MyClass()) + cases("hello") +} + +fun cases(obj : Any) { + when(obj) { + 1 -> println("One") + "Hello" -> println("Greeting") + !is String -> println("Not a string") + else -> println("Unknown") + } +} + +class MyClass() { +} diff --git a/translator/testFiles/webDemoExamples1/cases/printArg.kt b/translator/testFiles/webDemoExamples1/cases/printArg.kt new file mode 100644 index 00000000000..b4a6c70535b --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/printArg.kt @@ -0,0 +1,3 @@ +fun main(args : Array) { + print(args[0]); +} diff --git a/translator/testFiles/webDemoExamples1/cases/ranges.kt b/translator/testFiles/webDemoExamples1/cases/ranges.kt new file mode 100644 index 00000000000..c397c8fa28d --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/ranges.kt @@ -0,0 +1,35 @@ +import java.util.ArrayList; + +fun main(args : Array) { + val x = parseInt(args[0]) + //Check if a number lies within a range: + val y = 10 + if (x in 1..y-1) + println("OK") + + //Iterate over a range: + for (a in 1..5) + print(" ${a}") + + //Check if a number is out of range: + println() + val array = ArrayList(); + + array.add("aaa") + array.add("bbb") + array.add("ccc") + + if (x !in 0..array.size()) + println("Out: array has only ${array.size()} elements. x = ${x}") + + //Check if a collection contains an object: + if ("aaa" in array) // collection.contains(obj) is called + println("Yes: array contains aaa") + + if ("ddd" in array) // collection.contains(obj) is called + println("Yes: array contains ddd") + else + println("No: array doesn't contains ddd") +} + + diff --git a/translator/testFiles/webDemoExamples1/cases/whileLoop.kt b/translator/testFiles/webDemoExamples1/cases/whileLoop.kt new file mode 100644 index 00000000000..727b0a81c57 --- /dev/null +++ b/translator/testFiles/webDemoExamples1/cases/whileLoop.kt @@ -0,0 +1,5 @@ +fun main(args : Array) { + var i = 0 + while (i < args.size) + println(args[i++]) +}