42 lines
1.8 KiB
Python
42 lines
1.8 KiB
Python
"""The installed title wrapper must hide unknown levels and survive both locale formats."""
|
|
from pathlib import Path
|
|
import sys
|
|
import types
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class TradeLevelUITests(unittest.TestCase):
|
|
def run_case(self, level, fmt):
|
|
modules={n:types.ModuleType(n) for n in ('uiExchange','exchange','localeInfo','app')}
|
|
class Text:
|
|
def SetText(self,s):self.text=s
|
|
class Dialog:
|
|
def __init__(self):self.TitleName=Text();self.opened=0
|
|
def OpenDialog(self):self.opened+=1;self.TitleName.SetText('old title')
|
|
modules['uiExchange'].ExchangeDialog=Dialog
|
|
modules['exchange'].GetNameFromTarget=lambda:'player%name'
|
|
modules['exchange'].GetLevelFromTarget=lambda:level
|
|
modules['localeInfo'].EXCHANGE_TITLE=fmt
|
|
modules['app'].ENABLE_LEVEL_IN_TRADE=1
|
|
raw=(ROOT/'src/platform/ScriptLib/Ava2UICompatScript.inc').read_text()
|
|
code='try:\n import uiExchange'+raw.split('try:\n import uiExchange',1)[1].split('try:\n import introselect',1)[0]
|
|
with patch.dict(sys.modules,modules):exec(code,{})
|
|
dialog=Dialog();dialog.OpenDialog();self.assertEqual(dialog.opened,1)
|
|
return dialog.TitleName.text
|
|
|
|
def test_known_level_classic_and_ch5_format(self):
|
|
self.assertEqual(self.run_case(87,'%s trading'),'player%name (Lv.87)')
|
|
self.assertEqual(self.run_case(87,'%s (%d)'),'player%name (87)')
|
|
|
|
def test_unknown_level_is_not_fabricated(self):
|
|
self.assertEqual(self.run_case(0,'%s (%d)'),'player%name')
|
|
|
|
def test_bad_locale_format_does_not_break_open(self):
|
|
self.assertEqual(self.run_case(87,'%d %d'),'player%name')
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|