#!/bin/bash

# 批量测试脚本
# 遍历 test/test_case 目录下所有的 .sy 文件，并验证解析是否成功

if [ ! -f "./build/bin/compiler" ]; then
    echo "Compiler executable not found at ./build/bin/compiler. Please build the project first."
    exit 1
fi

FAIL_COUNT=0
PASS_COUNT=0
FAILED_FILES=()

echo "开始批量测试解析..."
echo "========================================="

# 查找所有 .sy 文件并进行测试
while IFS= read -r file; do
    # 运行解析器，将正常输出重定向到 /dev/null，保留错误输出用于判断
    ./build/bin/compiler --emit-parse-tree "$file" > /dev/null 2>&1
    
    if [ $? -ne 0 ]; then
        echo "❌ 解析失败: $file"
        FAIL_COUNT=$((FAIL_COUNT+1))
        FAILED_FILES+=("$file")
    else
        echo "✅ 解析成功: $file"
        PASS_COUNT=$((PASS_COUNT+1))
    fi
done < <(find test/test_case -type f -name "*.sy" | sort)

echo "========================================="
echo "测试完成！"
echo "成功: $PASS_COUNT"
echo "失败: $FAIL_COUNT"

if [ $FAIL_COUNT -ne 0 ]; then
    echo "失败的文件列表:"
    for f in "${FAILED_FILES[@]}"; do
        echo "  - $f"
    done
    exit 1
else
    echo "🎉 所有测试用例均解析成功！"
    exit 0
fi
