From 09ab87a28631163a29663a870eb9a3bc2ef95f39 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 25 Jul 2015 16:36:16 +0100 Subject: Tests and fixes over non-utf8 encodings --- libjsonpp/Jamfile.jam | 14 ++++++++++++++ libjsonpp/json.ll | 5 +++-- libjsonpp/testEncoding.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 libjsonpp/testEncoding.cpp diff --git a/libjsonpp/Jamfile.jam b/libjsonpp/Jamfile.jam index d97edea..3bc7916 100644 --- a/libjsonpp/Jamfile.jam +++ b/libjsonpp/Jamfile.jam @@ -55,4 +55,18 @@ run testSerialise ; +run + testEncoding.cpp + pch + : : : + ROOT=\"$(me)\" + BOOST_TEST_DYN_LINK + jsonpp + boost_utf + boost_system + boost_filesystem + : + testEncoding + ; + package.install install : . : : jsonpp : [ glob *.h ] ; diff --git a/libjsonpp/json.ll b/libjsonpp/json.ll index 58dff7a..860b98c 100644 --- a/libjsonpp/json.ll +++ b/libjsonpp/json.ll @@ -152,8 +152,9 @@ escape "\\" %% -json::jsonFlexLexer::jsonFlexLexer(std::istream & in, const std::string &) : - yyFlexLexer(&in, NULL) +json::jsonFlexLexer::jsonFlexLexer(std::istream & in, const std::string & enc) : + yyFlexLexer(&in, NULL), + encoding(enc) { yy_push_state(VALUE); acceptValues.push(boost::bind(&jsonFlexLexer::RootValue, this, _1)); diff --git a/libjsonpp/testEncoding.cpp b/libjsonpp/testEncoding.cpp new file mode 100644 index 0000000..025de2e --- /dev/null +++ b/libjsonpp/testEncoding.cpp @@ -0,0 +1,34 @@ +#include + +#define BOOST_TEST_MODULE encoding +#include + +#include "jsonpp.h" +#include +#include + +#define XSTR(s) STR(s) +#define STR(s) #s +const boost::filesystem::path root(XSTR(ROOT)); + +static +std::string +writeString(const json::Value & v, const std::string & enc) +{ + std::stringstream ss; + json::serializeValue(v, ss, enc); + return ss.str(); +} + +BOOST_AUTO_TEST_CASE( parse_latin1 ) +{ + std::stringstream ss("\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\""); + BOOST_REQUIRE_EQUAL(19, ss.str().length()); + BOOST_REQUIRE_EQUAL("A ÙñîçôÐè string.", boost::get(json::parseValue(ss, "latin1"))); +} + +BOOST_AUTO_TEST_CASE( write_latin1 ) +{ + BOOST_REQUIRE_EQUAL("\"A \xD9\xF1\xEE\xE7\xF4\xD0\xE8 string.\"", writeString(Glib::ustring("A ÙñîçôÐè string."), "latin1")); +} + -- cgit v1.2.3