Lumiera  0.pre.03
»edit your freedom«
type-display-test.cpp
Go to the documentation of this file.
1 /*
2  TypeDisplay(Test) - human readable simplified display of C++ types
3 
4  Copyright (C) Lumiera.org
5  2016, 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/meta/util.hpp"
30 #include "lib/format-cout.hpp"
31 
32 #include <string>
33 
34 
35 using std::string;
36 
37 
38 namespace lib {
39 namespace meta{
40 namespace test{
41 
42  namespace { // test fixture
43 
44  template<class T>
45  struct Outer
46  {
47  struct Inner { };
48 
49  static const T*
50  cloak (Inner&&)
51  {
52  return nullptr;
53  }
54  };
55 
56  struct Space { };
57 
58  auto CHALLENGE_1 = "some::arbitrary::BullShit<oh::RLY>*";
59  auto CHALLENGE_2 = "lib::Contrived<lib::meta::Barely,true>::ClusterFuck<const std::string& (const std::vector<steam::mobject::oh::RLY* const>)>";
60  auto CHALLENGE_3 = "std::function<special::(anonymous namespace)::Shit(lib::P<steam::asset::Clip, std::shared_ptr<steam::asset::Clip>>)>";
61 
62  }//(end)fixture
63 
64 
65 
66 
67  /***************************************************************************/
94  : public Test
95  {
96  void
97  run (Arg)
98  {
99  cout << "-----input----------------------"<<endl;
100  cout << CHALLENGE_1 <<endl;
101  cout << CHALLENGE_2 <<endl;
102  cout << CHALLENGE_3 <<endl;
103 
104  cout << "-----human-readable-------------"<<endl;
105  cout << humanReadableTypeID (CHALLENGE_1) <<endl;
106  cout << humanReadableTypeID (CHALLENGE_2) <<endl;
107  cout << humanReadableTypeID (CHALLENGE_3) <<endl;
108 
109  cout << "-----primary-component----------" <<endl;
110  cout << primaryTypeComponent (CHALLENGE_1) <<endl;
111  cout << primaryTypeComponent (CHALLENGE_2) <<endl;
112  cout << primaryTypeComponent (CHALLENGE_3) <<endl;
113 
114  cout << "-----sanitised-ID---------------" <<endl;
115  cout << sanitisedFullTypeName (CHALLENGE_1) <<endl;
116  cout << sanitisedFullTypeName (CHALLENGE_2) <<endl;
117  cout << sanitisedFullTypeName (CHALLENGE_3) <<endl;
118 
119 
120  Outer<Space> ship;
121  auto magic = ship.cloak;
122  CHECK (typeStr(&magic) == "Space const* (*)(Outer<Space>::Inner&&)");
123  CHECK (typeSymbol(&magic) == "Function");
124 
125  CHECK (typeStr <Outer<decltype(this)>::Inner>() == "Outer<test::TypeDisplay_test*>::Inner");
126  CHECK (typeSymbol<Outer<decltype(this)>::Inner>() == "Inner" );
127 
128  CHECK (primaryTypeComponent("") == "void" );
129  CHECK (primaryTypeComponent("Sym&") == "Sym" );
130  CHECK (primaryTypeComponent("Sym const *") == "Sym" );
131  CHECK (primaryTypeComponent("Sym const * const") == "Sym" );
132  CHECK (primaryTypeComponent("Sym§$<>%&/'* const") == "Sym§$<>%&/'"); // adornments stripped, but rest retained as-is
133  CHECK (primaryTypeComponent("Sym§$<>%&/)&* const") == "Function" ); // types ending with braces are classified as "Function"
134  CHECK (primaryTypeComponent("Sym <§$&ää>") == "Sym " ); // note the trailing whitespace
135  CHECK (primaryTypeComponent("One<§$&ää>::Two") == "Two" );
136  CHECK (primaryTypeComponent("One::Two<§$&ää>") == "Two" );
137  CHECK (primaryTypeComponent("One::Two<§$&ää>") == "Two" );
138  CHECK (primaryTypeComponent("Sym<<xx>") == "Sym<" ); // unbalanced braces
139  CHECK (primaryTypeComponent("Sym<>xx>") == "void" ); // opening brace never found
140  CHECK (primaryTypeComponent("<x>xx>*") == "void" );
141  CHECK (primaryTypeComponent("<x<xx>*") == "<x" );
142 
143  CHECK (sanitisedSymbol("bäälü9a/(6f*a☹☢☀s☭el_88☠") == "blafasel_88"); // note: picking up only valid identifier fragments
144 
145  }
146  };
147 
148  LAUNCHER (TypeDisplay_test, "unit common");
149 
150 
151 }}} // namespace lib::meta::test
152 
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:49
Simple and lightweight helpers for metaprogramming and type detection.
Implementation namespace for support and library code.
string primaryTypeComponent(Literal rawType)
extract core name component from a raw type spec
Definition: format-obj.cpp:270
string sanitisedFullTypeName(lib::Literal rawName)
build a sanitised ID from full type name
Definition: format-obj.cpp:308
Simple test class runner.
string humanReadableTypeID(Literal rawType)
pretty-print an internal C++ type representation
Definition: format-obj.cpp:199
string typeSymbol()
Short readable type identifier, not necessarily unique or complete.
Definition: genfunc.hpp:87
string sanitisedSymbol(string const &text)
condense a string and retain only valid identifiers
Definition: format-obj.cpp:315
std::string typeStr(TY const *obj=nullptr) noexcept
failsafe human readable type display
Definition: meta/util.hpp:325