114 lines
4.2 KiB
C++
114 lines
4.2 KiB
C++
//
|
|
// Created by ling on 24-2-4.
|
|
//
|
|
#include <gtest/gtest.h>
|
|
#include <Options.h>
|
|
#include <Order.h>
|
|
|
|
TEST(Option, 选项测试) {
|
|
try {
|
|
ling::Order order({"-pts", "--all", "--ok"});
|
|
const auto pts = order.addOption({"-pts", "--pts"});
|
|
const auto all = order.addOption({"-all", "--all"});
|
|
const auto ok = order.addOption({"--ok", "-ok"});
|
|
const auto cancel = order.addOption({"--cancel"});
|
|
order.analysis();
|
|
ASSERT_TRUE(pts.isOption());
|
|
ASSERT_TRUE(all.isOption());
|
|
ASSERT_TRUE(ok.isOption());
|
|
ASSERT_FALSE(cancel.isOption());
|
|
} catch (const std::runtime_error &e) {
|
|
std::cout << e.what() << std::endl;
|
|
ASSERT_FALSE("解析出错!");
|
|
}
|
|
}
|
|
|
|
TEST(Option, 参数测试) {
|
|
try {
|
|
ling::Order order({"-pts", "hello world!", "--all", "--ok"});
|
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
|
const auto all = order.addOption({"-all", "--all"});
|
|
const auto ok = order.addOption({"--ok", "-ok"});
|
|
const auto cancel = order.addOption({"--cancel"});
|
|
order.analysis();
|
|
ASSERT_TRUE(all.isOption());
|
|
ASSERT_TRUE(ok.isOption());
|
|
ASSERT_FALSE(cancel.isOption());
|
|
ASSERT_TRUE(pts.getString() == "hello world!");
|
|
} catch (const std::runtime_error &e) {
|
|
std::cout << e.what() << std::endl;
|
|
ASSERT_FALSE("解析出错!");
|
|
}
|
|
}
|
|
|
|
|
|
TEST(Option, 异常参数测试) {
|
|
try {
|
|
ling::Order order({"-pts", "--all", "--ok"});
|
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
|
const auto all = order.addOption({"-all", "--all"});
|
|
const auto ok = order.addOption({"--ok", "-ok"});
|
|
const auto cancel = order.addOption({"--cancel"});
|
|
order.analysis();
|
|
ASSERT_FALSE("纠正出错!");
|
|
} catch (const std::runtime_error &e) {
|
|
}
|
|
}
|
|
|
|
TEST(Option, 匿名参数测试) {
|
|
try {
|
|
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok"});
|
|
order.addAnonymity(1);
|
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
|
const auto all = order.addOption({"-all", "--all"});
|
|
const auto ok = order.addOption({"--ok", "-ok"});
|
|
const auto cancel = order.addOption({"--cancel"});
|
|
order.analysis();
|
|
ASSERT_TRUE(all.isOption());
|
|
ASSERT_TRUE(ok.isOption());
|
|
ASSERT_FALSE(cancel.isOption());
|
|
ASSERT_TRUE(pts.getString() == "hello world!");
|
|
ASSERT_TRUE(order.getAnonymity().size() == 1);
|
|
ASSERT_TRUE(order.getAnonymity()[0] == "hello!");
|
|
} catch (const std::runtime_error &e) {
|
|
std::cout << e.what() << std::endl;
|
|
ASSERT_FALSE("解析出错!");
|
|
}
|
|
}
|
|
|
|
TEST(Option, 匿名参数测试2) {
|
|
try {
|
|
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok", "world"});
|
|
order.addAnonymity(2);
|
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
|
const auto all = order.addOption({"-all", "--all"});
|
|
const auto ok = order.addOption({"--ok", "-ok"});
|
|
const auto cancel = order.addOption({"--cancel"});
|
|
order.analysis();
|
|
ASSERT_TRUE(all.isOption());
|
|
ASSERT_TRUE(ok.isOption());
|
|
ASSERT_FALSE(cancel.isOption());
|
|
ASSERT_TRUE(pts.getString() == "hello world!");
|
|
ASSERT_TRUE(order.getAnonymity().size() == 2);
|
|
ASSERT_TRUE(order.getAnonymity()[0] == "hello!");
|
|
ASSERT_TRUE(order.getAnonymity()[1] == "world");
|
|
} catch (const std::runtime_error &e) {
|
|
std::cout << e.what() << std::endl;
|
|
ASSERT_FALSE("解析出错!");
|
|
}
|
|
}
|
|
|
|
TEST(Option, 匿名参数测试3) {
|
|
try {
|
|
ling::Order order({"-pts", "hello world!", "hello!", "--all", "--ok", "world"});
|
|
order.addAnonymity(1);
|
|
const auto pts = order.addOption({"-pts", "--pts"}, ling::Order::Type::STRING);
|
|
const auto all = order.addOption({"-all", "--all"});
|
|
const auto ok = order.addOption({"--ok", "-ok"});
|
|
const auto cancel = order.addOption({"--cancel"});
|
|
order.analysis();
|
|
ASSERT_FALSE("匿名参数数量超出限制,但是没有抛出异常!");
|
|
} catch (const std::runtime_error &e) {
|
|
}
|
|
}
|