58 lines
2.8 KiB
Python
58 lines
2.8 KiB
Python
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ORIGINAL = ROOT.parent/'Original'
|
|
sys.path.insert(0, str(ROOT/'tools/extractors'))
|
|
from gadget_resources import CANVASES, export_gadget_resources
|
|
from omt import Database, FormatError
|
|
|
|
|
|
@unittest.skipUnless(ORIGINAL.exists(), 'Original assets are local integration fixtures')
|
|
class GadgetResourceTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.database = Database(ORIGINAL/'omt/screens.omt')
|
|
|
|
def test_exact_original_pixels_identities_and_dimensions_are_preserved(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory)/'gadgets'
|
|
manifest = export_gadget_resources(self.database, output)
|
|
self.assertEqual(manifest['sourceSha256'], 'e8e95231f5d302b41f4fbf33df1c5a08ce4645370f9f519527ff1727b73b7a5e')
|
|
self.assertEqual((manifest['referenceWidth'], manifest['referenceHeight']), (640, 480))
|
|
self.assertEqual(len(manifest['textures']), 54)
|
|
self.assertEqual(len({texture['id'] for texture in manifest['textures']}), 54)
|
|
self.assertEqual(manifest, json.loads((output/'gadgets.json').read_text()))
|
|
by_id = {texture['id']: texture for texture in manifest['textures']}
|
|
self.assertEqual(by_id[14]['name'], 'item04')
|
|
self.assertEqual(manifest['tiles'][3]['label'], 'SHRINK RAY')
|
|
self.assertEqual(by_id[119]['name'], '9jetpack')
|
|
self.assertEqual((by_id[253]['width'], by_id[253]['height']), (15, 210))
|
|
for texture in manifest['textures']:
|
|
rgba = (output/texture['path']).read_bytes()
|
|
self.assertEqual(len(rgba), texture['width']*texture['height']*4)
|
|
self.assertEqual(hashlib.sha256(rgba).hexdigest(), texture['rgbaSha256'])
|
|
self.assertTrue((output/texture['png']).is_file())
|
|
|
|
def test_exports_cannot_overwrite_any_original_asset_subtree(self):
|
|
for output in (ORIGINAL, ORIGINAL/'omt/generated', ORIGINAL/'ASE/generated', ORIGINAL/'png/generated'):
|
|
with self.subTest(output=output), self.assertRaises(ValueError):
|
|
export_gadget_resources(self.database, output)
|
|
|
|
def test_changed_resource_contract_rejects_before_writing(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output = Path(directory)/'gadgets'
|
|
with patch('gadget_resources.read_canvas', return_value=dict(width=1, height=1, rgba=b'\0'*4)):
|
|
with self.assertRaises(FormatError):
|
|
export_gadget_resources(self.database, output)
|
|
self.assertFalse(output.exists())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|