added test cases

This commit is contained in:
Niels 2016-04-30 00:03:47 +02:00
parent 82f5332cf4
commit 1d3b4dd158
7 changed files with 160 additions and 7 deletions

View file

@ -5,10 +5,12 @@ All notable changes to this project will be documented in this file. This projec
[Full Changelog](https://github.com/nlohmann/json/compare/v1.1.0...HEAD)
- Additional integration options [\#237](https://github.com/nlohmann/json/issues/237)
- Can't use basic\_json::iterator as a base iterator for std::move\_iterator [\#233](https://github.com/nlohmann/json/issues/233)
- Provide a FAQ [\#163](https://github.com/nlohmann/json/issues/163)
- Create PULL\_REQUEST\_TEMPLATE.md [\#213](https://github.com/nlohmann/json/pull/213) ([whackashoe](https://github.com/whackashoe))
- fixed noexcept; added constexpr [\#208](https://github.com/nlohmann/json/pull/208) ([nlohmann](https://github.com/nlohmann))
- Add support for afl-fuzz testing [\#207](https://github.com/nlohmann/json/pull/207) ([msm-](https://github.com/msm-))
- Add support for afl-fuzz testing [\#207](https://github.com/nlohmann/json/pull/207) ([mykter](https://github.com/mykter))
- Issue \#178 - Extending support to full uint64\_t/int64\_t range and unsigned type \(updated\) [\#193](https://github.com/nlohmann/json/pull/193) ([twelsby](https://github.com/twelsby))
- double values are serialized with commas as decimal points [\#228](https://github.com/nlohmann/json/issues/228)
@ -26,6 +28,7 @@ All notable changes to this project will be documented in this file. This projec
- Conflicting typedef of ssize\_t on Windows 32 bit when using Boost.Python [\#204](https://github.com/nlohmann/json/issues/204)
- Integer conversion to unsigned [\#178](https://github.com/nlohmann/json/issues/178)
- Implement additional integration options [\#238](https://github.com/nlohmann/json/pull/238) ([robertmrk](https://github.com/robertmrk))
- make serialization locale-independent [\#232](https://github.com/nlohmann/json/pull/232) ([nlohmann](https://github.com/nlohmann))
- fixes \#223 by updating README.md [\#227](https://github.com/nlohmann/json/pull/227) ([kevin--](https://github.com/kevin--))
- Use namespace std for int64\_t and uint64\_t [\#226](https://github.com/nlohmann/json/pull/226) ([lv-zheng](https://github.com/lv-zheng))

View file

@ -0,0 +1,34 @@
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// different JSON Pointers
json::json_pointer ptr1("");
json::json_pointer ptr2("/foo");
json::json_pointer ptr3("/foo/0");
json::json_pointer ptr4("/");
json::json_pointer ptr5("/a~1b");
json::json_pointer ptr6("/c%d");
json::json_pointer ptr7("/e^f");
json::json_pointer ptr8("/g|h");
json::json_pointer ptr9("/i\\j");
json::json_pointer ptr10("/k\"l");
json::json_pointer ptr11("/ ");
json::json_pointer ptr12("/m~0n");
std::cout << ptr1.to_string() << '\n'
<< ptr2.to_string() << '\n'
<< ptr3.to_string() << '\n'
<< ptr4.to_string() << '\n'
<< ptr5.to_string() << '\n'
<< ptr6.to_string() << '\n'
<< ptr7.to_string() << '\n'
<< ptr8.to_string() << '\n'
<< ptr9.to_string() << '\n'
<< ptr10.to_string() << '\n'
<< ptr11.to_string() << '\n'
<< ptr12.to_string() << std::endl;
}

View file

@ -0,0 +1 @@
<a target="_blank" href="http://melpon.org/wandbox/permlink/O4FbKx0TbZioFhfU"><b>online</b></a>

View file

@ -0,0 +1,12 @@
/foo
/foo/0
/
/a~1b
/c%d
/e^f
/g|h
/i\j
/k"l
/
/m~0n

View file

@ -8818,6 +8818,10 @@ basic_json_parser_63:
/*!
@brief JSON Pointer
A JSON pointer defines a string syntax for identifying a specific value
within a JSON document. It can be used with functions `at` and
`operator[]`. Furthermore, JSON pointers are the base for JSON patches.
@sa [RFC 6901](https://tools.ietf.org/html/rfc6901)
@since version 2.0.0
@ -8854,10 +8858,37 @@ basic_json_parser_63:
: reference_tokens(split(s))
{}
/// test for inequality
bool operator!=(const json_pointer& rhs) const
/*!
@brief return a string representation of the JSON pointer
@invariant For each JSON pointer `ptr`, it holds:
@code {.cpp}
ptr == json_pointer(ptr.to_string());
@endcode
@return a string representation of the JSON pointer
@liveexample{The example shows the result of `to_string`.,
json_pointer__to_string}
@since version 2.0.0
*/
std::string to_string() const noexcept
{
return reference_tokens != rhs.reference_tokens;
std::string result;
for (const auto& reference_token : reference_tokens)
{
result += "/" + escape(reference_token);
}
return result;
}
/// @copydoc to_string()
operator std::string() const
{
return to_string();
}
private:

View file

@ -8128,6 +8128,10 @@ class basic_json
/*!
@brief JSON Pointer
A JSON pointer defines a string syntax for identifying a specific value
within a JSON document. It can be used with functions `at` and
`operator[]`. Furthermore, JSON pointers are the base for JSON patches.
@sa [RFC 6901](https://tools.ietf.org/html/rfc6901)
@since version 2.0.0
@ -8164,10 +8168,37 @@ class basic_json
: reference_tokens(split(s))
{}
/// test for inequality
bool operator!=(const json_pointer& rhs) const
/*!
@brief return a string representation of the JSON pointer
@invariant For each JSON pointer `ptr`, it holds:
@code {.cpp}
ptr == json_pointer(ptr.to_string());
@endcode
@return a string representation of the JSON pointer
@liveexample{The example shows the result of `to_string`.,
json_pointer__to_string}
@since version 2.0.0
*/
std::string to_string() const noexcept
{
return reference_tokens != rhs.reference_tokens;
std::string result;
for (const auto& reference_token : reference_tokens)
{
result += "/" + escape(reference_token);
}
return result;
}
/// @copydoc to_string()
operator std::string() const
{
return to_string();
}
private:

View file

@ -12035,6 +12035,36 @@ TEST_CASE("Unicode", "[hide]")
// the array has 1112064 + 1 elemnts (a terminating "null" value)
CHECK(j.size() == 1112065);
SECTION("check JSON Pointers")
{
for (auto s : j)
{
// skip non-string JSON values
if (not s.is_string())
{
continue;
}
std::string ptr = s;
// tilde must be followed by 0 or 1
if (ptr == "~")
{
ptr += "0";
}
// JSON Pointers must begin with "/"
ptr = "/" + ptr;
CHECK_NOTHROW(json::json_pointer("/" + ptr));
// check escape/unescape roundtrip
auto escaped = json::json_pointer::escape(ptr);
json::json_pointer::unescape(escaped);
CHECK(escaped == ptr);
}
}
}
SECTION("ignore byte-order-mark")
@ -12389,6 +12419,17 @@ TEST_CASE("JSON pointers")
json j_object(json::value_t::object);
CHECK(j_object.flatten().unflatten() == json());
}
SECTION("string representation")
{
for (auto ptr :
{"", "/foo", "/foo/0", "/", "/a~1b", "/c%d", "/e^f", "/g|h", "/i\\j", "/k\"l", "/ ", "/m~0n"
})
{
CHECK(json::json_pointer(ptr).to_string() == ptr);
CHECK(json::json_pointer(ptr) == ptr);
}
}
}
TEST_CASE("JSON patch")