Lumiera  0.pre.03
»edit your freedom«
query-focus-test.cpp
Go to the documentation of this file.
1 /*
2  QueryFocus(Test) - verify proper management of current scope
3 
4  Copyright (C) Lumiera.org
5  2009, 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"
33 #include "lib/format-cout.hpp"
34 
35 
36 
37 namespace steam {
38 namespace mobject {
39 namespace session {
40 namespace test {
41 
42  namespace {
46  inline size_t
47  refs (QueryFocus const& focus)
48  {
49  return focus.currentPath().ref_count();
50  }
51  }
52 
53 
54 
55 
56  /*******************************************************************************/
68  : public Test
69  {
70 
71  virtual void
72  run (Arg)
73  {
74  // Prepare a (test)Session with
75  // some nested dummy placements
76  PPIdx index = build_testScopes();
77  PMO& root = index->getRoot();
78 
79  QueryFocus theFocus;
80  theFocus.reset();
81  CHECK (Scope(root) == Scope(theFocus));
82 
83  checkNavigation (theFocus);
84 
85  Scope scopePosition = Scope(theFocus);
87 
88  QueryFocus currentFocus;
89  CHECK (scopePosition == Scope(currentFocus));
90  CHECK (currentFocus == theFocus);
91  CHECK (2 == refs(currentFocus));
92  CHECK (2 == refs(theFocus));
93  }
94 
95 
96 
99  void
101  {
102  focus.reset();
103  CHECK (Scope(focus).isRoot());
104 
105  PMO& someObj = *focus.query<TestSubMO1>();
106  // by construction of the test fixture,
107  // we know this object is root -> ps2 -> ps3
108 
109  CHECK (Scope(focus).isRoot());
110  focus.shift (someObj);
111  CHECK (!Scope(focus).isRoot());
112  ScopePath path = focus.currentPath();
113  CHECK (someObj == path.getLeaf());
114  CHECK (Scope(focus).getParent().getParent().isRoot());
115 
116  focus.shift (path.getLeaf().getParent());
117  CHECK (Scope(focus) == path.getLeaf().getParent());
118  CHECK (someObj != Scope(focus));
119  CHECK (path.contains (focus.currentPath()));
120  CHECK (focus.currentPath().getLeaf().getParent().isRoot());
121 
122  // as the focus now has been moved up one level,
123  // we'll re-discover the original starting point as immediate child
124  CHECK (someObj == *focus.explore<TestSubMO1>());
125  }
126 
127 
128 
131  void
133  {
134  QueryFocus original; // automatically attaches to current stack top
135  uint num_refs = refs (original);
136  CHECK (num_refs > 1); // because the run() function also holds a ref
137 
138  QueryFocus subF = QueryFocus::push();
139  cout << subF << endl;
140  CHECK (subF == original);
141 
142  CHECK ( 1 == refs(subF) );
143  CHECK (num_refs == refs(original));
144 
145  { // temporarily creating an independent focus attached differently
146  QueryFocus subF2 = QueryFocus::push(Scope(subF).getParent());
147  CHECK (subF2 != subF);
148  CHECK (subF == original);
149  cout << subF2 << endl;
150 
152  while (ii) // drill down depth first
153  {
154  subF2.shift(*ii);
155  cout << subF2 << endl;
156  ii = subF2.explore<TestSubMO21>();
157  }
158  cout << subF2 << "<<<--discovery exhausted" << endl;
159 
160  subF2.pop(); // releasing this focus and re-attaching to what's on stack top
161  cout << subF2 << "<<<--after pop()" << endl;
162  CHECK (subF2 == subF);
163  CHECK (2 == refs(subF2)); // both are now attached to the same path
164  CHECK (2 == refs(subF));
165  }
166  // subF2 went out of scope, but no auto-pop happens (because subF is still there)
167  cout << subF << endl;
168 
169  CHECK ( 1 == refs(subF));
170  CHECK (num_refs == refs(original));
171  // when subF goes out of scope now, auto-pop will happen...
172  }
173 
174  };
175 
176 
177 
179  LAUNCHER (QueryFocus_test, "unit session");
180 
181 
182 }}}} // namespace steam::mobject::session::test
Representation of the current scope when navigating the session model.
Automatically use custom string conversion in C++ stream output.
Subclass-1 is not defined "processible", thus will always be handled as DummyMO...
Scope getParent() const
retrieve the parent scope which encloses this scope.
Definition: scope.cpp:218
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
Steam-Layer implementation namespace root.
QueryFocus & shift(Scope const &)
shift this QueryFocus to a container-like scope, causing it to navigate, changing the current ScopePa...
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
QueryFocus & pop()
cease to use this specific reference to the current frame.
A Placement scope within the high-level-model.
Definition: scope.hpp:78
Unit test helper to generate a system of nested test scopes.
Core of the session implementation datastructure.
Simple test class runner.
ScopeQuery< MO >::iterator explore() const
discover any matching object contained as immediate Child within current focus.
size_t refs(QueryFocus const &focus)
Helper: extract the refcount of the current path referred by the given focus.
QueryFocus & reset()
discard any state and navigate current focus path to model root
Definition: query-focus.cpp:77
ScopePath const & currentPath() const
ScopeQuery< MO >::iterator query() const
discover depth-first any matching object within current focus.
Current focus location to use as point-of reference for contents and location discovery queries...
Adapter for building an implementation of the »Lumiera Forward Iterator« concept. ...
static QueryFocus push()
push the "current QueryFocus" aside and open a new focus frame, which starts out at the same location...