Expand EvForm unit tests for #2205
This commit is contained in:
parent
14a5823796
commit
ea68f165d8
2 changed files with 153 additions and 85 deletions
|
|
@ -177,7 +177,7 @@ def _to_ansi(obj, regexable=False):
|
||||||
return ANSIString(obj, regexable=regexable)
|
return ANSIString(obj, regexable=regexable)
|
||||||
|
|
||||||
|
|
||||||
class EvForm(object):
|
class EvForm:
|
||||||
"""
|
"""
|
||||||
This object is instantiated with a text file and parses
|
This object is instantiated with a text file and parses
|
||||||
it for rectangular form fields. It can then be fed a
|
it for rectangular form fields. It can then be fed a
|
||||||
|
|
@ -463,40 +463,3 @@ class EvForm(object):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"Prints the form"
|
"Prints the form"
|
||||||
return str(ANSIString("\n").join([line for line in self.form]))
|
return str(ANSIString("\n").join([line for line in self.form]))
|
||||||
|
|
||||||
|
|
||||||
def _test():
|
|
||||||
"test evform. This is used by the unittest system."
|
|
||||||
form = EvForm("evennia.utils.tests.data.evform_example")
|
|
||||||
|
|
||||||
# add data to each tagged form cell
|
|
||||||
form.map(
|
|
||||||
cells={
|
|
||||||
"AA": "|gTom the Bouncer",
|
|
||||||
2: "|yGriatch",
|
|
||||||
3: "A sturdy fellow",
|
|
||||||
4: 12,
|
|
||||||
5: 10,
|
|
||||||
6: 5,
|
|
||||||
7: 18,
|
|
||||||
8: 10,
|
|
||||||
9: 3,
|
|
||||||
"F": "rev 1",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
# create the EvTables
|
|
||||||
tableA = EvTable("HP", "MV", "MP", table=[["**"], ["*****"], ["***"]], border="incols")
|
|
||||||
tableB = EvTable(
|
|
||||||
"Skill",
|
|
||||||
"Value",
|
|
||||||
"Exp",
|
|
||||||
table=[
|
|
||||||
["Shooting", "Herbalism", "Smithing"],
|
|
||||||
[12, 14, 9],
|
|
||||||
["550/1200", "990/1400", "205/900"],
|
|
||||||
],
|
|
||||||
border="incols",
|
|
||||||
)
|
|
||||||
# add the tables to the proper ids in the form
|
|
||||||
form.map(tables={"A": tableA, "B": tableB})
|
|
||||||
return str(form)
|
|
||||||
|
|
|
||||||
|
|
@ -3,60 +3,165 @@ Unit tests for the EvForm text form generator
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from evennia.utils import evform
|
from evennia.utils import evform, ansi, evtable
|
||||||
|
|
||||||
|
|
||||||
class TestEvForm(TestCase):
|
class TestEvForm(TestCase):
|
||||||
def test_form(self):
|
|
||||||
self.maxDiff = None
|
maxDiff = None
|
||||||
form1 = evform._test()
|
|
||||||
form2 = evform._test()
|
def _parse_form(self):
|
||||||
|
"test evform. This is used by the unittest system."
|
||||||
|
form = evform.EvForm("evennia.utils.tests.data.evform_example")
|
||||||
|
|
||||||
|
# add data to each tagged form cell
|
||||||
|
form.map(
|
||||||
|
cells={
|
||||||
|
"AA": "|gTom the Bouncer",
|
||||||
|
2: "|yGriatch",
|
||||||
|
3: "A sturdy fellow",
|
||||||
|
4: 12,
|
||||||
|
5: 10,
|
||||||
|
6: 5,
|
||||||
|
7: 18,
|
||||||
|
8: 10,
|
||||||
|
9: 3,
|
||||||
|
"F": "rev 1",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
# create the EvTables
|
||||||
|
tableA = evtable.EvTable("HP", "MV", "MP",
|
||||||
|
table=[["**"], ["*****"], ["***"]],
|
||||||
|
border="incols")
|
||||||
|
tableB = evtable.EvTable(
|
||||||
|
"Skill",
|
||||||
|
"Value",
|
||||||
|
"Exp",
|
||||||
|
table=[
|
||||||
|
["Shooting", "Herbalism", "Smithing"],
|
||||||
|
[12, 14, 9],
|
||||||
|
["550/1200", "990/1400", "205/900"],
|
||||||
|
],
|
||||||
|
border="incols",
|
||||||
|
)
|
||||||
|
# add the tables to the proper ids in the form
|
||||||
|
form.map(tables={"A": tableA, "B": tableB})
|
||||||
|
return str(form)
|
||||||
|
|
||||||
|
def _simple_form(self, form):
|
||||||
|
cellsdict = {1: "Apple", 2: "Banana", 3: "Citrus", 4: "Durian"}
|
||||||
|
formdict = {"FORMCHAR": 'x', "TABLECHAR": 'c', "FORM": form}
|
||||||
|
form = evform.EvForm(form=formdict)
|
||||||
|
form.map(cellsdict)
|
||||||
|
form = ansi.strip_ansi(str(form))
|
||||||
|
# this is necessary since editors/black tend to strip lines spaces
|
||||||
|
# from the end of lines for the comparison strings.
|
||||||
|
form = "\n".join(line.rstrip() for line in form.split("\n"))
|
||||||
|
return form
|
||||||
|
|
||||||
|
def test_form_consistency(self):
|
||||||
|
"""
|
||||||
|
Make sure form looks the same every time.
|
||||||
|
|
||||||
|
"""
|
||||||
|
form1 = self._parse_form()
|
||||||
|
form2 = self._parse_form()
|
||||||
|
|
||||||
self.assertEqual(form1, form2)
|
self.assertEqual(form1, form2)
|
||||||
|
|
||||||
# self.assertEqual(form1, "")
|
def test_form_output(self):
|
||||||
# '.------------------------------------------------.\n'
|
"""
|
||||||
# '| |\n'
|
Check the result of the form. We strip ansi for readability.
|
||||||
# '| Name: \x1b[0m\x1b[1m\x1b[32mTom\x1b[1m\x1b[32m \x1b'
|
|
||||||
# '[1m\x1b[32mthe\x1b[1m\x1b[32m \x1b[0m \x1b[0m '
|
"""
|
||||||
# 'Account: \x1b[0m\x1b[1m\x1b[33mGriatch '
|
|
||||||
# '\x1b[0m\x1b[0m\x1b[1m\x1b[32m\x1b[1m\x1b[32m\x1b[1m\x1b[32m\x1b[1m\x1b[32m\x1b[0m\x1b[0m '
|
form = self._parse_form()
|
||||||
# '|\n'
|
form_noansi = ansi.strip_ansi(form)
|
||||||
# '| \x1b[0m\x1b[1m\x1b[32mBouncer\x1b[0m \x1b[0m |\n'
|
# we must strip extra space at the end of output simply
|
||||||
# '| |\n'
|
# because editors tend to strip it when creating
|
||||||
# ' >----------------------------------------------< \n'
|
# the comparison string ...
|
||||||
# '| |\n'
|
form_noansi = "\n".join(line.rstrip() for line in form_noansi.split("\n"))
|
||||||
# '| Desc: \x1b[0mA sturdy \x1b[0m \x1b[0m'
|
|
||||||
# ' STR: \x1b[0m12 \x1b[0m\x1b[0m\x1b[0m\x1b[0m'
|
self.assertNotEqual(form, form_noansi)
|
||||||
# ' DEX: \x1b[0m10 \x1b[0m\x1b[0m\x1b[0m\x1b[0m |\n'
|
expected = """
|
||||||
# '| \x1b[0mfellow\x1b[0m \x1b[0m'
|
.------------------------------------------------.
|
||||||
# ' INT: \x1b[0m5 \x1b[0m\x1b[0m\x1b[0m\x1b[0m'
|
| |
|
||||||
# ' STA: \x1b[0m18 \x1b[0m\x1b[0m\x1b[0m\x1b[0m |\n'
|
| Name: Tom the Account: Griatch |
|
||||||
# '| \x1b[0m \x1b[0m'
|
| Bouncer |
|
||||||
# ' LUC: \x1b[0m10 \x1b[0m\x1b[0m\x1b[0m'
|
| |
|
||||||
# ' MAG: \x1b[0m3 \x1b[0m\x1b[0m\x1b[0m |\n'
|
>----------------------------------------------<
|
||||||
# '| |\n'
|
| |
|
||||||
# ' >----------.-----------------------------------< \n'
|
| Desc: A sturdy STR: 12 DEX: 10 |
|
||||||
# '| | |\n'
|
| fellow INT: 5 STA: 18 |
|
||||||
# '| \x1b[0mHP\x1b[0m|\x1b[0mMV \x1b[0m|\x1b[0mMP\x1b[0m '
|
| LUC: 10 MAG: 3 |
|
||||||
# '| \x1b[0mSkill \x1b[0m|\x1b[0mValue \x1b[0m'
|
| |
|
||||||
# '|\x1b[0mExp \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m |\n'
|
>----------.-----------------------------------<
|
||||||
# '| ~~+~~~+~~ | ~~~~~~~~~~~+~~~~~~~~~~+~~~~~~~~~~~ |\n'
|
| | |
|
||||||
# '| \x1b[0m**\x1b[0m|\x1b[0m***\x1b[0m\x1b[0m|\x1b[0m**\x1b[0m\x1b[0m '
|
| HP|MV |MP | Skill |Value |Exp |
|
||||||
# '| \x1b[0mShooting \x1b[0m|\x1b[0m12 \x1b[0m'
|
| ~~+~~~+~~ | ~~~~~~~~~~~+~~~~~~~~~~+~~~~~~~~~~~ |
|
||||||
# '|\x1b[0m550/1200 \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m |\n'
|
| **|***|** | Shooting |12 |550/1200 |
|
||||||
# '| \x1b[0m \x1b[0m|\x1b[0m**\x1b[0m \x1b[0m|\x1b[0m*\x1b[0m \x1b[0m '
|
| |** |* | Herbalism |14 |990/1400 |
|
||||||
# '| \x1b[0mHerbalism \x1b[0m|\x1b[0m14 \x1b[0m'
|
| | | | Smithing |9 |205/900 |
|
||||||
# '|\x1b[0m990/1400 \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m |\n'
|
| | |
|
||||||
# '| \x1b[0m \x1b[0m|\x1b[0m \x1b[0m|\x1b[0m \x1b[0m '
|
-----------`-------------------------------------
|
||||||
# '| \x1b[0mSmithing \x1b[0m|\x1b[0m9 \x1b[0m'
|
Footer: rev 1
|
||||||
# '|\x1b[0m205/900 \x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m\x1b[0m |\n'
|
info
|
||||||
# '| | |\n'
|
""".lstrip()
|
||||||
# ' -----------`-------------------------------------\n'
|
self.assertEqual(expected, form_noansi)
|
||||||
# ' Footer: \x1b[0mrev 1 \x1b[0m \n'
|
|
||||||
# ' info \n'
|
|
||||||
# ' ')
|
|
||||||
|
|
||||||
def test_ansi_escape(self):
|
def test_ansi_escape(self):
|
||||||
# note that in a msg() call, the result would be the correct |-----,
|
# note that in a msg() call, the result would be the correct |-----,
|
||||||
# in a print, ansi only gets called once, so ||----- is the result
|
# in a print, ansi only gets called once, so ||----- is the result
|
||||||
self.assertEqual(str(evform.EvForm(form={"FORM": "\n||-----"})), "||-----")
|
self.assertEqual(str(evform.EvForm(form={"FORM": "\n||-----"})), "||-----")
|
||||||
|
|
||||||
|
def test_stacked_form(self):
|
||||||
|
"""
|
||||||
|
Test simple stacked form.
|
||||||
|
|
||||||
|
"""
|
||||||
|
form = """
|
||||||
|
xxxx1xxxx
|
||||||
|
xxxx2xxxx
|
||||||
|
xxxx3xxxx
|
||||||
|
xxxx4xxxx
|
||||||
|
"""
|
||||||
|
expected = """
|
||||||
|
Apple
|
||||||
|
Banana
|
||||||
|
Citrus
|
||||||
|
Durian
|
||||||
|
""".lstrip()
|
||||||
|
result = self._simple_form(form)
|
||||||
|
self.assertEqual(expected, result)
|
||||||
|
|
||||||
|
def test_side_by_side_two_column(self):
|
||||||
|
"""
|
||||||
|
Side-by-side 2-column form (bug #2205)
|
||||||
|
|
||||||
|
"""
|
||||||
|
form = """
|
||||||
|
xxxx1xxxx xxxx2xxxx
|
||||||
|
xxxx3xxxx xxxx4xxxx
|
||||||
|
"""
|
||||||
|
expected = """
|
||||||
|
Apple Banana
|
||||||
|
Citrus Durian
|
||||||
|
""".lstrip()
|
||||||
|
result = self._simple_form(form)
|
||||||
|
self.assertEqual(expected, result)
|
||||||
|
|
||||||
|
def test_side_by_side_three_column(self):
|
||||||
|
"""
|
||||||
|
Side-by-side 3-column form (bug #2205)
|
||||||
|
|
||||||
|
"""
|
||||||
|
form = """
|
||||||
|
xxxx1xxxx xxxx2xxxx xxxx3xxxx
|
||||||
|
xxxx4xxxx
|
||||||
|
"""
|
||||||
|
expected = """
|
||||||
|
Apple Banana Citrus
|
||||||
|
Durian
|
||||||
|
""".lstrip()
|
||||||
|
result = self._simple_form(form)
|
||||||
|
self.assertEqual(expected, result)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue