spriteforge/tests/test_format.py

45 lines
2.0 KiB
Python
Raw Normal View History

import struct
import pytest
from spriteforge.format import Animation,FLAG_DEPTH, FLAG_NORMALS, Frame, encode_sfa, fnv1a32
from spriteforge.reader import decode_sfa
from spriteforge.palette import decode_sfp, encode_sfp
def sample():
return Frame(bytes([0,1,1,0,2,2,2,0,0,3,0,0]),4,3,2,3,83,bytes([0,0]*12),bytes([0,1,2,0,4,5,6,0,0,9,0,0]),-1.25,2.5)
def test_roundtrip_parallel_streams():
got=decode_sfa(encode_sfa([sample()],animation="idle",directions=(4500,)))
assert got.flags==FLAG_NORMALS|FLAG_DEPTH
assert got.animation=="idle" and got.directions==(4500,)
assert got.frames[0]==sample() and got.layer_orders==((0,),)
def test_empty_rows():
f=Frame(bytes([0]*8+[4,0,0,4]),4,3)
assert decode_sfa(encode_sfa([f])).frames[0]==f
@pytest.mark.parametrize("kind",["magic","size","stream"])
def test_corrupt_rejected(kind):
blob=bytearray(encode_sfa([sample()]))
if kind=="magic": blob[0]=0
elif kind=="size": struct.pack_into("<I",blob,12,len(blob)+1)
else:
fo=struct.unpack_from("<I",blob,40)[0]; struct.pack_into("<I",blob,fo+20,len(blob)+10)
with pytest.raises(ValueError): decode_sfa(bytes(blob))
def test_validation_hash_palette():
assert fnv1a32("idle")==0xC301CF93
with pytest.raises(ValueError): encode_sfa([Frame(b"\1",2,2)])
p=[(i,i,i) for i in range(256)]
blob=encode_sfp({"gray":p},{"identity":list(range(256))})
assert blob[:4]==b"SFP\0" and decode_sfp(blob).colormaps["identity"]==bytes(range(256))
with pytest.raises(ValueError): encode_sfp({"bad":p[:-1]})
def test_multiple_animations_and_layer_orders():
frames=[sample() for _ in range(6)]
blob=encode_sfa(frames,directions=(0,18000),layer_count=3,
animations=(Animation("idle",1),Animation("walk",2)),
layer_orders=((0,1,2),(2,1,0),(0,2,1),(1,2,0),(2,0,1),(1,0,2)))
decoded=decode_sfa(blob)
assert decoded.animations==(("idle",0,1),("walk",2,2))
assert decoded.layer_orders[1]==(2,1,0) and len(decoded.frames)==6