Lumiera  0.pre.03
»edit your freedom«
format-helper-test.cpp
Go to the documentation of this file.
1 /*
2  FormatHelper(Test) - validate formatting and diagnostics helpers
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/format-util.hpp"
21 #include "lib/format-string.hpp"
22 #include "lib/iter-adapter-stl.hpp"
23 #include "lib/test/test-helper.hpp"
24 #include "lib/error.hpp"
25 
26 #include <vector>
27 #include <string>
28 
29 
33 using lib::eachNum;
34 using util::_Fmt;
35 
36 using std::vector;
37 using std::string;
38 using std::to_string;
39 
40 
41 namespace util {
42 namespace test {
43 
44  namespace { // test fixture...
45 
46  class Reticent
47  { };
48 
49  class UnReticent
50  : public Reticent
51  {
52  public:
53  operator string() const { return "hey Joe!"; }
54  };
55 
56 
57 
59  {
60  static uint cnt;
61 
62  uint id_;
63  double d_;
64 
65  public:
66  AutoCounter(double d)
67  : id_(++cnt)
68  , d_(d*2)
69  { }
70 
71  operator string() const
72  {
73  return _Fmt("Nr.%02d(%3.1f)") % id_ % d_;
74  }
75  };
76  uint AutoCounter::cnt = 0;
77  }
78 
79 
80 
81  /***************************************************************************/
90  : public Test
91  {
92  void
93  run (Arg)
94  {
95  check2String();
96  checkStringify();
97  checkStringJoin();
98  checkPrefixSuffix();
99  }
100 
101 
103  void
105  {
106  Reticent closeLipped;
107  UnReticent chatterer;
108 
109  CHECK (toString (closeLipped) == "«Reticent»"_expect);
110  CHECK (toString (chatterer) == "hey Joe!"_expect);
111 
112  CHECK (toString (&chatterer) == "↗hey Joe!"_expect); // pointer indicated
113  CHECK (toString (nullptr) == "↯"_expect); // runtime exception, caught
114 
115  CHECK (toString (true) == "true"_expect); // special handling for bool
116  CHECK (toString (2+2 == 5) == "false"_expect);
117  CHECK (toString (12.34e55) == "1.234e+56"_expect);
118 
119  CHECK (toString (short(12))
120  +toString (345L)
121  +toString ("67")
122  +toString ('8') == "12345678"_expect); // these go through lexical_cast<string>
123  }
124 
125 
130  void
132  {
133  // use as transformer within an (iterator) pipeline
134  auto ss = stringify (eachNum (1.11, 10.2));
135 
136  CHECK (ss);
137  CHECK ("1.11" == *ss);
138  ++ss;
139  CHECK ("2.11" == *ss);
140 
141  string res{".."};
142  for (auto s : ss)
143  res += s;
144 
145  CHECK (res == "..2.113.114.115.116.117.118.119.1110.11"_expect);
146 
147 
148  using VecS = vector<string>;
149 
150  // another variant: collect arbitrary heterogeneous arguments
151  VecS vals = stringify (short(12), 345L, "67", '8');
152  CHECK (vals == VecS({"12", "345", "67", "8"}));
153 
154 
155  // stringify can both consume (RValue-Ref) or take a copy from its source
156  auto nn = snapshot (eachNum (5, 10));
157  CHECK (5 == *nn);
158  ++nn;
159  CHECK (6 == *nn);
160 
161  auto sn = stringify (nn);
162  CHECK ("6" == *sn);
163  ++sn;
164  CHECK ("7" == *sn);
165  CHECK (6 == *nn);
166  ++++nn;
167  CHECK (8 == *nn);
168  CHECK ("7" == *sn);
169 
170  sn = stringify (std::move(nn));
171  CHECK ("8" == *sn);
172  CHECK (isnil (nn)); // was consumed by moving it into sn
173  ++sn;
174  CHECK ("9" == *sn);
175  ++sn;
176  CHECK (isnil (sn));
177  }
178 
179 
190  void
192  {
193  vector<double> dubious;
194  for (uint i=0; i<10; ++i)
195  dubious.push_back(1.1*i);
196 
197  std::function<AutoCounter(double)> justCount = [](double d){ return AutoCounter(d); };
198 
199 
200  CHECK (join (dubious, "--+--")
201  == "0--+--"
202  "1.1--+--"
203  "2.2--+--"
204  "3.3--+--"
205  "4.4--+--"
206  "5.5--+--"
207  "6.6--+--"
208  "7.7--+--"
209  "8.8--+--"
210  "9.9"_expect);
211  CHECK (join (transformIterator(eachElm(dubious), justCount))
212  == "Nr.01(0.0), "
213  "Nr.02(2.2), "
214  "Nr.03(4.4), "
215  "Nr.04(6.6), "
216  "Nr.05(8.8), "
217  "Nr.06(11.0), "
218  "Nr.07(13.2), "
219  "Nr.08(15.4), "
220  "Nr.09(17.6), "
221  "Nr.10(19.8)"_expect);
222  }
223 
224 
226  void
228  {
229  string abcdef{"abcdef"};
230  CHECK (startsWith (abcdef, "abcdef"));
231  CHECK (startsWith (abcdef, "abcde"));
232  CHECK (startsWith (abcdef, "abcd"));
233  CHECK (startsWith (abcdef, "abc"));
234  CHECK (startsWith (abcdef, "ab"));
235  CHECK (startsWith (abcdef, "a"));
236  CHECK (startsWith (abcdef, ""));
237 
238  CHECK (endsWith (abcdef, "abcdef"));
239  CHECK (endsWith (abcdef, "bcdef"));
240  CHECK (endsWith (abcdef, "cdef"));
241  CHECK (endsWith (abcdef, "def"));
242  CHECK (endsWith (abcdef, "ef"));
243  CHECK (endsWith (abcdef, "f"));
244  CHECK (endsWith (abcdef, ""));
245 
246  CHECK (startsWith (string{}, ""));
247  CHECK (endsWith (string{}, ""));
248 
249  CHECK (not startsWith (string{"abc"}, "abcd"));
250  CHECK (not startsWith (string{"a"}, "ä"));
251  CHECK (not startsWith (string{"ä"}, "a"));
252 
253  CHECK (not endsWith (string{"abc"}, " abc"));
254  CHECK (not endsWith (string{"a"}, "ä"));
255  CHECK (not endsWith (string{"ä"}, "a"));
256 
257  string abc{"abcdef"};
258  removePrefix(abc, "ab");
259  CHECK ("cdef" == abc);
260  removeSuffix(abc, "ef");
261  CHECK ("cd" == abc);
262 
263  abc = "bcdef";
264  removePrefix(abc, "ab");
265  CHECK ("bcdef" == abc);
266  removeSuffix(abc, "abcdef");
267  CHECK ("bcdef" == abc);
268  removeSuffix(abc, "bcdef");
269  CHECK (isnil (abc));
270  }
271  };
272 
273  LAUNCHER (FormatHelper_test, "unit common");
274 
275 
276 }} // namespace util::test
277 
vector< string > stringify(ELMS const &...elms)
standard setup: convert to string into a vector
bool startsWith(string const &str, string const &prefix)
check if string starts with a given prefix
Definition: util.hpp:185
Definition: run.hpp:40
Front-end for printf-style string template interpolation.
iter_stl::IterSnapshot< VAL > snapshot(std::initializer_list< VAL > const &&ili)
Take a snapshot of the given std::initializer_list.
A front-end for using printf-style formatting.
Simplistic test class runner.
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
Definition: format-obj.hpp:191
A collection of frequently used helper functions to support unit testing.
_SeqT< CON >::Range eachElm(CON &coll)
Lumiera error handling (C++ interface).
string join(CON &&coll, string const &delim=", ")
enumerate a collection&#39;s contents, separated by delimiter.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
bool endsWith(string const &str, string const &suffix)
check if string ends with the given suffix
Definition: util.hpp:198
Preconfigured adapters for some STL container standard usage situations.
auto transformIterator(IT const &src, FUN processingFunc)
Build a TransformIter: convenience free function shortcut, picking up the involved types automaticall...
Definition: itertools.hpp:788
NumIter< INT > eachNum(INT start=std::numeric_limits< INT >::min(), INT end=std::numeric_limits< INT >::max())
convenience function to iterate "each number"