11import asyncio
22from contextlib import contextmanager
3+ from time import monotonic
34from types import FunctionType
45
56from computercraft .errors import LuaException , CommandException
@@ -15,12 +16,6 @@ async def id(api):
1516 await api .print ('Version' , await api .os .version ())
1617
1718
18- async def parallel (api ):
19- # Since os.sleep is mostly waiting for events, it doesn't block execution of parallel threads
20- # and this snippet takes approximately 2 seconds to complete.
21- await asyncio .gather (api .os .sleep (2 ), api .os .sleep (2 ))
22-
23-
2419async def move (api ):
2520 for _ in range (4 ):
2621 await api .turtle .forward ()
@@ -49,6 +44,15 @@ def assert_raises(etype):
4944 raise AssertionError (f'Exception of type { etype } was not raised' )
5045
5146
47+ @contextmanager
48+ def assert_takes_time (at_least , at_most ):
49+ t = monotonic ()
50+ yield
51+ dt = monotonic () - t
52+ # print(at_least, '<=', dt, '<=', at_most)
53+ assert at_least <= dt <= at_most
54+
55+
5256class AnyInstanceOf :
5357 def __init__ (self , cls ):
5458 self .c = cls
@@ -506,8 +510,130 @@ async def test_gps_command_computer(api):
506510
507511
508512async def test_keys_api (api ):
509- assert await api .keys .getName (65 ) == 'a'
510- assert await api .keys .getName (32 ) == 'space'
511- assert await api .keys .getName (13 ) is None # wtf?
513+ a = await api .keys .getCode ('a' )
514+ space = await api .keys .getCode ('space' )
515+ enter = await api .keys .getCode ('enter' )
516+ assert await api .keys .getCode ('doesnotexist' ) is None
517+ assert await api .keys .getCode ('getName' ) is None
518+ assert isinstance (a , int )
519+ assert isinstance (space , int )
520+ assert isinstance (enter , int )
521+
522+ assert await api .keys .getName (a ) == 'a'
523+ assert await api .keys .getName (space ) == 'space'
524+ assert await api .keys .getName (enter ) == 'enter'
525+
512526 # for i in range(255):
513527 # print(i, await api.keys.getName(i))
528+
529+ await api .print ('Test finished successfully' )
530+
531+
532+ async def test_help_api (api ):
533+ assert get_class_table (api .help .__class__ ) \
534+ == await get_object_table (api , 'help' )
535+
536+ await api .help .setPath ('/rom/help' )
537+
538+ assert await api .help .path () == '/rom/help'
539+
540+ assert await api .help .lookup ('disk' ) == 'rom/help/disk.txt'
541+ assert await api .help .lookup ('abracadabra' ) is None
542+
543+ ts = await api .help .topics ()
544+ assert isinstance (ts , list )
545+ assert len (ts ) > 2
546+ # print(ts)
547+ assert 'disk' in ts
548+
549+ assert await api .help .completeTopic ('di' ) == ['sk' ]
550+ assert await api .help .completeTopic ('abracadabra' ) == []
551+
552+ assert await api .help .setPath ('/kek' ) is None
553+ assert await api .help .path () == '/kek'
554+ assert await api .help .topics () == ['index' ]
555+ assert await api .help .setPath ('/rom/help' ) is None
556+
557+ await api .print ('Test finished successfully' )
558+
559+
560+ async def test_reboot (api ):
561+ assert await api .os .reboot () is None
562+ await api .print ('Test finished successfully' )
563+
564+
565+ async def test_shutdown (api ):
566+ assert await api .os .shutdown () is None
567+ await api .print ('Test finished successfully' )
568+
569+
570+ async def test_os_api (api ):
571+ tbl = await get_object_table (api , 'os' )
572+
573+ # use methods with get*
574+ del tbl ['function' ]['computerID' ]
575+ del tbl ['function' ]['computerLabel' ]
576+
577+ # use captureEvent
578+ del tbl ['function' ]['pullEvent' ]
579+ del tbl ['function' ]['pullEventRaw' ]
580+
581+ # we are in python world, loading lua modules is useless
582+ del tbl ['function' ]['loadAPI' ]
583+ del tbl ['function' ]['unloadAPI' ]
584+
585+ # remove complex date formatting function in favor of python stdlib
586+ del tbl ['function' ]['date' ]
587+
588+ tbl ['function' ]['captureEvent' ] = True
589+
590+ assert get_class_table (api .os .__class__ ) == tbl
591+
592+ with assert_takes_time (1.5 , 3 ):
593+ async with api .os .captureEvent ('timer' ) as timer_queue :
594+ timer_id = await api .os .startTimer (2 )
595+ async for etid , in timer_queue :
596+ if etid == timer_id :
597+ await api .print ('Timer reached' )
598+ break
599+
600+ timer_id = await api .os .startTimer (20 )
601+ assert isinstance (timer_id , int )
602+ assert await api .os .cancelTimer (timer_id ) is None
603+ assert await api .os .cancelTimer (timer_id ) is None
604+
605+ alarm_id = await api .os .setAlarm (0.0 )
606+ assert isinstance (alarm_id , int )
607+ assert await api .os .cancelAlarm (alarm_id ) is None
608+ assert await api .os .cancelAlarm (alarm_id ) is None
609+
610+ with assert_takes_time (1.5 , 3 ):
611+ assert await api .os .sleep (2 ) is None
612+
613+ assert (await api .os .version ()).startswith ('CraftOS ' )
614+ assert isinstance (await api .os .getComputerID (), int )
615+
616+ assert await api .os .setComputerLabel (None ) is None
617+ assert await api .os .getComputerLabel () is None
618+ assert await api .os .setComputerLabel ('altair' ) is None
619+ assert await api .os .getComputerLabel () == 'altair'
620+ assert await api .os .setComputerLabel (None ) is None
621+ assert await api .os .getComputerLabel () is None
622+
623+ assert isinstance (await api .os .epoch (), int )
624+ assert isinstance (await api .os .day (), int )
625+ assert isinstance (await api .os .time (), (int , float ))
626+ assert isinstance (await api .os .clock (), (int , float ))
627+
628+ # TODO: run method
629+
630+ await api .print ('Test finished successfully' )
631+
632+
633+ async def test_parallel (api ):
634+ with assert_takes_time (1.5 , 3 ):
635+ # Since os.sleep is mostly waiting for events, it doesn't block execution of parallel threads
636+ # and this snippet takes approximately 2 seconds to complete.
637+ await asyncio .gather (api .os .sleep (2 ), api .os .sleep (2 ))
638+
639+ await api .print ('Test finished successfully' )
0 commit comments