From a2530980e8dc336b8759d4f541e2ebc27737a07d Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Wed, 13 Dec 2017 03:39:24 -0800 Subject: [PATCH] test/ceph_json_formattable: add unitest Signed-off-by: Yehuda Sadeh --- src/test/common/CMakeLists.txt | 7 + src/test/common/test_json_formattable.cc | 175 +++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 src/test/common/test_json_formattable.cc diff --git a/src/test/common/CMakeLists.txt b/src/test/common/CMakeLists.txt index 5272fbef46785..25361405898f8 100644 --- a/src/test/common/CMakeLists.txt +++ b/src/test/common/CMakeLists.txt @@ -55,6 +55,13 @@ add_executable(unittest_str_map add_ceph_unittest(unittest_str_map) target_link_libraries(unittest_str_map ceph-common) +# unittest_json_formattable +add_executable(unittest_json_formattable + test_json_formattable.cc + ) +add_ceph_unittest(unittest_json_formattable ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest_json_formattable) +target_link_libraries(unittest_json_formattable ceph-common global ${BLKID_LIBRARIES}) + # unittest_sharedptr_registry add_executable(unittest_sharedptr_registry test_sharedptr_registry.cc diff --git a/src/test/common/test_json_formattable.cc b/src/test/common/test_json_formattable.cc new file mode 100644 index 0000000000000..3161f75d61f13 --- /dev/null +++ b/src/test/common/test_json_formattable.cc @@ -0,0 +1,175 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2013 Cloudwatt + * + * Author: Loic Dachary + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + */ + +#include +#include + +#include "common/ceph_json.h" + +#include + +using namespace std; + + +static void get_jf(const string& s, JSONFormattable *f) +{ + JSONParser p; + bool result = p.parse(s.c_str(), s.size()); + ASSERT_EQ(true, result); + try { + decode_json_obj(*f, &p); + } catch (JSONDecoder::err& e) { + ASSERT_TRUE(0 == "Failed to decode JSON object"); + } +} + +TEST(formatable, str) { + JSONFormattable f; + get_jf("{ \"foo\": \"bar\" }", &f); + ASSERT_EQ((string)f["foo"], "bar"); + ASSERT_EQ((string)f["fooz"], ""); + ASSERT_EQ((string)f["fooz"]("lala"), "lala"); +} + +TEST(formatable, str2) { + JSONFormattable f; + get_jf("{ \"foo\": \"bar\" }", &f); + ASSERT_EQ((string)f["foo"], "bar"); + ASSERT_EQ((string)f["fooz"], ""); + ASSERT_EQ((string)f["fooz"]("lala"), "lala"); + + JSONFormattable f2; + get_jf("{ \"foo\": \"bar\", \"fooz\": \"zzz\" }", &f2); + ASSERT_EQ((string)f2["foo"], "bar"); + ASSERT_NE((string)f2["fooz"], ""); + ASSERT_EQ((string)f2["fooz"], "zzz"); + ASSERT_EQ((string)f2["fooz"]("lala"), "zzz"); + +} + +TEST(formatable, int) { + JSONFormattable f; + get_jf("{ \"foo\": 1 }", &f); + ASSERT_EQ((int)f["foo"], 1); + ASSERT_EQ((int)f["fooz"], 0); + ASSERT_EQ((int)f["fooz"](3), 3); + + JSONFormattable f2; + get_jf("{ \"foo\": \"bar\", \"fooz\": \"123\" }", &f2); + ASSERT_EQ((string)f2["foo"], "bar"); + ASSERT_NE((int)f2["fooz"], 0); + ASSERT_EQ((int)f2["fooz"], 123); + ASSERT_EQ((int)f2["fooz"](111), 123); +} + +TEST(formatable, bool) { + JSONFormattable f; + get_jf("{ \"foo\": \"true\" }", &f); + ASSERT_EQ((bool)f["foo"], true); + ASSERT_EQ((bool)f["fooz"], false); + ASSERT_EQ((bool)f["fooz"](true), true); + + JSONFormattable f2; + get_jf("{ \"foo\": \"false\" }", &f); + ASSERT_EQ((bool)f["foo"], false); +} + +TEST(formatable, nested) { + JSONFormattable f; + get_jf("{ \"obj\": { \"foo\": 1, \"inobj\": { \"foo\": 2 } } }", &f); + ASSERT_EQ((int)f["foo"], 0); + ASSERT_EQ((int)f["obj"]["foo"], 1); + ASSERT_EQ((int)f["obj"]["inobj"]["foo"], 2); +} + +TEST(formatable, array) { + JSONFormattable f; + get_jf("{ \"arr\": [ { \"foo\": 1, \"inobj\": { \"foo\": 2 } }," + "{ \"foo\": 2 } ] }", &f); + + int i = 1; + for (auto a : f.array()) { + ASSERT_EQ((int)a["foo"], i); + ++i; + } + + JSONFormattable f2; + get_jf("{ \"arr\": [ 0, 1, 2, 3, 4 ]}", &f2); + + i = 0; + for (auto a : f2.array()) { + ASSERT_EQ((int)a, i); + ++i; + } +} + +TEST(formatable, bin_encode) { + JSONFormattable f, f2; + get_jf("{ \"arr\": [ { \"foo\": 1, \"bar\": \"aaa\", \"inobj\": { \"foo\": 2 } }," + "{ \"foo\": 2, \"inobj\": { \"foo\": 3 } } ] }", &f); + + int i = 1; + for (auto a : f.array()) { + ASSERT_EQ((int)a["foo"], i); + ASSERT_EQ((int)a["foo"]["inobj"], i + 1); + ASSERT_EQ((string)a["bar"], "aaa"); + ++i; + } + + bufferlist bl; + ::encode(f, bl); + bufferlist::iterator iter = bl.begin(); + try { + ::decode(f2, iter); + } catch (buffer::error& err) { + ASSERT_TRUE(0 == "Failed to decode object"); + } + + i = 1; + for (auto a : f2.array()) { + ASSERT_EQ((int)a["foo"], i); + ASSERT_EQ((int)a["foo"]["inobj"], i + 1); + ASSERT_EQ((string)a["bar"], "aaa"); + ++i; + } + +} + +TEST(formatable, json_encode) { + JSONFormattable f, f2; + get_jf("{ \"arr\": [ { \"foo\": 1, \"bar\": \"aaa\", \"inobj\": { \"foo\": 2 } }," + "{ \"foo\": 2, \"inobj\": { \"foo\": 3 } } ] }", &f); + + JSONFormatter formatter; + formatter.open_object_section("bla"); + ::encode_json("f", f, &formatter); + formatter.close_section(); + + stringstream ss; + formatter.flush(ss); + + get_jf(ss.str(), &f2); + + int i = 1; + for (auto a : f2.array()) { + ASSERT_EQ((int)a["foo"], i); + ASSERT_EQ((int)a["foo"]["inobj"], i + 1); + ASSERT_EQ((string)a["bar"], "aaa"); + ++i; + } + +} + -- 2.39.5