mxivideo/test_encoding.py

85 lines
2.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test script to verify encoding handling between Python and Rust
"""
import sys
import json
import os
def test_encoding():
"""Test various encoding scenarios"""
# Configure encoding
if os.name == 'nt': # Windows
try:
import subprocess
subprocess.run(['chcp', '65001'], shell=True, capture_output=True)
except:
pass
if hasattr(sys.stdout, 'reconfigure'):
try:
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
except:
pass
# Test cases with various characters
test_cases = [
{"type": "ascii", "text": "Hello World"},
{"type": "chinese", "text": "你好世界"},
{"type": "japanese", "text": "こんにちは"},
{"type": "emoji", "text": "🎉🚀✅"},
{"type": "mixed", "text": "Hello 你好 🎉"},
{"type": "special", "text": "Special chars: àáâãäåæçèéêë"},
]
print("Testing encoding compatibility...")
for i, test_case in enumerate(test_cases):
# Test regular print
print(f"Test {i+1}: {test_case['type']} - {test_case['text']}")
# Test JSON-RPC format with ensure_ascii=True
jsonrpc_response = {
"jsonrpc": "2.0",
"id": i,
"result": {
"status": True,
"message": test_case['text'],
"type": test_case['type']
}
}
json_str = json.dumps(jsonrpc_response, ensure_ascii=True, separators=(',', ':'))
output_line = f"JSONRPC:{json_str}"
if hasattr(sys.stdout, 'buffer'):
sys.stdout.buffer.write(output_line.encode('utf-8'))
sys.stdout.buffer.write(b'\n')
sys.stdout.buffer.flush()
else:
print(output_line)
sys.stdout.flush()
# Test final result
final_result = {
"status": True,
"message": "编码测试完成 - Encoding test completed 🎉",
"test_count": len(test_cases)
}
result_json = json.dumps(final_result, ensure_ascii=True, indent=2)
if hasattr(sys.stdout, 'buffer'):
sys.stdout.buffer.write(result_json.encode('utf-8'))
sys.stdout.buffer.write(b'\n')
sys.stdout.buffer.flush()
else:
print(result_json)
sys.stdout.flush()
if __name__ == "__main__":
test_encoding()