Initial commit
This commit is contained in:
188
tests/test_smart_json.cpp
Normal file
188
tests/test_smart_json.cpp
Normal file
@@ -0,0 +1,188 @@
|
||||
#include "../src/smart_json_wrapper.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
void printSeparator()
|
||||
{
|
||||
std::cout << "\n"
|
||||
<< std::string(60, '=') << "\n"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void printTestResult(const std::string &testName, bool passed)
|
||||
{
|
||||
std::cout << (passed ? "✅ " : "❌ ") << testName << std::endl;
|
||||
}
|
||||
|
||||
void testFunctionRegistry()
|
||||
{
|
||||
std::cout << "测试函数注册表..." << std::endl;
|
||||
|
||||
auto ®istry = FunctionRegistry::instance();
|
||||
auto functions = registry.getAllFunctions();
|
||||
|
||||
std::cout << "注册的函数数量: " << functions.size() << std::endl;
|
||||
for (const auto &[name, func] : functions)
|
||||
{
|
||||
std::cout << " - " << name << ": " << func->description << std::endl;
|
||||
}
|
||||
|
||||
printTestResult("函数注册表测试", functions.size() > 0);
|
||||
}
|
||||
|
||||
void testSmartJsonProcessor()
|
||||
{
|
||||
std::cout << "测试智能JSON处理器..." << std::endl;
|
||||
|
||||
SmartJsonProcessor processor;
|
||||
|
||||
// 测试用例
|
||||
std::vector<std::pair<std::string, std::string>> test_cases = {
|
||||
{"标准参数名",
|
||||
R"({
|
||||
"req_code": "TEST_001",
|
||||
"req_cmd": "add",
|
||||
"a": 10,
|
||||
"b": 20
|
||||
})"},
|
||||
{"别名参数名",
|
||||
R"({
|
||||
"req_code": "TEST_002",
|
||||
"req_cmd": "add",
|
||||
"num1": 30,
|
||||
"num2": 40
|
||||
})"},
|
||||
{"不同大小写",
|
||||
R"({
|
||||
"req_code": "TEST_003",
|
||||
"req_cmd": "ADD",
|
||||
"First": 50,
|
||||
"Second": 60
|
||||
})"},
|
||||
{"浮点数乘法",
|
||||
R"({
|
||||
"req_code": "TEST_004",
|
||||
"req_cmd": "multiply",
|
||||
"a": 2.5,
|
||||
"b": 4.0
|
||||
})"},
|
||||
{"斐波那契",
|
||||
R"({
|
||||
"req_code": "TEST_005",
|
||||
"req_cmd": "fibonacci",
|
||||
"n": 10
|
||||
})"}};
|
||||
|
||||
int passed_tests = 0;
|
||||
for (const auto &[test_name, json_request] : test_cases)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto response = processor.processRequest(json_request);
|
||||
std::cout << " " << test_name << ": ";
|
||||
if (response.success)
|
||||
{
|
||||
std::cout << "✅ (耗时: " << response.execution_time << "ms)" << std::endl;
|
||||
passed_tests++;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "❌ 错误: " << response.msg << std::endl;
|
||||
}
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cout << " " << test_name << ": ❌ 异常: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
printTestResult("智能JSON处理器测试", passed_tests == test_cases.size());
|
||||
}
|
||||
|
||||
void testFunctionInfo()
|
||||
{
|
||||
std::cout << "测试函数信息查询..." << std::endl;
|
||||
|
||||
SmartJsonProcessor processor;
|
||||
|
||||
std::vector<std::string> functions_to_test = {"add", "multiply", "fibonacci", "unknown_function"};
|
||||
|
||||
int passed_tests = 0;
|
||||
for (const auto &func_name : functions_to_test)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::string info = processor.getFunctionInfo(func_name);
|
||||
std::cout << " " << func_name << ": ";
|
||||
if (info.find("未找到") == std::string::npos)
|
||||
{
|
||||
std::cout << "✅" << std::endl;
|
||||
passed_tests++;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "❌ " << info << std::endl;
|
||||
}
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cout << " " << func_name << ": ❌ 异常: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
printTestResult("函数信息查询测试", passed_tests >= 3);
|
||||
}
|
||||
|
||||
void testAvailableFunctions()
|
||||
{
|
||||
std::cout << "测试可用函数列表..." << std::endl;
|
||||
|
||||
SmartJsonProcessor processor;
|
||||
auto functions = processor.getAvailableFunctions();
|
||||
|
||||
std::cout << "可用函数:" << std::endl;
|
||||
for (const auto &[name, func_info] : functions)
|
||||
{
|
||||
std::cout << " - " << name << " (" << func_info->description << ")" << std::endl;
|
||||
std::cout << " 参数: ";
|
||||
for (const auto ¶m : func_info->params)
|
||||
{
|
||||
std::cout << param.name << "(" << param.getTypeName() << ") ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
printTestResult("可用函数列表测试", !functions.empty());
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "\n";
|
||||
std::cout << "=========================================" << std::endl;
|
||||
std::cout << " Smart WASM JSON API 测试套件 " << std::endl;
|
||||
std::cout << "=========================================" << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
testFunctionRegistry();
|
||||
printSeparator();
|
||||
|
||||
testSmartJsonProcessor();
|
||||
printSeparator();
|
||||
|
||||
testFunctionInfo();
|
||||
printSeparator();
|
||||
|
||||
testAvailableFunctions();
|
||||
printSeparator();
|
||||
|
||||
std::cout << "\n🎉 所有测试完成!" << std::endl;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
std::cerr << "\n❌ 测试过程中发生异常: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user