#!/bin/bash

# 批量测试所有.sy文件的语法解析

test_dir="$(pwd)/test/test_case"
compiler="$(pwd)/build/bin/compiler"

if [ ! -f "$compiler" ]; then
    echo "错误：编译器不存在，请先构建项目"
    exit 1
fi

success_count=0
failed_count=0
failed_tests=()

echo "开始测试所有.sy文件的语法解析..."
echo "="

# 获取所有.sy文件并排序
for test_file in $(find "$test_dir" -name "*.sy" | sort); do
    echo "测试: $(basename "$test_file")"
    
    # 运行解析测试，捕获输出
    output=$("$compiler" --emit-parse-tree "$test_file" 2>&1)
    exit_code=$?
    
    if [ $exit_code -eq 0 ]; then
        echo "  ✓ 成功"
        ((success_count++))
    else
        echo "  ✗ 失败"
        echo "    错误信息: $output"
        ((failed_count++))
        failed_tests+=($(basename "$test_file"))
    fi
done

echo "="
echo "测试完成！"
echo "总测试数: $((success_count + failed_count))"
echo "成功: $success_count"
echo "失败: $failed_count"

if [ $failed_count -gt 0 ]; then
    echo "失败的测试用例:"
    for test in "${failed_tests[@]}"; do
        echo "  - $test"
    done
fi
