Lumiera  0.pre.03
»edit your freedom«
query-resolver-test.cpp
Go to the documentation of this file.
1 /*
2  QueryResolver(Test) - issuing typed queries over a generic interface
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"
29 #include "lib/test/test-helper.hpp"
30 #include "lib/format-cout.hpp"
31 #include "lib/depend.hpp"
32 
34 
35 #include <string>
36 
37 
38 
39 namespace lumiera {
40 namespace test{
41 
42  using lib::test::showSizeof;
43  using std::string;
44 
45 
46 
47 
48  namespace { // providing a test query resolving facility...
49 
50  typedef Goal::QueryID const& QID;
51 
53  template<typename TY>
55 
56  template<>
57  class DummySolutions<int>
58  {
59  int resNr_;
60 
61  public:
62  DummySolutions() : resNr_(7) {}
63 
64  int* next () { --resNr_; return &resNr_; }
65  bool exhausted() { return !bool(resNr_); }
66  };
67 
68  template<>
69  class DummySolutions<string>
70  : public DummySolutions<int>
71  {
72  string currentText_;
73 
74  public:
75  string*
76  next ()
77  {
78  static const char* lumi ="Lumiera";
79  currentText_ = string (lumi + *DummySolutions<int>::next());
80  return &currentText_;
81  }
82  };
83 
84 
92  template<typename TY>
94  : Resolution
95  {
96  DummySolutions<TY> solutions_;
97 
98  typedef typename Query<TY>::Cursor Cursor;
99 
100  Result
101  prepareResolution()
102  {
103  Cursor cursor;
104  cursor.point_at (solutions_.next());
105  return cursor;
106  }
107 
108  void
109  nextResult(Result& pos)
110  {
111  Cursor& cursor = static_cast<Cursor&> (pos);
112 
113  if (solutions_.exhausted())
114  cursor.point_at (0);
115  else
116  cursor.point_at (solutions_.next());
117  }
118  };
119 
120 
121 
128  : public QueryResolver
129  {
130  bool
131  canHandleQuery (QID qID) const
132  {
133  return Goal::GENERIC == qID.kind
134  && (wantResultType<int> (qID)
135  ||wantResultType<string>(qID));
136  }
137 
138  template<typename TY>
139  bool
140  wantResultType (QID qID) const
141  {
142  return qID.type == getResultTypeID<TY>();
143  }
144 
145 
146  template<typename TY>
147  static Resolution*
148  resolutionFunction (Goal const& goal)
149  {
150  QID qID = goal.getQID();
151  REQUIRE (qID.kind == Goal::GENERIC
152  && qID.type == getResultTypeID<TY>());
153 
154  return new DummyResultSet<TY>();
155  }
156 
157  operator string() const { return "Test-DummyQueryResolver"; }
158 
159  public:
161  : QueryResolver()
162  {
163  Goal::QueryID case1(Goal::GENERIC, getResultTypeID<int>());
164  Goal::QueryID case2(Goal::GENERIC, getResultTypeID<string>());
165 
166  installResolutionCase(case1, &resolutionFunction<int> );
167  installResolutionCase(case2, &resolutionFunction<string> );
168  }
169  };
170 
171 
173 
175  buildTestQueryResolver ()
176  {
177  return testResolver();
178  }
179 
180  } // (END) implementation of a test query resolving facility
181 
182 
183 
184 
185 
186 
187  /*******************************************************************************/
197  class QueryResolver_test : public Test
198  {
199 
200  virtual void
201  run (Arg)
202  {
203  QueryResolver& resolver = buildTestQueryResolver();
204  Query<int> firstQuery("");
205  explore (firstQuery.resolveBy (resolver));
206 
207  Query<string> secondQuery("");
208  explore (secondQuery.resolveBy(resolver));
209  }
210 
211  template<typename ITER>
212  static void
213  explore (ITER ii)
214  {
215  cout << "Query-Results: " << showSizeof(ii) << endl;
216  for ( ; ii; ++ii )
217  cout << *ii << endl;
218  }
219 
220  };
221 
222 
224  LAUNCHER (QueryResolver_test, "unit session");
225 
226 
227 }} // namespace lumiera::test
Query ABC: unspecific goal for resolution or retrieval.
Definition: query.hpp:125
Automatically use custom string conversion in C++ stream output.
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
Interface: a facility for resolving (some kind of) queries A concrete subclass has the ability to cre...
ABC representing the result set of an individual query resolution.
framework and to resolve logical queries.
Definition: run.hpp:49
a concrete "resolution" of the query is a set of "solutions", which can be explored by iteration...
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:289
Simple test class runner.
A collection of frequently used helper functions to support unit testing.
Singleton services and Dependency Injection.
Lumiera public interface.
Definition: advice.cpp:113
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:279
Single Solution, possibly part of a result set.
Definition: query.hpp:163