• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ Validator类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中Validator的典型用法代码示例。如果您正苦于以下问题:C++ Validator类的具体用法?C++ Validator怎么用?C++ Validator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Validator类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: TEST

TEST(ValidatorTest, ArrayCompactTooShort5) {
  std::string const value("\x13\x80\x05\x18", 4);
 
  Validator validator;
  ASSERT_VELOCYPACK_EXCEPTION(validator.validate(value.c_str(), value.size()), Exception::ValidatorInvalidLength);
}
开发者ID:arangodb,项目名称:velocypack,代码行数:6,代码来源:testsValidator.cpp


示例2: InvalidArgumentException

ParsedArgument ArgumentParser::parse(int argc, char** argv) {
    // put all the arguments into vector for easy manipulation
    vector<string> v;
    for (int i = 1; i < argc; i++) {
        v.push_back(string(argv[i]));
    }
    ParsedArgument pa;
    // ignore the first argument since the first argument is a program name
    for (size_t i = 0; i < v.size(); ++i) {
        string s = v[i];
        bool shortArg = cppargparser::isShortArg(s);
        bool longArg = cppargparser::isLongArg(s);
        if (!shortArg && !longArg) {
            throw InvalidArgumentException(s + " is an invalid argument");
        }
        string arg = s;
        if (longArg) {
            // if the argument has =, e.g. ---ccc=123 split it by = and then
            // modify the vector by modifying --ccc=123 with --ccc and adding
            // a new element 123
            size_t index = arg.find("=");
            if (index != string::npos) {
                string key = arg.substr(0, index);
                string value = arg.substr(index+1);
                arg = key;
                v[i] = key;
                v.insert(v.begin()+i+1, value);
            }
        }
        map<string, Argument>::iterator it = args.find(arg);
        if (it == args.end()) {
            throw InvalidArgumentException(arg + " is an invalid argument");
        }
        Argument argument = it->second;
        if (argument.getNumArgs() == Argument::INFINITY) {
            string value = "";
            do {
                value = v[++i];
                if (!cppargparser::isShortArg(value)) {
                    pa.putArgument(argument.getShortArg(), value);
                }
                if (!cppargparser::isLongArg(value)) {
                    pa.putArgument(argument.getLongArg(), value);
                }
            } while (!isShortArg(value) && !isLongArg(value));
        } else {
            i = i + 1;
            size_t n = i + argument.getNumArgs();
            // this condition means there's the argument doesn't need any value,
            // i.e. the numArgs is 0, thus there's no need to iterate each
            // argument value
            if (i == n) {
                pa.putArgument(argument.getShortArg(), "");
                pa.putArgument(argument.getLongArg(), "");
            }
            else {
                for (; i < n; ++i) {
                    if (i >= v.size()) {
                        throw InvalidArgumentException(
                            argument.getArg() + " requires " +
                            cppargparser::toString(argument.getNumArgs()) +
                            " argument(s)");
                    }
                    string value = v[i];
                    pa.putArgument(argument.getShortArg(), value);
                    pa.putArgument(argument.getLongArg(), value);
                }
            }
        }
        Validator* validator = argument.getValidator();
        if (validator != NULL) {
            if (argument.isShortArg()) {
                vector<string> values = pa.getValues(argument.getShortArg());
                if (!validator->validate(values)) {
                    throw InvalidArgumentException(cppargparser::toString(values) +
                        " is an invalid argument value");
                }
            } else {
                vector<string> values = pa.getValues(argument.getLongArg());
                if (!validator->validate(values)) {
                    throw InvalidArgumentException(cppargparser::toString(values) +
                        " is an invalid argument value");
                }
            }
        }
        args.erase(argument.getShortArg());
        args.erase(argument.getLongArg());
        // need to decrement i here because both inner and outer loops
        // increment i by 1
        --i;
    }
    // check if the there are still mandatory arguments in the args map
    // if there are, throw an InvalidArgumentException
    for (map<string, Argument>::const_iterator i = args.begin(); i != args.end(); ++i) {
        if (i->second.isMandatory()) {
            throw InvalidArgumentException(i->second.getArg() +
                " is a mandatory argument");
        }
    }

//.........这里部分代码省略.........
开发者ID:fredyw,项目名称:cpp-argparser,代码行数:101,代码来源:ArgumentParser.cpp


示例3: TEST_F

TEST_F(TestValidationErrors, AllOfConstraintFailure)
{
    // Load schema document
    rapidjson::Document schemaDocument;
    ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/schemas/allof_integers_and_numbers.schema.json", schemaDocument) );
    RapidJsonAdapter schemaAdapter(schemaDocument);

    // Parse schema document
    Schema schema;
    SchemaParser schemaParser;
    ASSERT_NO_THROW( schemaParser.populateSchema(schemaAdapter, schema) );

    // Load test document
    rapidjson::Document testDocument;
    ASSERT_TRUE( loadDocument(TEST_DATA_DIR "/documents/array_doubles_1_2_3.json", testDocument) );
    RapidJsonAdapter testAdapter(testDocument);

    Validator validator;
    ValidationResults results;
    EXPECT_FALSE( validator.validate(schema, testAdapter, &results) );

    ValidationResults::Error error;

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(2), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "[0]", error.context[1] );
    EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate item #0 in array.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(2), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "[1]", error.context[1] );
    EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate item #1 in array.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(2), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "[2]", error.context[1] );
    EXPECT_EQ( "Value type not permitted by 'type' constraint.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate item #2 in array.", error.description );

    EXPECT_TRUE( results.popError(error) );
    EXPECT_EQ( size_t(1), error.context.size() );
    EXPECT_EQ( "<root>", error.context[0] );
    EXPECT_EQ( "Failed to validate against child schema #0.", error.description );

    EXPECT_FALSE( results.popError(error) );

    while (results.popError(error)) {
        //std::cerr << error.context << std::endl;
        std::cerr << error.description << std::endl;
    }
}
开发者ID:btolfa,项目名称:valijson,代码行数:68,代码来源:test_validation_errors.cpp



注:本文中的Validator类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ ValidatorResult类代码示例发布时间:2022-05-31
下一篇:
C++ ValidParameters类代码示例发布时间:2022-05-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap