From d8397b915307631bb6170920fbb86f6f6612736c Mon Sep 17 00:00:00 2001 From: Aleksandar Fabijanic Date: Mon, 24 Sep 2012 03:51:40 +0000 Subject: [PATCH] - VS2012: build/tests - JSON: style and exceptions catch by ref fix - NumberParser: case-insensitive hex parsing - Dynamic::Var: const std::string& cast operator specialization (coaxing std::string ctor) - LocalDateTime: strftime fix (buffer too small for full tz string, use %Z instead of %z - makes no difference on windows but is more portable) - small buildwin.cmd vsvars32.bat call fix --- Foundation/include/Poco/Dynamic/Var.h | 10 +- Foundation/include/Poco/NumberParser.h | 4 +- Foundation/src/Var.cpp | 9 + .../testsuite/src/LocalDateTimeTest.cpp | 15 +- Foundation/testsuite/src/NumberParserTest.cpp | 3 + Foundation/testsuite/src/VarTest.cpp | 5021 +++++++++-------- JSON/include/Poco/JSON/Array.h | 49 +- JSON/include/Poco/JSON/DefaultHandler.h | 48 +- JSON/include/Poco/JSON/Handler.h | 24 +- JSON/include/Poco/JSON/JSON.h | 12 +- JSON/include/Poco/JSON/JSONException.h | 9 +- JSON/include/Poco/JSON/Object.h | 49 +- JSON/include/Poco/JSON/Parser.h | 28 +- JSON/include/Poco/JSON/Query.h | 30 +- JSON/include/Poco/JSON/Stringifier.h | 14 +- JSON/include/Poco/JSON/Template.h | 15 +- JSON/include/Poco/JSON/TemplateCache.h | 32 +- JSON/src/Array.cpp | 29 +- JSON/src/DefaultHandler.cpp | 20 +- JSON/src/Object.cpp | 20 +- JSON/src/Parser.cpp | 1384 ++--- JSON/src/Query.cpp | 25 +- JSON/src/Stringifier.cpp | 14 +- JSON/src/Template.cpp | 57 +- JSON/src/TemplateCache.cpp | 4 +- JSON/testsuite/src/JSONTest.cpp | 334 +- PDF/include/Poco/PDF/PDF.h | 8 +- Util/src/AbstractConfiguration.cpp | 16 +- Util/testsuite/src/JSONConfigurationTest.cpp | 159 +- buildwin.cmd | 12 +- 30 files changed, 3715 insertions(+), 3739 deletions(-) diff --git a/Foundation/include/Poco/Dynamic/Var.h b/Foundation/include/Poco/Dynamic/Var.h index 56b32e2c6..83d1e1971 100644 --- a/Foundation/include/Poco/Dynamic/Var.h +++ b/Foundation/include/Poco/Dynamic/Var.h @@ -156,6 +156,8 @@ public: if (!_pHolder) throw InvalidAccessException("Can not convert empty value."); + if (typeid(T) == _pHolder->type()) return extract(); + T result; _pHolder->convert(result); return result; @@ -188,6 +190,11 @@ public: } } + operator const std::string & (); + /// Specialization of the cast operator for const std::string reference. + /// The main reason for this specialization is to help compilers + /// with construction/assignment of Var to std::string. + template const T& extract() const /// Returns a const reference to the actual value. @@ -210,8 +217,6 @@ public: typeid(T).name())); } - - template Var& operator = (const T& other) /// Assignment operator for assigning POD to Var @@ -558,6 +563,7 @@ private: /// /// Var members /// + inline void Var::swap(Var& ptr) { std::swap(_pHolder, ptr._pHolder); diff --git a/Foundation/include/Poco/NumberParser.h b/Foundation/include/Poco/NumberParser.h index e9fd90bbb..8b37c390e 100644 --- a/Foundation/include/Poco/NumberParser.h +++ b/Foundation/include/Poco/NumberParser.h @@ -263,7 +263,7 @@ private: T n = 0; for (; it != end; ++it) { - if ((*it >= '0' && *it <= '9') || (*it >= 'A' && *it <= 'F')) + if ((*it >= '0' && *it <= '9') || (*it >= 'A' && *it <= 'F') || (*it >= 'a' && *it <= 'f')) { if (n > (std::numeric_limits::max() / base)) return false; @@ -272,6 +272,8 @@ private: n = n * base + *it - '0'; else if (*it >= 'A' && *it <= 'F') n = n * base + *it - 'A' + 10; + else if (*it >= 'a' && *it <= 'f') + n = n * base + *it - 'a' + 10; } else break; } diff --git a/Foundation/src/Var.cpp b/Foundation/src/Var.cpp index e9457946d..78debccec 100644 --- a/Foundation/src/Var.cpp +++ b/Foundation/src/Var.cpp @@ -69,6 +69,15 @@ Var::~Var() } +Var::operator const std::string & () +{ + if (typeid(std::string) != _pHolder->type()) + *this = this->convert(); + + return extract(); +} + + Var& Var::operator = (const Var& other) { Var tmp(other); diff --git a/Foundation/testsuite/src/LocalDateTimeTest.cpp b/Foundation/testsuite/src/LocalDateTimeTest.cpp index e070271de..960fbd738 100644 --- a/Foundation/testsuite/src/LocalDateTimeTest.cpp +++ b/Foundation/testsuite/src/LocalDateTimeTest.cpp @@ -396,23 +396,22 @@ void LocalDateTimeTest::testTimezone() if (then.tm_isdst >= 0) { std::string tzNow, tzThen; - char tzBuf[12]; - int iterations = 0; - std::strftime(&tzBuf[0], sizeof(tzBuf), "%z", &then); + char tzBuf[48] = {0}; + if (0 == std::strftime(&tzBuf[0], sizeof(tzBuf), "%Z", &then)) + fail ("Insufficient character array length."); + tzNow = tzThen = tzBuf; + int iterations = 0; while (iterations < 14) { // Add one month until the timezone changes or we roll // over 13 months. t += tINCREMENT; then = *std::localtime(&t); - std::strftime(&tzBuf[0], sizeof(tzBuf), "%z", &then); + std::strftime(&tzBuf[0], sizeof(tzBuf), "%Z", &then); tzThen = tzBuf; foundDST = (tzNow == tzThen); - if (foundDST) - { - break; - } + if (foundDST) break; ++iterations; } if (foundDST) diff --git a/Foundation/testsuite/src/NumberParserTest.cpp b/Foundation/testsuite/src/NumberParserTest.cpp index 8b93c03eb..835f98090 100644 --- a/Foundation/testsuite/src/NumberParserTest.cpp +++ b/Foundation/testsuite/src/NumberParserTest.cpp @@ -73,7 +73,10 @@ void NumberParserTest::testParse() assert(NumberParser::parse("-123") == -123); assert(NumberParser::parseUnsigned("123") == 123); assert(NumberParser::parseHex("12AB") == 0x12ab); + assert(NumberParser::parseHex("0X12AB") == 0x12ab); assert(NumberParser::parseHex("0x12AB") == 0x12ab); + assert(NumberParser::parseHex("0x12aB") == 0x12ab); + assert(NumberParser::parseHex("0X98Fe") == 0x98fe); assert(NumberParser::parseHex("0x0") == 0); assert(NumberParser::parseHex("00") == 0); assert(NumberParser::parseOct("123") == 0123); diff --git a/Foundation/testsuite/src/VarTest.cpp b/Foundation/testsuite/src/VarTest.cpp index 5624c25e4..5b89bc312 100644 --- a/Foundation/testsuite/src/VarTest.cpp +++ b/Foundation/testsuite/src/VarTest.cpp @@ -1,2510 +1,2517 @@ -// -// VarTest.cpp -// -// $Id: //poco/svn/Foundation/testsuite/src/VarTest.cpp#2 $ -// -// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. -// and Contributors. -// -// Permission is hereby granted, free of charge, to any person or organization -// obtaining a copy of the software and accompanying documentation covered by -// this license (the "Software") to use, reproduce, display, distribute, -// execute, and transmit the Software, and to prepare derivative works of the -// Software, and to permit third-parties to whom the Software is furnished to -// do so, all subject to the following: -// -// The copyright notices in the Software and this entire statement, including -// the above license grant, this restriction and the following disclaimer, -// must be included in all copies of the Software, in whole or in part, and -// all derivative works of the Software, unless such copies or derivative -// works are solely in the form of machine-executable object code generated by -// a source language processor. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// - - -#include "VarTest.h" -#include "CppUnit/TestCaller.h" -#include "CppUnit/TestSuite.h" -#include "Poco/Exception.h" -#include "Poco/Dynamic/Var.h" -#include "Poco/Bugcheck.h" -#include "Poco/Dynamic/Struct.h" -#include "Poco/Dynamic/Pair.h" -#include -#include - -#if defined(_MSC_VER) && _MSC_VER < 1400 - #pragma warning(disable:4800)//forcing value to bool 'true' or 'false' -#endif - - -using namespace Poco; -using namespace Poco::Dynamic; - - -class Dummy -{ -public: - Dummy(): _val(0) - { - } - - Dummy(int val): _val(val) - { - } - - operator int () const - { - return _val; - } - - bool operator == (int i) - { - return i == _val; - } - -private: - int _val; -}; - - -VarTest::VarTest(const std::string& name): CppUnit::TestCase(name) -{ -} - - -VarTest::~VarTest() -{ -} - - -void VarTest::testInt8() -{ - Poco::Int8 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::Int8)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - Int8 value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testInt16() -{ - Poco::Int16 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::Int16)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - Int16 value = a1.extract(); - assert (value == 32); - - try - { - Int32 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testInt32() -{ - Poco::Int32 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::Int32)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - Int32 value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testInt64() -{ - Poco::Int64 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::Int64)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - Int64 value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testUInt8() -{ - Poco::UInt8 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::UInt8)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - UInt8 value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testUInt16() -{ - Poco::UInt16 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::UInt16)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - UInt16 value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testUInt32() -{ - Poco::UInt32 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::UInt32)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - UInt32 value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testUInt64() -{ - Poco::UInt64 src = 32; - Var a1 = src; - - assert (a1.type() == typeid(Poco::UInt64)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - UInt64 value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testBool() -{ - bool src = true; - Var a1 = src; - - assert (a1.type() == typeid(bool)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 1); - assert (s15 == 1); - assert (s1 == "true"); - assert (s2 == 1); - assert (s3 == 1); - assert (s4 == 1); - assert (s5 == 1); - assert (s6 == 1); - assert (s7 == 1); - assert (s8 == 1); - assert (s9 == 1); - assert (s10 == 1.0f); - assert (s11 == 1.0); - assert (s12); - assert (s13 == '\x1'); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - bool value = a1.extract(); - assert (value); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } -} - - -void VarTest::testChar() -{ - char src = ' '; - Var a1 = src; - - assert (a1.type() == typeid(char)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == " "); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - char value = a1.extract(); - assert (value == ' '); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } -} - - -void VarTest::testFloat() -{ +// +// VarTest.cpp +// +// $Id: //poco/svn/Foundation/testsuite/src/VarTest.cpp#2 $ +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#include "VarTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Exception.h" +#include "Poco/Dynamic/Var.h" +#include "Poco/Bugcheck.h" +#include "Poco/Dynamic/Struct.h" +#include "Poco/Dynamic/Pair.h" +#include +#include + +#if defined(_MSC_VER) && _MSC_VER < 1400 + #pragma warning(disable:4800)//forcing value to bool 'true' or 'false' +#endif + + +using namespace Poco; +using namespace Poco::Dynamic; + + +class Dummy +{ +public: + Dummy(): _val(0) + { + } + + Dummy(int val): _val(val) + { + } + + operator int () const + { + return _val; + } + + bool operator == (int i) + { + return i == _val; + } + +private: + int _val; +}; + + +VarTest::VarTest(const std::string& name): CppUnit::TestCase(name) +{ +} + + +VarTest::~VarTest() +{ +} + + +void VarTest::testInt8() +{ + Poco::Int8 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::Int8)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + Int8 value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testInt16() +{ + Poco::Int16 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::Int16)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + Int16 value = a1.extract(); + assert (value == 32); + + try + { + Int32 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testInt32() +{ + Poco::Int32 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::Int32)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + Int32 value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testInt64() +{ + Poco::Int64 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::Int64)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + Int64 value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testUInt8() +{ + Poco::UInt8 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::UInt8)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + UInt8 value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testUInt16() +{ + Poco::UInt16 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::UInt16)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + UInt16 value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testUInt32() +{ + Poco::UInt32 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::UInt32)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + UInt32 value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testUInt64() +{ + Poco::UInt64 src = 32; + Var a1 = src; + + assert (a1.type() == typeid(Poco::UInt64)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + UInt64 value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testBool() +{ + bool src = true; + Var a1 = src; + + assert (a1.type() == typeid(bool)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 1); + assert (s15 == 1); + assert (s1 == "true"); + assert (s2 == 1); + assert (s3 == 1); + assert (s4 == 1); + assert (s5 == 1); + assert (s6 == 1); + assert (s7 == 1); + assert (s8 == 1); + assert (s9 == 1); + assert (s10 == 1.0f); + assert (s11 == 1.0); + assert (s12); + assert (s13 == '\x1'); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + bool value = a1.extract(); + assert (value); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } +} + + +void VarTest::testChar() +{ + char src = ' '; + Var a1 = src; + + assert (a1.type() == typeid(char)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == " "); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + char value = a1.extract(); + assert (value == ' '); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } +} + + +void VarTest::testFloat() +{ Var any("0"); - float f = any; - - float src = 32.0f; - Var a1 = src; - - assert (a1.type() == typeid(float)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - float value = a1.extract(); - assert (value == 32.0f); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1.0f; - assert (a3 == 33.0f); - a3 = a1 - 1.0f; - assert (a3 == 31.0f); - a3 += 1.0f; - assert (a3 == 32.0f); - a3 -= 1.0f; - assert (a3 == 31.0f); - a3 = a1 / 2.0f; - assert (a3 == 16.0f); - a3 = a1 * 2.0f; - assert (a3 == 64.0f); - a3 /= 2.0f; - assert (a3 == 32.0f); - a3 *= 2.0f; - assert (a3 == 64.0f); -} - - -void VarTest::testDouble() -{ + float f = any; + + float src = 32.0f; + Var a1 = src; + + assert (a1.type() == typeid(float)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + float value = a1.extract(); + assert (value == 32.0f); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1.0f; + assert (a3 == 33.0f); + a3 = a1 - 1.0f; + assert (a3 == 31.0f); + a3 += 1.0f; + assert (a3 == 32.0f); + a3 -= 1.0f; + assert (a3 == 31.0f); + a3 = a1 / 2.0f; + assert (a3 == 16.0f); + a3 = a1 * 2.0f; + assert (a3 == 64.0f); + a3 /= 2.0f; + assert (a3 == 32.0f); + a3 *= 2.0f; + assert (a3 == 64.0f); +} + + +void VarTest::testDouble() +{ double d = 0; Var v(d); - float f = v; - - double src = 32.0; - Var a1 = src; - - assert (a1.type() == typeid(double)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - double value = a1.extract(); - assert (value == 32.0); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - - Var a3 = a1 + 1.0; - assert (a3 == 33.0); - a3 = a1 - 1.0; - assert (a3 == 31.0); - a3 += 1.0; - assert (a3 == 32.0); - a3 -= 1.0; - assert (a3 == 31.0); - a3 = a1 / 2.0; - assert (a3 == 16.0); - a3 = a1 * 2.0; - assert (a3 == 64.0); - a3 /= 2.0; - assert (a3 == 32.0); - a3 *= 2.0; - assert (a3 == 64.0); -} - - -void VarTest::testString() -{ - Var a1("32"); - - assert (a1.type() == typeid(std::string)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == '3'); - - const std::string& value = a1.extract(); - assert (value == "32"); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a4(123); - std::string s("456"); - Var a5 = a4 + s; - assert (a5 == "123456"); - a4 += s; - assert (a4 == "123456"); - Var a6 = a4 + "789"; - assert (a6 == "123456789"); - a4 += "789"; - assert (a4 == "123456789"); - - a4 = ""; - assert(!a4); - a4 = "0"; - assert(!a4); - a4 = "FaLsE"; - assert(!a4); -} - - -void VarTest::testLong() -{ - long src = 32; - Var a1 = src; - - assert (a1.type() == typeid(long)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - long value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testULong() -{ - unsigned long src = 32; - Var a1 = src; - - assert (a1.type() == typeid(unsigned long)); - - std::string s1; - Poco::Int8 s2; - Poco::Int16 s3; - Poco::Int32 s4; - Poco::Int64 s5; - Poco::UInt8 s6; - Poco::UInt16 s7; - Poco::UInt32 s8; - Poco::UInt64 s9; - float s10; - double s11; - bool s12; - char s13; - a1.convert(s1); - a1.convert(s2); - a1.convert(s3); - a1.convert(s4); - a1.convert(s5); - a1.convert(s6); - a1.convert(s7); - a1.convert(s8); - a1.convert(s9); - a1.convert(s10); - a1.convert(s11); - a1.convert(s12); - a1.convert(s13); - long s14; - unsigned long s15; - a1.convert(s14); - a1.convert(s15); - assert (s14 == 32); - assert (s15 == 32); - assert (s1 == "32"); - assert (s2 == 32); - assert (s3 == 32); - assert (s4 == 32); - assert (s5 == 32); - assert (s6 == 32); - assert (s7 == 32); - assert (s8 == 32); - assert (s9 == 32); - assert (s10 == 32.0f); - assert (s11 == 32.0); - assert (s12); - assert (s13 == ' '); - Var a2(a1); - std::string t2; - a2.convert(t2); - assert (s1 == t2); - - unsigned long value = a1.extract(); - assert (value == 32); - - try - { - Int16 value2; value2 = a1.extract(); - fail("bad cast - must throw"); - } - catch (Poco::BadCastException&) - { - } - - Var a3 = a1 + 1; - assert (a3 == 33); - a3 = a1 - 1; - assert (a3 == 31); - a3 += 1; - assert (a3 == 32); - a3 -= 1; - assert (a3 == 31); - a3 = a1 / 2; - assert (a3 == 16); - a3 = a1 * 2; - assert (a3 == 64); - a3 /= 2; - assert (a3 == 32); - a3 *= 2; - assert (a3 == 64); -} - - -void VarTest::testUDT() -{ - Dummy d0; - assert (d0 == 0); - - Dummy d(1); - Var da = d; - assert (da.extract() == 1); - - Dummy d1 = d; - Var da1 = d1; - assert (da1.extract() == 1); - - try - { - float f = da1; - fail ("must fail"); - } - catch (BadCastException&) { } -} - - -void VarTest::testConversionOperator() -{ - Var any("42"); - int i = any; - assert (i == 42); - assert (any == i); - - any = 123; -#if defined(_MSC_VER) // gcc bombs on s(any) - std::string s(any); -#else - std::string s = any; -#endif - assert (s == "123"); - assert (s == any); - assert (any == s); - assert ("123" == any); - - any = 321; - s = any.convert(); - assert (s == "321"); - - any = "456"; - assert (any == "456"); - assert ("456" == any); - - Var any2 = "1.5"; - double d = any2; - assert (d == 1.5); - assert (any2 == d); -} - - -void VarTest::testComparisonOperators() -{ - Var any1 = 1; - Var any2 = "1"; - assert (any1 == any2); - assert (any1 == 1); - assert (1 == any1); - assert (any1 == "1"); - assert ("1" == any1); - assert (any1 <= 1); - assert (1 >= any1); - assert (any1 <= 2); - assert (2 >= any1); - assert (any1 < 2); - assert (2 > any1); - assert (any1 > 0); - assert (0 < any1); - assert (any1 >= 1); - assert (1 <= any1); - assert (any1 >= 0); - assert (0 <= any1); - - any1 = 1L; - assert (any1 == any2); - assert (any1 == 1L); - assert (1L == any1); - assert (any1 == "1"); - assert ("1" == any1); - assert (any1 != 2L); - assert (2L != any1); - assert (any1 != "2"); - assert ("2" != any1); - assert (any1 <= 1L); - assert (1L >= any1); - assert (any1 <= 2L); - assert (2L >= any1); - assert (any1 < 2L); - assert (2L > any1); - assert (any1 > 0); - assert (0 < any1); - assert (any1 >= 1L); - assert (1L <= any1); - assert (any1 >= 0); - assert (0 <= any1); - - any1 = 0x31; - assert (any1 == '1'); - assert ('1' == any1); - assert (any1 <= '1'); - assert ('1' >= any1); - assert (any1 <= '2'); - assert ('2' >= any1); - assert (any1 < '2'); - assert ('2' > any1); - assert (any1 > 0); - assert (0 < any1); - assert (any1 >= '1'); - assert ('1' <= any1); - assert (any1 >= 0); - assert (0 <= any1); - - any1 = "2"; - assert (any1 != any2); - assert (any1 != 1); - assert (1 != any1); - assert (any1 != "1"); - assert ("1" != any1); - - any1 = 1.5; - assert (any1 == 1.5); - assert (1.5 == any1); - assert (any1 == "1.5"); - assert ("1.5" == any1); - assert (any1 != 2.5); - assert (2.5 != any1); - assert (any1 != "2.5"); - assert ("2.5" != any1); - assert (any1 <= 1.5); - assert (1.5 >= any1); - assert (any1 <= 2.5); - assert (2.5 >= any1); - assert (any1 < 2.5); - assert (2.5 > any1); - assert (any1 > 0); - assert (0 < any1); - assert (any1 >= 1.5); - assert (1.5 <= any1); - assert (any1 >= 0); - assert (0 <= any1); - - any1 = 1.5f; - assert (any1 == 1.5f); - assert (1.5f == any1); - assert (any1 == "1.5"); - assert ("1.5" == any1); - assert (any1 != 2.5f); - assert (2.5f != any1); - assert (any1 != "2.5"); - assert ("2.5" != any1); - assert (any1 <= 1.5f); - assert (1.5f >= any1); - assert (any1 <= 2.5f); - assert (2.5f >= any1); - assert (any1 < 2.5f); - assert (2.5f > any1); - assert (any1 > 0); - assert (0 < any1); - assert (any1 >= 1.5f); - assert (1.5f <= any1); - assert (any1 >= 0); - assert (0 <= any1); -} - - -void VarTest::testArithmeticOperators() -{ - Var any1 = 1; - Var any2 = 2; - Var any3 = any1 + any2; - assert (any3 == 3); - int i = 1; - i += any1; - assert (2 == i); - - any1 = 3; - assert ((5 - any1) == 2); - any2 = 5; - any3 = any2 - any1; - assert (any3 == 2); - any3 -= 1; - assert (any3 == 1); - i = 5; - i -= any1; - assert (2 == i); - - any1 = 3; - assert ((5 * any1) == 15); - any2 = 5; - any3 = any1 * any2; - assert (any3 == 15); - any3 *= 3; - assert (any3 == 45); - i = 5; - i *= any1; - assert (15 == i); - - any1 = 3; - assert ((9 / any1) == 3); - any2 = 9; - any3 = any2 / any1; - assert (any3 == 3); - any3 /= 3; - assert (any3 == 1); - i = 9; - i /= any1; - assert (3 == i); - - any1 = 1.0f; - any2 = .5f; - any3 = .0f; - any3 = any1 + any2; - assert (any3 == 1.5f); - any3 += .5f; - assert (any3 == 2.0f); - - any1 = 1.0; - any2 = .5; - any3 = 0.0; - any3 = any1 + any2; - assert (any3 == 1.5); - any3 += .5; - assert (any3 == 2.0); - - any1 = 1; - any2 = "2"; - any3 = any1 + any2; - assert (any3 == 3); - any2 = "4"; - any3 += any2; - assert (any3 == 7); - assert (1 + any3 == 8); - - any1 = "123"; - any2 = "456"; - any3 = any1 + any2; - assert (any3 == "123456"); - any2 = "789"; - any3 += any2; - assert (any3 == "123456789"); - assert (("xyz" + any3) == "xyz123456789"); - - try { any3 = any1 - any2; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any3 -= any2; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any3 = any1 * any2; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any3 *= any2; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any3 = any1 / any2; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any3 /= any2; fail ("must fail"); } - catch (InvalidArgumentException&){} - - any1 = 10; - - assert (any1++ == 10); - assert (any1 == 11); - assert (++any1 == 12); - - assert (any1-- == 12); - assert (any1 == 11); - assert (--any1 == 10); - - any1 = 1.23; - - try { ++any1; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any1++; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { --any1; fail ("must fail"); } - catch (InvalidArgumentException&){} - - try { any1--; fail ("must fail"); } - catch (InvalidArgumentException&){} -} - - -void VarTest::testLimitsInt() -{ - testLimitsSigned(); - testLimitsSigned(); - testLimitsSigned(); - testLimitsFloatToInt(); - testLimitsFloatToInt(); - - testLimitsSigned(); - testLimitsSigned(); - testLimitsFloatToInt(); - testLimitsFloatToInt(); - - testLimitsSigned(); - testLimitsFloatToInt(); - testLimitsFloatToInt(); - - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsFloatToInt(); - testLimitsFloatToInt(); - - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsFloatToInt(); - testLimitsFloatToInt(); - - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsFloatToInt(); - testLimitsFloatToInt(); - - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsSignedUnsigned(); - testLimitsFloatToInt(); - testLimitsFloatToInt(); - - - testLimitsUnsigned(); - testLimitsUnsigned(); - testLimitsUnsigned(); - - testLimitsUnsigned(); - testLimitsUnsigned(); - - testLimitsUnsigned(); -} - - -void VarTest::testLimitsFloat() -{ - if (std::numeric_limits::max() != std::numeric_limits::max()) - { - double iMin = -1 * std::numeric_limits::max(); - Var da = iMin * 10; - try { float f; f = da; fail("must fail"); } - catch (RangeException&) {} - - double iMax = std::numeric_limits::max(); - da = iMax * 10; - try { float f; f = da; fail("must fail"); } - catch (RangeException&) {} - } -} - - -void VarTest::testCtor() -{ - // this is mainly to test a reported compiler error with assignment on HP aCC. - // (SF# 1733964) - - Var a1(42); - Var a2(a1); - Var a3; - - a3 = a1; - - assert (a2 == 42); - assert (a3 == 42); -} - - -void VarTest::testIsStruct() -{ - std::string s1("string"); - Poco::Int8 s2(-23); - Poco::Int16 s3(-33); - Poco::Int32 s4(-388); - Poco::Int64 s5(-23823838); - Poco::UInt8 s6(32); - Poco::UInt16 s7(16000); - Poco::UInt32 s8(334234); - Poco::UInt64 s9(2328328382); - float s10(13.333f); - double s11(13.555); - bool s12(true); - char s13('c'); - long s14(232323); - unsigned long s15(21233232); - std::vector s16; - Struct s17; - Struct s18; - - Var d1(s1); - Var d2(s2); - Var d3(s3); - Var d4(s4); - Var d5(s5); - Var d6(s6); - Var d7(s7); - Var d8(s8); - Var d9(s9); - Var d10(s10); - Var d11(s11); - Var d12(s12); - Var d13(s13); - Var d14(s14); - Var d15(s15); - Var d16(s16); - Var d17(s17); - Var d18(s18); - - assert (!d1.isStruct()); - assert (!d2.isStruct()); - assert (!d3.isStruct()); - assert (!d4.isStruct()); - assert (!d5.isStruct()); - assert (!d6.isStruct()); - assert (!d7.isStruct()); - assert (!d8.isStruct()); - assert (!d9.isStruct()); - assert (!d10.isStruct()); - assert (!d11.isStruct()); - assert (!d12.isStruct()); - assert (!d13.isStruct()); - assert (!d14.isStruct()); - assert (!d15.isStruct()); - assert (!d16.isStruct()); - assert (d17.isStruct()); - assert (d18.isStruct()); -} - - -void VarTest::testIsArray() -{ - std::string s1("string"); - Poco::Int8 s2(-23); - Poco::Int16 s3(-33); - Poco::Int32 s4(-388); - Poco::Int64 s5(-23823838); - Poco::UInt8 s6(32); - Poco::UInt16 s7(16000); - Poco::UInt32 s8(334234); - Poco::UInt64 s9(2328328382); - float s10(13.333f); - double s11(13.555); - bool s12(true); - char s13('c'); - long s14(232323); - unsigned long s15(21233232); - std::vector s16; - DynamicStruct s17; - - Var d1(s1); - Var d2(s2); - Var d3(s3); - Var d4(s4); - Var d5(s5); - Var d6(s6); - Var d7(s7); - Var d8(s8); - Var d9(s9); - Var d10(s10); - Var d11(s11); - Var d12(s12); - Var d13(s13); - Var d14(s14); - Var d15(s15); - Var d16(s16); - Var d17(s17); - - assert (!d1.isArray()); - assert (!d2.isArray()); - assert (!d3.isArray()); - assert (!d4.isArray()); - assert (!d5.isArray()); - assert (!d6.isArray()); - assert (!d7.isArray()); - assert (!d8.isArray()); - assert (!d9.isArray()); - assert (!d10.isArray()); - assert (!d11.isArray()); - assert (!d12.isArray()); - assert (!d13.isArray()); - assert (!d14.isArray()); - assert (!d15.isArray()); - assert (d16.isArray()); - assert (!d17.isArray()); -} - - -void VarTest::testArrayIdxOperator() -{ - std::string s1("string"); - Poco::Int8 s2(-23); - Poco::Int16 s3(-33); - Poco::Int32 s4(-388); - Poco::Int64 s5(-23823838); - Poco::UInt8 s6(32); - Poco::UInt16 s7(16000); - Poco::UInt32 s8(334234); - Poco::UInt64 s9(2328328382); - float s10(13.333f); - double s11(13.555); - bool s12(true); - char s13('c'); - long s14(232323); - unsigned long s15(21233232); - std::vector s16; - s16.push_back(s1); - s16.push_back(s2); - DynamicStruct s17; - - Var d1(s1); - Var d2(s2); - Var d3(s3); - Var d4(s4); - Var d5(s5); - Var d6(s6); - Var d7(s7); - Var d8(s8); - Var d9(s9); - Var d10(s10); - Var d11(s11); - Var d12(s12); - Var d13(s13); - Var d14(s14); - Var d15(s15); - Var d16(s16); - Var d17(s17); - - testGetIdxMustThrow(d1, 0); - testGetIdxMustThrow(d2, 0); - testGetIdxMustThrow(d3, 0); - testGetIdxMustThrow(d4, 0); - testGetIdxMustThrow(d5, 0); - testGetIdxMustThrow(d6, 0); - testGetIdxMustThrow(d7, 0); - testGetIdxMustThrow(d8, 0); - testGetIdxMustThrow(d9, 0); - testGetIdxMustThrow(d10, 0); - testGetIdxMustThrow(d11, 0); - testGetIdxMustThrow(d12, 0); - testGetIdxMustThrow(d13, 0); - testGetIdxMustThrow(d14, 0); - testGetIdxMustThrow(d15, 0); - testGetIdx(d16, 0, s1); - testGetIdx(d16, 1, s2); - testGetIdxMustThrow(d17, 0); -} - - -void VarTest::testDynamicStructBasics() -{ - DynamicStruct aStruct; - assert (aStruct.empty()); - assert (aStruct.size() == 0); - assert (aStruct.members().empty()); - - aStruct.insert("First Name", "Little"); - assert (!aStruct.empty()); - assert (aStruct.size() == 1); - assert (*(aStruct.members().begin()) == "First Name"); - assert (aStruct["First Name"] == "Little"); - aStruct.insert("Last Name", "POCO"); - assert (aStruct.members().size() == 2); - aStruct.erase("First Name"); - assert (aStruct.size() == 1); - assert (*(aStruct.members().begin()) == "Last Name"); -} - - -void VarTest::testDynamicStructString() -{ - DynamicStruct aStruct; - aStruct["First Name"] = "Junior"; - aStruct["Last Name"] = "POCO"; - Var a1(aStruct); - assert (a1["First Name"] == "Junior"); - assert (a1["Last Name"] == "POCO"); - a1["First Name"] = "Senior"; - assert (a1["First Name"] == "Senior"); - testGetIdxMustThrow(a1, 0); - - Struct s1; - s1["1"] = 1; - s1["2"] = 2; - s1["3"] = 3; - - Struct s2(s1); - assert (s2["1"] == 1); - assert (s2["2"] == 2); - assert (s2["3"] == 3); - - std::map m1; - m1["1"] = 1; - m1["2"] = 2; - m1["3"] = 3; - - Struct m2(m1); - assert (m2["1"] == 1); - assert (m2["2"] == 2); - assert (m2["3"] == 3); -} - - -void VarTest::testDynamicStructInt() -{ - Dynamic::Struct aStruct; - aStruct[0] = "Junior"; - aStruct[1] = "POCO"; - Var a1(aStruct); - assert (a1[0] == "Junior"); - assert (a1[1] == "POCO"); - a1[0] = "Senior"; - assert (a1[0] == "Senior"); - - Struct s1; - s1[1] = "1"; - s1[2] = "2"; - s1[3] = "3"; - - Struct s2(s1); - assert (s2[1] == "1"); - assert (s2[2] == "2"); - assert (s2[3] == "3"); - - std::map m1; - m1[1] = "1"; - m1[2] = "2"; - m1[3] = "3"; - - Struct m2(m1); - assert (m2[1] == "1"); - assert (m2[2] == "2"); - assert (m2[3] == "3"); -} - - -void VarTest::testDynamicPair() -{ - Pair aPair; - assert (0 == aPair.first()); - try - { - std::string s = aPair.second().convert(); - fail ("must fail"); - } - catch (InvalidAccessException&) { } - - Var va(aPair); - assert ("{ 0 : null }" == va.convert()); - - aPair = Pair(4, "123"); - assert ("123" == aPair.second()); - - va = aPair; - assert ("{ 4 : \"123\" }" == va.convert()); - - int i = 1; - std::string s = "2"; - Pair iPair(i, s); - assert (1 == iPair.first()); - assert ("2" == iPair.second()); - - Pair sPair(s, i); - assert ("2" == sPair.first()); - assert (1 == sPair.second()); - - std::pair p = std::make_pair(i, s); - Pair pPair(p); - assert (1 == pPair.first()); - assert ("2" == pPair.second()); - - Var vp(pPair); - assert ("{ 1 : \"2\" }" == vp.convert()); - - Var vs(sPair); - assert ("{ \"2\" : 1 }" == vs.convert()); -} - - -void VarTest::testArrayToString() -{ - std::string s1("string"); - Poco::Int8 s2(23); - std::vector s16; - s16.push_back(s1); - s16.push_back(s2); - Var a1(s16); - std::string res = a1.convert(); - std::string expected("[ \"string\", 23 ]"); - assert (res == expected); -} - - -void VarTest::testStructToString() -{ - DynamicStruct aStruct; - aStruct["First Name"] = "Junior"; - aStruct["Last Name"] = "POCO"; - aStruct["Age"] = 1; - Var a1(aStruct); - std::string res = a1.convert(); - std::string expected = "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }"; - assert (res == expected); -} - - -void VarTest::testArrayOfStructsToString() -{ - std::vector s16; - DynamicStruct aStruct; - aStruct["First Name"] = "Junior"; - aStruct["Last Name"] = "POCO"; - aStruct["Age"] = 1; - s16.push_back(aStruct); - aStruct["First Name"] = "Senior"; - aStruct["Last Name"] = "POCO"; - aStruct["Age"] = 100; - s16.push_back(aStruct); - std::vector s16Cpy = s16; - // recursive arrays! - s16Cpy.push_back(s16); - s16.push_back(s16Cpy); - Var a1(s16); - std::string res = a1.convert(); - std::string expected = "[ " - "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " - "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, " - "[ " - "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " - "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, " - "[ " - "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " - "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" } " - "] ] ]"; - - assert (res == expected); -} - - -void VarTest::testStructWithArraysToString() -{ - std::string s1("string"); - Poco::Int8 s2(23); - std::vector s16; - s16.push_back(s1); - s16.push_back(s2); - Var a1(s16); - DynamicStruct addr; - addr["Number"] = 4; - addr["Street"] = "Unknown"; - addr["Country"] = "Carinthia"; - DynamicStruct aStruct; - aStruct["First Name"] = "Junior"; - aStruct["Last Name"] = a1; - aStruct["Age"] = 1; - aStruct["Address"] = addr; - Var a2(aStruct); - std::string res = a2.convert(); - std::string expected = "{ \"Address\" : { \"Country\" : \"Carinthia\", \"Number\" : 4, \"Street\" : \"Unknown\" }, " - "\"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : [ \"string\", 23 ] }"; - - assert (res == expected); -} - - -void VarTest::testJSONDeserializeString() -{ - Var a("test"); - std::string tst = Var::toString(a); - Var b = Var::parse(tst); - assert (b.convert() == "test"); - - Var c('c'); - std::string tst2 = Var::toString(c); - Var b2 = Var::parse(tst2); - char cc = b2.convert(); - assert (cc == 'c'); -} - - -void VarTest::testJSONDeserializePrimitives() -{ - Poco::Int8 i8(-12); - Poco::UInt16 u16(2345); - Poco::Int32 i32(-24343); - Poco::UInt64 u64(1234567890); - u64 *= u64; - bool b = false; - float f = 3.1415f; - double d = 3.1415; - - std::string s8 = Var::toString(i8); - std::string s16 = Var::toString(u16); - std::string s32 = Var::toString(i32); - std::string s64 = Var::toString(u64); - std::string sb = Var::toString(b); - std::string sf = Var::toString(f); - std::string sd = Var::toString(d); - Var a8 = Var::parse(s8); - Var a16 = Var::parse(s16); - Var a32 = Var::parse(s32); - Var a64 = Var::parse(s64); - Var ab = Var::parse(sb); - Var af = Var::parse(sf); - Var ad = Var::parse(sd); - assert (a8 == i8); - assert (a16 == u16); - assert (a32 == i32); - assert (a64 == u64); - assert (ab == b); - assert (af == f); - assert (ad == d); -} - - -void VarTest::testJSONDeserializeArray() -{ - Poco::Int8 i8(-12); - Poco::UInt16 u16(2345); - Poco::Int32 i32(-24343); - Poco::UInt64 u64(1234567890); - u64 *= u64; - bool b = false; - float f = 3.1415f; - double d = 3.1415; - std::string s("test string"); - char c('x'); - std::vector aVec; - aVec.push_back(i8); - aVec.push_back(u16); - aVec.push_back(i32); - aVec.push_back(u64); - aVec.push_back(b); - aVec.push_back(f); - aVec.push_back(d); - aVec.push_back(s); - aVec.push_back(c); - - std::string sVec = Var::toString(aVec); - Var a = Var::parse(sVec); - assert (a[0] == i8); - assert (a[1] == u16); - assert (a[2] == i32); - assert (a[3] == u64); - assert (a[4] == b); - assert (a[5] == f); - assert (a[6] == d); - assert (a[7] == s); - assert (a[8] == c); -} - - -void VarTest::testJSONDeserializeComplex() -{ - Poco::Int8 i8(-12); - Poco::UInt16 u16(2345); - Poco::Int32 i32(-24343); - Poco::UInt64 u64(1234567890); - u64 *= u64; - bool b = false; - float f = 3.1415f; - double d = 3.1415; - std::string s("test string"); - char c('x'); - DynamicStruct aStr; - aStr["i8"] = i8; - aStr["u16"] = u16; - aStr["i32"] = i32; - aStr["u64"] = u64; - aStr["b"] = b; - aStr["f"] = f; - aStr["d"] = d; - aStr["s"] = s; - aStr["c"] = c; - std::vector aVec; - aVec.push_back(i8); - aVec.push_back(u16); - aVec.push_back(i32); - aVec.push_back(u64); - aVec.push_back(b); - aVec.push_back(f); - aVec.push_back(d); - aVec.push_back(s); - aVec.push_back(c); - aVec.push_back(aStr); - aStr["vec"] = aVec; - - std::string sStr = Var::toString(aStr); - Var a = Var::parse(sStr); - assert (a.isStruct()); - assert (aStr["i8"] == i8); - assert (aStr["u16"] == u16); - assert (aStr["i32"] == i32); - assert (aStr["u64"] == u64); - assert (aStr["b"] == b); - assert (aStr["f"] == f); - assert (aStr["d"] == d); - assert (aStr["s"] == s); - assert (aStr["c"] == c); - Var vecRet = a["vec"]; - assert (vecRet.isArray()); - assert (vecRet[0] == i8); - assert (vecRet[1] == u16); - assert (vecRet[2] == i32); - assert (vecRet[3] == u64); - assert (vecRet[4] == b); - assert (vecRet[5] == f); - assert (vecRet[6] == d); - assert (vecRet[7] == s); - assert (vecRet[8] == c); - Var strRet = vecRet[9]; - assert (strRet.isStruct()); -} - - -void VarTest::testJSONDeserializeStruct() -{ - Poco::Int8 i8(-12); - Poco::UInt16 u16(2345); - Poco::Int32 i32(-24343); - Poco::UInt64 u64(1234567890); - u64 *= u64; - bool b = false; - float f = 3.1415f; - double d = 3.1415; - std::string s("test string"); - char c('x'); - DynamicStruct aStr; - aStr["i8"] = i8; - aStr["u16"] = u16; - aStr["i32"] = i32; - aStr["u64"] = u64; - aStr["b"] = b; - aStr["f"] = f; - aStr["d"] = d; - aStr["s"] = s; - aStr["c"] = c; - - std::string sStr = Var::toString(aStr); - Var a = Var::parse(sStr); - assert (aStr["i8"] == i8); - assert (aStr["u16"] == u16); - assert (aStr["i32"] == i32); - assert (aStr["u64"] == u64); - assert (aStr["b"] == b); - assert (aStr["f"] == f); - assert (aStr["d"] == d); - assert (aStr["s"] == s); - assert (aStr["c"] == c); -} - - -void VarTest::testDate() -{ - Poco::DateTime dtNow(2007, 3, 13, 8, 12, 15); - - Poco::Timestamp tsNow = dtNow.timestamp(); - Poco::LocalDateTime ldtNow(dtNow.timestamp()); - Var dt(dtNow); - Var ts(tsNow); - Var ldt(ldtNow); - Var dtStr(dt.convert()); - Var tsStr(ts.convert()); - Var ldtStr(ldt.convert()); - DateTime dtRes = dtStr.convert(); - LocalDateTime ldtRes = ldtStr.convert(); - Timestamp tsRes = tsStr.convert(); - assert (dtNow == dtRes); - assert (ldtNow == ldtRes); - assert (tsNow == tsRes); -} - -void VarTest::testGetIdxMustThrow(Var& a1, std::vector::size_type n) -{ - try - { - Var& val1 = a1[n]; - fail("bad cast - must throw"); - val1 = 0; // silence the compiler - } - catch (Poco::InvalidAccessException&) - { - } - - try - { - const Var& c1 = a1; - const Var& cval1 = c1[n]; - fail("bad const cast - must throw"); - assert (cval1 == c1); // silence the compiler - } - catch (Poco::InvalidAccessException&) - { - } -} - - -void VarTest::testEmpty() -{ - Var da; - assert (da.isEmpty()); - assert (da.type() == typeid(void)); - assert (!da.isArray()); - assert (!da.isInteger()); - assert (!da.isNumeric()); - assert (!da.isSigned()); - assert (!da.isString()); - assert (!(da == da)); - assert (!(da != da)); - - da = "123"; - int i = da.convert(); - assert (123 == i); - std::string s = da.extract(); - assert ("123" == s); - assert (!da.isEmpty()); - da.empty(); - assert (da.isEmpty()); - assert (da.type() == typeid(void)); - assert (!da.isArray()); - assert (!da.isInteger()); - assert (!da.isNumeric()); - assert (!da.isSigned()); - assert (!da.isString()); - assert (!(da == da)); - assert (!(da != da)); - - assert (da != ""); - assert ("" != da); - assert (!(da == "")); - assert (!("" == da)); - - - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); - testEmptyComparisons(); -#ifdef POCO_LONG_IS_64_BIT - testEmptyComparisons(); - testEmptyComparisons(); -#endif - testEmptyComparisons(); - testEmptyComparisons(); - - try - { - int i = da; - fail ("must fail"); - } catch (InvalidAccessException&) { } - - try - { - int i = da.extract(); - fail ("must fail"); - } catch (InvalidAccessException&) { } -} - - -void VarTest::setUp() -{ -} - - -void VarTest::tearDown() -{ -} - - -CppUnit::Test* VarTest::suite() -{ - CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("VarTest"); - - CppUnit_addTest(pSuite, VarTest, testInt8); - CppUnit_addTest(pSuite, VarTest, testInt16); - CppUnit_addTest(pSuite, VarTest, testInt32); - CppUnit_addTest(pSuite, VarTest, testInt64); - CppUnit_addTest(pSuite, VarTest, testUInt8); - CppUnit_addTest(pSuite, VarTest, testUInt16); - CppUnit_addTest(pSuite, VarTest, testUInt32); - CppUnit_addTest(pSuite, VarTest, testUInt64); - CppUnit_addTest(pSuite, VarTest, testBool); - CppUnit_addTest(pSuite, VarTest, testChar); - CppUnit_addTest(pSuite, VarTest, testFloat); - CppUnit_addTest(pSuite, VarTest, testDouble); - CppUnit_addTest(pSuite, VarTest, testLong); - CppUnit_addTest(pSuite, VarTest, testULong); - CppUnit_addTest(pSuite, VarTest, testString); - CppUnit_addTest(pSuite, VarTest, testUDT); - CppUnit_addTest(pSuite, VarTest, testConversionOperator); - CppUnit_addTest(pSuite, VarTest, testComparisonOperators); - CppUnit_addTest(pSuite, VarTest, testArithmeticOperators); - CppUnit_addTest(pSuite, VarTest, testLimitsInt); - CppUnit_addTest(pSuite, VarTest, testLimitsFloat); - CppUnit_addTest(pSuite, VarTest, testCtor); - CppUnit_addTest(pSuite, VarTest, testIsStruct); - CppUnit_addTest(pSuite, VarTest, testIsArray); - CppUnit_addTest(pSuite, VarTest, testArrayIdxOperator); - CppUnit_addTest(pSuite, VarTest, testDynamicPair); - CppUnit_addTest(pSuite, VarTest, testDynamicStructBasics); - CppUnit_addTest(pSuite, VarTest, testDynamicStructString); - CppUnit_addTest(pSuite, VarTest, testDynamicStructInt); - CppUnit_addTest(pSuite, VarTest, testArrayToString); - CppUnit_addTest(pSuite, VarTest, testStructToString); - CppUnit_addTest(pSuite, VarTest, testArrayOfStructsToString); - CppUnit_addTest(pSuite, VarTest, testStructWithArraysToString); - CppUnit_addTest(pSuite, VarTest, testJSONDeserializeString); - CppUnit_addTest(pSuite, VarTest, testJSONDeserializePrimitives); - CppUnit_addTest(pSuite, VarTest, testJSONDeserializeArray); - CppUnit_addTest(pSuite, VarTest, testJSONDeserializeStruct); - CppUnit_addTest(pSuite, VarTest, testJSONDeserializeComplex); - CppUnit_addTest(pSuite, VarTest, testDate); - CppUnit_addTest(pSuite, VarTest, testEmpty); - - return pSuite; -} + float f = v; + + double src = 32.0; + Var a1 = src; + + assert (a1.type() == typeid(double)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + double value = a1.extract(); + assert (value == 32.0); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + + Var a3 = a1 + 1.0; + assert (a3 == 33.0); + a3 = a1 - 1.0; + assert (a3 == 31.0); + a3 += 1.0; + assert (a3 == 32.0); + a3 -= 1.0; + assert (a3 == 31.0); + a3 = a1 / 2.0; + assert (a3 == 16.0); + a3 = a1 * 2.0; + assert (a3 == 64.0); + a3 /= 2.0; + assert (a3 == 32.0); + a3 *= 2.0; + assert (a3 == 64.0); +} + + +void VarTest::testString() +{ + Var a1("32"); + + assert (a1.type() == typeid(std::string)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == '3'); + + const std::string& value = a1.extract(); + assert (value == "32"); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a4(123); + std::string s("456"); + Var a5 = a4 + s; + assert (a5 == "123456"); + a4 += s; + assert (a4 == "123456"); + Var a6 = a4 + "789"; + assert (a6 == "123456789"); + a4 += "789"; + assert (a4 == "123456789"); + + a4 = ""; + assert(!a4); + a4 = "0"; + assert(!a4); + a4 = "FaLsE"; + assert(!a4); +} + + +void VarTest::testLong() +{ + long src = 32; + Var a1 = src; + + assert (a1.type() == typeid(long)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + long value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testULong() +{ + unsigned long src = 32; + Var a1 = src; + + assert (a1.type() == typeid(unsigned long)); + + std::string s1; + Poco::Int8 s2; + Poco::Int16 s3; + Poco::Int32 s4; + Poco::Int64 s5; + Poco::UInt8 s6; + Poco::UInt16 s7; + Poco::UInt32 s8; + Poco::UInt64 s9; + float s10; + double s11; + bool s12; + char s13; + a1.convert(s1); + a1.convert(s2); + a1.convert(s3); + a1.convert(s4); + a1.convert(s5); + a1.convert(s6); + a1.convert(s7); + a1.convert(s8); + a1.convert(s9); + a1.convert(s10); + a1.convert(s11); + a1.convert(s12); + a1.convert(s13); + long s14; + unsigned long s15; + a1.convert(s14); + a1.convert(s15); + assert (s14 == 32); + assert (s15 == 32); + assert (s1 == "32"); + assert (s2 == 32); + assert (s3 == 32); + assert (s4 == 32); + assert (s5 == 32); + assert (s6 == 32); + assert (s7 == 32); + assert (s8 == 32); + assert (s9 == 32); + assert (s10 == 32.0f); + assert (s11 == 32.0); + assert (s12); + assert (s13 == ' '); + Var a2(a1); + std::string t2; + a2.convert(t2); + assert (s1 == t2); + + unsigned long value = a1.extract(); + assert (value == 32); + + try + { + Int16 value2; value2 = a1.extract(); + fail("bad cast - must throw"); + } + catch (Poco::BadCastException&) + { + } + + Var a3 = a1 + 1; + assert (a3 == 33); + a3 = a1 - 1; + assert (a3 == 31); + a3 += 1; + assert (a3 == 32); + a3 -= 1; + assert (a3 == 31); + a3 = a1 / 2; + assert (a3 == 16); + a3 = a1 * 2; + assert (a3 == 64); + a3 /= 2; + assert (a3 == 32); + a3 *= 2; + assert (a3 == 64); +} + + +void VarTest::testUDT() +{ + Dummy d0; + assert (d0 == 0); + + Dummy d(1); + Var da = d; + assert (da.extract() == 1); + + Dummy d1 = d; + Var da1 = d1; + assert (da1.extract() == 1); + + try + { + float f = da1; + fail ("must fail"); + } + catch (BadCastException&) { } +} + + +void VarTest::testConversionOperator() +{ + Var any("42"); + int i = any; + assert (i == 42); + assert (any == i); + + any = 123; +#ifdef _MSC_VER + std::string s1(any); +#else // gcc won't compile above (ambiguous cctor call) + std::string s1 = any; +#endif + assert (s1 == "123"); + assert (s1 == any); + assert (any == s1); + assert ("123" == any); + + any = 321; + s1 =any.convert(); + assert (s1 == "321"); + + any = "456"; + assert (any == "456"); + assert ("456" == any); + + any = 789; + std::string s2 = any; + assert (s2 == "789"); + assert (s2 == any); + assert (any == s2); + assert ("789" == any); + + Var any2 = "1.5"; + double d = any2; + assert (d == 1.5); + assert (any2 == d); +} + + +void VarTest::testComparisonOperators() +{ + Var any1 = 1; + Var any2 = "1"; + assert (any1 == any2); + assert (any1 == 1); + assert (1 == any1); + assert (any1 == "1"); + assert ("1" == any1); + assert (any1 <= 1); + assert (1 >= any1); + assert (any1 <= 2); + assert (2 >= any1); + assert (any1 < 2); + assert (2 > any1); + assert (any1 > 0); + assert (0 < any1); + assert (any1 >= 1); + assert (1 <= any1); + assert (any1 >= 0); + assert (0 <= any1); + + any1 = 1L; + assert (any1 == any2); + assert (any1 == 1L); + assert (1L == any1); + assert (any1 == "1"); + assert ("1" == any1); + assert (any1 != 2L); + assert (2L != any1); + assert (any1 != "2"); + assert ("2" != any1); + assert (any1 <= 1L); + assert (1L >= any1); + assert (any1 <= 2L); + assert (2L >= any1); + assert (any1 < 2L); + assert (2L > any1); + assert (any1 > 0); + assert (0 < any1); + assert (any1 >= 1L); + assert (1L <= any1); + assert (any1 >= 0); + assert (0 <= any1); + + any1 = 0x31; + assert (any1 == '1'); + assert ('1' == any1); + assert (any1 <= '1'); + assert ('1' >= any1); + assert (any1 <= '2'); + assert ('2' >= any1); + assert (any1 < '2'); + assert ('2' > any1); + assert (any1 > 0); + assert (0 < any1); + assert (any1 >= '1'); + assert ('1' <= any1); + assert (any1 >= 0); + assert (0 <= any1); + + any1 = "2"; + assert (any1 != any2); + assert (any1 != 1); + assert (1 != any1); + assert (any1 != "1"); + assert ("1" != any1); + + any1 = 1.5; + assert (any1 == 1.5); + assert (1.5 == any1); + assert (any1 == "1.5"); + assert ("1.5" == any1); + assert (any1 != 2.5); + assert (2.5 != any1); + assert (any1 != "2.5"); + assert ("2.5" != any1); + assert (any1 <= 1.5); + assert (1.5 >= any1); + assert (any1 <= 2.5); + assert (2.5 >= any1); + assert (any1 < 2.5); + assert (2.5 > any1); + assert (any1 > 0); + assert (0 < any1); + assert (any1 >= 1.5); + assert (1.5 <= any1); + assert (any1 >= 0); + assert (0 <= any1); + + any1 = 1.5f; + assert (any1 == 1.5f); + assert (1.5f == any1); + assert (any1 == "1.5"); + assert ("1.5" == any1); + assert (any1 != 2.5f); + assert (2.5f != any1); + assert (any1 != "2.5"); + assert ("2.5" != any1); + assert (any1 <= 1.5f); + assert (1.5f >= any1); + assert (any1 <= 2.5f); + assert (2.5f >= any1); + assert (any1 < 2.5f); + assert (2.5f > any1); + assert (any1 > 0); + assert (0 < any1); + assert (any1 >= 1.5f); + assert (1.5f <= any1); + assert (any1 >= 0); + assert (0 <= any1); +} + + +void VarTest::testArithmeticOperators() +{ + Var any1 = 1; + Var any2 = 2; + Var any3 = any1 + any2; + assert (any3 == 3); + int i = 1; + i += any1; + assert (2 == i); + + any1 = 3; + assert ((5 - any1) == 2); + any2 = 5; + any3 = any2 - any1; + assert (any3 == 2); + any3 -= 1; + assert (any3 == 1); + i = 5; + i -= any1; + assert (2 == i); + + any1 = 3; + assert ((5 * any1) == 15); + any2 = 5; + any3 = any1 * any2; + assert (any3 == 15); + any3 *= 3; + assert (any3 == 45); + i = 5; + i *= any1; + assert (15 == i); + + any1 = 3; + assert ((9 / any1) == 3); + any2 = 9; + any3 = any2 / any1; + assert (any3 == 3); + any3 /= 3; + assert (any3 == 1); + i = 9; + i /= any1; + assert (3 == i); + + any1 = 1.0f; + any2 = .5f; + any3 = .0f; + any3 = any1 + any2; + assert (any3 == 1.5f); + any3 += .5f; + assert (any3 == 2.0f); + + any1 = 1.0; + any2 = .5; + any3 = 0.0; + any3 = any1 + any2; + assert (any3 == 1.5); + any3 += .5; + assert (any3 == 2.0); + + any1 = 1; + any2 = "2"; + any3 = any1 + any2; + assert (any3 == 3); + any2 = "4"; + any3 += any2; + assert (any3 == 7); + assert (1 + any3 == 8); + + any1 = "123"; + any2 = "456"; + any3 = any1 + any2; + assert (any3 == "123456"); + any2 = "789"; + any3 += any2; + assert (any3 == "123456789"); + assert (("xyz" + any3) == "xyz123456789"); + + try { any3 = any1 - any2; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any3 -= any2; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any3 = any1 * any2; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any3 *= any2; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any3 = any1 / any2; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any3 /= any2; fail ("must fail"); } + catch (InvalidArgumentException&){} + + any1 = 10; + + assert (any1++ == 10); + assert (any1 == 11); + assert (++any1 == 12); + + assert (any1-- == 12); + assert (any1 == 11); + assert (--any1 == 10); + + any1 = 1.23; + + try { ++any1; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any1++; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { --any1; fail ("must fail"); } + catch (InvalidArgumentException&){} + + try { any1--; fail ("must fail"); } + catch (InvalidArgumentException&){} +} + + +void VarTest::testLimitsInt() +{ + testLimitsSigned(); + testLimitsSigned(); + testLimitsSigned(); + testLimitsFloatToInt(); + testLimitsFloatToInt(); + + testLimitsSigned(); + testLimitsSigned(); + testLimitsFloatToInt(); + testLimitsFloatToInt(); + + testLimitsSigned(); + testLimitsFloatToInt(); + testLimitsFloatToInt(); + + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsFloatToInt(); + testLimitsFloatToInt(); + + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsFloatToInt(); + testLimitsFloatToInt(); + + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsFloatToInt(); + testLimitsFloatToInt(); + + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsSignedUnsigned(); + testLimitsFloatToInt(); + testLimitsFloatToInt(); + + + testLimitsUnsigned(); + testLimitsUnsigned(); + testLimitsUnsigned(); + + testLimitsUnsigned(); + testLimitsUnsigned(); + + testLimitsUnsigned(); +} + + +void VarTest::testLimitsFloat() +{ + if (std::numeric_limits::max() != std::numeric_limits::max()) + { + double iMin = -1 * std::numeric_limits::max(); + Var da = iMin * 10; + try { float f; f = da; fail("must fail"); } + catch (RangeException&) {} + + double iMax = std::numeric_limits::max(); + da = iMax * 10; + try { float f; f = da; fail("must fail"); } + catch (RangeException&) {} + } +} + + +void VarTest::testCtor() +{ + // this is mainly to test a reported compiler error with assignment on HP aCC. + // (SF# 1733964) + + Var a1(42); + Var a2(a1); + Var a3; + + a3 = a1; + + assert (a2 == 42); + assert (a3 == 42); +} + + +void VarTest::testIsStruct() +{ + std::string s1("string"); + Poco::Int8 s2(-23); + Poco::Int16 s3(-33); + Poco::Int32 s4(-388); + Poco::Int64 s5(-23823838); + Poco::UInt8 s6(32); + Poco::UInt16 s7(16000); + Poco::UInt32 s8(334234); + Poco::UInt64 s9(2328328382); + float s10(13.333f); + double s11(13.555); + bool s12(true); + char s13('c'); + long s14(232323); + unsigned long s15(21233232); + std::vector s16; + Struct s17; + Struct s18; + + Var d1(s1); + Var d2(s2); + Var d3(s3); + Var d4(s4); + Var d5(s5); + Var d6(s6); + Var d7(s7); + Var d8(s8); + Var d9(s9); + Var d10(s10); + Var d11(s11); + Var d12(s12); + Var d13(s13); + Var d14(s14); + Var d15(s15); + Var d16(s16); + Var d17(s17); + Var d18(s18); + + assert (!d1.isStruct()); + assert (!d2.isStruct()); + assert (!d3.isStruct()); + assert (!d4.isStruct()); + assert (!d5.isStruct()); + assert (!d6.isStruct()); + assert (!d7.isStruct()); + assert (!d8.isStruct()); + assert (!d9.isStruct()); + assert (!d10.isStruct()); + assert (!d11.isStruct()); + assert (!d12.isStruct()); + assert (!d13.isStruct()); + assert (!d14.isStruct()); + assert (!d15.isStruct()); + assert (!d16.isStruct()); + assert (d17.isStruct()); + assert (d18.isStruct()); +} + + +void VarTest::testIsArray() +{ + std::string s1("string"); + Poco::Int8 s2(-23); + Poco::Int16 s3(-33); + Poco::Int32 s4(-388); + Poco::Int64 s5(-23823838); + Poco::UInt8 s6(32); + Poco::UInt16 s7(16000); + Poco::UInt32 s8(334234); + Poco::UInt64 s9(2328328382); + float s10(13.333f); + double s11(13.555); + bool s12(true); + char s13('c'); + long s14(232323); + unsigned long s15(21233232); + std::vector s16; + DynamicStruct s17; + + Var d1(s1); + Var d2(s2); + Var d3(s3); + Var d4(s4); + Var d5(s5); + Var d6(s6); + Var d7(s7); + Var d8(s8); + Var d9(s9); + Var d10(s10); + Var d11(s11); + Var d12(s12); + Var d13(s13); + Var d14(s14); + Var d15(s15); + Var d16(s16); + Var d17(s17); + + assert (!d1.isArray()); + assert (!d2.isArray()); + assert (!d3.isArray()); + assert (!d4.isArray()); + assert (!d5.isArray()); + assert (!d6.isArray()); + assert (!d7.isArray()); + assert (!d8.isArray()); + assert (!d9.isArray()); + assert (!d10.isArray()); + assert (!d11.isArray()); + assert (!d12.isArray()); + assert (!d13.isArray()); + assert (!d14.isArray()); + assert (!d15.isArray()); + assert (d16.isArray()); + assert (!d17.isArray()); +} + + +void VarTest::testArrayIdxOperator() +{ + std::string s1("string"); + Poco::Int8 s2(-23); + Poco::Int16 s3(-33); + Poco::Int32 s4(-388); + Poco::Int64 s5(-23823838); + Poco::UInt8 s6(32); + Poco::UInt16 s7(16000); + Poco::UInt32 s8(334234); + Poco::UInt64 s9(2328328382); + float s10(13.333f); + double s11(13.555); + bool s12(true); + char s13('c'); + long s14(232323); + unsigned long s15(21233232); + std::vector s16; + s16.push_back(s1); + s16.push_back(s2); + DynamicStruct s17; + + Var d1(s1); + Var d2(s2); + Var d3(s3); + Var d4(s4); + Var d5(s5); + Var d6(s6); + Var d7(s7); + Var d8(s8); + Var d9(s9); + Var d10(s10); + Var d11(s11); + Var d12(s12); + Var d13(s13); + Var d14(s14); + Var d15(s15); + Var d16(s16); + Var d17(s17); + + testGetIdxMustThrow(d1, 0); + testGetIdxMustThrow(d2, 0); + testGetIdxMustThrow(d3, 0); + testGetIdxMustThrow(d4, 0); + testGetIdxMustThrow(d5, 0); + testGetIdxMustThrow(d6, 0); + testGetIdxMustThrow(d7, 0); + testGetIdxMustThrow(d8, 0); + testGetIdxMustThrow(d9, 0); + testGetIdxMustThrow(d10, 0); + testGetIdxMustThrow(d11, 0); + testGetIdxMustThrow(d12, 0); + testGetIdxMustThrow(d13, 0); + testGetIdxMustThrow(d14, 0); + testGetIdxMustThrow(d15, 0); + testGetIdx(d16, 0, s1); + testGetIdx(d16, 1, s2); + testGetIdxMustThrow(d17, 0); +} + + +void VarTest::testDynamicStructBasics() +{ + DynamicStruct aStruct; + assert (aStruct.empty()); + assert (aStruct.size() == 0); + assert (aStruct.members().empty()); + + aStruct.insert("First Name", "Little"); + assert (!aStruct.empty()); + assert (aStruct.size() == 1); + assert (*(aStruct.members().begin()) == "First Name"); + assert (aStruct["First Name"] == "Little"); + aStruct.insert("Last Name", "POCO"); + assert (aStruct.members().size() == 2); + aStruct.erase("First Name"); + assert (aStruct.size() == 1); + assert (*(aStruct.members().begin()) == "Last Name"); +} + + +void VarTest::testDynamicStructString() +{ + DynamicStruct aStruct; + aStruct["First Name"] = "Junior"; + aStruct["Last Name"] = "POCO"; + Var a1(aStruct); + assert (a1["First Name"] == "Junior"); + assert (a1["Last Name"] == "POCO"); + a1["First Name"] = "Senior"; + assert (a1["First Name"] == "Senior"); + testGetIdxMustThrow(a1, 0); + + Struct s1; + s1["1"] = 1; + s1["2"] = 2; + s1["3"] = 3; + + Struct s2(s1); + assert (s2["1"] == 1); + assert (s2["2"] == 2); + assert (s2["3"] == 3); + + std::map m1; + m1["1"] = 1; + m1["2"] = 2; + m1["3"] = 3; + + Struct m2(m1); + assert (m2["1"] == 1); + assert (m2["2"] == 2); + assert (m2["3"] == 3); +} + + +void VarTest::testDynamicStructInt() +{ + Dynamic::Struct aStruct; + aStruct[0] = "Junior"; + aStruct[1] = "POCO"; + Var a1(aStruct); + assert (a1[0] == "Junior"); + assert (a1[1] == "POCO"); + a1[0] = "Senior"; + assert (a1[0] == "Senior"); + + Struct s1; + s1[1] = "1"; + s1[2] = "2"; + s1[3] = "3"; + + Struct s2(s1); + assert (s2[1] == "1"); + assert (s2[2] == "2"); + assert (s2[3] == "3"); + + std::map m1; + m1[1] = "1"; + m1[2] = "2"; + m1[3] = "3"; + + Struct m2(m1); + assert (m2[1] == "1"); + assert (m2[2] == "2"); + assert (m2[3] == "3"); +} + + +void VarTest::testDynamicPair() +{ + Pair aPair; + assert (0 == aPair.first()); + try + { + std::string s = aPair.second().convert(); + fail ("must fail"); + } + catch (InvalidAccessException&) { } + + Var va(aPair); + assert ("{ 0 : null }" == va.convert()); + + aPair = Pair(4, "123"); + assert ("123" == aPair.second()); + + va = aPair; + assert ("{ 4 : \"123\" }" == va.convert()); + + int i = 1; + std::string s = "2"; + Pair iPair(i, s); + assert (1 == iPair.first()); + assert ("2" == iPair.second()); + + Pair sPair(s, i); + assert ("2" == sPair.first()); + assert (1 == sPair.second()); + + std::pair p = std::make_pair(i, s); + Pair pPair(p); + assert (1 == pPair.first()); + assert ("2" == pPair.second()); + + Var vp(pPair); + assert ("{ 1 : \"2\" }" == vp.convert()); + + Var vs(sPair); + assert ("{ \"2\" : 1 }" == vs.convert()); +} + + +void VarTest::testArrayToString() +{ + std::string s1("string"); + Poco::Int8 s2(23); + std::vector s16; + s16.push_back(s1); + s16.push_back(s2); + Var a1(s16); + std::string res = a1.convert(); + std::string expected("[ \"string\", 23 ]"); + assert (res == expected); +} + + +void VarTest::testStructToString() +{ + DynamicStruct aStruct; + aStruct["First Name"] = "Junior"; + aStruct["Last Name"] = "POCO"; + aStruct["Age"] = 1; + Var a1(aStruct); + std::string res = a1.convert(); + std::string expected = "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }"; + assert (res == expected); +} + + +void VarTest::testArrayOfStructsToString() +{ + std::vector s16; + DynamicStruct aStruct; + aStruct["First Name"] = "Junior"; + aStruct["Last Name"] = "POCO"; + aStruct["Age"] = 1; + s16.push_back(aStruct); + aStruct["First Name"] = "Senior"; + aStruct["Last Name"] = "POCO"; + aStruct["Age"] = 100; + s16.push_back(aStruct); + std::vector s16Cpy = s16; + // recursive arrays! + s16Cpy.push_back(s16); + s16.push_back(s16Cpy); + Var a1(s16); + std::string res = a1.convert(); + std::string expected = "[ " + "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " + "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, " + "[ " + "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " + "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, " + "[ " + "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, " + "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" } " + "] ] ]"; + + assert (res == expected); +} + + +void VarTest::testStructWithArraysToString() +{ + std::string s1("string"); + Poco::Int8 s2(23); + std::vector s16; + s16.push_back(s1); + s16.push_back(s2); + Var a1(s16); + DynamicStruct addr; + addr["Number"] = 4; + addr["Street"] = "Unknown"; + addr["Country"] = "Carinthia"; + DynamicStruct aStruct; + aStruct["First Name"] = "Junior"; + aStruct["Last Name"] = a1; + aStruct["Age"] = 1; + aStruct["Address"] = addr; + Var a2(aStruct); + std::string res = a2.convert(); + std::string expected = "{ \"Address\" : { \"Country\" : \"Carinthia\", \"Number\" : 4, \"Street\" : \"Unknown\" }, " + "\"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : [ \"string\", 23 ] }"; + + assert (res == expected); +} + + +void VarTest::testJSONDeserializeString() +{ + Var a("test"); + std::string tst = Var::toString(a); + Var b = Var::parse(tst); + assert (b.convert() == "test"); + + Var c('c'); + std::string tst2 = Var::toString(c); + Var b2 = Var::parse(tst2); + char cc = b2.convert(); + assert (cc == 'c'); +} + + +void VarTest::testJSONDeserializePrimitives() +{ + Poco::Int8 i8(-12); + Poco::UInt16 u16(2345); + Poco::Int32 i32(-24343); + Poco::UInt64 u64(1234567890); + u64 *= u64; + bool b = false; + float f = 3.1415f; + double d = 3.1415; + + std::string s8 = Var::toString(i8); + std::string s16 = Var::toString(u16); + std::string s32 = Var::toString(i32); + std::string s64 = Var::toString(u64); + std::string sb = Var::toString(b); + std::string sf = Var::toString(f); + std::string sd = Var::toString(d); + Var a8 = Var::parse(s8); + Var a16 = Var::parse(s16); + Var a32 = Var::parse(s32); + Var a64 = Var::parse(s64); + Var ab = Var::parse(sb); + Var af = Var::parse(sf); + Var ad = Var::parse(sd); + assert (a8 == i8); + assert (a16 == u16); + assert (a32 == i32); + assert (a64 == u64); + assert (ab == b); + assert (af == f); + assert (ad == d); +} + + +void VarTest::testJSONDeserializeArray() +{ + Poco::Int8 i8(-12); + Poco::UInt16 u16(2345); + Poco::Int32 i32(-24343); + Poco::UInt64 u64(1234567890); + u64 *= u64; + bool b = false; + float f = 3.1415f; + double d = 3.1415; + std::string s("test string"); + char c('x'); + std::vector aVec; + aVec.push_back(i8); + aVec.push_back(u16); + aVec.push_back(i32); + aVec.push_back(u64); + aVec.push_back(b); + aVec.push_back(f); + aVec.push_back(d); + aVec.push_back(s); + aVec.push_back(c); + + std::string sVec = Var::toString(aVec); + Var a = Var::parse(sVec); + assert (a[0] == i8); + assert (a[1] == u16); + assert (a[2] == i32); + assert (a[3] == u64); + assert (a[4] == b); + assert (a[5] == f); + assert (a[6] == d); + assert (a[7] == s); + assert (a[8] == c); +} + + +void VarTest::testJSONDeserializeComplex() +{ + Poco::Int8 i8(-12); + Poco::UInt16 u16(2345); + Poco::Int32 i32(-24343); + Poco::UInt64 u64(1234567890); + u64 *= u64; + bool b = false; + float f = 3.1415f; + double d = 3.1415; + std::string s("test string"); + char c('x'); + DynamicStruct aStr; + aStr["i8"] = i8; + aStr["u16"] = u16; + aStr["i32"] = i32; + aStr["u64"] = u64; + aStr["b"] = b; + aStr["f"] = f; + aStr["d"] = d; + aStr["s"] = s; + aStr["c"] = c; + std::vector aVec; + aVec.push_back(i8); + aVec.push_back(u16); + aVec.push_back(i32); + aVec.push_back(u64); + aVec.push_back(b); + aVec.push_back(f); + aVec.push_back(d); + aVec.push_back(s); + aVec.push_back(c); + aVec.push_back(aStr); + aStr["vec"] = aVec; + + std::string sStr = Var::toString(aStr); + Var a = Var::parse(sStr); + assert (a.isStruct()); + assert (aStr["i8"] == i8); + assert (aStr["u16"] == u16); + assert (aStr["i32"] == i32); + assert (aStr["u64"] == u64); + assert (aStr["b"] == b); + assert (aStr["f"] == f); + assert (aStr["d"] == d); + assert (aStr["s"] == s); + assert (aStr["c"] == c); + Var vecRet = a["vec"]; + assert (vecRet.isArray()); + assert (vecRet[0] == i8); + assert (vecRet[1] == u16); + assert (vecRet[2] == i32); + assert (vecRet[3] == u64); + assert (vecRet[4] == b); + assert (vecRet[5] == f); + assert (vecRet[6] == d); + assert (vecRet[7] == s); + assert (vecRet[8] == c); + Var strRet = vecRet[9]; + assert (strRet.isStruct()); +} + + +void VarTest::testJSONDeserializeStruct() +{ + Poco::Int8 i8(-12); + Poco::UInt16 u16(2345); + Poco::Int32 i32(-24343); + Poco::UInt64 u64(1234567890); + u64 *= u64; + bool b = false; + float f = 3.1415f; + double d = 3.1415; + std::string s("test string"); + char c('x'); + DynamicStruct aStr; + aStr["i8"] = i8; + aStr["u16"] = u16; + aStr["i32"] = i32; + aStr["u64"] = u64; + aStr["b"] = b; + aStr["f"] = f; + aStr["d"] = d; + aStr["s"] = s; + aStr["c"] = c; + + std::string sStr = Var::toString(aStr); + Var a = Var::parse(sStr); + assert (aStr["i8"] == i8); + assert (aStr["u16"] == u16); + assert (aStr["i32"] == i32); + assert (aStr["u64"] == u64); + assert (aStr["b"] == b); + assert (aStr["f"] == f); + assert (aStr["d"] == d); + assert (aStr["s"] == s); + assert (aStr["c"] == c); +} + + +void VarTest::testDate() +{ + Poco::DateTime dtNow(2007, 3, 13, 8, 12, 15); + + Poco::Timestamp tsNow = dtNow.timestamp(); + Poco::LocalDateTime ldtNow(dtNow.timestamp()); + Var dt(dtNow); + Var ts(tsNow); + Var ldt(ldtNow); + Var dtStr(dt.convert()); + Var tsStr(ts.convert()); + Var ldtStr(ldt.convert()); + DateTime dtRes = dtStr.convert(); + LocalDateTime ldtRes = ldtStr.convert(); + Timestamp tsRes = tsStr.convert(); + assert (dtNow == dtRes); + assert (ldtNow == ldtRes); + assert (tsNow == tsRes); +} + +void VarTest::testGetIdxMustThrow(Var& a1, std::vector::size_type n) +{ + try + { + Var& val1 = a1[n]; + fail("bad cast - must throw"); + val1 = 0; // silence the compiler + } + catch (Poco::InvalidAccessException&) + { + } + + try + { + const Var& c1 = a1; + const Var& cval1 = c1[n]; + fail("bad const cast - must throw"); + assert (cval1 == c1); // silence the compiler + } + catch (Poco::InvalidAccessException&) + { + } +} + + +void VarTest::testEmpty() +{ + Var da; + assert (da.isEmpty()); + assert (da.type() == typeid(void)); + assert (!da.isArray()); + assert (!da.isInteger()); + assert (!da.isNumeric()); + assert (!da.isSigned()); + assert (!da.isString()); + assert (!(da == da)); + assert (!(da != da)); + + da = "123"; + int i = da.convert(); + assert (123 == i); + std::string s = da.extract(); + assert ("123" == s); + assert (!da.isEmpty()); + da.empty(); + assert (da.isEmpty()); + assert (da.type() == typeid(void)); + assert (!da.isArray()); + assert (!da.isInteger()); + assert (!da.isNumeric()); + assert (!da.isSigned()); + assert (!da.isString()); + assert (!(da == da)); + assert (!(da != da)); + + assert (da != ""); + assert ("" != da); + assert (!(da == "")); + assert (!("" == da)); + + + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); + testEmptyComparisons(); +#ifdef POCO_LONG_IS_64_BIT + testEmptyComparisons(); + testEmptyComparisons(); +#endif + testEmptyComparisons(); + testEmptyComparisons(); + + try + { + int i = da; + fail ("must fail"); + } catch (InvalidAccessException&) { } + + try + { + int i = da.extract(); + fail ("must fail"); + } catch (InvalidAccessException&) { } +} + + +void VarTest::setUp() +{ +} + + +void VarTest::tearDown() +{ +} + + +CppUnit::Test* VarTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("VarTest"); + + CppUnit_addTest(pSuite, VarTest, testInt8); + CppUnit_addTest(pSuite, VarTest, testInt16); + CppUnit_addTest(pSuite, VarTest, testInt32); + CppUnit_addTest(pSuite, VarTest, testInt64); + CppUnit_addTest(pSuite, VarTest, testUInt8); + CppUnit_addTest(pSuite, VarTest, testUInt16); + CppUnit_addTest(pSuite, VarTest, testUInt32); + CppUnit_addTest(pSuite, VarTest, testUInt64); + CppUnit_addTest(pSuite, VarTest, testBool); + CppUnit_addTest(pSuite, VarTest, testChar); + CppUnit_addTest(pSuite, VarTest, testFloat); + CppUnit_addTest(pSuite, VarTest, testDouble); + CppUnit_addTest(pSuite, VarTest, testLong); + CppUnit_addTest(pSuite, VarTest, testULong); + CppUnit_addTest(pSuite, VarTest, testString); + CppUnit_addTest(pSuite, VarTest, testUDT); + CppUnit_addTest(pSuite, VarTest, testConversionOperator); + CppUnit_addTest(pSuite, VarTest, testComparisonOperators); + CppUnit_addTest(pSuite, VarTest, testArithmeticOperators); + CppUnit_addTest(pSuite, VarTest, testLimitsInt); + CppUnit_addTest(pSuite, VarTest, testLimitsFloat); + CppUnit_addTest(pSuite, VarTest, testCtor); + CppUnit_addTest(pSuite, VarTest, testIsStruct); + CppUnit_addTest(pSuite, VarTest, testIsArray); + CppUnit_addTest(pSuite, VarTest, testArrayIdxOperator); + CppUnit_addTest(pSuite, VarTest, testDynamicPair); + CppUnit_addTest(pSuite, VarTest, testDynamicStructBasics); + CppUnit_addTest(pSuite, VarTest, testDynamicStructString); + CppUnit_addTest(pSuite, VarTest, testDynamicStructInt); + CppUnit_addTest(pSuite, VarTest, testArrayToString); + CppUnit_addTest(pSuite, VarTest, testStructToString); + CppUnit_addTest(pSuite, VarTest, testArrayOfStructsToString); + CppUnit_addTest(pSuite, VarTest, testStructWithArraysToString); + CppUnit_addTest(pSuite, VarTest, testJSONDeserializeString); + CppUnit_addTest(pSuite, VarTest, testJSONDeserializePrimitives); + CppUnit_addTest(pSuite, VarTest, testJSONDeserializeArray); + CppUnit_addTest(pSuite, VarTest, testJSONDeserializeStruct); + CppUnit_addTest(pSuite, VarTest, testJSONDeserializeComplex); + CppUnit_addTest(pSuite, VarTest, testDate); + CppUnit_addTest(pSuite, VarTest, testEmpty); + + return pSuite; +} diff --git a/JSON/include/Poco/JSON/Array.h b/JSON/include/Poco/JSON/Array.h index 4cb56e634..98477b0dd 100644 --- a/JSON/include/Poco/JSON/Array.h +++ b/JSON/include/Poco/JSON/Array.h @@ -46,10 +46,8 @@ #include "Poco/SharedPtr.h" #include "Poco/Dynamic/Var.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { class Object; @@ -58,37 +56,28 @@ class JSON_API Array { public: - typedef std::vector ValueVector; - - + typedef std::vector ValueVector; typedef SharedPtr Ptr; - Array(); /// Default constructor - Array(const Array& copy); /// Copy Constructor - virtual ~Array(); /// Destructor - ValueVector::const_iterator begin() const; /// Returns iterator - ValueVector::const_iterator end() const; /// Returns iterator - - DynamicAny get(unsigned int index) const; + Dynamic::Var get(unsigned int index) const; /// Retrieves an element. Will return an empty value /// when the element doesn't exist. - Array::Ptr getArray(unsigned int index) const; /// Retrieves an array. When the element is not /// an array or doesn't exist, an empty SharedPtr is returned. @@ -101,11 +90,10 @@ public: /// exceptions for invalid values. /// Note: This will not work for an array or an object. { - DynamicAny value = get(index); + Dynamic::Var value = get(index); return value.convert(); } - SharedPtr getObject(unsigned int index) const; /// Retrieves an object. When the element is not /// an object or doesn't exist, an empty SharedPtr is returned. @@ -113,20 +101,16 @@ public: unsigned int size() const; /// Returns the size of the array - bool isArray(unsigned int index) const; /// Returns true when the element is an array - bool isNull(unsigned int index) const; /// Returns true when the element is null or /// when the element doesn't exist. - bool isObject(unsigned int index) const; /// Returns true when the element is an object - template T optElement(unsigned int index, const T& def) const /// Returns the element at the given index. When @@ -149,24 +133,20 @@ public: return value; } - - void add(const DynamicAny& value) + void add(const Dynamic::Var& value) /// Add the given value to the array { _values.push_back(value); } - void stringify(std::ostream& out, unsigned int indent) const; /// Prints the array to out. When indent is 0, the array /// will be printed on one line without indentation. - void remove(unsigned int index); /// Removes the element on the given index. private: - ValueVector _values; }; @@ -176,28 +156,32 @@ inline Array::ValueVector::const_iterator Array::begin() const return _values.begin(); } + inline Array::ValueVector::const_iterator Array::end() const { return _values.end(); } + inline unsigned int Array::size() const { return _values.size(); } + inline bool Array::isArray(unsigned int index) const { - DynamicAny value = get(index); + Dynamic::Var value = get(index); return value.type() == typeid(Array::Ptr); } + inline bool Array::isNull(unsigned int index) const { if ( index < _values.size() ) { - DynamicAny value = _values[index]; + Dynamic::Var value = _values[index]; return value.isEmpty(); } return true; @@ -211,11 +195,9 @@ inline void Array::remove(unsigned int index) }} // Namespace Poco::JSON -namespace Poco -{ -namespace Dynamic -{ +namespace Poco { +namespace Dynamic { template <> class VarHolderImpl: public VarHolder @@ -355,8 +337,7 @@ private: JSON::Array::Ptr _val; }; -} // Namespace Dynamic -} // Namespace Poco +}} // namespace Poco::JSON #endif // JSON_Array_INCLUDED diff --git a/JSON/include/Poco/JSON/DefaultHandler.h b/JSON/include/Poco/JSON/DefaultHandler.h index 1b9e582ce..2801dfa8a 100644 --- a/JSON/include/Poco/JSON/DefaultHandler.h +++ b/JSON/include/Poco/JSON/DefaultHandler.h @@ -40,13 +40,12 @@ #include "Poco/JSON/Handler.h" - #include -namespace Poco -{ -namespace JSON -{ + +namespace Poco { +namespace JSON { + class JSON_API DefaultHandler : public Handler /// Provides a default handler for the JSON parser. @@ -55,82 +54,60 @@ class JSON_API DefaultHandler : public Handler { public: - DefaultHandler(); /// Default Constructor - virtual ~DefaultHandler(); /// Destructor - void startObject(); /// Handles a {, meaning a new object will be read - void endObject(); /// Handles a }, meaning the object is read - void startArray(); /// Handles a [, meaning a new array will be read - void endArray(); /// Handles a ], meaning the array is read - void key(const std::string& k); /// A key is read - - DynamicAny result() const; + Dynamic::Var result() const; /// Returns the result of the parser. Which is an object or an array. - virtual void value(int v); /// An integer value is read - #if defined(POCO_HAVE_INT64) virtual void value(Int64 v); /// A 64-bit integer value is read #endif - virtual void value(const std::string& s); /// A string value is read. - virtual void value(double d); /// A double value is read - virtual void value(bool b); /// A boolean value is read - virtual void null(); /// A null value is read - private: + void setValue(const Poco::Dynamic::Var& value); - - void setValue(const Poco::DynamicAny& value); - - - std::stack _stack; - - - std::string _key; - - - DynamicAny _result; + std::stack _stack; + std::string _key; + Dynamic::Var _result; }; -inline DynamicAny DefaultHandler::result() const +inline Dynamic::Var DefaultHandler::result() const { return _result; } @@ -141,6 +118,7 @@ inline void DefaultHandler::value(int v) setValue(v); } + #if defined(POCO_HAVE_INT64) inline void DefaultHandler::value(Int64 v) { @@ -169,11 +147,11 @@ inline void DefaultHandler::value(bool b) inline void DefaultHandler::null() { - Poco::DynamicAny empty; + Poco::Dynamic::Var empty; setValue(empty); } -}} // Namespace Poco::JSON +}} // namespace Poco::JSON #endif // JSON_DefaultHandler_INCLUDED diff --git a/JSON/include/Poco/JSON/Handler.h b/JSON/include/Poco/JSON/Handler.h index c8054a41c..d1e31d560 100644 --- a/JSON/include/Poco/JSON/Handler.h +++ b/JSON/include/Poco/JSON/Handler.h @@ -39,65 +39,51 @@ #define JSON_Handler_INCLUDED -#include "Poco/DynamicAny.h" +#include "Poco/Dynamic/Var.h" #include "Poco/JSON/JSON.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { + class JSON_API Handler { public: - - virtual void startObject() = 0; /// The parser has read a {, meaning a new object will be read - virtual void endObject() = 0; /// The parser has read a }, meaning the object is read - virtual void startArray() = 0; /// The parser has read a [, meaning a new array will be read - virtual void endArray() = 0; /// The parser has read a ], meaning the array is read - virtual void key(const std::string& k) = 0; /// A key of an object is read - virtual void null() = 0; /// A null value is read - virtual void value(int v) = 0; /// An integer value is read - #if defined(POCO_HAVE_INT64) virtual void value(Int64 v) = 0; /// A 64-bit integer value is read #endif - virtual void value(const std::string& value) = 0; /// A string value is read. - virtual void value(double d) = 0; /// A double value is read - virtual void value(bool b) = 0; /// A boolean value is read - protected: virtual ~Handler(); @@ -107,6 +93,6 @@ private: }; -}} // Namespace Poco::JSON +}} // namespace Poco::JSON #endif // JSON_Handler_INCLUDED diff --git a/JSON/include/Poco/JSON/JSON.h b/JSON/include/Poco/JSON/JSON.h index a7e62b473..205229d50 100644 --- a/JSON/include/Poco/JSON/JSON.h +++ b/JSON/include/Poco/JSON/JSON.h @@ -54,16 +54,16 @@ // defined with this macro as being exported. // #if defined(_WIN32) && defined(POCO_DLL) -#if defined(JSON_EXPORTS) -#define JSON_API __declspec(dllexport) -#else -#define JSON_API __declspec(dllimport) -#endif + #if defined(JSON_EXPORTS) + #define JSON_API __declspec(dllexport) + #else + #define JSON_API __declspec(dllimport) + #endif #endif #if !defined(JSON_API) -#define JSON_API + #define JSON_API #endif diff --git a/JSON/include/Poco/JSON/JSONException.h b/JSON/include/Poco/JSON/JSONException.h index 1dba2f088..d9eefea23 100644 --- a/JSON/include/Poco/JSON/JSONException.h +++ b/JSON/include/Poco/JSON/JSONException.h @@ -43,13 +43,12 @@ #include "Poco/Exception.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { POCO_DECLARE_EXCEPTION(JSON_API, JSONException, Poco::Exception) -}} // Namespace Poco::JSON +}} // namespace Poco::JSON + #endif //JSON_JSONException_INCLUDED diff --git a/JSON/include/Poco/JSON/Object.h b/JSON/include/Poco/JSON/Object.h index c2c2bee24..012dcd35b 100644 --- a/JSON/include/Poco/JSON/Object.h +++ b/JSON/include/Poco/JSON/Object.h @@ -50,10 +50,8 @@ #include "Poco/JSON/JSON.h" #include "Poco/JSON/Array.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { class JSON_API Object @@ -63,34 +61,29 @@ public: typedef SharedPtr Ptr; - Object(); /// Default constructor Object(const Object& copy); /// Copy constructor - virtual ~Object(); /// Destructor - DynamicAny get(const std::string& key) const; + Dynamic::Var get(const std::string& key) const; /// Retrieves a property. An empty value is /// returned when the property doesn't exist. - Array::Ptr getArray(const std::string& key) const; /// Returns a SharedPtr to an array when the property /// is an array. An empty SharedPtr is returned when /// the element doesn't exist or is not an array. - Object::Ptr getObject(const std::string& key) const; /// Returns a SharedPtr to an object when the property /// is an object. An empty SharedPtr is returned when /// the property doesn't exist or is not an object - template T getValue(const std::string& key) const /// Retrieves the property with the given name and will @@ -99,30 +92,25 @@ public: /// which can also throw exceptions for invalid values. /// Note: This will not work for an array or an object. { - DynamicAny value = get(key); + Dynamic::Var value = get(key); return value.convert(); } void getNames(std::vector& names) const; /// Returns all property names - bool has(const std::string& key) const; /// Returns true when the given property exists - bool isArray(const std::string& key) const; /// Returns true when the given property contains an array - bool isNull(const std::string& key) const; /// Returns true when the given property contains a null value - bool isObject(const std::string& key) const; /// Returns true when the given property contains an object - template T optValue(const std::string& key, const T& def) const /// Returns the value of a property when the property exists @@ -146,27 +134,21 @@ public: return value; } - unsigned int size() const; /// Returns the number of properties - - void set(const std::string& key, const DynamicAny& value); + void set(const std::string& key, const Dynamic::Var& value); /// Sets a new value - void stringify(std::ostream& out, unsigned int indent = 0) const; /// Prints the object to out. When indent is 0, the object /// will be printed on one line without indentation. - void remove(const std::string& key); /// Removes the property with the given key - private: - - typedef std::map ValueMap; + typedef std::map ValueMap; ValueMap _values; }; @@ -177,47 +159,52 @@ inline bool Object::has(const std::string& key) const return it != _values.end(); } + inline bool Object::isArray(const std::string& key) const { ValueMap::const_iterator it = _values.find(key); return it != _values.end() || it->second.type() == typeid(Array::Ptr); } + inline bool Object::isNull(const std::string& key) const { ValueMap::const_iterator it = _values.find(key); return it == _values.end() || it->second.isEmpty(); } + inline bool Object::isObject(const std::string& key) const { ValueMap::const_iterator it = _values.find(key); return it != _values.end() || it->second.type() == typeid(Object::Ptr); } -inline void Object::set(const std::string& key, const DynamicAny& value) + +inline void Object::set(const std::string& key, const Dynamic::Var& value) { _values[key] = value; } + inline unsigned int Object::size() const { return _values.size(); } + inline void Object::remove(const std::string& key) { _values.erase(key); } + }} // Namespace Poco::JSON -namespace Poco -{ +namespace Poco { +namespace Dynamic { -namespace Dynamic -{ template <> class VarHolderImpl: public VarHolder @@ -357,7 +344,7 @@ private: JSON::Object::Ptr _val; }; -} -} +}} // namespace Poco::JSON + #endif // JSON_Object_INCLUDED diff --git a/JSON/include/Poco/JSON/Parser.h b/JSON/include/Poco/JSON/Parser.h index e685010ee..6ef1c7c12 100644 --- a/JSON/include/Poco/JSON/Parser.h +++ b/JSON/include/Poco/JSON/Parser.h @@ -42,7 +42,7 @@ #include #include -#include "Poco/DynamicAny.h" +#include "Poco/Dynamic/Var.h" #include "Poco/StreamTokenizer.h" #include "Poco/JSON/JSON.h" @@ -50,10 +50,9 @@ #include "Poco/JSON/Array.h" #include "Poco/JSON/Handler.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { + class JSON_API Parser /// A class for passing JSON strings or streams @@ -63,33 +62,26 @@ public: Parser(); /// Constructor - virtual ~Parser(); /// Destructor - void parse(const std::string& source); /// Parses a string - void parse(std::istream& in); /// Parses a JSON from the input stream - void setHandler(Handler* handler); /// Set the handler Handler* getHandler(); /// Returns the handler - private: - const Token* nextToken(); /// Returns the next token - void readObject(); /// Starts reading an object @@ -97,23 +89,17 @@ private: void readArray(); /// Starts reading an array - bool readRow(bool firstCall = false); /// Reads a property value pair. Returns true when a next row is expected. - void readValue(const Token* token); /// Read a value from the token - bool readElements(bool firstCall = false); /// Read all elements of an array - StreamTokenizer _tokenizer; - - - Handler* _handler; + Handler* _handler; }; @@ -135,6 +121,8 @@ inline Handler* Parser::getHandler() return _handler; } -}} // Namespace Poco::JSON + +}} // namespace Poco::JSON + #endif // JSON_JSONParser_INCLUDED diff --git a/JSON/include/Poco/JSON/Query.h b/JSON/include/Poco/JSON/Query.h index 48e7d4c67..d5cd03efe 100644 --- a/JSON/include/Poco/JSON/Query.h +++ b/JSON/include/Poco/JSON/Query.h @@ -44,40 +44,34 @@ #include "Poco/JSON/Array.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { + class JSON_API Query /// Class that can be used to search for a value in a JSON object or array. { public: - Query(const DynamicAny& source); + Query(const Dynamic::Var& source); /// Constructor. Pass the start object/array. - virtual ~Query(); /// Destructor - Object::Ptr findObject(const std::string& path) const; /// Search for an object. When the object can't be found, an empty /// SharedPtr is returned. - Array::Ptr findArray(const std::string& path) const; /// Search for an array. When the array can't be found, an empty /// SharedPtr is returned. - - DynamicAny find(const std::string& path) const; + Dynamic::Var find(const std::string& path) const; /// Searches a value /// For example: "person.children[0].name" will return the /// the name of the first child. When the value can't be found /// an empty value is returned. - template T findValue(const std::string& path, const T& def) const /// Searches for a value will convert it to the given type. @@ -85,16 +79,14 @@ public: /// the default value will be returned. { T result = def; - DynamicAny value = find(path); + Dynamic::Var value = find(path); if ( ! value.isEmpty() ) { try { result = value.convert(); } - catch(...) - { - } + catch(...) { } } return result; } @@ -108,11 +100,11 @@ public: } private: - - DynamicAny _source; - + Dynamic::Var _source; }; -}} // Namespace JSON + +}} // namespace Poco::JSON + #endif // JSON_JSONQuery_INCLUDED diff --git a/JSON/include/Poco/JSON/Stringifier.h b/JSON/include/Poco/JSON/Stringifier.h index 80af2bb7f..d9c9f0fe4 100644 --- a/JSON/include/Poco/JSON/Stringifier.h +++ b/JSON/include/Poco/JSON/Stringifier.h @@ -41,24 +41,24 @@ #include -#include "Poco/DynamicAny.h" +#include "Poco/Dynamic/Var.h" #include "Poco/JSON/JSON.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { + class JSON_API Stringifier /// Helper class for creating a String from a JSON object or array { public: - static void stringify(const DynamicAny& any, std::ostream& out, unsigned int indent = 0); + static void stringify(const Dynamic::Var& any, std::ostream& out, unsigned int indent = 0); /// Writes a String representation of the value to the output stream. /// When indent is 0, the String will be created as small as possible. }; -}} // Namespace Poco::JSON + +}} // namespace Poco::JSON #endif // JSON_JSONStringifier_INCLUDED diff --git a/JSON/include/Poco/JSON/Template.h b/JSON/include/Poco/JSON/Template.h index 3d9c57b62..117f4fae5 100644 --- a/JSON/include/Poco/JSON/Template.h +++ b/JSON/include/Poco/JSON/Template.h @@ -43,21 +43,22 @@ #include #include "Poco/JSON/JSON.h" -#include "Poco/DynamicAny.h" +#include "Poco/Dynamic/Var.h" #include "Poco/SharedPtr.h" #include "Poco/Path.h" #include "Poco/Timestamp.h" -namespace Poco -{ -namespace JSON -{ +namespace Poco { +namespace JSON { + class MultiPart; + POCO_DECLARE_EXCEPTION(JSON_API, JSONTemplateException, Poco::Exception) + class JSON_API Template /// Template is a template engine which uses JSON as input /// for generating output. There are commands for @@ -125,7 +126,7 @@ public: /// Returns the time when the template was parsed - void render(const DynamicAny& data, std::ostream& out) const; + void render(const Dynamic::Var& data, std::ostream& out) const; /// Renders the template and send the output to the stream @@ -177,6 +178,6 @@ inline Timestamp Template::parseTime() const return _parseTime; } -}} // Namespace Poco::JSON +}} // namespace Poco::JSON #endif // JSON_JSONTemplate_INCLUDED diff --git a/JSON/include/Poco/JSON/TemplateCache.h b/JSON/include/Poco/JSON/TemplateCache.h index dbc2c6bc2..72d55e712 100644 --- a/JSON/include/Poco/JSON/TemplateCache.h +++ b/JSON/include/Poco/JSON/TemplateCache.h @@ -45,13 +45,12 @@ #include "Poco/Path.h" #include "Poco/SharedPtr.h" #include "Poco/Logger.h" - #include "Poco/JSON/Template.h" -namespace Poco -{ -namespace JSON -{ + +namespace Poco { +namespace JSON { + class JSON_API TemplateCache /// Use to cache parsed templates. Templates are @@ -66,16 +65,13 @@ public: /// Constructor. The cache must be created /// and not destroyed as long as it is used. - virtual ~TemplateCache(); /// Destructor - void addPath(const Path& path); /// Add a path for resolving template paths. /// The order of check is FIFO. - Template::Ptr getTemplate(const Path& path); /// Returns a template from the cache. /// When the template file is not yet loaded @@ -85,32 +81,20 @@ public: /// even when the template isn't stored anymore in /// the cache. - static TemplateCache* instance(); /// Returns the only instance of this cache - void setLogger(Logger& logger); /// Sets the logger for the cache. - private: - static TemplateCache* _instance; - - - std::vector _includePaths; - - + static TemplateCache* _instance; + std::vector _includePaths; std::map _cache; - - - Logger* _logger; - - + Logger* _logger; + void setup(); - - Path resolvePath(const Path& path) const; }; diff --git a/JSON/src/Array.cpp b/JSON/src/Array.cpp index 4ff4d2a85..0782db243 100644 --- a/JSON/src/Array.cpp +++ b/JSON/src/Array.cpp @@ -37,45 +37,52 @@ #include "Poco/JSON/Object.h" #include "Poco/JSON/Stringifier.h" -namespace Poco -{ -namespace JSON -{ + +using Poco::Dynamic::Var; + + +namespace Poco { +namespace JSON { + Array::Array() { } + Array::Array(const Array& copy) : _values(copy._values) { } + Array::~Array() { } -DynamicAny Array::get(unsigned int index) const + +Var Array::get(unsigned int index) const { - DynamicAny value; + Var value; try { value = _values.at(index); } - catch(std::out_of_range) + catch(std::out_of_range&) { //Ignore, we return an empty value } return value; } + Array::Ptr Array::getArray(unsigned int index) const { Array::Ptr result; - DynamicAny value = get(index); + Var value = get(index); if ( value.type() == typeid(Array::Ptr) ) { result = value.extract(); @@ -88,7 +95,7 @@ Object::Ptr Array::getObject(unsigned int index) const { Object::Ptr result; - DynamicAny value = get(index); + Var value = get(index); if ( value.type() == typeid(Object::Ptr) ) { result = value.extract(); @@ -96,9 +103,10 @@ Object::Ptr Array::getObject(unsigned int index) const return result; } + bool Array::isObject(unsigned int index) const { - DynamicAny value = get(index); + Var value = get(index); return value.type() == typeid(Object::Ptr); } @@ -144,4 +152,5 @@ void Array::stringify(std::ostream& out, unsigned int indent) const out << "]"; } + }} // Namespace Poco::JSON diff --git a/JSON/src/DefaultHandler.cpp b/JSON/src/DefaultHandler.cpp index d632714fa..129a5c3f8 100644 --- a/JSON/src/DefaultHandler.cpp +++ b/JSON/src/DefaultHandler.cpp @@ -36,15 +36,19 @@ #include "Poco/JSON/DefaultHandler.h" #include "Poco/JSON/Object.h" -namespace Poco -{ -namespace JSON -{ + +using Poco::Dynamic::Var; + + +namespace Poco { +namespace JSON { + DefaultHandler::DefaultHandler() : Handler() { } + DefaultHandler::~DefaultHandler() { } @@ -60,7 +64,7 @@ void DefaultHandler::startObject() } else { - DynamicAny parent = _stack.top(); + Var parent = _stack.top(); if ( parent.type() == typeid(Array::Ptr) ) { @@ -96,7 +100,7 @@ void DefaultHandler::startArray() } else { - DynamicAny parent = _stack.top(); + Var parent = _stack.top(); if ( parent.type() == typeid(Array::Ptr) ) { @@ -128,9 +132,9 @@ void DefaultHandler::key(const std::string& k) } -void DefaultHandler::setValue(const Poco::DynamicAny& value) +void DefaultHandler::setValue(const Var& value) { - DynamicAny parent = _stack.top(); + Var parent = _stack.top(); if ( parent.type() == typeid(Array::Ptr) ) { diff --git a/JSON/src/Object.cpp b/JSON/src/Object.cpp index 3f8845419..01b750122 100644 --- a/JSON/src/Object.cpp +++ b/JSON/src/Object.cpp @@ -40,10 +40,12 @@ #include #include -namespace Poco -{ -namespace JSON -{ + +using Poco::Dynamic::Var; + + +namespace Poco { +namespace JSON { Object::Object() @@ -51,19 +53,21 @@ Object::Object() } + Object::Object(const Object& copy) : _values(copy._values) { } + Object::~Object() { } -DynamicAny Object::get(const std::string& key) const +Var Object::get(const std::string& key) const { - DynamicAny value; + Var value; ValueMap::const_iterator it = _values.find(key); if ( it != _values.end() ) @@ -78,7 +82,7 @@ Array::Ptr Object::getArray(const std::string& key) const { Array::Ptr result; - DynamicAny value = get(key); + Var value = get(key); if ( value.type() == typeid(Array::Ptr) ) { result = value.extract(); @@ -91,7 +95,7 @@ Object::Ptr Object::getObject(const std::string& key) const { Object::Ptr result; - DynamicAny value = get(key); + Var value = get(key); if ( value.type() == typeid(Object::Ptr) ) { result = value.extract(); diff --git a/JSON/src/Parser.cpp b/JSON/src/Parser.cpp index a867b0795..64502f406 100644 --- a/JSON/src/Parser.cpp +++ b/JSON/src/Parser.cpp @@ -1,694 +1,694 @@ -// -// Parser.cpp -// -// $Id$ -// -// Library: JSON -// Package: JSON -// Module: Parser -// -// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. -// and Contributors. -// -// Permission is hereby granted, free of charge, to any person or organization -// obtaining a copy of the software and accompanying documentation covered by -// this license (the "Software") to use, reproduce, display, distribute, -// execute, and transmit the Software, and to prepare derivative works of the -// Software, and to permit third-parties to whom the Software is furnished to -// do so, all subject to the following: -// -// The copyright notices in the Software and this entire statement, including -// the above license grant, this restriction and the following disclaimer, -// must be included in all copies of the Software, in whole or in part, and -// all derivative works of the Software, unless such copies or derivative -// works are solely in the form of machine-executable object code generated by -// a source language processor. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// - -#include "Poco/Ascii.h" -#include "Poco/Token.h" -#include "Poco/JSON/Parser.h" -#include "Poco/JSON/JSONException.h" +// +// Parser.cpp +// +// $Id$ +// +// Library: JSON +// Package: JSON +// Module: Parser +// +// Copyright (c) 2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + +#include "Poco/Ascii.h" +#include "Poco/Token.h" +#include "Poco/JSON/Parser.h" +#include "Poco/JSON/JSONException.h" #undef min #undef max -#include - - -namespace Poco -{ -namespace JSON -{ - - -class SeparatorToken: public Token -{ -public: - SeparatorToken() - { - } - - virtual ~SeparatorToken() - { - } - - Class tokenClass() const - { - return Token::SEPARATOR_TOKEN; - } - - bool start(char c, std::istream& istr) - { - if ( c == '{' - || c == '}' - || c == ']' - || c == '[' - || c == ',' - || c == ':' ) - { - _value = c; - return true; - } - - if ( c == '\'' ) - { - throw JSONException("Invalid quote found"); - } - - else return false; - } - - void finish(std::istream& istr) - { - } -}; - -class StringToken: public Token -{ -public: - StringToken() - { - } - - virtual ~StringToken() - { - } - - Class tokenClass() const - { - return Token::STRING_LITERAL_TOKEN; - } - - bool start(char c, std::istream& istr) - { - if ( c == '"') - { - _value = ""; // We don't need the quote! - return true; - } - else return false; - } - - void finish(std::istream& istr) - { - int c = istr.get(); - while (c != -1) - { - if ( c == 0 ) - { - throw JSONException("Null byte not allowed"); - } - - if ( 0 < c && c <= 0x1F ) - { - throw JSONException(format("Control character 0x%x not allowed", (unsigned int) c)); - } - - if ( c == '"' ) - break; - - if ( c == '\\' ) // Escaped String - { - c = istr.get(); - switch(c) - { - case '"' : - c = '"'; - break; - case '\\' : - c = '\\'; - break; - case '/' : - c = '/'; - break; - case 'b' : - c = '\b'; - break; - case 'f' : - c = '\f'; - break; - case 'n' : - c = '\n'; - break; - case 'r' : - c = '\r'; - break; - case 't' : - c = '\t'; - break; - case 'u' : // Unicode - { - Poco::Int32 unicode = decodeUnicode(istr); - if ( unicode == 0 ) - { - throw JSONException("\\u0000 is not allowed"); - } - if ( unicode >= 0xD800 && unicode <= 0xDBFF ) - { - c = istr.get(); - if ( c != '\\' ) - { - throw JSONException("Invalid unicode surrogate pair"); - } - c = istr.get(); - if ( c != 'u' ) - { - throw JSONException("Invalid unicode surrogate pair"); - } - Poco::Int32 surrogatePair = decodeUnicode(istr); - if ( 0xDC00 <= surrogatePair && surrogatePair <= 0xDFFF ) - { - unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); - } - else - { - throw JSONException("Invalid unicode surrogate pair"); - } - } - else if ( 0xDC00 <= unicode && unicode <= 0xDFFF ) - { - throw JSONException("Invalid unicode"); - } - c = unicode; - break; - } - default: - { - throw JSONException(format("Invalid escape '%c' character used", (char) c)); - } - } - } - _value += c; - c = istr.get(); - } - - if ( c == -1 ) - { - throw JSONException("Unterminated string found"); - } - } - - Poco::Int32 decodeUnicode(std::istream& istr) - { - Poco::Int32 value = 0; - - for(int i = 0; i < 4; i++) - { - value <<= 4; - int nc = istr.peek(); - if ( nc == -1 ) - { - throw JSONException("Invalid unicode sequence"); - } - istr.get(); // No EOF, so read the character - - if (nc >= '0' && nc <= '9') - value += nc - '0'; - else if (nc >= 'A' && nc <= 'F') - value += 10 + nc - 'A'; - else if (nc >= 'a' && nc <= 'f') - value += 10 + nc - 'a'; - else - throw JSONException("Invalid unicode sequence. Hexadecimal digit expected"); - } - - return value; - } -}; - -class KeywordToken : public Token -{ -public: - KeywordToken() - { - } - - virtual ~KeywordToken() - { - } - - Class tokenClass() const - { - return Token::KEYWORD_TOKEN; - } - - bool start(char c, std::istream& istr) - { - if ( Ascii::isAlpha(c) ) - { - _value = c; - return true; - } - return false; - } - - void finish(std::istream& istr) - { - int c = istr.peek(); - while (c != -1 && Ascii::isAlpha(c) ) - { - istr.get(); - _value += c; - c = istr.peek(); - } - } -}; - -class NumberToken: public Token -{ -public: - NumberToken() : _activeClass(INTEGER_LITERAL_TOKEN) - { - } - - virtual ~NumberToken() - { - } - - Class tokenClass() const - { - return _activeClass; - } - - bool start(char c, std::istream& istr) - { - // Reset the active class to integer - _activeClass = INTEGER_LITERAL_TOKEN; - - if ( c == -1 ) - return false; - - if ( Ascii::isDigit(c) ) - { - if ( c == '0' ) - { - int nc = istr.peek(); - if ( Ascii::isDigit(nc) ) // A digit after a zero is not allowed - { - throw JSONException("Number can't start with a zero"); - } - } - _value = c; - return true; - } - - if ( c == '-' ) - { - _value = c; - - int nc = istr.peek(); - if ( Ascii::isDigit(nc) ) - { - if ( nc == '0' ) - { - _value += '0'; - istr.get(); - - nc = istr.peek(); - if ( Ascii::isDigit(nc) ) // A digit after -0 is not allowed - { - throw JSONException("Number can't start with a zero"); - } - } - return true; - } - } - - return false; - } - - void finish(std::istream& istr) - { - int c; - while( (c = istr.peek()) != -1) - { - if ( Ascii::isDigit(c) ) - { - _value += c; - istr.get(); - } - else - { - switch(c) - { - case '.': // Float - { - if ( _activeClass == Token::FLOAT_LITERAL_TOKEN ) - { - throw JSONException("Invalid float value"); - } - _activeClass = Token::FLOAT_LITERAL_TOKEN; - - _value += c; - istr.get(); - - // After a . we need a digit - c = istr.peek(); - if ( ! Ascii::isDigit(c) ) - { - throw JSONException("Invalid float value"); - } - - break; - } - case 'E': - case 'e': - { - if ( _activeClass == Token::DOUBLE_LITERAL_TOKEN ) - { - throw JSONException("Invalid double value"); - } - _activeClass = Token::DOUBLE_LITERAL_TOKEN; - - // Add the e or E - _value += c; - istr.get(); - - // When the next char is - or + then read the next char - c = istr.peek(); - if ( c == '-' || c == '+' ) - { - _value += c; - istr.get(); - c = istr.peek(); - } - - if ( ! Ascii::isDigit(c) ) - { - throw JSONException("Invalid double value"); - } - - break; - } - default: - return; // End of number token - } - - istr.get(); // If we get here we have a valid character for a number - _value += c; - } - } - } - -private: - - Class _activeClass; - -}; - - -Parser::Parser() : _tokenizer(), _handler(NULL) -{ - _tokenizer.addToken(new WhitespaceToken()); - _tokenizer.addToken(new InvalidToken()); - _tokenizer.addToken(new SeparatorToken()); - _tokenizer.addToken(new StringToken()); - _tokenizer.addToken(new NumberToken()); - _tokenizer.addToken(new KeywordToken()); -} - - -Parser::~Parser() -{ - -} - - -const Token* Parser::nextToken() -{ - const Token* token = _tokenizer.next(); - if ( token->is(Token::EOF_TOKEN) ) - { - throw JSONException("Unexpected EOF found"); - } - return token; -} - - -void Parser::parse(std::istream& in) -{ - _tokenizer.attachToStream(in); - const Token* token = nextToken(); - - if ( token->is(Token::SEPARATOR_TOKEN) ) - { - // This must be a { or a [ - if ( token->asChar() == '{' ) - { - readObject(); - } - else if ( token->asChar() == '[' ) - { - readArray(); - } - else - { - throw JSONException(format("Invalid separator '%c' found. Expecting { or [", token->asChar())); - } - token = _tokenizer.next(); - if ( ! token->is(Token::EOF_TOKEN) ) - { - throw JSONException(format("EOF expected but found '%s'", token->asString())); - } - } - else - { - throw JSONException(format("Invalid token '%s' found. Expecting { or [", token->asString())); - } -} - - -void Parser::readObject() -{ - if ( _handler != NULL ) - { - _handler->startObject(); - } - - if ( readRow(true) ) // First call is special: check for empty object - { - while(readRow()); - } - - if ( _handler != NULL ) - { - _handler->endObject(); - } -} - - -bool Parser::readRow(bool firstCall) -{ - const Token* token = nextToken(); - - if ( firstCall && token->tokenClass() == Token::SEPARATOR_TOKEN && token->asChar() == '}' ) - { - return false; // End of object is possible for an empty object - } - - if ( token->tokenClass() == Token::STRING_LITERAL_TOKEN ) - { - std::string propertyName = token->tokenString(); - if ( _handler != NULL ) - { - _handler->key(propertyName); - } - - token = nextToken(); - - if ( token->is(Token::SEPARATOR_TOKEN) - && token->asChar() == ':' ) - { - readValue(nextToken()); - - token = nextToken(); - - if ( token->is(Token::SEPARATOR_TOKEN) ) - { - if ( token->asChar() == ',' ) - { - return true; // Read next row - } - else if ( token->asChar() == '}' ) - { - return false; // End of object - } - else - { - throw JSONException(format("Invalid separator '%c' found. Expecting , or }", token->asChar())); - } - } - else - { - throw JSONException(format("Invalid token '%s' found. Expecting , or }", token->asString())); - } - } - else - { - throw JSONException(format("Invalid token '%s' found. Expecting :", token->asString())); - } - } - else - { - throw JSONException(format("Invalid token '%s' found. Expecting key", token->asString())); - } -} - - -void Parser::readValue(const Token* token) -{ - switch(token->tokenClass()) - { - case Token::INTEGER_LITERAL_TOKEN: - if ( _handler != NULL ) - { - int value = token->asInteger(); -#if defined(POCO_HAVE_INT64) - if ( value == std::numeric_limits::max() - || value == std::numeric_limits::min() ) - { - _handler->value(NumberParser::parse64(token->asString())); - } - else - { - _handler->value(token->asInteger()); - } -#else - _handle->value(value); -#endif - } - break; - case Token::KEYWORD_TOKEN: - { - if ( token->tokenString().compare("null") == 0 ) - { - if ( _handler != NULL ) - { - _handler->null(); - } - } - else if ( token->tokenString().compare("true") == 0 ) - { - if ( _handler != NULL ) - { - _handler->value(true); - } - } - else if ( token->tokenString().compare("false") == 0 ) - { - if ( _handler != NULL ) - { - _handler->value(false); - } - } - else - { - throw JSONException(format("Invalid keyword '%s' found", token->asString())); - } - break; - } - case Token::FLOAT_LITERAL_TOKEN: - // Fall through - case Token::DOUBLE_LITERAL_TOKEN: - if ( _handler != NULL ) - { - _handler->value(token->asFloat()); - } - break; - case Token::STRING_LITERAL_TOKEN: - if ( _handler != NULL ) - { - _handler->value(token->tokenString()); - } - break; - case Token::SEPARATOR_TOKEN: - { - if ( token->asChar() == '{' ) - { - readObject(); - } - else if ( token->asChar() == '[' ) - { - readArray(); - } - break; - } - } -} - - -void Parser::readArray() -{ - if ( _handler != NULL ) - { - _handler->startArray(); - } - - if ( readElements(true) ) // First call is special: check for empty array - { - while(readElements()); - } - - if ( _handler != NULL ) - { - _handler->endArray(); - } -} - - -bool Parser::readElements(bool firstCall) -{ - const Token* token = nextToken(); - - if ( firstCall && token->is(Token::SEPARATOR_TOKEN) && token->asChar() == ']' ) - { - // End of array is possible for an empty array - return false; - } - - readValue(token); - - token = nextToken(); - - if ( token->is(Token::SEPARATOR_TOKEN) ) - { - if ( token->asChar() == ']' ) - return false; // End of array - - if ( token->asChar() == ',' ) - return true; - - throw JSONException(format("Invalid separator '%c' found. Expecting , or ]", token->asChar())); - } - - throw JSONException(format("Invalid token '%s' found.", token->asString())); -} - -}} +#include + + +namespace Poco +{ +namespace JSON +{ + + +class SeparatorToken: public Token +{ +public: + SeparatorToken() + { + } + + virtual ~SeparatorToken() + { + } + + Class tokenClass() const + { + return Token::SEPARATOR_TOKEN; + } + + bool start(char c, std::istream& istr) + { + if ( c == '{' + || c == '}' + || c == ']' + || c == '[' + || c == ',' + || c == ':' ) + { + _value = c; + return true; + } + + if ( c == '\'' ) + { + throw JSONException("Invalid quote found"); + } + + else return false; + } + + void finish(std::istream& istr) + { + } +}; + +class StringToken: public Token +{ +public: + StringToken() + { + } + + virtual ~StringToken() + { + } + + Class tokenClass() const + { + return Token::STRING_LITERAL_TOKEN; + } + + bool start(char c, std::istream& istr) + { + if ( c == '"') + { + _value = ""; // We don't need the quote! + return true; + } + else return false; + } + + void finish(std::istream& istr) + { + int c = istr.get(); + while (c != -1) + { + if ( c == 0 ) + { + throw JSONException("Null byte not allowed"); + } + + if ( 0 < c && c <= 0x1F ) + { + throw JSONException(format("Control character 0x%x not allowed", (unsigned int) c)); + } + + if ( c == '"' ) + break; + + if ( c == '\\' ) // Escaped String + { + c = istr.get(); + switch(c) + { + case '"' : + c = '"'; + break; + case '\\' : + c = '\\'; + break; + case '/' : + c = '/'; + break; + case 'b' : + c = '\b'; + break; + case 'f' : + c = '\f'; + break; + case 'n' : + c = '\n'; + break; + case 'r' : + c = '\r'; + break; + case 't' : + c = '\t'; + break; + case 'u' : // Unicode + { + Poco::Int32 unicode = decodeUnicode(istr); + if ( unicode == 0 ) + { + throw JSONException("\\u0000 is not allowed"); + } + if ( unicode >= 0xD800 && unicode <= 0xDBFF ) + { + c = istr.get(); + if ( c != '\\' ) + { + throw JSONException("Invalid unicode surrogate pair"); + } + c = istr.get(); + if ( c != 'u' ) + { + throw JSONException("Invalid unicode surrogate pair"); + } + Poco::Int32 surrogatePair = decodeUnicode(istr); + if ( 0xDC00 <= surrogatePair && surrogatePair <= 0xDFFF ) + { + unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); + } + else + { + throw JSONException("Invalid unicode surrogate pair"); + } + } + else if ( 0xDC00 <= unicode && unicode <= 0xDFFF ) + { + throw JSONException("Invalid unicode"); + } + c = unicode; + break; + } + default: + { + throw JSONException(format("Invalid escape '%c' character used", (char) c)); + } + } + } + _value += c; + c = istr.get(); + } + + if ( c == -1 ) + { + throw JSONException("Unterminated string found"); + } + } + + Poco::Int32 decodeUnicode(std::istream& istr) + { + Poco::Int32 value = 0; + + for(int i = 0; i < 4; i++) + { + value <<= 4; + int nc = istr.peek(); + if ( nc == -1 ) + { + throw JSONException("Invalid unicode sequence"); + } + istr.get(); // No EOF, so read the character + + if (nc >= '0' && nc <= '9') + value += nc - '0'; + else if (nc >= 'A' && nc <= 'F') + value += 10 + nc - 'A'; + else if (nc >= 'a' && nc <= 'f') + value += 10 + nc - 'a'; + else + throw JSONException("Invalid unicode sequence. Hexadecimal digit expected"); + } + + return value; + } +}; + +class KeywordToken : public Token +{ +public: + KeywordToken() + { + } + + virtual ~KeywordToken() + { + } + + Class tokenClass() const + { + return Token::KEYWORD_TOKEN; + } + + bool start(char c, std::istream& istr) + { + if ( Ascii::isAlpha(c) ) + { + _value = c; + return true; + } + return false; + } + + void finish(std::istream& istr) + { + int c = istr.peek(); + while (c != -1 && Ascii::isAlpha(c) ) + { + istr.get(); + _value += c; + c = istr.peek(); + } + } +}; + +class NumberToken: public Token +{ +public: + NumberToken() : _activeClass(INTEGER_LITERAL_TOKEN) + { + } + + virtual ~NumberToken() + { + } + + Class tokenClass() const + { + return _activeClass; + } + + bool start(char c, std::istream& istr) + { + // Reset the active class to integer + _activeClass = INTEGER_LITERAL_TOKEN; + + if ( c == -1 ) + return false; + + if ( Ascii::isDigit(c) ) + { + if ( c == '0' ) + { + int nc = istr.peek(); + if ( Ascii::isDigit(nc) ) // A digit after a zero is not allowed + { + throw JSONException("Number can't start with a zero"); + } + } + _value = c; + return true; + } + + if ( c == '-' ) + { + _value = c; + + int nc = istr.peek(); + if ( Ascii::isDigit(nc) ) + { + if ( nc == '0' ) + { + _value += '0'; + istr.get(); + + nc = istr.peek(); + if ( Ascii::isDigit(nc) ) // A digit after -0 is not allowed + { + throw JSONException("Number can't start with a zero"); + } + } + return true; + } + } + + return false; + } + + void finish(std::istream& istr) + { + int c; + while( (c = istr.peek()) != -1) + { + if ( Ascii::isDigit(c) ) + { + _value += c; + istr.get(); + } + else + { + switch(c) + { + case '.': // Float + { + if ( _activeClass == Token::FLOAT_LITERAL_TOKEN ) + { + throw JSONException("Invalid float value"); + } + _activeClass = Token::FLOAT_LITERAL_TOKEN; + + _value += c; + istr.get(); + + // After a . we need a digit + c = istr.peek(); + if ( ! Ascii::isDigit(c) ) + { + throw JSONException("Invalid float value"); + } + + break; + } + case 'E': + case 'e': + { + if ( _activeClass == Token::DOUBLE_LITERAL_TOKEN ) + { + throw JSONException("Invalid double value"); + } + _activeClass = Token::DOUBLE_LITERAL_TOKEN; + + // Add the e or E + _value += c; + istr.get(); + + // When the next char is - or + then read the next char + c = istr.peek(); + if ( c == '-' || c == '+' ) + { + _value += c; + istr.get(); + c = istr.peek(); + } + + if ( ! Ascii::isDigit(c) ) + { + throw JSONException("Invalid double value"); + } + + break; + } + default: + return; // End of number token + } + + istr.get(); // If we get here we have a valid character for a number + _value += c; + } + } + } + +private: + + Class _activeClass; + +}; + + +Parser::Parser() : _tokenizer(), _handler(NULL) +{ + _tokenizer.addToken(new WhitespaceToken()); + _tokenizer.addToken(new InvalidToken()); + _tokenizer.addToken(new SeparatorToken()); + _tokenizer.addToken(new StringToken()); + _tokenizer.addToken(new NumberToken()); + _tokenizer.addToken(new KeywordToken()); +} + + +Parser::~Parser() +{ + +} + + +const Token* Parser::nextToken() +{ + const Token* token = _tokenizer.next(); + if ( token->is(Token::EOF_TOKEN) ) + { + throw JSONException("Unexpected EOF found"); + } + return token; +} + + +void Parser::parse(std::istream& in) +{ + _tokenizer.attachToStream(in); + const Token* token = nextToken(); + + if ( token->is(Token::SEPARATOR_TOKEN) ) + { + // This must be a { or a [ + if ( token->asChar() == '{' ) + { + readObject(); + } + else if ( token->asChar() == '[' ) + { + readArray(); + } + else + { + throw JSONException(format("Invalid separator '%c' found. Expecting { or [", token->asChar())); + } + token = _tokenizer.next(); + if ( ! token->is(Token::EOF_TOKEN) ) + { + throw JSONException(format("EOF expected but found '%s'", token->asString())); + } + } + else + { + throw JSONException(format("Invalid token '%s' found. Expecting { or [", token->asString())); + } +} + + +void Parser::readObject() +{ + if ( _handler != NULL ) + { + _handler->startObject(); + } + + if ( readRow(true) ) // First call is special: check for empty object + { + while(readRow()); + } + + if ( _handler != NULL ) + { + _handler->endObject(); + } +} + + +bool Parser::readRow(bool firstCall) +{ + const Token* token = nextToken(); + + if ( firstCall && token->tokenClass() == Token::SEPARATOR_TOKEN && token->asChar() == '}' ) + { + return false; // End of object is possible for an empty object + } + + if ( token->tokenClass() == Token::STRING_LITERAL_TOKEN ) + { + std::string propertyName = token->tokenString(); + if ( _handler != NULL ) + { + _handler->key(propertyName); + } + + token = nextToken(); + + if ( token->is(Token::SEPARATOR_TOKEN) + && token->asChar() == ':' ) + { + readValue(nextToken()); + + token = nextToken(); + + if ( token->is(Token::SEPARATOR_TOKEN) ) + { + if ( token->asChar() == ',' ) + { + return true; // Read next row + } + else if ( token->asChar() == '}' ) + { + return false; // End of object + } + else + { + throw JSONException(format("Invalid separator '%c' found. Expecting , or }", token->asChar())); + } + } + else + { + throw JSONException(format("Invalid token '%s' found. Expecting , or }", token->asString())); + } + } + else + { + throw JSONException(format("Invalid token '%s' found. Expecting :", token->asString())); + } + } + else + { + throw JSONException(format("Invalid token '%s' found. Expecting key", token->asString())); + } +} + + +void Parser::readValue(const Token* token) +{ + switch(token->tokenClass()) + { + case Token::INTEGER_LITERAL_TOKEN: + if ( _handler != NULL ) + { + int value = token->asInteger(); +#if defined(POCO_HAVE_INT64) + if ( value == std::numeric_limits::max() + || value == std::numeric_limits::min() ) + { + _handler->value(NumberParser::parse64(token->asString())); + } + else + { + _handler->value(token->asInteger()); + } +#else + _handle->value(value); +#endif + } + break; + case Token::KEYWORD_TOKEN: + { + if ( token->tokenString().compare("null") == 0 ) + { + if ( _handler != NULL ) + { + _handler->null(); + } + } + else if ( token->tokenString().compare("true") == 0 ) + { + if ( _handler != NULL ) + { + _handler->value(true); + } + } + else if ( token->tokenString().compare("false") == 0 ) + { + if ( _handler != NULL ) + { + _handler->value(false); + } + } + else + { + throw JSONException(format("Invalid keyword '%s' found", token->asString())); + } + break; + } + case Token::FLOAT_LITERAL_TOKEN: + // Fall through + case Token::DOUBLE_LITERAL_TOKEN: + if ( _handler != NULL ) + { + _handler->value(token->asFloat()); + } + break; + case Token::STRING_LITERAL_TOKEN: + if ( _handler != NULL ) + { + _handler->value(token->tokenString()); + } + break; + case Token::SEPARATOR_TOKEN: + { + if ( token->asChar() == '{' ) + { + readObject(); + } + else if ( token->asChar() == '[' ) + { + readArray(); + } + break; + } + } +} + + +void Parser::readArray() +{ + if ( _handler != NULL ) + { + _handler->startArray(); + } + + if ( readElements(true) ) // First call is special: check for empty array + { + while(readElements()); + } + + if ( _handler != NULL ) + { + _handler->endArray(); + } +} + + +bool Parser::readElements(bool firstCall) +{ + const Token* token = nextToken(); + + if ( firstCall && token->is(Token::SEPARATOR_TOKEN) && token->asChar() == ']' ) + { + // End of array is possible for an empty array + return false; + } + + readValue(token); + + token = nextToken(); + + if ( token->is(Token::SEPARATOR_TOKEN) ) + { + if ( token->asChar() == ']' ) + return false; // End of array + + if ( token->asChar() == ',' ) + return true; + + throw JSONException(format("Invalid separator '%c' found. Expecting , or ]", token->asChar())); + } + + throw JSONException(format("Invalid token '%s' found.", token->asString())); +} + +}} diff --git a/JSON/src/Query.cpp b/JSON/src/Query.cpp index 7f2cfe191..931ea6035 100644 --- a/JSON/src/Query.cpp +++ b/JSON/src/Query.cpp @@ -41,24 +41,29 @@ #include "Poco/JSON/Query.h" -namespace Poco -{ -namespace JSON -{ -Query::Query(const DynamicAny& source) : _source(source) +using Poco::Dynamic::Var; + + +namespace Poco { +namespace JSON { + + +Query::Query(const Var& source) : _source(source) { } + Query::~Query() { } + Object::Ptr Query::findObject(const std::string& path) const { Object::Ptr obj; - DynamicAny result = find(path); + Var result = find(path); if ( result.type() == typeid(Object::Ptr) ) { obj = result.extract(); @@ -66,10 +71,11 @@ Object::Ptr Query::findObject(const std::string& path) const return obj; } + Array::Ptr Query::findArray(const std::string& path) const { Array::Ptr arr; - DynamicAny result = find(path); + Var result = find(path); if ( result.type() == typeid(Array::Ptr) ) { arr = result.extract(); @@ -78,9 +84,9 @@ Array::Ptr Query::findArray(const std::string& path) const } -DynamicAny Query::find(const std::string& path) const +Var Query::find(const std::string& path) const { - DynamicAny result = _source; + Var result = _source; StringTokenizer tokenizer(path, "."); for(StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++) { @@ -138,4 +144,5 @@ DynamicAny Query::find(const std::string& path) const return result; } + }} // Namespace Poco::JSON diff --git a/JSON/src/Stringifier.cpp b/JSON/src/Stringifier.cpp index 6211311a4..6c3834236 100644 --- a/JSON/src/Stringifier.cpp +++ b/JSON/src/Stringifier.cpp @@ -39,12 +39,15 @@ #include "Poco/JSON/Array.h" #include "Poco/JSON/Object.h" -namespace Poco -{ -namespace JSON -{ -void Stringifier::stringify(const DynamicAny& any, std::ostream& out, unsigned int indent) +using Poco::Dynamic::Var; + + +namespace Poco { +namespace JSON { + + +void Stringifier::stringify(const Var& any, std::ostream& out, unsigned int indent) { if ( any.type() == typeid(Object::Ptr) ) { @@ -111,4 +114,5 @@ void Stringifier::stringify(const DynamicAny& any, std::ostream& out, unsigned i } } + }} // Namespace Poco::JSON diff --git a/JSON/src/Template.cpp b/JSON/src/Template.cpp index f1c1c6def..70d8ef2af 100644 --- a/JSON/src/Template.cpp +++ b/JSON/src/Template.cpp @@ -38,13 +38,17 @@ #include "Poco/JSON/TemplateCache.h" #include "Poco/JSON/Query.h" -namespace Poco -{ -namespace JSON -{ + +using Poco::Dynamic::Var; + + +namespace Poco { +namespace JSON { + POCO_IMPLEMENT_EXCEPTION(JSONTemplateException, Exception, "Template Exception") + class Part { public: @@ -58,11 +62,12 @@ public: { } - virtual void render(const DynamicAny& data, std::ostream& out) const = 0; + virtual void render(const Var& data, std::ostream& out) const = 0; typedef std::vector > VectorParts; }; + class StringPart : public Part { public: @@ -81,7 +86,7 @@ public: } - void render(const DynamicAny& data, std::ostream& out) const + void render(const Var& data, std::ostream& out) const { out << _content; } @@ -103,6 +108,7 @@ private: std::string _content; }; + class MultiPart : public Part { public: @@ -125,7 +131,7 @@ public: } - void render(const DynamicAny& data, std::ostream& out) const + void render(const Var& data, std::ostream& out) const { for(VectorParts::const_iterator it = _parts.begin(); it != _parts.end(); ++it) { @@ -138,6 +144,7 @@ protected: VectorParts _parts; }; + class EchoPart : public Part { public: @@ -151,10 +158,10 @@ public: } - void render(const DynamicAny& data, std::ostream& out) const + void render(const Var& data, std::ostream& out) const { Query query(data); - DynamicAny value = query.find(_query); + Var value = query.find(_query); if ( ! value.isEmpty() ) { @@ -167,6 +174,7 @@ private: std::string _query; }; + class LogicQuery { public: @@ -178,18 +186,18 @@ public: { } - virtual bool apply(const DynamicAny& data) const + virtual bool apply(const Var& data) const { bool logic = false; Query query(data); - DynamicAny value = query.find(_queryString); + Var value = query.find(_queryString); if ( ! value.isEmpty() ) // When empty, logic will be false { if ( value.isString() ) // An empty string must result in false, otherwise true - // Which is not the case when we convert to bool with DynamicAny + // Which is not the case when we convert to bool with Var { std::string s = value.convert(); logic = ! s.empty(); @@ -198,7 +206,7 @@ public: { // All other values, try to convert to bool // An empty object or array will turn into false - // all other values depend on the convert<> in DynamicAny + // all other values depend on the convert<> in Var logic = value.convert(); } } @@ -210,6 +218,7 @@ protected: std::string _queryString; }; + class LogicExistQuery : public LogicQuery { public: @@ -221,10 +230,10 @@ public: { } - virtual bool apply(const DynamicAny& data) const + virtual bool apply(const Var& data) const { Query query(data); - DynamicAny value = query.find(_queryString); + Var value = query.find(_queryString); return !value.isEmpty(); } @@ -242,7 +251,7 @@ public: { } - virtual bool apply(const DynamicAny& data) const + virtual bool apply(const Var& data) const { return true; } @@ -273,7 +282,7 @@ public: _queries.push_back(new LogicElseQuery()); } - void render(const DynamicAny& data, std::ostream& out) const + void render(const Var& data, std::ostream& out) const { int count = 0; for(std::vector >::const_iterator it = _queries.begin(); it != _queries.end(); ++it, ++count) @@ -291,6 +300,7 @@ private: std::vector > _queries; }; + class LoopPart : public MultiPart { public: @@ -304,7 +314,7 @@ public: } - void render(const DynamicAny& data, std::ostream& out) const + void render(const Var& data, std::ostream& out) const { Query query(data); @@ -316,7 +326,7 @@ public: { for(int i = 0; i < array->size(); i++) { - DynamicAny value = array->get(i); + Var value = array->get(i); dataObject->set(_name, value); MultiPart::render(data, out); } @@ -361,7 +371,7 @@ public: } - void render(const DynamicAny& data, std::ostream& out) const + void render(const Var& data, std::ostream& out) const { TemplateCache* cache = TemplateCache::instance(); if ( cache == NULL ) @@ -383,12 +393,14 @@ private: }; + Template::Template(const Path& templatePath) : _parts(NULL) , _templatePath(templatePath) { } + Template::Template() : _parts(NULL) { @@ -713,6 +725,7 @@ void Template::readWhiteSpace(std::istream& in) } } + std::string Template::readString(std::istream& in) { std::string str; @@ -728,9 +741,11 @@ std::string Template::readString(std::istream& in) return str; } -void Template::render(const DynamicAny& data, std::ostream& out) const + +void Template::render(const Var& data, std::ostream& out) const { _parts->render(data, out); } + }} // Namespace Poco::JSON diff --git a/JSON/src/TemplateCache.cpp b/JSON/src/TemplateCache.cpp index 997336e1d..409431792 100644 --- a/JSON/src/TemplateCache.cpp +++ b/JSON/src/TemplateCache.cpp @@ -94,7 +94,7 @@ Template::Ptr TemplateCache::getTemplate(const Path& path) tpl->parse(); _cache[templatePathname] = tpl; } - catch(JSONTemplateException jte) + catch(JSONTemplateException& jte) { if ( _logger ) { @@ -128,7 +128,7 @@ Template::Ptr TemplateCache::getTemplate(const Path& path) tpl->parse(); _cache[templatePathname] = tpl; } - catch(JSONTemplateException jte) + catch(JSONTemplateException& jte) { if ( _logger ) { diff --git a/JSON/testsuite/src/JSONTest.cpp b/JSON/testsuite/src/JSONTest.cpp index fe2de30b6..8e24480c3 100644 --- a/JSON/testsuite/src/JSONTest.cpp +++ b/JSON/testsuite/src/JSONTest.cpp @@ -40,7 +40,6 @@ #include "Poco/JSON/JSONException.h" #include "Poco/JSON/Stringifier.h" #include "Poco/JSON/DefaultHandler.h" -//#include "Poco/Util/JSONConfiguration.h" #include "Poco/JSON/Template.h" #include "Poco/Path.h" @@ -51,6 +50,9 @@ #include +using namespace Poco::JSON; +using namespace Poco::Dynamic; + JSONTest::JSONTest(const std::string& name): CppUnit::TestCase("JSON") { @@ -77,26 +79,26 @@ void JSONTest::tearDown() void JSONTest::testNullProperty() { std::string json = "{ \"test\" : null }"; - Poco::JSON::Parser parser; + Parser parser; - Poco::DynamicAny result; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); + Object::Ptr object = result.extract(); assert(object->isNull("test")); - Poco::DynamicAny test = object->get("test"); + Var test = object->get("test"); assert(test.isEmpty()); } @@ -104,26 +106,26 @@ void JSONTest::testNullProperty() void JSONTest::testTrueProperty() { std::string json = "{ \"test\" : true }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); + Object::Ptr object = result.extract(); + Var test = object->get("test"); assert(test.type() == typeid(bool)); bool value = test; assert(value); @@ -133,26 +135,26 @@ void JSONTest::testTrueProperty() void JSONTest::testFalseProperty() { std::string json = "{ \"test\" : false }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); + Object::Ptr object = result.extract(); + Var test = object->get("test"); assert(test.type() == typeid(bool)); bool value = test; assert(!value); @@ -162,26 +164,26 @@ void JSONTest::testFalseProperty() void JSONTest::testNumberProperty() { std::string json = "{ \"test\" : 1969 }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); + Object::Ptr object = result.extract(); + Var test = object->get("test"); assert(test.isInteger()); int value = test; assert(value == 1969); @@ -191,26 +193,26 @@ void JSONTest::testNumberProperty() void JSONTest::testStringProperty() { std::string json = "{ \"test\" : \"value\" }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); + Object::Ptr object = result.extract(); + Var test = object->get("test"); assert(test.isString()); std::string value = test; assert(value.compare("value") == 0); @@ -220,25 +222,25 @@ void JSONTest::testStringProperty() void JSONTest::testEmptyObject() { std::string json = "{}"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); + Object::Ptr object = result.extract(); assert(object->size() == 0); } @@ -246,26 +248,26 @@ void JSONTest::testEmptyObject() void JSONTest::testDoubleProperty() { std::string json = "{ \"test\" : 123.45 }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); + Object::Ptr object = result.extract(); + Var test = object->get("test"); assert(test.isNumeric()); double value = test; assert(value == 123.45); @@ -275,26 +277,26 @@ void JSONTest::testDoubleProperty() void JSONTest::testDouble2Property() { std::string json = "{ \"test\" : 12e34 }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); + Object::Ptr object = result.extract(); + Var test = object->get("test"); assert(test.isNumeric()); double value = test; assert(value == 12e34); @@ -304,26 +306,26 @@ void JSONTest::testDouble2Property() void JSONTest::testDouble3Property() { std::string json = "{ \"test\" : 12e-34 }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); + Object::Ptr object = result.extract(); + Var test = object->get("test"); assert(test.isNumeric()); double value = test; assert(value == 12e-34); @@ -333,28 +335,28 @@ void JSONTest::testDouble3Property() void JSONTest::testObjectProperty() { std::string json = "{ \"test\" : { \"property\" : \"value\" } }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); - Poco::DynamicAny test = object->get("test"); - assert(test.type() == typeid(Poco::JSON::Object::Ptr)); - object = test.extract(); + Object::Ptr object = result.extract(); + Var test = object->get("test"); + assert(test.type() == typeid(Object::Ptr)); + object = test.extract(); test = object->get("property"); assert(test.isString()); @@ -366,25 +368,25 @@ void JSONTest::testObjectProperty() void JSONTest::testEmptyArray() { std::string json = "[]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); + Array::Ptr array = result.extract(); assert(array->size() == 0); } @@ -392,25 +394,25 @@ void JSONTest::testEmptyArray() void JSONTest::testNestedArray() { std::string json = "[[[[]]]]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); + Array::Ptr array = result.extract(); assert(array->size() == 1); } @@ -418,27 +420,27 @@ void JSONTest::testNestedArray() void JSONTest::testNullElement() { std::string json = "[ null ]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); + Array::Ptr array = result.extract(); assert(array->isNull(0)); - Poco::DynamicAny test = array->get(0); + Var test = array->get(0); assert(test.isEmpty()); } @@ -446,26 +448,26 @@ void JSONTest::testNullElement() void JSONTest::testTrueElement() { std::string json = "[ true ]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); - Poco::DynamicAny test = array->get(0); + Array::Ptr array = result.extract(); + Var test = array->get(0); assert(test.type() == typeid(bool)); bool value = test; assert(value); @@ -475,26 +477,26 @@ void JSONTest::testTrueElement() void JSONTest::testFalseElement() { std::string json = "[ false ]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); - Poco::DynamicAny test = array->get(0); + Array::Ptr array = result.extract(); + Var test = array->get(0); assert(test.type() == typeid(bool)); bool value = test; assert(!value); @@ -504,26 +506,26 @@ void JSONTest::testFalseElement() void JSONTest::testNumberElement() { std::string json = "[ 1969 ]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); - Poco::DynamicAny test = array->get(0); + Array::Ptr array = result.extract(); + Var test = array->get(0); assert(test.isInteger()); int value = test; assert(value == 1969); @@ -533,26 +535,26 @@ void JSONTest::testNumberElement() void JSONTest::testStringElement() { std::string json = "[ \"value\" ]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); - Poco::DynamicAny test = array->get(0); + Array::Ptr array = result.extract(); + Var test = array->get(0); assert(test.isString()); std::string value = test; assert(value.compare("value") == 0); @@ -562,26 +564,26 @@ void JSONTest::testStringElement() void JSONTest::testEmptyObjectElement() { std::string json = "[{}]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); - Poco::JSON::Object::Ptr object = array->getObject(0); + Array::Ptr array = result.extract(); + Object::Ptr object = array->getObject(0); assert(object->size() == 0); } @@ -589,26 +591,26 @@ void JSONTest::testEmptyObjectElement() void JSONTest::testDoubleElement() { std::string json = "[ 123.45 ]"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Array::Ptr)); + assert(result.type() == typeid(Array::Ptr)); - Poco::JSON::Array::Ptr array = result.extract(); - Poco::DynamicAny test = array->get(0); + Array::Ptr array = result.extract(); + Var test = array->get(0); assert(test.isNumeric()); double value = test; assert(value == 123.45); @@ -618,25 +620,25 @@ void JSONTest::testDoubleElement() void JSONTest::testOptValue() { std::string json = "{ }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Object::Ptr object = result.extract(); + Object::Ptr object = result.extract(); int n = object->optValue("test", 123); assert(n == 123); } @@ -645,25 +647,25 @@ void JSONTest::testOptValue() void JSONTest::testQuery() { std::string json = "{ \"name\" : \"Franky\", \"children\" : [ \"Jonas\", \"Ellen\" ] }"; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(json); result = handler.result(); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { std::cout << jsone.message() << std::endl; assert(false); } - assert(result.type() == typeid(Poco::JSON::Object::Ptr)); + assert(result.type() == typeid(Object::Ptr)); - Poco::JSON::Query query(result); + Query query(result); std::string firstChild = query.findValue("children[0]", ""); assert(firstChild.compare("Jonas") == 0); @@ -689,21 +691,28 @@ void JSONTest::testValidJanssonFiles() Poco::FileInputStream fis(filePath.toString()); std::cout << filePath.toString() << std::endl; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(fis); result = handler.result(); std::cout << "Ok!" << std::endl; } - catch(Poco::JSON::JSONException jsone) + catch(JSONException& jsone) { - // We shouldn't get here. - assert(false); + std::string err = jsone.displayText(); + std::cout << "Failed:" << err << std::endl; + fail (err); + } + catch(Poco::Exception& e) + { + std::string err = e.displayText(); + std::cout << "Failed:" << err << std::endl; + fail (err); } } } @@ -730,28 +739,25 @@ void JSONTest::testInvalidJanssonFiles() Poco::FileInputStream fis(filePath.toString()); std::cout << filePath.toString() << std::endl; - Poco::JSON::Parser parser; - Poco::DynamicAny result; + Parser parser; + Var result; try { - Poco::JSON::DefaultHandler handler; + DefaultHandler handler; parser.setHandler(&handler); parser.parse(fis); result = handler.result(); // We shouldn't get here. std::cout << "We didn't get an exception. This is the result: " << result.convert() << std::endl; - assert(false); + fail(result.convert()); } - catch(Poco::JSON::JSONException jsone) + catch(JSONException&) { - std::cout << "Ok! We got an exception " << jsone.message() << std::endl; continue; - } - catch(Poco::SyntaxException se) - { - std::cout << "Ok! We got an exception " << se.message() << std::endl; } + catch(Poco::SyntaxException&) + { } } } } @@ -760,11 +766,11 @@ void JSONTest::testInvalidJanssonFiles() void JSONTest::testTemplate() { - Poco::JSON::Template tpl; + Template tpl; tpl.parse("Hello world! From \nYou're to old\n"); - Poco::JSON::Object::Ptr data = new Poco::JSON::Object(); - Poco::JSON::Object::Ptr person = new Poco::JSON::Object(); + Object::Ptr data = new Object(); + Object::Ptr person = new Object(); data->set("person", person); person->set("name", "Franky"); person->set("toOld", true); diff --git a/PDF/include/Poco/PDF/PDF.h b/PDF/include/Poco/PDF/PDF.h index 6f97b85e1..86670d220 100644 --- a/PDF/include/Poco/PDF/PDF.h +++ b/PDF/include/Poco/PDF/PDF.h @@ -42,8 +42,12 @@ #define PDF_PDF_INCLUDED #if defined(_MSC_VER) && !defined(POCO_MSVC_SECURE_WARNINGS) && (!defined(_CRT_SECURE_NO_WARNINGS) || !defined(_CRT_SECURE_NO_DEPRECATE)) - #define _CRT_SECURE_NO_WARNINGS - #define _CRT_SECURE_NO_DEPRECATE + #ifndef _CRT_SECURE_NO_WARNINGS + #define _CRT_SECURE_NO_WARNINGS + #endif + #ifndef _CRT_SECURE_NO_DEPRECATE + #define _CRT_SECURE_NO_DEPRECATE + #endif #endif #include "Poco/Foundation.h" diff --git a/Util/src/AbstractConfiguration.cpp b/Util/src/AbstractConfiguration.cpp index 6b979236e..d2baef702 100644 --- a/Util/src/AbstractConfiguration.cpp +++ b/Util/src/AbstractConfiguration.cpp @@ -450,8 +450,8 @@ std::string AbstractConfiguration::uncheckedExpand(const std::string& value) con int AbstractConfiguration::parseInt(const std::string& value) { - if (value.compare(0, 2, "0x") == 0) - return NumberParser::parseHex(value.substr(2)); + if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0)) + return NumberParser::parseHex(value); else return NumberParser::parse(value); } @@ -459,8 +459,8 @@ int AbstractConfiguration::parseInt(const std::string& value) int AbstractConfiguration::parseUInt(const std::string& value) { - if (value.compare(0, 2, "0x") == 0) - return NumberParser::parseHex(value.substr(2)); + if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0)) + return NumberParser::parseHex(value); else return NumberParser::parseUnsigned(value); } @@ -468,8 +468,8 @@ int AbstractConfiguration::parseUInt(const std::string& value) Int64 AbstractConfiguration::parseInt64(const std::string& value) { - if (value.compare(0, 2, "0x") == 0) - return NumberParser::parseHex64(value.substr(2)); + if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0)) + return NumberParser::parseHex64(value); else return NumberParser::parse64(value); } @@ -477,8 +477,8 @@ Int64 AbstractConfiguration::parseInt64(const std::string& value) UInt64 AbstractConfiguration::parseUInt64(const std::string& value) { - if (value.compare(0, 2, "0x") == 0) - return NumberParser::parseHex64(value.substr(2)); + if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0)) + return NumberParser::parseHex64(value); else return NumberParser::parseUnsigned64(value); } diff --git a/Util/testsuite/src/JSONConfigurationTest.cpp b/Util/testsuite/src/JSONConfigurationTest.cpp index ce4f0d9ec..b81bfa80e 100644 --- a/Util/testsuite/src/JSONConfigurationTest.cpp +++ b/Util/testsuite/src/JSONConfigurationTest.cpp @@ -1,56 +1,57 @@ -// -// JSONConfigurationTest.cpp -// -// $Id$ -// -// Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH. -// and Contributors. -// -// Permission is hereby granted, free of charge, to any person or organization -// obtaining a copy of the software and accompanying documentation covered by -// this license (the "Software") to use, reproduce, display, distribute, -// execute, and transmit the Software, and to prepare derivative works of the -// Software, and to permit third-parties to whom the Software is furnished to -// do so, all subject to the following: -// -// The copyright notices in the Software and this entire statement, including -// the above license grant, this restriction and the following disclaimer, -// must be included in all copies of the Software, in whole or in part, and -// all derivative works of the Software, unless such copies or derivative -// works are solely in the form of machine-executable object code generated by -// a source language processor. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT -// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE -// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, -// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -#include "JSONConfigurationTest.h" -#include "CppUnit/TestCaller.h" -#include "CppUnit/TestSuite.h" -#include "Poco/Util/JSONConfiguration.h" -#include "Poco/JSON/JSONException.h" - -using Poco::Util::JSONConfiguration; -using Poco::Util::AbstractConfiguration; -using Poco::AutoPtr; -using Poco::NotImplementedException; -using Poco::NotFoundException; -using Poco::JSON::JSONException; - - -JSONConfigurationTest::JSONConfigurationTest(const std::string& name) : AbstractConfigurationTest(name) -{ -} - - -JSONConfigurationTest::~JSONConfigurationTest() -{ -} - +// +// JSONConfigurationTest.cpp +// +// $Id$ +// +// Copyright (c) 2004-2012, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +#include "JSONConfigurationTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" +#include "Poco/Util/JSONConfiguration.h" +#include "Poco/JSON/JSONException.h" + +using Poco::Util::JSONConfiguration; +using Poco::Util::AbstractConfiguration; +using Poco::AutoPtr; +using Poco::NotImplementedException; +using Poco::NotFoundException; +using Poco::JSON::JSONException; + + +JSONConfigurationTest::JSONConfigurationTest(const std::string& name) : AbstractConfigurationTest(name) +{ +} + + +JSONConfigurationTest::~JSONConfigurationTest() +{ +} + + void JSONConfigurationTest::testLoad() { JSONConfiguration config; @@ -100,29 +101,29 @@ void JSONConfigurationTest::testLoad() } } - -AbstractConfiguration* JSONConfigurationTest::allocConfiguration() const -{ - return new JSONConfiguration; -} - - -void JSONConfigurationTest::setUp() -{ -} - - -void JSONConfigurationTest::tearDown() -{ -} - - -CppUnit::Test* JSONConfigurationTest::suite() -{ - CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONConfigurationTest"); - - AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest); - CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad); - - return pSuite; -} + +AbstractConfiguration* JSONConfigurationTest::allocConfiguration() const +{ + return new JSONConfiguration; +} + + +void JSONConfigurationTest::setUp() +{ +} + + +void JSONConfigurationTest::tearDown() +{ +} + + +CppUnit::Test* JSONConfigurationTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("JSONConfigurationTest"); + + AbstractConfigurationTest_addTests(pSuite, JSONConfigurationTest); + CppUnit_addTest(pSuite, JSONConfigurationTest, testLoad); + + return pSuite; +} diff --git a/buildwin.cmd b/buildwin.cmd index 7973d68e6..9cd3f23f8 100644 --- a/buildwin.cmd +++ b/buildwin.cmd @@ -52,16 +52,16 @@ if "%1"=="" goto usage set VS_VERSION=vs%1 if not defined VCINSTALLDIR ( - if %1==71 (set VSENV="%VS71COMNTOOLS%vsvars32.bat") else ( - if %1==80 (set VSENV="%VS80COMNTOOLS%vsvars32.bat") else ( - if %1==90 (set VSENV="%VS90COMNTOOLS%vsvars32.bat") else ( - if %1==100 (set VSENV="%VS100COMNTOOLS%vsvars32.bat") + if %1==71 (call "%VS71COMNTOOLS%vsvars32.bat") else ( + if %1==80 (call "%VS80COMNTOOLS%vsvars32.bat") else ( + if %1==90 (call "%VS90COMNTOOLS%vsvars32.bat") else ( + if %1==100 (call "%VS100COMNTOOLS%vsvars32.bat") )))) - call %VSENV% + if not defined VSINSTALLDIR ( echo Error: No Visual C++ environment found. echo Please run this script from a Visual Studio Command Prompt - echo or run "%%VSnnCOMNTOOLS%%\vcvars32.bat" first. + echo or run "%%VSnnCOMNTOOLS%%\vsvars32.bat" first. goto :EOF ) )