From f743784bc9e8bcca1b93bc10420f2ae94c0511b2 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 23 Jan 2016 17:10:44 +0100 Subject: [PATCH] add accessor for Nth child to our Record type --- src/lib/diff/record.hpp | 11 +++++++++++ .../diff/generic-record-representation-test.cpp | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/lib/diff/record.hpp b/src/lib/diff/record.hpp index 764ce2cd1..180312db8 100644 --- a/src/lib/diff/record.hpp +++ b/src/lib/diff/record.hpp @@ -230,6 +230,17 @@ namespace diff{ return extractVal (*found); } + Access + child (size_t idx) const + { + if (children_.size() <= idx) + throw error::Invalid ("Child index " +util::toString(idx) + +" out of bounds [0.."+util::toString(children_.size()) + +"["); + return children_[idx]; + } + + /** * While otherwise immutable, * a Record object can be remoulded diff --git a/tests/library/diff/generic-record-representation-test.cpp b/tests/library/diff/generic-record-representation-test.cpp index a065d49fe..cbe258b0f 100644 --- a/tests/library/diff/generic-record-representation-test.cpp +++ b/tests/library/diff/generic-record-representation-test.cpp @@ -150,6 +150,8 @@ namespace test{ CHECK (enterprise.getType() == "starship"); CHECK (enterprise.get("Registry") == "NCC-1701-D"); + CHECK (enterprise.child(0) == "Picard"); + CHECK (enterprise.child(2) == "Data"); CHECK (enterprise.hasAttribute("Owner")); CHECK (!enterprise.hasAttribute("owner")); @@ -160,6 +162,7 @@ namespace test{ CHECK (util::contains (enterprise, "Worf")); VERIFY_ERROR (INVALID, enterprise.get("warp10")); + VERIFY_ERROR (INVALID, enterprise.child(12)); cout << "enterprise = " << enterprise <