@@ -3361,115 +3361,51 @@ int main(int argc, const char* argv[]) {
33613361 return runner.runCommandLine (argc, argv);
33623362}
33633363
3364- struct TemplatedAs : JsonTest::TestCase {};
3365-
3366- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorCString) {
3367- Json::Value json = " hello world" ;
3368- JSONTEST_ASSERT_STRING_EQUAL (json.asCString (), json.as <const char *>());
3369- }
3370-
3371- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorString) {
3372- Json::Value json = " hello world" ;
3373- JSONTEST_ASSERT_STRING_EQUAL (json.asString (), json.as <Json::String>());
3374- }
3364+ struct MemberTemplateAs : JsonTest::TestCase {
3365+ template <typename T, typename F>
3366+ JsonTest::TestResult& EqEval (T v, F f) const {
3367+ const Json::Value j = v;
3368+ return JSONTEST_ASSERT_EQUAL (j.as <T>(), f (j));
3369+ }
3370+ };
33753371
3372+ JSONTEST_FIXTURE_LOCAL (MemberTemplateAs, BehavesSameAsNamedAs) {
3373+ const Json::Value jstr = " hello world" ;
3374+ JSONTEST_ASSERT_STRING_EQUAL (jstr.as <const char *>(), jstr.asCString ());
3375+ JSONTEST_ASSERT_STRING_EQUAL (jstr.as <Json::String>(), jstr.asString ());
33763376#ifdef JSON_USE_CPPTL
3377- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorConstString) {
3378- Json::Value json = " hello world" ;
3379- JSONTEST_ASSERT_STRING_EQUAL (json.asConstString (),
3380- json.as <CppTL::ConstString>());
3381- }
3377+ JSONTEST_ASSERT_STRING_EQUAL (js.as <CppTL::ConstString>(), js.asConstString ());
33823378#endif
3383-
3384- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorInt) {
3385- Json::Value json = Json::Int (64 );
3386- JSONTEST_ASSERT_EQUAL (json.asInt (), json.as <Json::Int>());
3387- }
3388-
3389- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorUInt) {
3390- Json::Value json = Json::UInt (64 );
3391- JSONTEST_ASSERT_EQUAL (json.asUInt (), json.as <Json::UInt>());
3392- }
3393-
3379+ EqEval (Json::Int (64 ), [](const Json::Value& j) { return j.asInt (); });
3380+ EqEval (Json::UInt (64 ), [](const Json::Value& j) { return j.asUInt (); });
33943381#if defined(JSON_HAS_INT64)
3395- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorInt64) {
3396- Json::Value json = Json::Int64 (64 );
3397- JSONTEST_ASSERT_EQUAL (json.asUInt64 (), json.as <Json::Int64>());
3398- }
3399-
3400- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorUInt64) {
3401- Json::Value json = Json::UInt64 (64 );
3402- JSONTEST_ASSERT_EQUAL (json.asUInt64 (), json.as <Json::UInt64>());
3403- }
3382+ EqEval (Json::Int64 (64 ), [](const Json::Value& j) { return j.asInt64 (); });
3383+ EqEval (Json::UInt64 (64 ), [](const Json::Value& j) { return j.asUInt64 (); });
34043384#endif // if defined(JSON_HAS_INT64)
3405-
3406- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorLargestInt) {
3407- Json::Value json = Json::LargestInt (64 );
3408- JSONTEST_ASSERT_EQUAL (json.asLargestInt (), json.as <Json::LargestInt>());
3409- }
3410-
3411- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorLargestUInt) {
3412- Json::Value json = Json::LargestUInt (64 );
3413- JSONTEST_ASSERT_EQUAL (json.asLargestUInt (), json.as <Json::LargestUInt>());
3414- }
3415-
3416- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorFloat) {
3417- Json::Value json = float (69.69 );
3418- JSONTEST_ASSERT_EQUAL (json.asFloat (), json.as <float >());
3419- }
3420-
3421- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorDouble) {
3422- Json::Value json = double (69.69 );
3423- JSONTEST_ASSERT_EQUAL (json.asDouble (), json.as <double >());
3424- }
3425-
3426- JSONTEST_FIXTURE_LOCAL (TemplatedAs, equalBehaviorBool) {
3427- Json::Value jsonTrue = true ;
3428- Json::Value jsonFalse = false ;
3429- JSONTEST_ASSERT_EQUAL (jsonTrue.asBool (), jsonTrue.as <bool >());
3430- JSONTEST_ASSERT_EQUAL (jsonFalse.asBool (), jsonFalse.as <bool >());
3431- }
3432-
3433- struct TemplatedIs : JsonTest::TestCase {};
3434-
3435- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsBool) {
3436- Json::Value json = true ;
3437- JSONTEST_ASSERT_EQUAL (json.isBool (), json.is <bool >());
3438- }
3439-
3440- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsInt) {
3441- Json::Value json = 142 ;
3442- JSONTEST_ASSERT_EQUAL (json.isInt (), json.is <Json::Int>());
3443- }
3444-
3445- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsInt64) {
3446- Json::Value json = 142 ;
3447- JSONTEST_ASSERT_EQUAL (json.isInt64 (), json.is <Json::Int64>());
3448- }
3449-
3450- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsUInt) {
3451- Json::Value json = 142 ;
3452- JSONTEST_ASSERT_EQUAL (json.isUInt (), json.is <Json::UInt>());
3453- }
3454-
3455- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsUInt64) {
3456- Json::Value json = 142 ;
3457- JSONTEST_ASSERT_EQUAL (json.isUInt64 (), json.is <Json::UInt64>());
3458- }
3459-
3460- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsDouble) {
3461- Json::Value json = 40.63 ;
3462- JSONTEST_ASSERT_EQUAL (json.isDouble (), json.is <double >());
3463- }
3464-
3465- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsCString) {
3466- Json::Value json = " hello world" ;
3467- JSONTEST_ASSERT_EQUAL (json.isString (), json.is <const char *>());
3468- }
3469-
3470- JSONTEST_FIXTURE_LOCAL (TemplatedIs, equalBehaviorIsString) {
3471- Json::Value json = " hello world" ;
3472- JSONTEST_ASSERT_EQUAL (json.isString (), json.is <Json::String>());
3385+ EqEval (Json::LargestInt (64 ),
3386+ [](const Json::Value& j) { return j.asLargestInt (); });
3387+ EqEval (Json::LargestUInt (64 ),
3388+ [](const Json::Value& j) { return j.asLargestUInt (); });
3389+
3390+ EqEval (69 .69f , [](const Json::Value& j) { return j.asFloat (); });
3391+ EqEval (69.69 , [](const Json::Value& j) { return j.asDouble (); });
3392+ EqEval (false , [](const Json::Value& j) { return j.asBool (); });
3393+ EqEval (true , [](const Json::Value& j) { return j.asBool (); });
3394+ }
3395+
3396+ class MemberTemplateIs : public JsonTest ::TestCase {};
3397+
3398+ JSONTEST_FIXTURE_LOCAL (MemberTemplateIs, BehavesSameAsNamedIs) {
3399+ const Json::Value values[] = {true , 142 , 40.63 , " hello world" };
3400+ for (const Json::Value& j : values) {
3401+ JSONTEST_ASSERT_EQUAL (j.is <bool >(), j.isBool ());
3402+ JSONTEST_ASSERT_EQUAL (j.is <Json::Int>(), j.isInt ());
3403+ JSONTEST_ASSERT_EQUAL (j.is <Json::Int64>(), j.isInt64 ());
3404+ JSONTEST_ASSERT_EQUAL (j.is <Json::UInt>(), j.isUInt ());
3405+ JSONTEST_ASSERT_EQUAL (j.is <Json::UInt64>(), j.isUInt64 ());
3406+ JSONTEST_ASSERT_EQUAL (j.is <double >(), j.isDouble ());
3407+ JSONTEST_ASSERT_EQUAL (j.is <Json::String>(), j.isString ());
3408+ }
34733409}
34743410
34753411#if defined(__GNUC__)
0 commit comments