|
| 1 | +import pytest |
| 2 | +import python_minifier |
| 3 | +import tempfile |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +def test_minify_utf8_file(): |
| 8 | + """Test minifying a Python file with UTF-8 characters not in Windows default encoding.""" |
| 9 | + |
| 10 | + # Create Python source with UTF-8 characters that are not in Windows-1252 |
| 11 | + # Using Greek letters, Cyrillic, and mathematical symbols |
| 12 | + source_code = '''# -*- coding: utf-8 -*- |
| 13 | +""" |
| 14 | +This module contains UTF-8 characters that are not in Windows-1252 encoding: |
| 15 | +- Greek: α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω |
| 16 | +- Cyrillic: а б в г д е ё ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ы ь э ю я |
| 17 | +- Mathematical: ∀ ∃ ∈ ∉ ∅ ∞ ∑ ∏ √ ∫ ∇ ∂ ≠ ≤ ≥ ≈ ≡ ⊂ ⊃ ⊆ ⊇ |
| 18 | +- Arrows: ← → ↑ ↓ ↔ ↕ ↖ ↗ ↘ ↙ |
| 19 | +""" |
| 20 | +
|
| 21 | +def greet_in_greek(): |
| 22 | + return "Γεια σας κόσμος" # "Hello world" in Greek |
| 23 | +
|
| 24 | +def mathematical_formula(): |
| 25 | + # Using mathematical symbols in comments |
| 26 | + # ∀x ∈ ℝ: x² ≥ 0 |
| 27 | + return "∑ from i=1 to ∞ of 1/i² = π²/6" |
| 28 | +
|
| 29 | +def arrow_symbols(): |
| 30 | + directions = { |
| 31 | + "left": "←", |
| 32 | + "right": "→", |
| 33 | + "up": "↑", |
| 34 | + "down": "↓" |
| 35 | + } |
| 36 | + return directions |
| 37 | +
|
| 38 | +if __name__ == "__main__": |
| 39 | + print(greet_in_greek()) |
| 40 | + print(greet_in_russian()) |
| 41 | + print(mathematical_formula()) |
| 42 | + print(arrow_symbols()) |
| 43 | +''' |
| 44 | + |
| 45 | + # Write to temporary file with UTF-8 encoding |
| 46 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8') as f: |
| 47 | + f.write(source_code) |
| 48 | + temp_file = f.name |
| 49 | + |
| 50 | + try: |
| 51 | + # Read the file and minify it |
| 52 | + with open(temp_file, 'r', encoding='utf-8') as f: |
| 53 | + original_content = f.read() |
| 54 | + |
| 55 | + # This should work - minify the UTF-8 content |
| 56 | + minified = python_minifier.minify(original_content) |
| 57 | + |
| 58 | + # Verify the minified code still contains the UTF-8 characters |
| 59 | + assert "Γεια σας κόσμος" in minified |
| 60 | + assert "∑ from i=1 to ∞" in minified |
| 61 | + assert "←" in minified and "→" in minified |
| 62 | + |
| 63 | + finally: |
| 64 | + # Clean up |
| 65 | + os.unlink(temp_file) |
| 66 | + |
| 67 | + |
| 68 | +def test_minify_utf8_file_direct(): |
| 69 | + """Test minifying a file directly with UTF-8 characters.""" |
| 70 | + |
| 71 | + # Create Python source with UTF-8 characters |
| 72 | + source_code = '''# UTF-8 test file |
| 73 | +def emoji_function(): |
| 74 | + """Function with emoji and special characters: 🐍 ∆ ∑ ∫ ∞""" |
| 75 | + return "Python is 🐍 awesome! Math symbols: ∆x ≈ 0, ∑∞ = ∞" |
| 76 | +
|
| 77 | +class UnicodeClass: |
| 78 | + """Class with unicode: ñ ü ö ä ë ï ÿ""" |
| 79 | + def __init__(self): |
| 80 | + self.message = "Héllö Wörld with àccénts!" |
| 81 | + |
| 82 | + def get_symbols(self): |
| 83 | + return "Symbols: ™ © ® ° ± × ÷ ≠ ≤ ≥" |
| 84 | +''' |
| 85 | + |
| 86 | + # Test direct minification |
| 87 | + minified = python_minifier.minify(source_code) |
| 88 | + |
| 89 | + # Verify UTF-8 characters are preserved |
| 90 | + assert "🐍" in minified |
| 91 | + assert "∆" in minified |
| 92 | + assert "∑" in minified |
| 93 | + assert "∞" in minified |
| 94 | + assert "Héllö" in minified |
| 95 | + assert "àccénts" in minified |
| 96 | + assert "™" in minified |
| 97 | + assert "©" in minified |
0 commit comments