本目录包含Parser项目的单元测试,使用Google Test框架。
lexer/- 词法分析器相关测试parser/- 语法分析器相关测试interpreter/- 解释器相关测试optimizer/- 优化器相关测试
使用Google Test框架进行单元测试,每个模块都有独立的测试套件。
test_lexer_comprehensive.cpp- 综合测试test_lexer.cpp- 基本测试test_value_variables.cpp- 值变量测试test_string_operations.cpp- 字符串操作测试test_operator_design.cpp- 运算符设计测试test_operator_type.cpp- 运算符类型测试test_merged_target_type.cpp- 合并目标类型测试
test_parser_comprehensive.cpp- 综合测试test_parser.cpp- 基本测试test_parser_constant.cpp- 常量测试test_parser_helpers.cpp- 辅助函数测试test_parser_simple.cpp- 简单测试test_constant_expression.cpp- 常量表达式测试test_expression_parsing.cpp- 表达式解析测试test_leaf_expression.cpp- 叶子表达式测试test_simplified_expressions.cpp- 简化表达式测试test_simplified_binary.cpp- 简化二元表达式测试test_cast_expression.cpp- 强制转换表达式测试test_assignment_binary.cpp- 赋值二元表达式测试test_ast_only.cpp- AST结构测试
test_interpreter_comprehensive.cpp- 综合测试test_interpreter.cpp- 基本测试test_interpreter_execution.cpp- 执行测试test_interpreter_simple.cpp- 简单测试test_simple_interpreter.cpp- 简单解释器测试test_builtin_function.cpp- 内置函数测试test_builtin_functions_as_identifiers.cpp- 内置函数作为标识符测试test_builtin_refactor.cpp- 内置函数重构测试test_visitor.cpp- 访问者模式测试test_value_visitor.cpp- 值访问者测试
test_optimizer_comprehensive.cpp- 综合测试test_optimizer.cpp- 基本测试test_operator_precedence.cpp- 运算符优先级测试test_updated_parser.cpp- 更新解析器测试
以下是从根目录移动过来的原有测试文件:
test_*.cpp- 各种功能测试文件
# 在项目根目录运行
./run_tests.sh# 创建并进入build目录
mkdir -p build && cd build
# 配置CMake
cmake .. -DCMAKE_BUILD_TYPE=Debug
# 编译
make -j$(nproc)
# 运行特定测试
./test_lexer
./test_parser
./test_interpretercd build
make test
# 或者直接运行
ctest- 基本类型定义(int, double, bool, string)
- 运算符定义和优先级
- 值类型创建和操作
- 布尔常量
- 类型比较
- 常量表达式
- 变量表达式
- 二元表达式
- 表达式语句
- 变量声明
- 程序结构
- 整数运算
- 浮点数运算
- 布尔运算
- 字符串运算
- 类型转换
- 比较运算
- AST结构验证
- 表达式求值
- 语句执行
- 访问者模式
- 创建新的测试文件,例如
test_new_feature.cpp - 使用Google Test框架编写测试用例
- 在
CMakeLists.txt中添加新的测试文件 - 重新编译并运行测试
本项目使用Google Test框架,提供以下功能:
- 测试夹具(Test Fixtures)
- 断言宏(EXPECT_, ASSERT_)
- 参数化测试
- 死亡测试
- 测试发现和运行
- 所有测试文件都应该包含
#include <gtest/gtest.h> - 测试类应该继承自
::testing::Test - 使用
TEST_F宏定义测试用例 - 记得在测试中正确管理内存(delete指针)
- 测试应该独立且可重复运行