[JS TESTS] Rewrite web demo tests using new test infrastructure

This commit is contained in:
Ivan Kylchik
2021-10-20 22:15:16 +03:00
parent af0e40a0d2
commit 8a1362de03
45 changed files with 369 additions and 579 deletions
@@ -1,3 +1,5 @@
// MAIN_ARGS: [2]
/**
* This example implements the famous "99 Bottles of Beer" program
* See http://99-bottles-of-beer.net/
@@ -1,3 +1,5 @@
// MAIN_ARGS: [over9000]
/**
* This is an example of a Type-Safe Groovy-style Builder
*
+11
View File
@@ -0,0 +1,11 @@
// MAIN_ARGS: [a,b,c]
fun main(args: Array<String>) {
for (arg in args)
println(arg)
// or
println()
for (i in args.indices)
println(args[i])
}
@@ -0,0 +1,7 @@
a
b
c
a
b
c
@@ -1,3 +1,5 @@
// MAIN_ARGS: [123]
fun main(args: Array<String>) {
for (arg in args)
println(arg)
@@ -0,0 +1,3 @@
123
123
@@ -1,3 +1,5 @@
// MAIN_ARGS: [10,20]
fun main(args: Array<String>) {
println(maxOf(args[0].toInt(), args[1].toInt()))
}
@@ -0,0 +1 @@
20
@@ -1,3 +1,5 @@
// MAIN_ARGS: []
fun main(args: Array<String>) {
println(getStringLength("aaa"))
println(getStringLength(1))
+2
View File
@@ -0,0 +1,2 @@
3
null
@@ -1,3 +1,5 @@
// MAIN_ARGS: [2]
/**
* This is a straightforward implementation of The Game of Life
* See http://en.wikipedia.org/wiki/Conway's_Game_of_Life
@@ -1,3 +1,5 @@
// MAIN_ARGS: []
/**
* Let's Walk Through a Maze.
*
@@ -19,16 +21,19 @@
* as I possibly can by finding a shortest path through the maze.
*/
fun <E> MutableList<E>.offer(element: E) = this.add(element)
fun <E> MutableList<E>.poll() = this.removeAt(0)
/**
* This function looks for a path from max.start to maze.end through
* free space (a path does not go through walls). One can move only
* straightly up, down, left or right, no diagonal moves allowed.
*/
fun findPath(maze: Maze): List<Pair<Int, Int>>? {
val previous = HashMap<Pair<Int, Int>, Pair<Int, Int>>
val previous = HashMap<Pair<Int, Int>, Pair<Int, Int>>()
val queue = LinkedList<Pair<Int, Int>>
val visited = HashSet<Pair<Int, Int>>
val queue = ArrayDeque<Pair<Int, Int>>()
val visited = HashSet<Pair<Int, Int>>()
queue.offer(maze.start)
visited.add(maze.start)
@@ -36,7 +41,7 @@ fun findPath(maze: Maze): List<Pair<Int, Int>>? {
val cell = queue.poll()
if (cell == maze.end) break
for (newCell in maze.neighbors(cell._1, cell._2)) {
for (newCell in maze.neighbors(cell.first, cell.second)) {
if (newCell in visited) continue
previous[newCell] = cell
queue.offer(newCell)
@@ -49,7 +54,7 @@ fun findPath(maze: Maze): List<Pair<Int, Int>>? {
val path = ArrayList<Pair<Int, Int>>()
var current = previous[maze.end]
while (current != maze.start) {
path.add(0, current)
path.add(0, current!!)
current = previous[current]
}
return path
@@ -59,7 +64,7 @@ fun findPath(maze: Maze): List<Pair<Int, Int>>? {
* Find neighbors of the (i, j) cell that are not walls
*/
fun Maze.neighbors(i: Int, j: Int): List<Pair<Int, Int>> {
val result = ArrayList<Pair<Int, Int>>
val result = ArrayList<Pair<Int, Int>>()
addIfFree(i - 1, j, result)
addIfFree(i, j - 1, result)
addIfFree(i + 1, j, result)
@@ -67,7 +72,7 @@ fun Maze.neighbors(i: Int, j: Int): List<Pair<Int, Int>> {
return result
}
fun Maze.addIfFree(i: Int, j: Int, result: List<Pair<Int, Int>>) {
fun Maze.addIfFree(i: Int, j: Int, result: MutableList<Pair<Int, Int>>) {
if (i !in 0..height - 1) return
if (j !in 0..width - 1) return
if (walls[i][j]) return
@@ -79,16 +84,16 @@ fun Maze.addIfFree(i: Int, j: Int, result: List<Pair<Int, Int>>) {
* A data class that represents a maze
*/
class Maze(
// Number or columns
val width: Int,
// Number of rows
val height: Int,
// true for a wall, false for free space
val walls: Array<out Array<out Boolean>>,
// The starting point (must not be a wall)
val start: Pair<Int, Int>,
// The target point (must not be a wall)
val end: Pair<Int, Int>
// Number or columns
val width: Int,
// Number of rows
val height: Int,
// true for a wall, false for free space
val walls: Array<out Array<out Boolean>>,
// The starting point (must not be a wall)
val start: Pair<Int, Int>,
// The target point (must not be a wall)
val end: Pair<Int, Int>
) {
}
@@ -102,7 +107,7 @@ fun main(args: Array<String>) {
O
O
O I
""")
""".trimIndent())
printMaze("""
OOOOOOOOOOO
O $ O
@@ -113,7 +118,7 @@ fun main(args: Array<String>) {
O OOOOOOOOO
O OO
OOOOOO IO
""")
""".trimIndent())
printMaze("""
OOOOOOOOOOOOOOOOO
O O
@@ -124,7 +129,7 @@ fun main(args: Array<String>) {
O O I O
O O
OOOOOOOOOOOOOOOOO
""")
""".trimIndent())
}
// UTILITIES
@@ -138,11 +143,11 @@ fun printMaze(str: String) {
for (j in 0..maze.width - 1) {
val cell = Pair(i, j)
print(
if (maze.walls[i][j]) "O"
else if (cell == maze.start) "I"
else if (cell == maze.end) "$"
else if (path != null && path.contains(cell)) "~"
else " "
if (maze.walls[i][j]) "O"
else if (cell == maze.start) "I"
else if (cell == maze.end) "$"
else if (path != null && path.contains(cell)) "~"
else " "
)
}
println("")
@@ -171,12 +176,12 @@ fun printMaze(str: String) {
*/
fun makeMaze(s: String): Maze {
val lines = s.split("\n")!!
val w = max<String?>(lines.toList(), comparator<String?> { o1, o2 ->
val l1: Int = o1?.size ?: 0
val l2 = o2?.size ?: 0
val w = lines.maxWithOrNull(Comparator { o1, o2 ->
val l1: Int = o1?.length ?: 0
val l2 = o2?.length ?: 0
l1 - l2
})!!
val data = Array<Array<Boolean>>(lines.size) { Array<Boolean>(w.size) { false } }
val data = Array<Array<Boolean>>(lines.size) { Array<Boolean>(w.length) { false } }
var start: Pair<Int, Int>? = null
var end: Pair<Int, Int>? = null
@@ -202,24 +207,9 @@ fun makeMaze(s: String): Maze {
throw IllegalArgumentException("No goal point in the maze (should be indicated with a '$' sign)")
}
return Maze(w.size, lines.size, data, start!!, end!!)
return Maze(w.length, lines.size, data, start!!, end!!)
}
// An excerpt from the Standard Library
val String?.indices: IntRange get() = IntRange(0, this!!.size)
fun <K, V> Map<K, V>.set(k: K, v: V) {
put(k, v)
}
fun comparator<T> (f: (T, T) -> Int): Comparator<T> = object : Comparator<T> {
override fun compare(o1: T, o2: T): Int = f(o1, o2)
override fun equals(p: Any?): Boolean = false
}
fun <T, C : Collection<T>> Array<T>.to(result: C): C {
for (elem in this)
result.add(elem)
return result
}
val String?.indices: IntRange get() = IntRange(0, this!!.length)
@@ -7,39 +7,34 @@ I O $
Result: No path
Maze:
O $
O ~
O ~
O ~
O ~~~~~~~~~I
O $
O ~
O ~
O ~
O ~~~~~~~~~I
Result: Path found
Maze:
OOOOOOOOOOO
O $~~~~~ O
O $~~~~~ O
OOOOOOO~OOO
O ~~~ O
O ~~~ O
OOOOO~OOOOO
O~~~~~ O
O~~~~~ O
O~OOOOOOOOO
O~~~~~~ OO
O~~~~~~ OO
OOOOOO~~~IO
Result: Path found
Maze:
OOOOOOOOOOOOOOOOO
O ~~~ O
O$~~O~ O
OOOOO~ O
O ~~~~ O
O ~~~ O
O$~~O~ O
OOOOO~ O
O ~~~~ O
O ~OOOOOOOOOOOOOO
O ~ O I O
O ~ O I O
O ~~~~~~~~~~~~~ O
OOOOOOOOOOOOOOOOO
Result: Path found
@@ -1,3 +1,5 @@
// MAIN_ARGS: [FR]
fun main(args: Array<String>) {
val language = if (args.size == 0) "EN" else args[0]
println(when (language) {
@@ -0,0 +1 @@
Salut!
@@ -1,3 +1,5 @@
// MAIN_ARGS: []
// Return null if str does not hold a number
fun myParseInt(str: String): Int? = str.toIntOrNull().also {
if (it == null) println("One of arguments isn't Int")
@@ -0,0 +1 @@
No number supplied
@@ -0,0 +1,21 @@
// MAIN_ARGS: [2,3]
// Return null if str does not hold a number
fun myParseInt(str: String): Int? = str.toIntOrNull().also {
if (it == null) println("One of arguments isn't Int")
}
fun main(args: Array<String>) {
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
}
}
}
@@ -0,0 +1 @@
6
@@ -1,3 +1,5 @@
// MAIN_ARGS: [Pavel]
class Greeter(name: String) {
val name = name
fun greet() {
@@ -0,0 +1 @@
Hello, Pavel!
@@ -1,3 +1,5 @@
// MAIN_ARGS: []
fun main(args: Array<String>) {
cases("Hello")
cases(1)
@@ -0,0 +1,4 @@
Greeting
One
Not a string
Unknown
@@ -1,3 +1,5 @@
// MAIN_ARGS: [Hello_world!]
fun main(args: Array<String>) {
print(args[0]);
}
@@ -0,0 +1 @@
Hello_world!
@@ -1,3 +1,4 @@
// MAIN_ARGS: [4]
fun main(args: Array<String>) {
val x = args[0].toInt()
+5
View File
@@ -0,0 +1,5 @@
OK
1 2 3 4 5
Out: array has only 3 elements. x = 4
Yes: array contains aaa
No: array doesn't contains ddd
+35
View File
@@ -0,0 +1,35 @@
// MAIN_ARGS: [10]
fun main(args: Array<String>) {
val x = args[0].toInt()
//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 = mutableListOf<String>()
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")
}
+4
View File
@@ -0,0 +1,4 @@
1 2 3 4 5
Out: array has only 3 elements. x = 10
Yes: array contains aaa
No: array doesn't contains ddd
@@ -1,3 +1,5 @@
// MAIN_ARGS: [guest1,guest2,guest3,guest4]
fun main(args: Array<String>) {
var i = 0
while (i < args.size)
@@ -0,0 +1,4 @@
guest1
guest2
guest3
guest4
-301
View File
@@ -1,301 +0,0 @@
The "99 bottles of beer" song
99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.
Take one down, pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer.
Take one down, pass it around, 96 bottles of beer on the wall.
96 bottles of beer on the wall, 96 bottles of beer.
Take one down, pass it around, 95 bottles of beer on the wall.
95 bottles of beer on the wall, 95 bottles of beer.
Take one down, pass it around, 94 bottles of beer on the wall.
94 bottles of beer on the wall, 94 bottles of beer.
Take one down, pass it around, 93 bottles of beer on the wall.
93 bottles of beer on the wall, 93 bottles of beer.
Take one down, pass it around, 92 bottles of beer on the wall.
92 bottles of beer on the wall, 92 bottles of beer.
Take one down, pass it around, 91 bottles of beer on the wall.
91 bottles of beer on the wall, 91 bottles of beer.
Take one down, pass it around, 90 bottles of beer on the wall.
90 bottles of beer on the wall, 90 bottles of beer.
Take one down, pass it around, 89 bottles of beer on the wall.
89 bottles of beer on the wall, 89 bottles of beer.
Take one down, pass it around, 88 bottles of beer on the wall.
88 bottles of beer on the wall, 88 bottles of beer.
Take one down, pass it around, 87 bottles of beer on the wall.
87 bottles of beer on the wall, 87 bottles of beer.
Take one down, pass it around, 86 bottles of beer on the wall.
86 bottles of beer on the wall, 86 bottles of beer.
Take one down, pass it around, 85 bottles of beer on the wall.
85 bottles of beer on the wall, 85 bottles of beer.
Take one down, pass it around, 84 bottles of beer on the wall.
84 bottles of beer on the wall, 84 bottles of beer.
Take one down, pass it around, 83 bottles of beer on the wall.
83 bottles of beer on the wall, 83 bottles of beer.
Take one down, pass it around, 82 bottles of beer on the wall.
82 bottles of beer on the wall, 82 bottles of beer.
Take one down, pass it around, 81 bottles of beer on the wall.
81 bottles of beer on the wall, 81 bottles of beer.
Take one down, pass it around, 80 bottles of beer on the wall.
80 bottles of beer on the wall, 80 bottles of beer.
Take one down, pass it around, 79 bottles of beer on the wall.
79 bottles of beer on the wall, 79 bottles of beer.
Take one down, pass it around, 78 bottles of beer on the wall.
78 bottles of beer on the wall, 78 bottles of beer.
Take one down, pass it around, 77 bottles of beer on the wall.
77 bottles of beer on the wall, 77 bottles of beer.
Take one down, pass it around, 76 bottles of beer on the wall.
76 bottles of beer on the wall, 76 bottles of beer.
Take one down, pass it around, 75 bottles of beer on the wall.
75 bottles of beer on the wall, 75 bottles of beer.
Take one down, pass it around, 74 bottles of beer on the wall.
74 bottles of beer on the wall, 74 bottles of beer.
Take one down, pass it around, 73 bottles of beer on the wall.
73 bottles of beer on the wall, 73 bottles of beer.
Take one down, pass it around, 72 bottles of beer on the wall.
72 bottles of beer on the wall, 72 bottles of beer.
Take one down, pass it around, 71 bottles of beer on the wall.
71 bottles of beer on the wall, 71 bottles of beer.
Take one down, pass it around, 70 bottles of beer on the wall.
70 bottles of beer on the wall, 70 bottles of beer.
Take one down, pass it around, 69 bottles of beer on the wall.
69 bottles of beer on the wall, 69 bottles of beer.
Take one down, pass it around, 68 bottles of beer on the wall.
68 bottles of beer on the wall, 68 bottles of beer.
Take one down, pass it around, 67 bottles of beer on the wall.
67 bottles of beer on the wall, 67 bottles of beer.
Take one down, pass it around, 66 bottles of beer on the wall.
66 bottles of beer on the wall, 66 bottles of beer.
Take one down, pass it around, 65 bottles of beer on the wall.
65 bottles of beer on the wall, 65 bottles of beer.
Take one down, pass it around, 64 bottles of beer on the wall.
64 bottles of beer on the wall, 64 bottles of beer.
Take one down, pass it around, 63 bottles of beer on the wall.
63 bottles of beer on the wall, 63 bottles of beer.
Take one down, pass it around, 62 bottles of beer on the wall.
62 bottles of beer on the wall, 62 bottles of beer.
Take one down, pass it around, 61 bottles of beer on the wall.
61 bottles of beer on the wall, 61 bottles of beer.
Take one down, pass it around, 60 bottles of beer on the wall.
60 bottles of beer on the wall, 60 bottles of beer.
Take one down, pass it around, 59 bottles of beer on the wall.
59 bottles of beer on the wall, 59 bottles of beer.
Take one down, pass it around, 58 bottles of beer on the wall.
58 bottles of beer on the wall, 58 bottles of beer.
Take one down, pass it around, 57 bottles of beer on the wall.
57 bottles of beer on the wall, 57 bottles of beer.
Take one down, pass it around, 56 bottles of beer on the wall.
56 bottles of beer on the wall, 56 bottles of beer.
Take one down, pass it around, 55 bottles of beer on the wall.
55 bottles of beer on the wall, 55 bottles of beer.
Take one down, pass it around, 54 bottles of beer on the wall.
54 bottles of beer on the wall, 54 bottles of beer.
Take one down, pass it around, 53 bottles of beer on the wall.
53 bottles of beer on the wall, 53 bottles of beer.
Take one down, pass it around, 52 bottles of beer on the wall.
52 bottles of beer on the wall, 52 bottles of beer.
Take one down, pass it around, 51 bottles of beer on the wall.
51 bottles of beer on the wall, 51 bottles of beer.
Take one down, pass it around, 50 bottles of beer on the wall.
50 bottles of beer on the wall, 50 bottles of beer.
Take one down, pass it around, 49 bottles of beer on the wall.
49 bottles of beer on the wall, 49 bottles of beer.
Take one down, pass it around, 48 bottles of beer on the wall.
48 bottles of beer on the wall, 48 bottles of beer.
Take one down, pass it around, 47 bottles of beer on the wall.
47 bottles of beer on the wall, 47 bottles of beer.
Take one down, pass it around, 46 bottles of beer on the wall.
46 bottles of beer on the wall, 46 bottles of beer.
Take one down, pass it around, 45 bottles of beer on the wall.
45 bottles of beer on the wall, 45 bottles of beer.
Take one down, pass it around, 44 bottles of beer on the wall.
44 bottles of beer on the wall, 44 bottles of beer.
Take one down, pass it around, 43 bottles of beer on the wall.
43 bottles of beer on the wall, 43 bottles of beer.
Take one down, pass it around, 42 bottles of beer on the wall.
42 bottles of beer on the wall, 42 bottles of beer.
Take one down, pass it around, 41 bottles of beer on the wall.
41 bottles of beer on the wall, 41 bottles of beer.
Take one down, pass it around, 40 bottles of beer on the wall.
40 bottles of beer on the wall, 40 bottles of beer.
Take one down, pass it around, 39 bottles of beer on the wall.
39 bottles of beer on the wall, 39 bottles of beer.
Take one down, pass it around, 38 bottles of beer on the wall.
38 bottles of beer on the wall, 38 bottles of beer.
Take one down, pass it around, 37 bottles of beer on the wall.
37 bottles of beer on the wall, 37 bottles of beer.
Take one down, pass it around, 36 bottles of beer on the wall.
36 bottles of beer on the wall, 36 bottles of beer.
Take one down, pass it around, 35 bottles of beer on the wall.
35 bottles of beer on the wall, 35 bottles of beer.
Take one down, pass it around, 34 bottles of beer on the wall.
34 bottles of beer on the wall, 34 bottles of beer.
Take one down, pass it around, 33 bottles of beer on the wall.
33 bottles of beer on the wall, 33 bottles of beer.
Take one down, pass it around, 32 bottles of beer on the wall.
32 bottles of beer on the wall, 32 bottles of beer.
Take one down, pass it around, 31 bottles of beer on the wall.
31 bottles of beer on the wall, 31 bottles of beer.
Take one down, pass it around, 30 bottles of beer on the wall.
30 bottles of beer on the wall, 30 bottles of beer.
Take one down, pass it around, 29 bottles of beer on the wall.
29 bottles of beer on the wall, 29 bottles of beer.
Take one down, pass it around, 28 bottles of beer on the wall.
28 bottles of beer on the wall, 28 bottles of beer.
Take one down, pass it around, 27 bottles of beer on the wall.
27 bottles of beer on the wall, 27 bottles of beer.
Take one down, pass it around, 26 bottles of beer on the wall.
26 bottles of beer on the wall, 26 bottles of beer.
Take one down, pass it around, 25 bottles of beer on the wall.
25 bottles of beer on the wall, 25 bottles of beer.
Take one down, pass it around, 24 bottles of beer on the wall.
24 bottles of beer on the wall, 24 bottles of beer.
Take one down, pass it around, 23 bottles of beer on the wall.
23 bottles of beer on the wall, 23 bottles of beer.
Take one down, pass it around, 22 bottles of beer on the wall.
22 bottles of beer on the wall, 22 bottles of beer.
Take one down, pass it around, 21 bottles of beer on the wall.
21 bottles of beer on the wall, 21 bottles of beer.
Take one down, pass it around, 20 bottles of beer on the wall.
20 bottles of beer on the wall, 20 bottles of beer.
Take one down, pass it around, 19 bottles of beer on the wall.
19 bottles of beer on the wall, 19 bottles of beer.
Take one down, pass it around, 18 bottles of beer on the wall.
18 bottles of beer on the wall, 18 bottles of beer.
Take one down, pass it around, 17 bottles of beer on the wall.
17 bottles of beer on the wall, 17 bottles of beer.
Take one down, pass it around, 16 bottles of beer on the wall.
16 bottles of beer on the wall, 16 bottles of beer.
Take one down, pass it around, 15 bottles of beer on the wall.
15 bottles of beer on the wall, 15 bottles of beer.
Take one down, pass it around, 14 bottles of beer on the wall.
14 bottles of beer on the wall, 14 bottles of beer.
Take one down, pass it around, 13 bottles of beer on the wall.
13 bottles of beer on the wall, 13 bottles of beer.
Take one down, pass it around, 12 bottles of beer on the wall.
12 bottles of beer on the wall, 12 bottles of beer.
Take one down, pass it around, 11 bottles of beer on the wall.
11 bottles of beer on the wall, 11 bottles of beer.
Take one down, pass it around, 10 bottles of beer on the wall.
10 bottles of beer on the wall, 10 bottles of beer.
Take one down, pass it around, 9 bottles of beer on the wall.
9 bottles of beer on the wall, 9 bottles of beer.
Take one down, pass it around, 8 bottles of beer on the wall.
8 bottles of beer on the wall, 8 bottles of beer.
Take one down, pass it around, 7 bottles of beer on the wall.
7 bottles of beer on the wall, 7 bottles of beer.
Take one down, pass it around, 6 bottles of beer on the wall.
6 bottles of beer on the wall, 6 bottles of beer.
Take one down, pass it around, 5 bottles of beer on the wall.
5 bottles of beer on the wall, 5 bottles of beer.
Take one down, pass it around, 4 bottles of beer on the wall.
4 bottles of beer on the wall, 4 bottles of beer.
Take one down, pass it around, 3 bottles of beer on the wall.
3 bottles of beer on the wall, 3 bottles of beer.
Take one down, pass it around, 2 bottles of beer on the wall.
2 bottles of beer on the wall, 2 bottles of beer.
Take one down, pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down, pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.
-38
View File
@@ -1,38 +0,0 @@
<html>
<head>
<title>
XML encoding with Kotlin
</title>
</head>
<body>
<h1>
XML encoding with Kotlin
</h1>
<p>
this format can be used as an alternative markup to XML
</p>
<a href="https://jetbrains.com/kotlin">
Kotlin
</a>
<p>
This is some
<b>
mixed
</b>
text. For more see the
<a href="https://jetbrains.com/kotlin">
Kotlin
</a>
project
</p>
<p>
some text
</p>
<p>
Command line arguments were:
<ul>
</ul>
</p>
</body>
</html>