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)
5  2009, 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"
20 #include "lib/test/test-helper.hpp"
21 #include "lib/format-cout.hpp"
22 #include "lib/depend.hpp"
23 
25 
26 #include <string>
27 
28 
29 
30 namespace lumiera {
31 namespace test{
32 
33  using lib::test::showSizeof;
34  using std::string;
35 
36 
37 
38 
39  namespace { // providing a test query resolving facility...
40 
41  typedef Goal::QueryID const& QID;
42 
44  template<typename TY>
46 
47  template<>
48  class DummySolutions<int>
49  {
50  int resNr_;
51 
52  public:
53  DummySolutions() : resNr_(7) {}
54 
55  int* next () { --resNr_; return &resNr_; }
56  bool exhausted() { return !bool(resNr_); }
57  };
58 
59  template<>
60  class DummySolutions<string>
61  : public DummySolutions<int>
62  {
63  string currentText_;
64 
65  public:
66  string*
67  next ()
68  {
69  static const char* lumi ="Lumiera";
70  currentText_ = string (lumi + *DummySolutions<int>::next());
71  return &currentText_;
72  }
73  };
74 
75 
83  template<typename TY>
85  : Resolution
86  {
87  DummySolutions<TY> solutions_;
88 
89  typedef typename Query<TY>::Cursor Cursor;
90 
91  Result
92  prepareResolution()
93  {
94  Cursor cursor;
95  cursor.point_at (solutions_.next());
96  return cursor;
97  }
98 
99  void
100  nextResult(Result& pos)
101  {
102  Cursor& cursor = static_cast<Cursor&> (pos);
103 
104  if (solutions_.exhausted())
105  cursor.point_at (0);
106  else
107  cursor.point_at (solutions_.next());
108  }
109  };
110 
111 
112 
119  : public QueryResolver
120  {
121  bool
122  canHandleQuery (QID qID) const
123  {
124  return Goal::GENERIC == qID.kind
125  && (wantResultType<int> (qID)
126  ||wantResultType<string>(qID));
127  }
128 
129  template<typename TY>
130  bool
131  wantResultType (QID qID) const
132  {
133  return qID.type == getResultTypeID<TY>();
134  }
135 
136 
137  template<typename TY>
138  static Resolution*
139  resolutionFunction (Goal const& goal)
140  {
141  QID qID = goal.getQID();
142  REQUIRE (qID.kind == Goal::GENERIC
143  && qID.type == getResultTypeID<TY>());
144 
145  return new DummyResultSet<TY>();
146  }
147 
148  operator string() const { return "Test-DummyQueryResolver"; }
149 
150  public:
152  : QueryResolver()
153  {
154  Goal::QueryID case1(Goal::GENERIC, getResultTypeID<int>());
155  Goal::QueryID case2(Goal::GENERIC, getResultTypeID<string>());
156 
157  installResolutionCase(case1, &resolutionFunction<int> );
158  installResolutionCase(case2, &resolutionFunction<string> );
159  }
160  };
161 
162 
164 
166  buildTestQueryResolver ()
167  {
168  return testResolver();
169  }
170 
171  } // (END) implementation of a test query resolving facility
172 
173 
174 
175 
176 
177 
178  /*******************************************************************************/
188  class QueryResolver_test : public Test
189  {
190 
191  virtual void
192  run (Arg)
193  {
194  QueryResolver& resolver = buildTestQueryResolver();
195  Query<int> firstQuery("");
196  explore (firstQuery.resolveBy (resolver));
197 
198  Query<string> secondQuery("");
199  explore (secondQuery.resolveBy(resolver));
200  }
201 
202  template<typename ITER>
203  static void
204  explore (ITER ii)
205  {
206  cout << "Query-Results: " << showSizeof(ii) << endl;
207  for ( ; ii; ++ii )
208  cout << *ii << endl;
209  }
210 
211  };
212 
213 
215  LAUNCHER (QueryResolver_test, "unit session");
216 
217 
218 }} // namespace lumiera::test
Query ABC: unspecific goal for resolution or retrieval.
Definition: query.hpp:116
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:40
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:280
Simplistic 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:104
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:270
Single Solution, possibly part of a result set.
Definition: query.hpp:154