Lumiera  0.pre.03
»edit your freedom«
session-element-query-test.cpp
Go to the documentation of this file.
1 /*
2  SessionElementQuery(Test) - querying and retrieving elements from the session
3 
4  Copyright (C) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
34 #include "lib/util.hpp"
35 
36 #include <functional>
37 #include <string>
38 
39 
40 
41 namespace steam {
42 namespace mobject {
43 namespace session {
44 namespace test {
45 
46  using std::placeholders::_1;
47  using std::function;
48  using std::bind;
49  using std::string;
50 
51  using util::contains;
52 
53 
54  namespace { // helpers and shortcuts....
55 
56  typedef Placement<DummyMO> const& PDummy; // note const& required by ElementQuery filter definition
57 
58 
64  inline function<bool(PDummy)>
65  elementID_contains (string expectedText)
66  {
67  return [=] (PDummy candidate)
68  {
69  REQUIRE (candidate.isValid());
70  string desc(candidate->operator string());
71  return contains(desc, expectedText);
72  };
73  }
74 
75  }
76 
77 
78 
79  /******************************************************************************************/
94  class SessionElementQuery_test : public Test
95  {
96  virtual void
97  run (Arg)
98  {
99  // Prepare an (test)Index (dummy "session")
100  PPIdx testSession (build_testScopes());
101 
102  ElementQuery queryAPI;
103 
104  MORef<DummyMO> dummy1 = queryAPI.pick (elementID_contains("MO2"));
105  CHECK (dummy1);
106  CHECK (dummy1->isValid());
107  INFO (test, "Location in Tree: %s", cStr(ScopePath(dummy1.getPlacement())));
108 
109  string elementID = dummy1->operator string();
110  CHECK (contains (elementID, "MO2"));
111 
112  string specificID = elementID.substr(10); // should contain the random int-ID
113  MORef<DummyMO> dummy2;
114  CHECK (!dummy2);
115  dummy2 = queryAPI.pick (elementID_contains(specificID));
116  CHECK (dummy2); // found the same object again
117  CHECK (dummy2->isValid());
118  CHECK (dummy2 == dummy1);
119 
120 
121  // put aside a new handle holding onto the MObject
122  PDum newPlacement(dummy1.getPlacement());
123  CHECK (testSession->contains(dummy1));
124  CHECK (!testSession->contains(newPlacement));
125 
126  // and now remove the placement and all contained elements
127  testSession->clear (dummy1);
128  CHECK (!testSession->contains(dummy1));
129 
130  MORef<DummyMO> findAgain = queryAPI.pick (elementID_contains(specificID));
131  CHECK (!findAgain); // empty result because searched element was removed from session...
132 
133  MORef<DummyMO> otherElm = queryAPI.pick (elementID_contains("MO21"));
134  CHECK (otherElm); // now pick just some other arbitrary element
135 
136  testSession->insert(newPlacement, otherElm);
137  dummy2 = queryAPI.pick (elementID_contains(specificID));
138  CHECK (dummy2);
139  CHECK (dummy2 != dummy1);
140  CHECK (dummy2 != newPlacement);
141  CHECK (isSharedPointee(newPlacement, dummy2.getPlacement()));
142  CHECK (Scope::containing (dummy2.getRef()) == Scope (otherElm));
143  INFO (test, "New treelocation: %s", cStr(ScopePath(dummy2.getPlacement())));
144  }
145  };
146 
147 
148 
150  LAUNCHER (SessionElementQuery_test, "function session");
151 
152 
153 }}}} // namespace steam::mobject::session::test
_PickRes< PRED >::Result pick(PRED const &searchPredicate)
pick the first element from session satisfying a predicate.
An active (smart-ptr like) external reference to a specifically placed MObject "instance" within the ...
Definition: mobject-ref.hpp:94
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:68
Definition: run.hpp:49
PPIdx build_testScopes()
helper for tests: create a pseudo-session (actually just a PlacementIndex), which contains some neste...
Definition: test-scopes.cpp:47
bool isSharedPointee(MORef< MOX > const &ref1, MORef< MOY > const &ref2)
check if the two references actually share ownership on the same underlying MObject (as opposed to re...
static Scope containing(PlacementMO const &aPlacement)
discover the enclosing scope of a given Placement
Definition: scope.cpp:193
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:74
Sequence of nested scopes within the high-level model.
Definition: scope-path.hpp:135
A Placement scope within the high-level-model.
Definition: scope.hpp:78
Unit test helper to generate a system of nested test scopes.
External MObject/Placement reference.
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Search and query services to discover contents of the session.
function< bool(PDummy)> elementID_contains(string expectedText)
a filter predicate to pick some objects from a resultset, based on string match with the element&#39;s se...
An Object representing a sequence of nested scopes within the Session.
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:80
PlacementRef< MO > const & getRef() const
allow to use a MObjectRef like a (bare) PlacementRef
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255
Access point to session global search and query functions.