"""
测试 placeholder_impl.py 中的 X 和 Y 工具
"""
import sys
import os
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)
from vools.functional.placeholder_impl import X, Y
class vicList(list):
def map(self, f):
return self.__class__(f(i) for i in self)
def test_x_basic():
"""测试 X 的基本功能"""
result = X.strip()[' hello ']
assert result == 'hello', f"Expected 'hello', got {result}"
result = X.strip().split(',')['a,b,c']
assert result == ['a', 'b', 'c'], f"Expected ['a', 'b', 'c'], got {result}"
result = X.split(',')[None, 1]['a,b,c']
assert result == 'b', f"Expected 'b', got {result}"
print("X 基本功能测试通过")
def test_x_direct_index():
"""测试 X[idx] 直接构建索引操作"""
result = X[0]['hello']
assert result == 'h', f"Expected 'h', got {result}"
result = X[-1]['hello']
assert result == 'o', f"Expected 'o', got {result}"
result = X[1:4]['hello']
assert result == 'ell', f"Expected 'ell', got {result}"
result = X[0].upper()['hello']
assert result == 'H', f"Expected 'H', got {result}"
result = X[0].upper()['hello world', 0]
assert result == 'H', f"Expected 'H', got {result}"
result = X['name'][{'name': 'Alice'}]
assert result == 'Alice', f"Expected 'Alice', got {result}"
result = X['name'].upper()[{'name': 'alice'}]
assert result == 'ALICE', f"Expected 'ALICE', got {result}"
result = X[0][None, 1][[[1, 2], [3, 4]]]
assert result == 2, f"Expected 2, got {result}"
print("X 直接索引操作测试通过")
def test_x_index_pipeline():
"""测试 X 的索引管道功能 [None, idx...]"""
result = X.split(',')[None, 0]['a,b,c']
assert result == 'a', f"Expected 'a', got {result}"
result = X.split(',')[None, 1:3]['a,b,c,d']
assert result == ['b', 'c'], f"Expected ['b', 'c'], got {result}"
result = X.strip()[None, 0].upper()[' hello ']
assert result == 'H', f"Expected 'H', got {result}"
result = X.split(',')[None, 0].upper()['a,b,c']
assert result == 'A', f"Expected 'A', got {result}"
print("X 索引管道功能测试通过")
def test_x_with_extra_index():
"""测试 X 的执行时额外索引 [target, idx...]"""
result = X.split(',')['a,b,c,d', 1]
assert result == 'b', f"Expected 'b', got {result}"
result = X.split(',')['a,b,c,d', 1:3]
assert result == ['b', 'c'], f"Expected ['b', 'c'], got {result}"
result = X.strip().split(',')[' a,b,c,d ', 1]
assert result == 'b', f"Expected 'b', got {result}"
print("X 执行时额外索引测试通过")
def test_x_with_f():
"""测试 X 的 f() 工厂方法"""
result = X.split(',').f(vicList)['a,b,c']
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
result = X.strip().split(',').f(vicList)[' a,b,c ']
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
result = X.split(',')[None, 0].upper().f(str.lower)['a,b,c']
assert result == 'a', f"Expected 'a', got {result}"
print("X f() 工厂方法测试通过")
def test_x_as_function():
"""测试 X 的 as_function() 方法"""
f = X.strip().as_function()
assert callable(f), f"Expected callable, got {type(f)}"
result = f(' hello ')
assert result == 'hello', f"Expected 'hello', got {result}"
f = X.strip().split(',').as_function()
result = f(' a,b,c ')
assert result == ['a', 'b', 'c'], f"Expected ['a', 'b', 'c'], got {result}"
f = X.split(',').as_function()
result = f('a,b,c', f=vicList)
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
f = X['name'].as_function()
result = f({'name': 'Alice'})
assert result == 'Alice', f"Expected 'Alice', got {result}"
f = X['name'].upper().as_function()
result = f({'name': 'alice'})
assert result == 'ALICE', f"Expected 'ALICE', got {result}"
f = X[0][None, 1].as_function()
result = f([[1, 2], [3, 4]])
assert result == 2, f"Expected 2, got {result}"
f = X.split(',').f(vicList).as_function()
result = f('a,b,c')
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
print("X as_function() 方法测试通过")
def test_y_basic():
"""测试 Y 的基本功能"""
result = Y.strip()(' hello ', exe=True)
assert result == 'hello', f"Expected 'hello', got {result}"
result = Y.strip().split(',')('a,b,c', exe=True)
assert result == ['a', 'b', 'c'], f"Expected ['a', 'b', 'c'], got {result}"
result = Y.split(',')[1]('a,b,c', exe=True)
assert result == 'b', f"Expected 'b', got {result}"
print("Y 基本功能测试通过")
def test_y_as_function():
"""测试 Y 的 as_function() 新方法"""
f = Y.strip().as_function()
assert callable(f), f"Expected callable, got {type(f)}"
result = f(' hello ')
assert result == 'hello', f"Expected 'hello', got {result}"
f = Y.strip().split(',').as_function()
result = f(' a,b,c ')
assert result == ['a', 'b', 'c'], f"Expected ['a', 'b', 'c'], got {result}"
f = Y.split(',').as_function()
result = f('a,b,c', f=vicList)
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
f = Y['name'].as_function()
result = f({'name': 'Alice'})
assert result == 'Alice', f"Expected 'Alice', got {result}"
f = Y['name'].upper().as_function()
result = f({'name': 'alice'})
assert result == 'ALICE', f"Expected 'ALICE', got {result}"
f = Y[0][1].as_function()
result = f([[1, 2], [3, 4]])
assert result == 2, f"Expected 2, got {result}"
f = Y.strip().f(vicList).as_function()
result = f(' hello ')
assert isinstance(result, list), f"Expected list, got {type(result)}"
assert result == ['h', 'e', 'l', 'l', 'o'], f"Expected ['h', 'e', 'l', 'l', 'o'], got {result}"
print("Y as_function() 方法测试通过")
def test_y_f_method():
"""测试 Y 的 f() 工厂方法"""
result = Y.split(',').f(vicList)('a,b,c', exe=True)
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
result = Y.strip().split(',').f(vicList)(' a,b,c ', exe=True)
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
result = Y.split(',')[0].upper().f(str.lower)('a,b,c', exe=True)
assert result == 'a', f"Expected 'a', got {result}"
print("Y f() 方法测试通过")
def test_y_as_subscript():
"""测试 Y 的 as_subscript() 方法"""
sub = Y.strip().as_subscript()
result = sub[' hello ']
assert result == 'hello', f"Expected 'hello', got {result}"
sub = Y.strip().split(',').as_subscript()
result = sub[' a,b,c ']
assert result == ['a', 'b', 'c'], f"Expected ['a', 'b', 'c'], got {result}"
sub = Y.split(',').as_subscript()
result = sub['a,b,c', 1]
assert result == 'b', f"Expected 'b', got {result}"
sub = Y.split(',').as_subscript()
result = sub['a,b,c,d', 1:3]
assert result == ['b', 'c'], f"Expected ['b', 'c'], got {result}"
sub = Y['name'].as_subscript()
result = sub[{'name': 'Alice'}]
assert result == 'Alice', f"Expected 'Alice', got {result}"
sub = Y['name'].upper().as_subscript()
result = sub[{'name': 'alice'}]
assert result == 'ALICE', f"Expected 'ALICE', got {result}"
sub = Y[0][1].as_subscript()
result = sub[[[1, 2], [3, 4]]]
assert result == 2, f"Expected 2, got {result}"
sub = Y.split(',').f(vicList).as_subscript()
result = sub['a,b,c']
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
print("Y as_subscript() 方法测试通过")
def test_y_with_f():
"""测试 Y 的 f 工厂参数"""
result = Y.split(',')('a,b,c', exe=True, f=vicList)
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
result = Y.strip().split(',')(' a,b,c ', exe=True, f=vicList)
assert isinstance(result, vicList), f"Expected vicList, got {type(result)}"
assert result == vicList(['a', 'b', 'c']), f"Expected vicList(['a', 'b', 'c']), got {result}"
print("Y f 工厂参数测试通过")
def test_y_index():
"""测试 Y 的索引操作"""
result = Y['name']({'name': 'Alice'}, exe=True)
assert result == 'Alice', f"Expected 'Alice', got {result}"
result = Y[0][1]([[1, 2], [3, 4]], exe=True)
assert result == 2, f"Expected 2, got {result}"
print("Y 索引操作测试通过")
def test_error_handling():
"""测试错误处理"""
try:
X.strip()[1]
assert False, "Expected AttributeError"
except AttributeError:
pass
try:
Y.strip()(1, exe=True)
assert False, "Expected AttributeError"
except AttributeError:
pass
try:
X.split(',').f(None)['a,b,c']
assert False, "Expected TypeError"
except TypeError:
pass
try:
Y.split(',')('a,b,c', exe=True, f=123)
assert False, "Expected TypeError"
except TypeError:
pass
try:
Y.strip()(exe=True)
assert False, "Expected TypeError"
except TypeError:
pass
try:
X.strip()[None]
assert False, "Expected ValueError"
except ValueError:
pass
try:
X.strip[()]
assert False, "Expected ValueError"
except ValueError:
pass
f = Y.split(',').as_function()
try:
f('a,b,c', f=123)
assert False, "Expected TypeError"
except TypeError:
pass
print("错误处理测试通过")
def test_chained_combinations():
"""测试各种链式组合"""
result = X.strip().upper()[' hello ']
assert result == 'HELLO', f"Expected 'HELLO', got {result}"
result = X.split(',')[None, 0]['a,b,c']
assert result == 'a', f"Expected 'a', got {result}"
result = X.split(',')[None, 0].upper()['a,b,c']
assert result == 'A', f"Expected 'A', got {result}"
result = X[0].upper()['hello']
assert result == 'H', f"Expected 'H', got {result}"
result = X[0][None, 1][[[1, 2], [3, 4]]]
assert result == 2, f"Expected 2, got {result}"
result = Y.strip().upper()(' hello ', exe=True)
assert result == 'HELLO', f"Expected 'HELLO', got {result}"
result = Y.split(',')[0]('a,b,c', exe=True)
assert result == 'a', f"Expected 'a', got {result}"
f = Y.strip().upper().as_function()
result = f(' hello ')
assert result == 'HELLO', f"Expected 'HELLO', got {result}"
print("链式组合测试通过")
if __name__ == '__main__':
test_x_basic()
test_x_direct_index()
test_x_index_pipeline()
test_x_with_extra_index()
test_x_with_f()
test_x_as_function()
test_y_basic()
test_y_as_function()
test_y_f_method()
test_y_as_subscript()
test_y_with_f()
test_y_index()
test_error_handling()
test_chained_combinations()
print("\n=== 所有测试通过 ===")