import os import re import sys import tempfile import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import unity_assets as ua class TestGuid(unittest.TestCase): def test_new_guid_is_32_hex(self): g = ua.new_guid() self.assertTrue(re.fullmatch(r"[0-9a-f]{32}", g)) def test_guid_for_reuses_existing_meta(self): with tempfile.TemporaryDirectory() as d: asset = os.path.join(d, "a.png") with open(asset + ".meta", "w") as f: f.write("fileFormatVersion: 2\nguid: 0123456789abcdef0123456789abcdef\n") self.assertEqual(ua.guid_for(asset), "0123456789abcdef0123456789abcdef") def test_guid_for_generates_when_no_meta(self): with tempfile.TemporaryDirectory() as d: g = ua.guid_for(os.path.join(d, "b.png")) self.assertTrue(re.fullmatch(r"[0-9a-f]{32}", g)) class TestTemplates(unittest.TestCase): def test_texture_meta_srgb_on_off(self): on = ua.texture_meta("a" * 32, srgb=True, max_size=2048) off = ua.texture_meta("b" * 32, srgb=False, max_size=2048) self.assertIn("sRGBTexture: 1", on) self.assertIn("sRGBTexture: 0", off) self.assertIn("maxTextureSize: 2048", on) self.assertIn("guid: " + "a" * 32, on) def test_material_yaml_refs_and_keyword(self): y = ua.material_yaml("tong_wood", "s" * 32, "c" * 32, "d" * 32, alpha_test=True) self.assertIn("m_Name: tong_wood", y) self.assertIn("guid: " + "s" * 32, y) # shader self.assertIn("guid: " + "c" * 32, y) # base tex self.assertIn("guid: " + "d" * 32, y) # mix tex self.assertIn("_ALPHATEST_ON", y) self.assertIn("_AlphaTest: 1", y) def test_material_yaml_no_keyword_when_opaque(self): y = ua.material_yaml("m", "s" * 32, "c" * 32, "d" * 32, alpha_test=False) self.assertNotIn("_ALPHATEST_ON", y) self.assertIn("_AlphaTest: 0", y) def test_model_meta_external_objects(self): m = ua.model_meta("e" * 32, [("wood", "f" * 32), ("iron", "1" * 32)]) self.assertIn("name: wood", m) self.assertIn("guid: " + "f" * 32, m) self.assertIn("name: iron", m) self.assertIn("guid: " + "e" * 32, m) class TestSanitize(unittest.TestCase): def test_safe_name(self): self.assertEqual(ua.safe_name("Mat 01.a/b"), "Mat_01_a_b") if __name__ == "__main__": unittest.main()