From 5b4dcfd8a4be647b8a316829549f210fea3aaddd Mon Sep 17 00:00:00 2001 From: Niels Date: Sun, 8 Feb 2015 18:27:16 +0100 Subject: [PATCH] more test cases --- .travis.yml | 2 +- test/unit.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 01308341..881f50cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,6 @@ script: after_success: - make clean - - make json_unit CXXFLAGS="-fprofile-arcs -ftest-coverage" + - make json_unit CXXFLAGS="-fprofile-arcs -ftest-coverage -std=c++11" - ./json_unit - coveralls --exclude test/catch.hpp --exclude test/unit.cpp --include src/json.hpp --gcov-options '\-lp' --gcov 'gcov-4.9' diff --git a/test/unit.cpp b/test/unit.cpp index f0f7c4f6..c8022ed3 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -2055,12 +2055,12 @@ TEST_CASE("element access") } } } - + SECTION("object") { - json j = {{"integer", 1}, {"floating", 42.23}, {"null", nullptr}, {"string", "hello world"}, {"boolean", true}, {"object", json::object()}, {"array", {1,2,3}}}; + json j = {{"integer", 1}, {"floating", 42.23}, {"null", nullptr}, {"string", "hello world"}, {"boolean", true}, {"object", json::object()}, {"array", {1, 2, 3}}}; const json j_const = j; - + SECTION("access specified element with bounds checking") { SECTION("access within bounds") @@ -2192,5 +2192,86 @@ TEST_CASE("element access") } } } + + SECTION("find an element in an object") + { + SECTION("existing element") + { + for (auto key : + {"integer", "floating", "null", "string", "boolean", "object", "array" + }) + { + CHECK(j.find(key) != j.end()); + CHECK(*j.find(key) == j.at(key)); + CHECK(j_const.find(key) != j_const.end()); + CHECK(*j_const.find(key) == j_const.at(key)); + } + } + + SECTION("nonexisting element") + { + CHECK(j.find("foo") == j.end()); + CHECK(j_const.find("foo") == j_const.end()); + } + + SECTION("all types") + { + SECTION("null") + { + json j_nonarray(json::value_t::null); + const json j_nonarray_const(j_nonarray); + CHECK(j_nonarray.find("foo") == j_nonarray.end()); + CHECK(j_nonarray_const.find("foo") == j_nonarray_const.end()); + } + + SECTION("string") + { + json j_nonarray(json::value_t::string); + const json j_nonarray_const(j_nonarray); + CHECK(j_nonarray.find("foo") == j_nonarray.end()); + CHECK(j_nonarray_const.find("foo") == j_nonarray_const.end()); + } + + SECTION("object") + { + json j_nonarray(json::value_t::object); + const json j_nonarray_const(j_nonarray); + CHECK(j_nonarray.find("foo") == j_nonarray.end()); + CHECK(j_nonarray_const.find("foo") == j_nonarray_const.end()); + } + + SECTION("array") + { + json j_nonarray(json::value_t::array); + const json j_nonarray_const(j_nonarray); + CHECK(j_nonarray.find("foo") == j_nonarray.end()); + CHECK(j_nonarray_const.find("foo") == j_nonarray_const.end()); + } + + SECTION("boolean") + { + json j_nonarray(json::value_t::boolean); + const json j_nonarray_const(j_nonarray); + CHECK(j_nonarray.find("foo") == j_nonarray.end()); + CHECK(j_nonarray_const.find("foo") == j_nonarray_const.end()); + } + + SECTION("number (integer)") + { + json j_nonarray(json::value_t::number_integer); + const json j_nonarray_const(j_nonarray); + CHECK(j_nonarray.find("foo") == j_nonarray.end()); + CHECK(j_nonarray_const.find("foo") == j_nonarray_const.end()); + } + + SECTION("number (floating-point)") + { + json j_nonarray(json::value_t::number_float); + const json j_nonarray_const(j_nonarray); + CHECK(j_nonarray.find("foo") == j_nonarray.end()); + CHECK(j_nonarray_const.find("foo") == j_nonarray_const.end()); + } + } + } } }