#!/usr/bin/env python3 """ Test script to verify that preset names with Unicode characters like 'ö' work correctly. This script tests the validation logic outside of Blender. """ import re import json import os import tempfile def test_preset_name_validation(): """Test the new preset name validation regex""" # Test cases with expected results test_cases = [ # Valid names (should pass) ("Test Preset", True), ("Mein schönes Preset", True), # German with ö ("Préférence française", True), # French with accents ("Configuración español", True), # Spanish with ñ ("Test_123", True), ("Hello-World", True), ("Тест", True), # Cyrillic ("テスト", True), # Japanese ("测试", True), # Chinese # Invalid names (should fail) ("TestPreset", False), # Contains > ("Test:Preset", False), # Contains : ("Test\"Preset", False), # Contains " ("Test|Preset", False), # Contains | ("Test?Preset", False), # Contains ? ("Test*Preset", False), # Contains * ("Test/Preset", False), # Contains / ("Test\\Preset", False), # Contains \ ("", False), # Empty string ] print("Testing preset name validation...") for preset_name, should_pass in test_cases: # Test the Unicode regex pattern unicode_pattern_match = bool(re.match(r'^[\w\s\-]+$', preset_name, re.UNICODE)) # Test filesystem-unsafe characters invalid_chars = ['<', '>', ':', '"', '|', '?', '*', '/', '\\'] has_invalid_chars = any(char in preset_name for char in invalid_chars) # Combined validation (same as in the actual code) validation_passes = unicode_pattern_match and not has_invalid_chars and preset_name.strip() status = "✅ PASS" if validation_passes == should_pass else "❌ FAIL" print(f"{status}: '{preset_name}' -> Expected: {should_pass}, Got: {validation_passes}") if validation_passes != should_pass: print(f" Unicode match: {unicode_pattern_match}, Has invalid chars: {has_invalid_chars}") def test_json_serialization(): """Test that Unicode characters work correctly with JSON serialization/deserialization""" print("\nTesting JSON serialization with Unicode characters...") test_data = { 'name': 'Schönes Preset', 'text': 'Höllö Wörld', 'texture_width': 1024, 'texture_height': 1024, 'font_size': 96 } try: # Test JSON encoding/decoding json_str = json.dumps(test_data, indent=2, ensure_ascii=False) decoded_data = json.loads(json_str) print("✅ JSON serialization successful") print(f"Original name: {test_data['name']}") print(f"Decoded name: {decoded_data['name']}") print(f"Names match: {test_data['name'] == decoded_data['name']}") # Test file I/O with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: json.dump(test_data, f, indent=2, ensure_ascii=False) temp_file = f.name with open(temp_file, 'r', encoding='utf-8') as f: file_data = json.load(f) os.unlink(temp_file) print("✅ File I/O successful") print(f"File data name: {file_data['name']}") print(f"File names match: {test_data['name'] == file_data['name']}") except Exception as e: print(f"❌ JSON serialization failed: {e}") def test_filesystem_compatibility(): """Test creating files with Unicode names""" print("\nTesting filesystem compatibility...") test_names = [ "schönes_preset", "café_preset", "naïve_preset", "test_ñ_preset" ] temp_dir = tempfile.mkdtemp() print(f"Testing in directory: {temp_dir}") for name in test_names: try: filename = f"{name}.json" filepath = os.path.join(temp_dir, filename) # Create file with open(filepath, 'w', encoding='utf-8') as f: json.dump({'name': name}, f, ensure_ascii=False) # Read file back with open(filepath, 'r', encoding='utf-8') as f: data = json.load(f) # Clean up os.unlink(filepath) print(f"✅ {name}: File created and read successfully") except Exception as e: print(f"❌ {name}: Failed - {e}") # Clean up temp directory os.rmdir(temp_dir) if __name__ == "__main__": print("=" * 60) print("Testing Text Texture Generator Preset Unicode Support") print("=" * 60) test_preset_name_validation() test_json_serialization() test_filesystem_compatibility() print("\n" + "=" * 60) print("Test completed! Check results above.") print("=" * 60)