Lumiera  0.pre.03
»edit your freedom«
tree-mutator-test.cpp
Go to the documentation of this file.
1 /*
2  TreeMutator(Test) - customisable intermediary to abstract tree changing operations
3 
4  Copyright (C)
5  2015, 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/format-util.hpp"
21 #include "lib/test/test-helper.hpp"
23 #include "lib/format-cout.hpp"
24 #include "lib/format-util.hpp"
25 #include "lib/util.hpp"
26 
27 #include <string>
28 #include <vector>
29 
30 using util::isnil;
31 using util::join;
32 using util::typeStr;
33 
34 using std::string;
35 using std::vector;
36 
37 
38 namespace lib {
39 namespace diff{
40 namespace test{
41 
42 
43 
44 
45 
46  /*****************************************************************************/
59  class TreeMutator_test : public Test
60  {
61 
62  virtual void
63  run (Arg)
64  {
65  simpleAttributeBinding();
66  simpleCollectionBinding();
67  }
68 
69 
70  void
71  simpleAttributeBinding()
72  {
74  string localData;
75  auto mutator =
77  .change("data", [&](string val)
78  {
79  cout << "\"data\" closure received something "<<val<<endl;
80  localData = val;
81  });
82 
83  cout << "concrete TreeMutator size=" << sizeof(mutator)
84  << " type="<< typeStr(mutator)
85  << endl;
86 
87  mutator.init();
88 
89  CHECK (isnil (localData));
90  string testValue{"that would be acceptable"};
91  mutator.assignElm ({"lore", testValue});
92  CHECK ( isnil (localData)); // nothing happens, nothing changed
93  mutator.assignElm ({"data", testValue});
94  CHECK (!isnil (localData));
95  cout << "localData changed to: "<<localData<<endl;
96  CHECK (localData == "that would be acceptable");
97  }
98 
99 
100  void
101  simpleCollectionBinding()
102  {
104  vector<string> values;
105  values.push_back("a");
106  values.push_back("b");
107 
108  cout << join(values) <<endl;
109  CHECK (2 == values.size());
110  CHECK ("a, b" == join(values));
111 
112  auto mutator =
114  .attach (collection(values));
115 
116  cout << "concrete TreeMutator size=" << sizeof(mutator)
117  << " type="<< typeStr(mutator)
118  << endl;
119 
120  mutator.init();
121 
122  CHECK (isnil (values));
123  CHECK (mutator.matchSrc (GenNode("a")));
124  mutator.skipSrc (GenNode("a"));
125  CHECK (mutator.matchSrc (GenNode("b")));
126  CHECK (mutator.injectNew (GenNode("c")));
127  CHECK (mutator.acceptSrc (GenNode("b")));
128 
129  cout << join(values) <<endl;
130  CHECK (2 == values.size());
131  CHECK ("c, b" == join(values));
132  }
133  };
134 
135 
137  LAUNCHER (TreeMutator_test, "unit common");
138 
139 
140 
141 }}} // namespace lib::diff::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
Implementation namespace for support and library code.
static Builder< TreeMutator > build()
DSL: start building a custom adapted tree mutator, where the operations are tied by closures or wrapp...
#define MARK_TEST_FUN
Macro to mark the current test function in STDOUT.
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
Customisable intermediary to abstract generic tree mutation operations.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
auto collection(COLL &coll)
Entry point to a nested DSL for setup and configuration of a collection binding.
generic data element node within a tree
Definition: gen-node.hpp:222