From 5599e5d0242b37f10e2b8e431f7ec04eedbd43d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=BB=E9=AD=82=E5=9C=A3=E4=BD=BF?= Date: Tue, 25 Jun 2024 23:51:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A016=E8=BF=9B=E5=88=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Order.cpp | 11 +++- test_main.cpp | 145 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 98 insertions(+), 58 deletions(-) diff --git a/src/Order.cpp b/src/Order.cpp index b1ef1f2..35e8e07 100644 --- a/src/Order.cpp +++ b/src/Order.cpp @@ -91,6 +91,15 @@ namespace ling { return anonymity; } + /// 自动处理16进制 + static inline int64_t toInt64(const std::string &str) { + if (str.find("0x") == std::string::npos) + return std::stoll(str); + int64_t value; + ::sscanf(str.c_str(), "0x%lxx", &value); + return value; + } + void Order::analysis() { for (auto it = cmd.begin(); it != cmd.end(); ++it) { auto rules = data.find(*it); @@ -127,7 +136,7 @@ namespace ling { if (std::next(it)->at(0) == '-') throw OptionsException("参数不足!" + *it + " 需要提供参数"); ++it; - opt_int[rules->second.id] = std::stoll(*it); + opt_int[rules->second.id] = toInt64(*it); break; case DOUBLE: if (opt_double.find(rules->second.id) != opt_double.end()) diff --git a/test_main.cpp b/test_main.cpp index a23c5cf..5c0dbd7 100644 --- a/test_main.cpp +++ b/test_main.cpp @@ -4,37 +4,57 @@ #include #include #include +#include "OptionsException.h" + +TEST(Option, 16进制测试) { + try { + ling::Order order({"--hex", "0x1F", "--hex2", "0x1f"}); + const auto hex = order.addOption({"--hex"}, ling::Order::INT); + const auto hex2 = order.addOption({"--hex2"}, ling::Order::INT); + order.analysis(); + ASSERT_TRUE(hex.isExistence()); + ASSERT_TRUE(hex2.isExistence()); + ASSERT_EQ(hex.getInt64(), 0x1f); + ASSERT_EQ(hex2.getInt64(), 0x1f); + } catch (const ling::OptionsException &e) { + ASSERT_FALSE(e.what()); + } +} TEST(Option, 选项测试) { - 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"}); - if (!order.analysis()) { - std::cout << order.getError() << std::endl; + 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 ling::OptionsException &e) { + std::cout << e.what() << std::endl; ASSERT_FALSE("解析出错!"); } - ASSERT_TRUE(pts.isOption()); - ASSERT_TRUE(all.isOption()); - ASSERT_TRUE(ok.isOption()); - ASSERT_FALSE(cancel.isOption()); } TEST(Option, 参数测试) { - 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"}); - if (!order.analysis()) { - std::cout << order.getError() << std::endl; + 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 ling::OptionsException &e) { + std::cout << e.what() << std::endl; ASSERT_FALSE("解析出错!"); } - ASSERT_TRUE(all.isOption()); - ASSERT_TRUE(ok.isOption()); - ASSERT_FALSE(cancel.isOption()); - ASSERT_TRUE(pts.getString() == "hello world!"); } @@ -44,47 +64,55 @@ TEST(Option, 异常参数测试) { const auto all = order.addOption({"-all", "--all"}); const auto ok = order.addOption({"--ok", "-ok"}); const auto cancel = order.addOption({"--cancel"}); - ASSERT_FALSE(order.analysis()); + try { + order.analysis(); + ASSERT_FALSE("异常处理失败"); + } catch (const ling::OptionsException &e) { + } } TEST(Option, 匿名参数测试) { - 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"}); - if (!order.analysis()) { - std::cout << order.getError() << std::endl; + 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 ling::OptionsException &e) { + std::cout << e.what() << std::endl; ASSERT_FALSE("解析出错!"); } - 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!"); } TEST(Option, 匿名参数测试2) { - 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"}); - if (!order.analysis()) { - std::cout << order.getError() << std::endl; + 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 ling::OptionsException &e) { + std::cout << e.what() << std::endl; ASSERT_FALSE("解析出错!"); } - - 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"); } TEST(Option, 匿名参数测试3) { @@ -94,8 +122,11 @@ TEST(Option, 匿名参数测试3) { const auto all = order.addOption({"-all", "--all"}); const auto ok = order.addOption({"--ok", "-ok"}); const auto cancel = order.addOption({"--cancel"}); - if (order.analysis()) + try { + order.analysis(); ASSERT_FALSE("匿名参数数量超出限制,但是没有抛出异常!"); + } catch (const ling::OptionsException &e) { + } } @@ -103,9 +134,9 @@ TEST(Option, Help生成) { 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"},"测试参数"); + const auto all = order.addOption({"-all", "--all"}, "测试参数"); + const auto ok = order.addOption({"-ok", "--ok"}, "测试参数"); + const auto cancel = order.addOption({"--cancel"}, "测试参数"); auto help = order.generateHelp("test [选项] .. [参数]\n测试命令"); std::cout << help << std::endl; }