From 0cfd0f5d68eaf94df4ad5d338a05c0e62b9e4bf2 Mon Sep 17 00:00:00 2001 From: Niels Date: Mon, 9 Feb 2015 20:21:26 +0100 Subject: [PATCH] added test cases and fixed some warnings --- src/json.hpp | 8 ++-- src/json.hpp.re2c | 8 ++-- test/unit.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 8 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index b958deed..361e0a32 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -443,8 +443,9 @@ class basic_json } /// copy assignment - inline reference operator=(basic_json other) noexcept + inline reference& operator=(basic_json other) noexcept { + assert(false); // not sure if function will ever be called std::swap(m_type, other.m_type); std::swap(m_value, other.m_value); return *this; @@ -1704,6 +1705,7 @@ class basic_json /// copy assignment inline iterator& operator=(const iterator& other) noexcept { + assert(false); // not sure if function will ever be called m_object = other.m_object; m_it = other.m_it; return *this; @@ -2085,7 +2087,7 @@ class basic_json } /// copy assignment - inline const_iterator operator=(const const_iterator& other) noexcept + inline const_iterator& operator=(const const_iterator& other) noexcept { m_object = other.m_object; m_it = other.m_it; @@ -3329,7 +3331,7 @@ json_parser_60: } } - inline std::string token_type_name(token_type t) + inline static std::string token_type_name(token_type t) { switch (t) { diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 987164ef..47882f39 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -443,8 +443,9 @@ class basic_json } /// copy assignment - inline reference operator=(basic_json other) noexcept + inline reference& operator=(basic_json other) noexcept { + assert(false); // not sure if function will ever be called std::swap(m_type, other.m_type); std::swap(m_value, other.m_value); return *this; @@ -1704,6 +1705,7 @@ class basic_json /// copy assignment inline iterator& operator=(const iterator& other) noexcept { + assert(false); // not sure if function will ever be called m_object = other.m_object; m_it = other.m_it; return *this; @@ -2085,7 +2087,7 @@ class basic_json } /// copy assignment - inline const_iterator operator=(const const_iterator& other) noexcept + inline const_iterator& operator=(const const_iterator& other) noexcept { m_object = other.m_object; m_it = other.m_it; @@ -2689,7 +2691,7 @@ class basic_json } } - inline std::string token_type_name(token_type t) + inline static std::string token_type_name(token_type t) { switch (t) { diff --git a/test/unit.cpp b/test/unit.cpp index 2b685ac7..e6307619 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -2518,8 +2518,8 @@ TEST_CASE("iterators") SECTION("const json + begin/end") { - json::iterator it_begin = j_const.begin(); - json::iterator it_end = j_const.end(); + json::const_iterator it_begin = j_const.begin(); + json::const_iterator it_end = j_const.end(); auto it = it_begin; CHECK(it != it_end); @@ -3464,4 +3464,107 @@ TEST_CASE("modifiers") CHECK(j == json(json::value_t::null)); } } + + SECTION("swap()") + { + SECTION("json") + { + SECTION("member swap") + { + json j("hello world"); + json k(42.23); + + j.swap(k); + + CHECK(j == json(42.23)); + CHECK(k == json("hello world")); + } + + SECTION("nonmember swap") + { + json j("hello world"); + json k(42.23); + + std::swap(j, k); + + CHECK(j == json(42.23)); + CHECK(k == json("hello world")); + } + } + + SECTION("array_t") + { + SECTION("array_t type") + { + json j = {1, 2, 3, 4}; + json::array_t a = {"foo", "bar", "baz"}; + + j.swap(a); + + CHECK(j == json({"foo", "bar", "baz"})); + + j.swap(a); + + CHECK(j == json({1, 2, 3, 4})); + } + + SECTION("non-array_t type") + { + json j = 17; + json::array_t a = {"foo", "bar", "baz"}; + + CHECK_THROWS_AS(j.swap(a), std::runtime_error); + } + } + + SECTION("object_t") + { + SECTION("object_t type") + { + json j = {{"one", 1}, {"two", 2}}; + json::object_t o = {{"cow", "Kuh"}, {"chicken", "Huhn"}}; + + j.swap(o); + + CHECK(j == json({{"cow", "Kuh"}, {"chicken", "Huhn"}})); + + j.swap(o); + + CHECK(j == json({{"one", 1}, {"two", 2}})); + } + + SECTION("non-object_t type") + { + json j = 17; + json::object_t o = {{"cow", "Kuh"}, {"chicken", "Huhn"}}; + + CHECK_THROWS_AS(j.swap(o), std::runtime_error); + } + } + + SECTION("string_t") + { + SECTION("string_t type") + { + json j = "Hello world"; + json::string_t s = "Hallo Welt"; + + j.swap(s); + + CHECK(j == json("Hallo Welt")); + + j.swap(s); + + CHECK(j == json("Hello world")); + } + + SECTION("non-string_t type") + { + json j = 17; + json::string_t s = "Hallo Welt"; + + CHECK_THROWS_AS(j.swap(s), std::runtime_error); + } + } + } } \ No newline at end of file