From 68f377cedac6d5c74e19f284c41c8af33af4203f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=BB=E9=AD=82=E5=9C=A3=E4=BD=BF?= Date: Sun, 30 Jun 2024 15:23:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BD=BF=E7=94=A8=E8=B4=9F?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Order.cpp | 6 ------ test_main.cpp | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Order.cpp b/src/Order.cpp index 35e8e07..9aecd82 100644 --- a/src/Order.cpp +++ b/src/Order.cpp @@ -123,8 +123,6 @@ namespace ling { throw OptionsException("重复提供参数:" + *it); if (std::next(it) == cmd.end()) throw OptionsException("参数不足!" + *it + " 需要提供参数"); - if (std::next(it)->at(0) == '-') - throw OptionsException("参数不足!" + *it + " 需要提供参数"); ++it; opt_str[rules->second.id] = *it; break; @@ -133,8 +131,6 @@ namespace ling { throw OptionsException("重复提供参数:" + *it); if (std::next(it) == cmd.end()) throw OptionsException("参数不足!" + *it + " 需要提供参数"); - if (std::next(it)->at(0) == '-') - throw OptionsException("参数不足!" + *it + " 需要提供参数"); ++it; opt_int[rules->second.id] = toInt64(*it); break; @@ -143,8 +139,6 @@ namespace ling { throw OptionsException("重复提供参数:" + *it); if (std::next(it) == cmd.end()) throw OptionsException("参数不足!" + *it + " 需要提供参数"); - if (std::next(it)->at(0) == '-') - throw OptionsException("参数不足!" + *it + " 需要提供参数"); ++it; opt_double[rules->second.id] = std::stod(*it); break; diff --git a/test_main.cpp b/test_main.cpp index 5c0dbd7..e2f5fec 100644 --- a/test_main.cpp +++ b/test_main.cpp @@ -6,6 +6,18 @@ #include #include "OptionsException.h" +TEST(Option, 负数测试) { + try { + ling::Order order({"--number", "-10"}); + const auto number = order.addOption({"-n", "--number"}, ling::Order::INT); + order.analysis(); + ASSERT_TRUE(number.isExistence()); + ASSERT_EQ(number.getInt64(), -10); + } catch (const ling::OptionsException &e) { + ASSERT_FALSE(e.what()); + } +} + TEST(Option, 16进制测试) { try { ling::Order order({"--hex", "0x1F", "--hex2", "0x1f"}); @@ -66,8 +78,12 @@ TEST(Option, 异常参数测试) { const auto cancel = order.addOption({"--cancel"}); try { order.analysis(); - ASSERT_FALSE("异常处理失败"); + ASSERT_EQ(pts.getString(), "--all"); + ASSERT_FALSE(all.isExistence()); + ASSERT_TRUE(ok.isExistence()); + ASSERT_FALSE(cancel.isExistence()); } catch (const ling::OptionsException &e) { + ASSERT_FALSE("解析失败"); } }