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)
5  2014, 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 
20 #include "lib/test/run.hpp"
21 #include "lib/verb-token.hpp"
22 #include "lib/format-string.hpp"
23 #include "lib/format-cout.hpp"
24 
25 #include <string>
26 #include <vector>
27 
28 using std::string;
29 using util::_Fmt;
30 using std::vector;
31 
32 
33 namespace lib {
34 namespace test{
35 
36 
37  namespace { // Test Fixture
38 
40  class Receiver
41  {
42  public:
43  virtual ~Receiver() { }
44 
45  virtual string woof() =0;
46  virtual string honk() =0;
47  virtual string moo() =0;
48  virtual string meh() =0;
49  };
50 
51  const string BEGINNING("silence");
52 
54  using VerbSeq = vector<Verb>;
55 
56 
57  Verb VERB(Receiver, woof);
58  Verb VERB(Receiver, honk);
59  Verb VERB(Receiver, moo);
60  Verb VERB(Receiver, meh);
61 
62 
68  : public Receiver
69  {
70  string woof() { return "Woof-Woof!"; }
71  string honk() { return "Honk-Honk!"; }
72  string moo() { return "Moo-Moo!"; }
73  string meh() { return "Meh!"; }
74  };
75 
76 
81  : public Receiver
82  {
83  string verb_;
84  _Fmt fmt_;
85 
86  string
87  buildResultTerm (string nextToken)
88  {
89  string resultExpression (fmt_ % verb_ % nextToken);
90  verb_ = nextToken;
91  return resultExpression;
92  }
93 
94 
95  string woof() { return buildResultTerm (VERB_woof); }
96  string honk() { return buildResultTerm (VERB_honk); }
97  string moo() { return buildResultTerm (VERB_moo); }
98  string meh() { return buildResultTerm (VERB_meh); }
99 
100 
101  public:
103  : verb_(BEGINNING)
104  , fmt_("%s followed by %s")
105  { }
106  };
107 
108  }//(End) Test fixture
109 
110 
111 
112 
113 
114 
115  /**********************************************************************/
124  class VerbFunctionDispatch_test : public Test
125  {
126 
127  virtual void
128  run (Arg)
129  {
130  VerbSeq tokens = build_test_feed();
131  render_verbose (tokens);
132  verify_dispatch (tokens);
133  }
134 
135 
138  VerbSeq
140  {
141  return {
142  VERB_woof,
143  VERB_honk,
144  VERB_moo,
145  VERB_meh
146  };
147  }
148 
149 
155  void
156  render_verbose (VerbSeq tokens)
157  {
158  VerboseRenderer receiver;
159  for (Verb verb : tokens)
160  cout << "consuming " << verb
161  << " -> '"
162  << verb.applyTo(receiver)
163  << "'\n";
164  }
165 
166 
170  void
171  verify_dispatch (VerbSeq tokens)
172  {
173  RecollectingReceiver receiver;
174  string previous = BEGINNING;
175  for (Verb verb : tokens)
176  {
177  CHECK (previous+" followed by "+string(verb) == verb.applyTo(receiver));
178  previous = string(verb);
179  }
180  }
181  };
182 
183 
185  LAUNCHER (VerbFunctionDispatch_test, "unit common");
186 
187 
188 
189 }} // namespace lib::test
Action token implemented by double dispatch to a handler function, as defined in the "receiver" inter...
Definition: verb-token.hpp:69
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:40
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.
Simplistic test class runner.