Skip to content

Commit c7e1c3d

Browse files
committed
Test for term api
1 parent 7e4c733 commit c7e1c3d

File tree

3 files changed

+123
-12
lines changed

3 files changed

+123
-12
lines changed

computercraft/subapis/colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ColorsAPI(BaseSubAPI):
2424
red = 0x4000
2525
black = 0x8000
2626

27+
# use these chars for term.blit
2728
chars = {
2829
'0': white,
2930
'1': orange,
@@ -43,6 +44,10 @@ class ColorsAPI(BaseSubAPI):
4344
'f': black,
4445
}
4546

47+
def __iter__(self):
48+
for c in self.chars.values():
49+
yield c
50+
4651
async def combine(self, *colors: int) -> int:
4752
return integer(await self._send('combine', *colors))
4853

computercraft/subapis/mixins.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ async def getSize(self) -> Tuple[int, int]:
3737
async def scroll(self, lines: int):
3838
return nil(await self._send('scroll', lines))
3939

40-
async def setTextColor(self, color: int):
41-
return nil(await self._send('setTextColor', color))
40+
async def setTextColor(self, colorID: int):
41+
return nil(await self._send('setTextColor', colorID))
4242

4343
async def getTextColor(self) -> int:
4444
return integer(await self._send('getTextColor'))
4545

46-
async def setBackgroundColor(self, color: int):
47-
return nil(await self._send('setBackgroundColor', color))
46+
async def setBackgroundColor(self, colorID: int):
47+
return nil(await self._send('setBackgroundColor', colorID))
4848

4949
async def getBackgroundColor(self) -> int:
5050
return integer(await self._send('getBackgroundColor'))
5151

52-
async def getPaletteColor(self, index: int) -> Tuple[float, float, float]:
53-
return tuple3_number(await self._send('getPaletteColor', index))
52+
async def getPaletteColor(self, colorID: int) -> Tuple[float, float, float]:
53+
return tuple3_number(await self._send('getPaletteColor', colorID))
5454

55-
async def setPaletteColor(self, index: int, r: float, g: float, b: float):
56-
return nil(await self._send('setPaletteColor', index, r, g, b))
55+
async def setPaletteColor(self, colorID: int, r: float, g: float, b: float):
56+
return nil(await self._send('setPaletteColor', colorID, r, g, b))
5757

58-
async def nativePaletteColor(self, index: int) -> Tuple[float, float, float]:
59-
return tuple3_number(await self._send('nativePaletteColor', index))
58+
async def nativePaletteColor(self, colorID: int) -> Tuple[float, float, float]:
59+
return tuple3_number(await self._send('nativePaletteColor', colorID))

testmod.py

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,116 @@ async def test_parallel(api):
639639
await api.print('Test finished successfully')
640640

641641

642+
async def term_step(api, text):
643+
for color in api.colors:
644+
r, g, b = await api.term.nativePaletteColor(color)
645+
await api.term.setPaletteColor(color, r, g, b)
646+
await api.term.setBackgroundColor(api.colors.black)
647+
await api.term.setTextColor(api.colors.white)
648+
await api.term.clear()
649+
await api.term.setCursorPos(1, 1)
650+
await api.term.setCursorBlink(True)
651+
await step(api, text)
652+
653+
642654
async def test_term_api(api):
643-
from pprint import pprint
655+
from computercraft.subapis.mixins import TermMixin
656+
644657
tbl = await get_object_table(api, 'term')
645658

646-
pprint(tbl)
659+
# not defined in TermMixin
660+
del tbl['function']['redirect']
661+
del tbl['function']['current']
662+
del tbl['function']['native']
663+
664+
# remove British method names to make API lighter
665+
del tbl['function']['getBackgroundColour']
666+
del tbl['function']['getPaletteColour']
667+
del tbl['function']['getTextColour']
668+
del tbl['function']['isColour']
669+
del tbl['function']['nativePaletteColour']
670+
del tbl['function']['setBackgroundColour']
671+
del tbl['function']['setPaletteColour']
672+
del tbl['function']['setTextColour']
673+
674+
assert get_class_table(TermMixin) == tbl
675+
676+
await step(api, 'Detach all monitors\nUse advanced computer for colors\nScreen will be cleared')
677+
678+
assert await api.term.getSize() == (51, 19)
679+
assert await api.term.isColor() is True
680+
assert await api.term.clear() is None
681+
assert await api.term.setCursorPos(1, 1) is None
682+
assert await api.term.getCursorPos() == (1, 1)
683+
assert await api.term.write('Alpha') is None
684+
assert await api.term.getCursorPos() == (6, 1)
685+
assert await api.term.setCursorBlink(False) is None
686+
assert await api.term.getCursorBlink() is False
687+
assert await api.term.setCursorBlink(True) is None
688+
assert await api.term.getCursorBlink() is True
689+
await asyncio.sleep(2)
690+
691+
await term_step(api, 'You must have seen word Alpha with blinking cursor')
692+
693+
assert await api.term.clear() is None
694+
for offs, (tc, bc) in enumerate((
695+
(api.colors.lime, api.colors.green),
696+
(api.colors.yellow, api.colors.brown),
697+
(api.colors.red, api.colors.orange),
698+
), start=1):
699+
assert await api.term.setTextColor(tc) is None
700+
assert await api.term.getTextColor() == tc
701+
assert await api.term.setBackgroundColor(bc) is None
702+
assert await api.term.getBackgroundColor() == bc
703+
assert await api.term.setCursorPos(offs * 2, offs) is None
704+
assert await api.term.getCursorPos() == (offs * 2, offs)
705+
assert await api.term.write('text with colors') is None
706+
assert await api.term.setBackgroundColor(api.colors.black) is None
707+
await asyncio.sleep(1)
708+
for i in range(3):
709+
assert await api.term.scroll(-2) is None
710+
await asyncio.sleep(0.5)
711+
for i in range(6):
712+
assert await api.term.scroll(1) is None
713+
await asyncio.sleep(0.25)
714+
715+
await term_step(api, 'You must have seen three texts with different colors scrolling')
716+
717+
assert await api.term.clear() is None
718+
for i in range(1, 10):
719+
assert await api.term.setCursorPos(1, i) is None
720+
assert await api.term.write((str(i) + ' ') * 10) is None
721+
await asyncio.sleep(2)
722+
for i in range(2, 10, 2):
723+
assert await api.term.setCursorPos(1, i) is None
724+
assert await api.term.clearLine() is None
725+
await asyncio.sleep(2)
726+
727+
await term_step(api, 'You must have seen some lines disappearing')
728+
729+
assert await api.term.clear() is None
730+
assert await api.term.setCursorPos(1, 1) is None
731+
assert await api.term.blit(
732+
'rainbowrainbow',
733+
'e14d3ba0000000',
734+
'fffffffe14d3ba',
735+
) is None
736+
await asyncio.sleep(3)
737+
738+
await term_step(api, 'You must have seen per-letter colored text')
739+
740+
assert await api.term.setBackgroundColor(api.colors.white) is None
741+
assert await api.term.clear() is None
742+
assert await api.term.setCursorPos(1, 1) is None
743+
for i, color in enumerate(api.colors):
744+
await api.term.setPaletteColor(color, i / 15, 0, 0)
745+
assert await api.term.blit(
746+
' redtextappears!',
747+
'0123456789abcdef',
748+
'0000000000000000',
749+
) is None
750+
await asyncio.sleep(3)
751+
752+
await term_step(api, 'You must have seen different shades of red made using palettes')
647753

648754
await api.print('Test finished successfully')

0 commit comments

Comments
 (0)