67 lines
3.4 KiB
Python
67 lines
3.4 KiB
Python
import json
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
|
|
import spellpower_gap as rule
|
|
|
|
|
|
def entry(sid, kind=0, cost=0, **kwargs):
|
|
return dict(SpellID=sid, PowerType=kind, ManaCost=cost, **kwargs)
|
|
|
|
|
|
class SpellPowerGapTest(unittest.TestCase):
|
|
def test_all_cost_forms_and_units(self):
|
|
source = [entry(10, 0, 300), entry(11, 1, 150), entry(12, 3, 40),
|
|
entry(13, 2, 25), entry(14, PowerCostPct=12),
|
|
entry(15, ManaCostPerLevel=6), entry(16, ManaPerSecond=8)]
|
|
rows = rule.plan(source, [], [dict(SpellID=r['SpellID']) for r in source], 100)['rows']
|
|
self.assertEqual([r['ManaCost'] for r in rows[:4]], [300, 150, 40, 25])
|
|
self.assertEqual([r['PowerType'] for r in rows[:4]], [0, 1, 3, 2])
|
|
self.assertEqual(rows[4]['PowerCostPct'], 12.0)
|
|
self.assertEqual(rows[5]['ManaCostPerLevel'], 6)
|
|
self.assertEqual(rows[6]['ManaPerSecond'], 8)
|
|
self.assertTrue(all(r['PowerPctPerSecond'] == 0 for r in rows))
|
|
|
|
def test_preserves_existing_rows_and_routes_only_known_spells(self):
|
|
source = [entry(1, cost=500), entry(2, cost=200), entry(3, cost=100), entry(4)]
|
|
existing = [dict(ID=90, SpellID=1, ManaCost=123, RequiredAuraSpellID=88)]
|
|
before = json.dumps(existing)
|
|
rows = rule.plan(source, existing, [dict(SpellID=1), dict(SpellID=2), dict(SpellID=4)], 91)['rows']
|
|
self.assertEqual([r['SpellID'] for r in rows], [2])
|
|
self.assertEqual(json.dumps(existing), before)
|
|
more = rule.plan(source, existing + rows, [dict(SpellID=2)], 92, [3])['rows']
|
|
self.assertEqual([r['SpellID'] for r in more], [3])
|
|
self.assertEqual(rule.plan(source, existing + rows + more, [dict(SpellID=1), dict(SpellID=2)], 93, [3])['rows'], [])
|
|
|
|
def test_holds_nonrepresentable_costs_and_rejects_collisions(self):
|
|
result = rule.plan([entry(1, 0xfffffffe, 10), entry(2, ManaPerSecondPerLevel=2)], [], [dict(SpellID=1), dict(SpellID=2)], 1)
|
|
self.assertEqual(result['rows'], [])
|
|
self.assertEqual([r['SpellID'] for r in result['held']], [1, 2])
|
|
with self.assertRaises(ValueError):
|
|
rule.plan([], [dict(ID=100, SpellID=1)], [], 100)
|
|
with self.assertRaises(ValueError):
|
|
rule.plan([entry(1), entry(1)], [], [], 1)
|
|
with self.assertRaises(ValueError):
|
|
rule.plan([entry(1, cost=-1)], [], [dict(SpellID=1)], 1)
|
|
|
|
def test_dbc_disk_offsets_and_legacy_tsv(self):
|
|
row = [0] * 173
|
|
row[0], row[31], row[32], row[33], row[34], row[35], row[156] = 50000, 1, 150, 2, 3, 4, 5
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / 'Spell.dbc'
|
|
path.write_bytes(struct.pack('<4s4I', b'WDBC', 1, 173, 692, 1) + struct.pack('<173I', *row) + b'\0')
|
|
values = rule.source_dbc(path)[0]
|
|
self.assertEqual(values, dict(SpellID=50000, PowerType=1, ManaCost=150, ManaCostPerLevel=2, ManaPerSecond=3, ManaPerSecondPerLevel=4, PowerCostPct=5))
|
|
tsv = Path(tmp) / 'source.tsv'
|
|
tsv.write_text('entry\tmanaCost\tperLevel\tperSecond\tpct\ttype\n50000\t150\t2\t3\t5\t1\t0\tName\n')
|
|
expected = dict(values); expected.pop('ManaPerSecondPerLevel')
|
|
self.assertEqual(rule.source_tsv(tsv), [expected])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|