diff options
author | Håvard Pettersen <havardpe@oath.com> | 2017-10-04 09:45:07 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2017-10-04 11:45:13 +0000 |
commit | ca78e9f4754ce708500d7cb0f6a8c29e581cb877 (patch) | |
tree | 0c49264ed891f9da60e61feb90b601c238fa9119 | |
parent | 81b9d23e0e85e73626ff6edea446741bb263b465 (diff) |
slime toplevel convenience access
-rw-r--r-- | vespalib/src/tests/slime/slime_test.cpp | 47 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/data/slime/slime.h | 6 |
2 files changed, 53 insertions, 0 deletions
diff --git a/vespalib/src/tests/slime/slime_test.cpp b/vespalib/src/tests/slime/slime_test.cpp index 02d9efb0e1e..fae61f4194b 100644 --- a/vespalib/src/tests/slime/slime_test.cpp +++ b/vespalib/src/tests/slime/slime_test.cpp @@ -4,6 +4,7 @@ LOG_SETUP("slime_test"); #include <vespa/vespalib/testkit/testapp.h> #include <vespa/vespalib/data/slime/slime.h> #include <vespa/vespalib/data/slime/strfmt.h> +#include <type_traits> using namespace vespalib::slime::convenience; @@ -396,4 +397,50 @@ TEST("require that we can resolve to symbol table from a cursor") { EXPECT_TRUE(sd == slime.lookup(D)); } +template <typename T> +void verify_cursor_ref(T &&) { + EXPECT_TRUE((std::is_same<Cursor&,T>::value)); +} + +template <typename T> +void verify_inspector_ref(T &&) { + EXPECT_TRUE((std::is_same<Inspector&,T>::value)); +} + +TEST("require that top-level convenience accessors work as expected for objects") { + Slime object; + Cursor &c = object.setObject(); + c.setLong("a", 10); + c.setLong("b", 20); + c.setLong("c", 30); + Symbol sym_b = object.lookup("b"); + const Slime &const_object = object; + TEST_DO(verify_cursor_ref(object[0])); + TEST_DO(verify_inspector_ref(const_object[0])); + EXPECT_EQUAL(object[0].asLong(), 0); + EXPECT_EQUAL(object[sym_b].asLong(), 20); + EXPECT_EQUAL(object["c"].asLong(), 30); + EXPECT_EQUAL(const_object[0].asLong(), 0); + EXPECT_EQUAL(const_object[sym_b].asLong(), 20); + EXPECT_EQUAL(const_object["c"].asLong(), 30); +} + +TEST("require that top-level convenience accessors work as expected for arrays") { + Slime array; + Cursor &c = array.setArray(); + c.addLong(10); + c.addLong(20); + c.addLong(30); + Symbol sym_b(1); + const Slime &const_array = array; + TEST_DO(verify_cursor_ref(array[0])); + TEST_DO(verify_inspector_ref(const_array[0])); + EXPECT_EQUAL(array[0].asLong(), 10); + EXPECT_EQUAL(array[sym_b].asLong(), 0); + EXPECT_EQUAL(array["c"].asLong(), 0); + EXPECT_EQUAL(const_array[0].asLong(), 10); + EXPECT_EQUAL(const_array[sym_b].asLong(), 0); + EXPECT_EQUAL(const_array["c"].asLong(), 0); +} + TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/vespa/vespalib/data/slime/slime.h b/vespalib/src/vespa/vespalib/data/slime/slime.h index af081887c9a..aa44b38b353 100644 --- a/vespalib/src/vespa/vespalib/data/slime/slime.h +++ b/vespalib/src/vespa/vespalib/data/slime/slime.h @@ -130,6 +130,12 @@ public: Inspector &get() const { return _root.get(); } + template <typename ID> + Inspector &operator[](ID id) const { return get()[id]; } + + template <typename ID> + Cursor &operator[](ID id) { return get()[id]; } + Cursor &setNix() { return _root.set(slime::NixValueFactory()); } |