#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#define YAML_CPP_STATIC_DEFINE
#include "../libYaml/include/yaml-cpp/yaml.h"
#pragma comment(lib, "../libYaml/Debug/libYaml.lib")
// 测试1:解析YAML字符串
void TestParseString() {
std::cout << "=== 测试1:解析YAML字符串 ===" << std::endl;
std::string yaml_content = R"(
name: John Doe
age: 30
address:
city: Beijing
street: Chang'an Avenue
hobbies:
- reading
- swimming
- coding
)";
try {
YAML::Node doc = YAML::Load(yaml_content);
// 读取基本类型
std::string name = doc["name"].as<std::string>();
int age = doc["age"].as<int>();
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
// 读取嵌套节点
std::string city = doc["address"]["city"].as<std::string>();
std::string street = doc["address"]["street"].as<std::string>();
std::cout << "City: " << city << ", Street: " << street << std::endl;
// 读取数组
std::cout << "Hobbies: ";
for (const auto& hobby : doc["hobbies"]) {
std::cout << hobby.as<std::string>() << " ";
}
std::cout << std::endl << std::endl;
}
catch (const YAML::Exception& e)
{
std::cerr << "YAML解析错误: " << e.what() << std::endl;
}
}
// 测试2:从文件加载YAML
void TestLoadFromFile()
{
std::cout << "=== 测试2:从文件加载YAML ===" << std::endl;
// 先创建一个测试文件
std::ofstream outfile("test.yaml");
outfile << "server:\n";
outfile << " host: localhost\n";
outfile << " port: 8080\n";
outfile << "database:\n";
outfile << " name: mydb\n";
outfile << " user: admin\n";
outfile << " password: secret\n";
outfile.close();
try {
YAML::Node config = YAML::LoadFile("test.yaml");
std::string host = config["server"]["host"].as<std::string>();
int port = config["server"]["port"].as<int>();
std::string db_name = config["database"]["name"].as<std::string>();
std::cout << "Server: " << host << ":" << port << std::endl;
std::cout << "Database: " << db_name << std::endl << std::endl;
}
catch (const YAML::Exception& e) {
std::cerr << "文件加载错误: " << e.what() << std::endl;
}
}
// 测试3:创建和修改YAML节点
void TestCreateAndModify()
{
std::cout << "=== 测试3:创建和修改YAML节点 ===" << std::endl;
YAML::Node node;
// 创建节点
node["app"]["name"] = "MyApp";
node["app"]["version"] = "1.0.0";
node["features"]["logging"] = true;
node["features"]["cache"]["enabled"] = true;
node["features"]["cache"]["ttl"] = 3600;
// 添加数组
node["servers"].push_back("192.168.1.1");
node["servers"].push_back("192.168.1.2");
node["servers"].push_back("192.168.1.3");
// 输出YAML
std::cout << "生成的YAML内容:" << std::endl;
std::cout << node << std::endl;
// 保存到文件
std::ofstream fout("output.yaml");
fout << node;
fout.close();
std::cout << "已保存到 output.yaml" << std::endl << std::endl;
}
// 测试4:节点检查和默认值
void TestNodeChecking()
{
std::cout << "=== 测试4:节点检查和默认值 ===" << std::endl;
YAML::Node doc = YAML::Load("name: Alice");
// 检查节点是否存在
if (doc["name"])
{
std::cout << "Name exists: " << doc["name"].as<std::string>() << std::endl;
}
// 检查节点类型
if (doc["name"].IsScalar())
{
std::cout << "name 是标量类型" << std::endl;
}
// 安全的默认值读取
std::string email = doc["email"].IsDefined() ? doc["email"].as<std::string>() : "default@example.com";
int age = doc["age"] ? doc["age"].as<int>() : 0;
std::cout << "Email (默认): " << email << std::endl;
std::cout << "Age (默认): " << age << std::endl << std::endl;
}
// 测试5:迭代遍历节点
void TestIteration()
{
std::cout << "=== 测试5:迭代遍历节点 ===" << std::endl;
YAML::Node config = YAML::Load(R"(
users:
- name: Alice
role: admin
- name: Bob
role: user
- name: Charlie
role: moderator
)");
// 遍历数组
std::cout << "用户列表:" << std::endl;
for (const auto& user : config["users"]) {
std::string name = user["name"].as<std::string>();
std::string role = user["role"].as<std::string>();
std::cout << " - " << name << " (" << role << ")" << std::endl;
}
std::cout << std::endl;
// 遍历映射
YAML::Node settings = YAML::Load(R"(
timeout: 30
retries: 3
debug: true
)");
std::cout << "所有配置项:" << std::endl;
for (YAML::const_iterator it = settings.begin(); it != settings.end(); ++it)
{
std::string key = it->first.as<std::string>();
std::string value = it->second.as<std::string>();
std::cout << " " << key << " = " << value << std::endl;
}
std::cout << std::endl;
}
int main()
{
std::cout << "YAML-CPP 测试程序启动" << std::endl;
std::cout << "======================" << std::endl << std::endl;
try {
TestParseString();
TestLoadFromFile();
TestCreateAndModify();
TestNodeChecking();
TestIteration();
std::cout << "所有测试完成!" << std::endl;
}
catch (const std::exception& e) {
std::cerr << "发生异常: " << e.what() << std::endl;
return 1;
}
system("pause"); // 如果需要暂停查看结果
return 0;
}