Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
import os
|
||
import sys
|
||
import tempfile
|
||
import unittest
|
||
|
||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
import uv_preview as up
|
||
from PIL import Image
|
||
|
||
|
||
class TestToPx(unittest.TestCase):
|
||
def test_origin_bottom_left_maps_to_bottom_left_pixel(self):
|
||
# UV(0,0) 原点在左下 -> 图像左下角(x=pad, y=size-pad)
|
||
x, y = up._to_px(0.0, 0.0, size=100, pad=10)
|
||
self.assertAlmostEqual(x, 10.0)
|
||
self.assertAlmostEqual(y, 90.0)
|
||
|
||
def test_top_right(self):
|
||
x, y = up._to_px(1.0, 1.0, size=100, pad=10)
|
||
self.assertAlmostEqual(x, 90.0)
|
||
self.assertAlmostEqual(y, 10.0)
|
||
|
||
|
||
class TestRenderUvPng(unittest.TestCase):
|
||
def setUp(self):
|
||
self.tmp = tempfile.mkdtemp()
|
||
|
||
def _out(self, name="uv.png"):
|
||
return os.path.join(self.tmp, name)
|
||
|
||
def test_writes_png_of_requested_size(self):
|
||
polys = [[[0.1, 0.1], [0.4, 0.1], [0.25, 0.4]],
|
||
[[0.6, 0.6], [0.9, 0.6], [0.9, 0.9], [0.6, 0.9]]]
|
||
out = up.render_uv_png(polys, self._out(), size=128)
|
||
self.assertTrue(os.path.isfile(out))
|
||
with Image.open(out) as im:
|
||
self.assertEqual(im.size, (128, 128))
|
||
self.assertEqual(im.format, "PNG")
|
||
|
||
def test_empty_polygons_still_writes_blank(self):
|
||
out = up.render_uv_png([], self._out("blank.png"), size=64)
|
||
self.assertTrue(os.path.isfile(out))
|
||
with Image.open(out) as im:
|
||
self.assertEqual(im.size, (64, 64))
|
||
|
||
def test_degenerate_polygon_skipped_no_crash(self):
|
||
out = up.render_uv_png([[[0.5, 0.5]]], self._out("deg.png"), size=64)
|
||
self.assertTrue(os.path.isfile(out))
|