初始提交,未完全测试
This commit is contained in:
122
test.py
Normal file
122
test.py
Normal file
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/env python3.9
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
JFinal Test - Basic Test Script for JFinal Python Template Engine
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the parent directory to Python path to allow importing from py_enjoy
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
print(f"Python path: {sys.path}")
|
||||
def test_kit_classes():
|
||||
"""Test kit classes"""
|
||||
print("Testing kit classes...")
|
||||
|
||||
# Test StrKit
|
||||
from py_enjoy.kit.StrKit import StrKit
|
||||
assert StrKit.first_char_to_lower_case("Hello") == "hello"
|
||||
assert StrKit.first_char_to_upper_case("hello") == "Hello"
|
||||
assert StrKit.is_blank(" ") == True
|
||||
assert StrKit.is_blank("test") == False
|
||||
assert StrKit.not_blank("test") == True
|
||||
print("✓ StrKit tests passed")
|
||||
|
||||
# Test HashKit
|
||||
from py_enjoy.kit.HashKit import HashKit
|
||||
md5_result = HashKit.md5("test")
|
||||
assert len(md5_result) == 32
|
||||
assert HashKit.slow_equals(b"test", b"test") == True
|
||||
assert HashKit.slow_equals(b"test", b"test2") == False
|
||||
print("✓ HashKit tests passed")
|
||||
|
||||
# Test Kv
|
||||
from py_enjoy.kit.Kv import Kv
|
||||
kv = Kv.of("name", "John").set("age", 25)
|
||||
assert kv.get("name") == "John"
|
||||
assert kv.get_int("age") == 25
|
||||
assert kv.get_str("name") == "John"
|
||||
print("✓ Kv tests passed")
|
||||
|
||||
# Test Prop
|
||||
from py_enjoy.kit.Prop import Prop
|
||||
# Create a test properties file
|
||||
test_content = """
|
||||
name=John
|
||||
age=25
|
||||
enabled=true
|
||||
"""
|
||||
prop = Prop(test_content, is_file=False)
|
||||
assert prop.get("name") == "John"
|
||||
assert prop.get_int("age") == 25
|
||||
assert prop.get_boolean("enabled") == True
|
||||
print("✓ Prop tests passed")
|
||||
|
||||
print("All kit class tests passed!")
|
||||
|
||||
|
||||
def test_template_engine():
|
||||
"""Test template engine"""
|
||||
print("\nTesting template engine...")
|
||||
|
||||
from py_enjoy.template.Engine import Engine
|
||||
from py_enjoy.kit.Kv import Kv
|
||||
|
||||
engine = Engine.use()
|
||||
|
||||
# Test basic template rendering
|
||||
template = engine.get_template_by_string("Hello, #(name)!")
|
||||
result = template.render_to_string(Kv.of("name", "World"))
|
||||
assert result == "Hello, World!"
|
||||
print("✓ Basic template test passed")
|
||||
|
||||
# Test expression evaluation
|
||||
template2 = engine.get_template_by_string("Result: #(1 + 2 * 3)")
|
||||
result2 = template2.render_to_string()
|
||||
assert "7" in result2
|
||||
print("✓ Expression template test passed")
|
||||
|
||||
print("All template engine tests passed!")
|
||||
|
||||
|
||||
def test_type_conversions():
|
||||
"""Test type conversions"""
|
||||
print("\nTesting type conversions...")
|
||||
|
||||
from py_enjoy.kit.TypeKit import TypeKit
|
||||
|
||||
assert TypeKit.to_int("123") == 123
|
||||
assert TypeKit.to_float("3.14") == 3.14
|
||||
assert TypeKit.to_boolean("true") == True
|
||||
assert TypeKit.to_boolean("1") == True
|
||||
assert TypeKit.to_boolean("0") == False
|
||||
assert TypeKit.to_int(None) is None
|
||||
print("✓ TypeKit tests passed")
|
||||
|
||||
print("All type conversion tests passed!")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all tests"""
|
||||
print("Running JFinal Python Template Engine Tests...")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
test_kit_classes()
|
||||
test_type_conversions()
|
||||
test_template_engine()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("✓ All tests passed successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n✗ Test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user