Skip to content

Commit 3088c9c

Browse files
committed
Fix tests
1 parent 07ddb7d commit 3088c9c

File tree

9 files changed

+56
-22
lines changed

9 files changed

+56
-22
lines changed

.sbtopts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xmx2G

demo/src/main/scala/main.scala

-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import dev.sacode.flowrun.*
1313
val flowRunEditors = document.querySelectorAll(".flowrun-instance")
1414
for elem <- flowRunEditors do
1515
val mountElem = elem.asInstanceOf[dom.html.Element]
16-
val editable = mountElem.classList.contains("flowrun--editable")
1716
val flowRunEditor: FlowRunEditor = FlowRunEditor(
1817
colorScheme = colorScheme,
1918
mountElem = mountElem,
20-
editable = editable,
2119
mountCallback = { fr =>
2220
// JSON text is hidden
2321
// only when program is mounted then we show that DIV

editor/src/main/scala/dev/sacode/flowrun/FlowRunEditor.scala

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ import dev.sacode.flowrun.edit.CodeArea
3030
class FlowRunEditor(
3131
mountElem: dom.html.Element,
3232
colorScheme: ColorScheme = null,
33-
editable: Boolean = true,
33+
readonly: Option[Boolean] = None,
3434
programJson: String = null,
3535
mountCallback: js.Function1[FlowRunEditor, Unit] = null,
3636
changeCallback: js.Function1[FlowRunEditor, Unit] = null
3737
) {
3838

39+
private val editable = readonly.getOrElse {
40+
mountElem.classList.contains("flowrun--editable")
41+
}
42+
3943
// resolve initial program
4044
private val jsonSourceOpt = Option(programJson).filterNot(_.trim.isEmpty).orElse {
4145
val mountElemText = mountElem.innerText.trim
@@ -66,7 +70,7 @@ class FlowRunEditor(
6670
private val flowrunChannel = Channel[FlowRun.Event]
6771
private val programModel = ProgramModel(program, flowrunChannel)
6872
private var interpreter = newInterpeter
69-
73+
7074
private def newInterpeter = Interpreter(
7175
programModel,
7276
flowrunChannel,
@@ -123,7 +127,7 @@ class FlowRunEditor(
123127
end if
124128
flowrunChannel := FlowRun.Event.StmtSelected
125129
case ("EDGE", n) =>
126-
ctxMenu.handleEdgeRightClick(event, n)
130+
if editable then ctxMenu.handleEdgeRightClick(event, n)
127131
case _ =>
128132
flowrunChannel := FlowRun.Event.Deselected
129133
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package dev.sacode.flowrun
2+
3+
import scala.scalajs.js
4+
5+
object Fixtures {
6+
val setInterval: (Long, => Unit) => Any =
7+
(interval, body) => js.timers.setInterval(interval.toDouble)(body)
8+
9+
val clearInterval: Any => Unit =
10+
(handle) => js.timers.clearInterval(handle.asInstanceOf[js.timers.SetIntervalHandle])
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dev.sacode.flowrun
2+
3+
import java.util.concurrent.Executors
4+
import java.util.concurrent.TimeUnit
5+
import java.util.concurrent.ScheduledFuture
6+
7+
object Fixtures {
8+
private val pool = Executors.newScheduledThreadPool(3)
9+
10+
val setInterval: (Long, => Unit) => Any =
11+
(interval, body) => pool.scheduleAtFixedRate(() => body, 0, interval, TimeUnit.MILLISECONDS)
12+
13+
val clearInterval: Any => Unit =
14+
(handle) => handle.asInstanceOf[ScheduledFuture[?]].cancel(true)
15+
16+
}

interpreter/shared/src/main/scala/dev/sacode/flowrun/codegen/KotlinGenerator.scala

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class KotlinGenerator(val programAst: Program) extends CodeGenerator {
144144
case Ln => s"log(${argOpt(0)})"
145145
case Log10 => s"log10(${argOpt(0)})"
146146
case Log2 => s"log10(${argOpt(0)})/log10(2)"
147+
case Sqrt => s"sqrt(${argOpt(0)})"
148+
case Pow => s"pow(${argOpt(0)}, ${argOpt(1)})"
147149
case Length => s"${argOpt(0)}.length()"
148150
case CharAt => s"${argOpt(0)}.charAt(${argOpt(1)})"
149151
case RealToInteger => s"(int)${argOpt(0)}"

interpreter/shared/src/main/scala/dev/sacode/flowrun/eval/Interpreter.scala

+8-6
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,15 @@ final class Interpreter(
479479
// poll every 50ms to check the state of program
480480
// - if RUNNING set as success, continue evaluating the program
481481
// - if FINISHED_STOPPED set as failed
482-
val pingHandle = setInterval(50, {
483-
if !p.isCompleted then {
484-
if state == State.RUNNING then p.success({})
485-
else if state == State.FINISHED_STOPPED then p.failure(StoppedException("Program stopped"))
482+
val pingHandle = setInterval(
483+
50, {
484+
if !p.isCompleted then {
485+
if state == State.RUNNING then p.success({})
486+
else if state == State.FINISHED_STOPPED then p.failure(StoppedException("Program stopped"))
487+
}
486488
}
487-
})
488-
489+
)
490+
489491
val f = p.future
490492
f.onComplete { _ =>
491493
clearInterval(pingHandle)

interpreter/shared/src/test/scala/dev/sacode/flowrun/eval/InterpreterTest.scala

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object InterpreterTests extends TestSuite {
2525
val main = newFun(List())
2626
val flowrunChannel = Channel[FlowRun.Event]
2727
val programModel = ProgramModel(Program("p1", "program", FlowRunConfig.default, main), flowrunChannel)
28-
val interpreter = Interpreter(programModel, flowrunChannel)
28+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
2929
interpreter.run().map { _ =>
3030
assert(1 == 1)
3131
}
@@ -40,7 +40,7 @@ object InterpreterTests extends TestSuite {
4040

4141
val flowrunChannel = Channel[FlowRun.Event]
4242
val programModel = ProgramModel(Program("p2", "program", FlowRunConfig.default, main), flowrunChannel)
43-
val interpreter = Interpreter(programModel, flowrunChannel)
43+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
4444

4545
interpreter.run().map { _ =>
4646
val scope = interpreter.symTab.globalScope.childScopes.head
@@ -64,7 +64,7 @@ object InterpreterTests extends TestSuite {
6464
)
6565
val flowrunChannel = Channel[FlowRun.Event]
6666
val programModel = ProgramModel(Program("p2", "program", FlowRunConfig.default, main), flowrunChannel)
67-
val interpreter = Interpreter(programModel, flowrunChannel)
67+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
6868

6969
interpreter.run().map { _ =>
7070
val scope = interpreter.symTab.globalScope.childScopes.head
@@ -89,7 +89,7 @@ object InterpreterTests extends TestSuite {
8989
)
9090
val flowrunChannel = Channel[FlowRun.Event]
9191
val programModel = ProgramModel(Program("p3", "program", FlowRunConfig.default, main), flowrunChannel)
92-
val interpreter = Interpreter(programModel, flowrunChannel)
92+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
9393

9494
interpreter.run().map { _ =>
9595
val scope = interpreter.symTab.globalScope.childScopes.head
@@ -111,7 +111,7 @@ object InterpreterTests extends TestSuite {
111111
)
112112
val flowrunChannel = Channel[FlowRun.Event]
113113
val programModel = ProgramModel(Program("p4", "program", FlowRunConfig.default, main), flowrunChannel)
114-
val interpreter = Interpreter(programModel, flowrunChannel)
114+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
115115

116116
interpreter.run().map { _ =>
117117
val scope = interpreter.symTab.globalScope.childScopes.head
@@ -138,7 +138,7 @@ object InterpreterTests extends TestSuite {
138138
)
139139
val flowrunChannel = Channel[FlowRun.Event]
140140
val programModel = ProgramModel(Program("p4", "program", FlowRunConfig.default, main), flowrunChannel)
141-
val interpreter = Interpreter(programModel, flowrunChannel)
141+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
142142

143143
interpreter.run().map { _ =>
144144
val scope = interpreter.symTab.globalScope.childScopes.head
@@ -167,7 +167,7 @@ object InterpreterTests extends TestSuite {
167167
)
168168
val flowrunChannel = Channel[FlowRun.Event]
169169
val programModel = ProgramModel(Program("p5", "program", FlowRunConfig.default, main), flowrunChannel)
170-
val interpreter = Interpreter(programModel, flowrunChannel)
170+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
171171

172172
interpreter.run().map { _ =>
173173
val scope = interpreter.symTab.globalScope.childScopes.head

interpreter/shared/src/test/scala/dev/sacode/flowrun/eval/PredefinedFunctionsTest.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object PredefinedFunctionsTest extends TestSuite {
2121
val main = newFun(List())
2222
val flowrunChannel = Channel[FlowRun.Event]
2323
val programModel = ProgramModel(Program("p1", "program", FlowRunConfig.default, main), flowrunChannel)
24-
val interpreter = Interpreter(programModel, flowrunChannel)
24+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
2525
val intAssertions = Seq((5, 5), (-0, 0), (-7, 7))
2626
.map((n, e) => (RunVal.IntegerVal(n), RunVal.IntegerVal(e)))
2727
val realAssertions = Seq((5.5, 5.5), (-0.0, 0.0), (-7.7, 7.7))
@@ -38,7 +38,7 @@ object PredefinedFunctionsTest extends TestSuite {
3838
val main = newFun(List())
3939
val flowrunChannel = Channel[FlowRun.Event]
4040
val programModel = ProgramModel(Program("p1", "program", FlowRunConfig.default, main), flowrunChannel)
41-
val interpreter = Interpreter(programModel, flowrunChannel)
41+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
4242
val realAssertions = Seq((5.5, 5.0), (0.0, 0.0), (-7.7, -8.0))
4343
.map((n, e) => (RunVal.RealVal(n), RunVal.RealVal(e)))
4444
val allAssertions = realAssertions
@@ -53,7 +53,7 @@ object PredefinedFunctionsTest extends TestSuite {
5353
val main = newFun(List())
5454
val flowrunChannel = Channel[FlowRun.Event]
5555
val programModel = ProgramModel(Program("p1", "program", FlowRunConfig.default, main), flowrunChannel)
56-
val interpreter = Interpreter(programModel, flowrunChannel)
56+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
5757
val realAssertions = Seq((5.5, 6.0), (0.0, 0.0), (-7.7, -7.0))
5858
.map((n, e) => (RunVal.RealVal(n), RunVal.RealVal(e)))
5959
val allAssertions = realAssertions
@@ -68,7 +68,7 @@ object PredefinedFunctionsTest extends TestSuite {
6868
val main = newFun(List())
6969
val flowrunChannel = Channel[FlowRun.Event]
7070
val programModel = ProgramModel(Program("p1", "program", FlowRunConfig.default, main), flowrunChannel)
71-
val interpreter = Interpreter(programModel, flowrunChannel)
71+
val interpreter = Interpreter(programModel, flowrunChannel, Fixtures.setInterval, Fixtures.clearInterval)
7272
val realAssertions = Seq(("", 0), ("abc", 3), (" ", 5))
7373
.map((n, e) => (RunVal.StringVal(n), RunVal.IntegerVal(e)))
7474
val allAssertions = realAssertions

0 commit comments

Comments
 (0)