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) Lumiera.org
5  2015, 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/format-util.hpp"
30 #include "lib/test/test-helper.hpp"
32 #include "lib/format-cout.hpp"
33 #include "lib/format-util.hpp"
34 #include "lib/util.hpp"
35 
36 #include <string>
37 #include <vector>
38 
39 using util::isnil;
40 using util::join;
41 using util::typeStr;
42 
43 using std::string;
44 using std::vector;
45 
46 
47 namespace lib {
48 namespace diff{
49 namespace test{
50 
51 
52 
53 
54 
55  /*****************************************************************************/
68  class TreeMutator_test : public Test
69  {
70 
71  virtual void
72  run (Arg)
73  {
74  simpleAttributeBinding();
75  simpleCollectionBinding();
76  }
77 
78 
79  void
80  simpleAttributeBinding()
81  {
83  string localData;
84  auto mutator =
86  .change("data", [&](string val)
87  {
88  cout << "\"data\" closure received something "<<val<<endl;
89  localData = val;
90  });
91 
92  cout << "concrete TreeMutator size=" << sizeof(mutator)
93  << " type="<< typeStr(mutator)
94  << endl;
95 
96  mutator.init();
97 
98  CHECK (isnil (localData));
99  string testValue{"that would be acceptable"};
100  mutator.assignElm ({"lore", testValue});
101  CHECK ( isnil (localData)); // nothing happens, nothing changed
102  mutator.assignElm ({"data", testValue});
103  CHECK (!isnil (localData));
104  cout << "localData changed to: "<<localData<<endl;
105  CHECK (localData == "that would be acceptable");
106  }
107 
108 
109  void
110  simpleCollectionBinding()
111  {
113  vector<string> values;
114  values.push_back("a");
115  values.push_back("b");
116 
117  cout << join(values) <<endl;
118  CHECK (2 == values.size());
119  CHECK ("a, b" == join(values));
120 
121  auto mutator =
123  .attach (collection(values));
124 
125  cout << "concrete TreeMutator size=" << sizeof(mutator)
126  << " type="<< typeStr(mutator)
127  << endl;
128 
129  mutator.init();
130 
131  CHECK (isnil (values));
132  CHECK (mutator.matchSrc (GenNode("a")));
133  mutator.skipSrc (GenNode("a"));
134  CHECK (mutator.matchSrc (GenNode("b")));
135  CHECK (mutator.injectNew (GenNode("c")));
136  CHECK (mutator.acceptSrc (GenNode("b")));
137 
138  cout << join(values) <<endl;
139  CHECK (2 == values.size());
140  CHECK ("c, b" == join(values));
141  }
142  };
143 
144 
146  LAUNCHER (TreeMutator_test, "unit common");
147 
148 
149 
150 }}} // namespace lib::diff::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
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.
Simple 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:231