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)
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
25 #include "lib/util.hpp"
26 
27 #include <functional>
28 #include <string>
29 
30 
31 
32 namespace steam {
33 namespace mobject {
34 namespace session {
35 namespace test {
36 
37  using std::placeholders::_1;
38  using std::function;
39  using std::bind;
40  using std::string;
41 
42  using util::contains;
43 
44 
45  namespace { // helpers and shortcuts....
46 
47  typedef Placement<DummyMO> const& PDummy; // note const& required by ElementQuery filter definition
48 
49 
55  inline function<bool(PDummy)>
56  elementID_contains (string expectedText)
57  {
58  return [=] (PDummy candidate)
59  {
60  REQUIRE (candidate.isValid());
61  string desc(candidate->operator string());
62  return contains(desc, expectedText);
63  };
64  }
65 
66  }
67 
68 
69 
70  /******************************************************************************************/
85  class SessionElementQuery_test : public Test
86  {
87  virtual void
88  run (Arg)
89  {
90  // Prepare an (test)Index (dummy "session")
91  PPIdx testSession (build_testScopes());
92 
93  ElementQuery queryAPI;
94 
95  MORef<DummyMO> dummy1 = queryAPI.pick (elementID_contains("MO2"));
96  CHECK (dummy1);
97  CHECK (dummy1->isValid());
98  INFO (test, "Location in Tree: %s", cStr(ScopePath(dummy1.getPlacement())));
99 
100  string elementID = dummy1->operator string();
101  CHECK (contains (elementID, "MO2"));
102 
103  string specificID = elementID.substr(10); // should contain the random int-ID
104  MORef<DummyMO> dummy2;
105  CHECK (!dummy2);
106  dummy2 = queryAPI.pick (elementID_contains(specificID));
107  CHECK (dummy2); // found the same object again
108  CHECK (dummy2->isValid());
109  CHECK (dummy2 == dummy1);
110 
111 
112  // put aside a new handle holding onto the MObject
113  PDum newPlacement(dummy1.getPlacement());
114  CHECK (testSession->contains(dummy1));
115  CHECK (!testSession->contains(newPlacement));
116 
117  // and now remove the placement and all contained elements
118  testSession->clear (dummy1);
119  CHECK (!testSession->contains(dummy1));
120 
121  MORef<DummyMO> findAgain = queryAPI.pick (elementID_contains(specificID));
122  CHECK (!findAgain); // empty result because searched element was removed from session...
123 
124  MORef<DummyMO> otherElm = queryAPI.pick (elementID_contains("MO21"));
125  CHECK (otherElm); // now pick just some other arbitrary element
126 
127  testSession->insert(newPlacement, otherElm);
128  dummy2 = queryAPI.pick (elementID_contains(specificID));
129  CHECK (dummy2);
130  CHECK (dummy2 != dummy1);
131  CHECK (dummy2 != newPlacement);
132  CHECK (isSharedPointee(newPlacement, dummy2.getPlacement()));
133  CHECK (Scope::containing (dummy2.getRef()) == Scope (otherElm));
134  INFO (test, "New treelocation: %s", cStr(ScopePath(dummy2.getPlacement())));
135  }
136  };
137 
138 
139 
141  LAUNCHER (SessionElementQuery_test, "function session");
142 
143 
144 }}}} // 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:85
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:59
Definition: run.hpp:40
PPIdx build_testScopes()
helper for tests: create a pseudo-session (actually just a PlacementIndex), which contains some neste...
Definition: test-scopes.cpp:38
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:184
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:65
Sequence of nested scopes within the high-level model.
Definition: scope-path.hpp:126
A Placement scope within the high-level-model.
Definition: scope.hpp:69
Unit test helper to generate a system of nested test scopes.
External MObject/Placement reference.
Simplistic 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:71
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.