Lumiera  0.pre.03
»edit your freedom«
verb-function-dispatch-test.cpp
Go to the documentation of this file.
1 /*
2  VerbFunctionDispatch(Test) - Concept to dispatch according to the verbs of a DSL
3 
4  Copyright (C) Lumiera.org
5  2014, 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 
29 #include "lib/test/run.hpp"
30 #include "lib/verb-token.hpp"
31 #include "lib/format-string.hpp"
32 #include "lib/format-cout.hpp"
33 
34 #include <string>
35 #include <vector>
36 
37 using std::string;
38 using util::_Fmt;
39 using std::vector;
40 
41 
42 namespace lib {
43 namespace test{
44 
45 
46  namespace { // Test Fixture
47 
49  class Receiver
50  {
51  public:
52  virtual ~Receiver() { }
53 
54  virtual string woof() =0;
55  virtual string honk() =0;
56  virtual string moo() =0;
57  virtual string meh() =0;
58  };
59 
60  const string BEGINNING("silence");
61 
63  using VerbSeq = vector<Verb>;
64 
65 
66  Verb VERB(Receiver, woof);
67  Verb VERB(Receiver, honk);
68  Verb VERB(Receiver, moo);
69  Verb VERB(Receiver, meh);
70 
71 
77  : public Receiver
78  {
79  string woof() { return "Woof-Woof!"; }
80  string honk() { return "Honk-Honk!"; }
81  string moo() { return "Moo-Moo!"; }
82  string meh() { return "Meh!"; }
83  };
84 
85 
90  : public Receiver
91  {
92  string verb_;
93  _Fmt fmt_;
94 
95  string
96  buildResultTerm (string nextToken)
97  {
98  string resultExpression (fmt_ % verb_ % nextToken);
99  verb_ = nextToken;
100  return resultExpression;
101  }
102 
103 
104  string woof() { return buildResultTerm (VERB_woof); }
105  string honk() { return buildResultTerm (VERB_honk); }
106  string moo() { return buildResultTerm (VERB_moo); }
107  string meh() { return buildResultTerm (VERB_meh); }
108 
109 
110  public:
112  : verb_(BEGINNING)
113  , fmt_("%s followed by %s")
114  { }
115  };
116 
117  }//(End) Test fixture
118 
119 
120 
121 
122 
123 
124  /**********************************************************************/
133  class VerbFunctionDispatch_test : public Test
134  {
135 
136  virtual void
137  run (Arg)
138  {
139  VerbSeq tokens = build_test_feed();
140  render_verbose (tokens);
141  verify_dispatch (tokens);
142  }
143 
144 
147  VerbSeq
149  {
150  return {
151  VERB_woof,
152  VERB_honk,
153  VERB_moo,
154  VERB_meh
155  };
156  }
157 
158 
164  void
165  render_verbose (VerbSeq tokens)
166  {
167  VerboseRenderer receiver;
168  for (Verb verb : tokens)
169  cout << "consuming " << verb
170  << " -> '"
171  << verb.applyTo(receiver)
172  << "'\n";
173  }
174 
175 
179  void
180  verify_dispatch (VerbSeq tokens)
181  {
182  RecollectingReceiver receiver;
183  string previous = BEGINNING;
184  for (Verb verb : tokens)
185  {
186  CHECK (previous+" followed by "+string(verb) == verb.applyTo(receiver));
187  previous = string(verb);
188  }
189  }
190  };
191 
192 
194  LAUNCHER (VerbFunctionDispatch_test, "unit common");
195 
196 
197 
198 }} // namespace lib::test
Action token implemented by double dispatch to a handler function, as defined in the "receiver" inter...
Definition: verb-token.hpp:78
Automatically use custom string conversion in C++ stream output.
Building blocks for a simple DSL using double dispatch to a handler function.
Definition: run.hpp:49
Front-end for printf-style string template interpolation.
A front-end for using printf-style formatting.
VerbSeq build_test_feed()
prepare a sequence of verbs for the actual tests to work on
Implementation namespace for support and library code.
Simple test class runner.