forked from Mirror/wren
Tests for Timer.sleep().
This commit is contained in:
@ -19,7 +19,7 @@ EXPECT_ERROR_PATTERN = re.compile(r'// expect error(?! line)')
|
||||
EXPECT_ERROR_LINE_PATTERN = re.compile(r'// expect error line (\d+)')
|
||||
EXPECT_RUNTIME_ERROR_PATTERN = re.compile(r'// expect runtime error: (.+)')
|
||||
ERROR_PATTERN = re.compile(r'\[.* line (\d+)\] Error')
|
||||
STACK_TRACE_PATTERN = re.compile(r'\[.* line (\d+)\] in')
|
||||
STACK_TRACE_PATTERN = re.compile(r'\[main line (\d+)\] in')
|
||||
STDIN_PATTERN = re.compile(r'// stdin: (.*)')
|
||||
SKIP_PATTERN = re.compile(r'// skip: (.*)')
|
||||
NONTEST_PATTERN = re.compile(r'// nontest')
|
||||
@ -159,11 +159,17 @@ class Test:
|
||||
self.runtime_error_message)
|
||||
self.fail(error_lines[0])
|
||||
|
||||
# Make sure the stack trace has the right line.
|
||||
match = STACK_TRACE_PATTERN.search(error_lines[1])
|
||||
# Make sure the stack trace has the right line. Skip over any lines that
|
||||
# come from builtin libraries.
|
||||
stack_lines = error_lines[1:]
|
||||
for stack_line in stack_lines:
|
||||
match = STACK_TRACE_PATTERN.search(stack_line)
|
||||
if match: break
|
||||
|
||||
if not match:
|
||||
self.fail('Expected stack trace and got:')
|
||||
self.fail(error_lines[1])
|
||||
for stack_line in stack_lines:
|
||||
self.fail(stack_line)
|
||||
else:
|
||||
stack_line = int(match.group(1))
|
||||
if stack_line != self.runtime_error_line:
|
||||
|
||||
@ -11,6 +11,8 @@
|
||||
static const char* timerLibSource =
|
||||
"class Timer {\n"
|
||||
" static sleep(milliseconds) {\n"
|
||||
" if (!(milliseconds is Num)) Fiber.abort(\"Milliseconds must be a number.\")\n"
|
||||
" if (milliseconds < 0) Fiber.abort(\"Milliseconds cannot be negative.\")\n"
|
||||
" startTimer_(milliseconds, Fiber.current)\n"
|
||||
" Fiber.yield()\n"
|
||||
" }\n"
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
|
||||
#include "wren.h"
|
||||
|
||||
// TODO: Coherent naming scheme.
|
||||
char* timerGetSource();
|
||||
|
||||
WrenForeignMethodFn timerBindForeign(
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
class Timer {
|
||||
static sleep(milliseconds) {
|
||||
if (!(milliseconds is Num)) Fiber.abort("Milliseconds must be a number.")
|
||||
if (milliseconds < 0) Fiber.abort("Milliseconds cannot be negative.")
|
||||
startTimer_(milliseconds, Fiber.current)
|
||||
Fiber.yield()
|
||||
}
|
||||
|
||||
19
test/timer/sleep_fibers.wren
Normal file
19
test/timer/sleep_fibers.wren
Normal file
@ -0,0 +1,19 @@
|
||||
import "timer" for Timer
|
||||
|
||||
Fiber.new {
|
||||
Timer.sleep(2)
|
||||
IO.print("a")
|
||||
}.call()
|
||||
|
||||
Fiber.new {
|
||||
Timer.sleep(1)
|
||||
IO.print("b")
|
||||
}.call()
|
||||
|
||||
IO.print("main")
|
||||
Timer.sleep(3)
|
||||
IO.print("done")
|
||||
// expect: main
|
||||
// expect: b
|
||||
// expect: a
|
||||
// expect: done
|
||||
20
test/timer/sleep_float.wren
Normal file
20
test/timer/sleep_float.wren
Normal file
@ -0,0 +1,20 @@
|
||||
import "timer" for Timer
|
||||
|
||||
// These are both rounded to 1, so "a" should complete first.
|
||||
Fiber.new {
|
||||
Timer.sleep(1.5)
|
||||
IO.print("a")
|
||||
}.call()
|
||||
|
||||
Fiber.new {
|
||||
Timer.sleep(1.3)
|
||||
IO.print("b")
|
||||
}.call()
|
||||
|
||||
IO.print("main")
|
||||
Timer.sleep(3)
|
||||
IO.print("done")
|
||||
// expect: main
|
||||
// expect: a
|
||||
// expect: b
|
||||
// expect: done
|
||||
9
test/timer/sleep_main_fiber.wren
Normal file
9
test/timer/sleep_main_fiber.wren
Normal file
@ -0,0 +1,9 @@
|
||||
import "timer" for Timer
|
||||
|
||||
IO.print("1") // expect: 1
|
||||
Timer.sleep(3)
|
||||
IO.print("2") // expect: 2
|
||||
Timer.sleep(3)
|
||||
IO.print("3") // expect: 3
|
||||
Timer.sleep(3)
|
||||
IO.print("4") // expect: 4
|
||||
3
test/timer/sleep_negative.wren
Normal file
3
test/timer/sleep_negative.wren
Normal file
@ -0,0 +1,3 @@
|
||||
import "timer" for Timer
|
||||
|
||||
Timer.sleep(-1) // expect runtime error: Milliseconds cannot be negative.
|
||||
3
test/timer/sleep_not_num.wren
Normal file
3
test/timer/sleep_not_num.wren
Normal file
@ -0,0 +1,3 @@
|
||||
import "timer" for Timer
|
||||
|
||||
Timer.sleep("wat") // expect runtime error: Milliseconds must be a number.
|
||||
27
test/timer/sleep_zero.wren
Normal file
27
test/timer/sleep_zero.wren
Normal file
@ -0,0 +1,27 @@
|
||||
import "timer" for Timer
|
||||
|
||||
Fiber.new {
|
||||
Timer.sleep(0)
|
||||
IO.print("a")
|
||||
}.call()
|
||||
|
||||
Fiber.new {
|
||||
Timer.sleep(0)
|
||||
IO.print("b")
|
||||
}.call()
|
||||
|
||||
Fiber.new {
|
||||
Timer.sleep(0)
|
||||
IO.print("c")
|
||||
}.call()
|
||||
|
||||
IO.print("main")
|
||||
Timer.sleep(0) // This is enough to let the other fiber run.
|
||||
IO.print("done")
|
||||
|
||||
// expect: main
|
||||
// Run in the order they were enqueued.
|
||||
// expect: a
|
||||
// expect: b
|
||||
// expect: c
|
||||
// expect: done
|
||||
Reference in New Issue
Block a user