unit-test(#985): define more tests
This commit is contained in:
parent
3672873ae6
commit
88120eba1a
9 changed files with 245 additions and 133 deletions
|
|
@ -28,8 +28,23 @@
|
|||
** by the representation of the pointee. When the displayed entity defines an
|
||||
** `operator string()`, this custom string conversion will be used (suppressing
|
||||
** any exceptions, of course). As fallback, a simplified type string is printed.
|
||||
**
|
||||
** \par policy
|
||||
** What shall be expected from a generic toString conversion?
|
||||
** It should be _minimal_, it should be _transparent_ and it should
|
||||
** always work and deliver a string, irrespective of the circumstances.
|
||||
** By extension, this means that we do not want to differentiate much
|
||||
** between values, references and pointers, which also means, we do
|
||||
** not want to indicate pointers explicitly (just signal NULL, when
|
||||
** encountered). The situation is slightly different for the `ostream`
|
||||
** inserter; in a modern GUI application, there isn't much use for
|
||||
** STDOUT and STDERR, beyond error messages and unit testing.
|
||||
** Thus, we can strive at building a more convenient flavour
|
||||
** here, which does indeed even show the address of pointers.
|
||||
**
|
||||
** @see FormatCOUT_test
|
||||
** @see FormatHelper_test
|
||||
** @see [generic string conversion helper](\ref util::toString)
|
||||
** @see [frontend for boost::format, printf-style](format-string.hpp)
|
||||
**
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -24,15 +24,19 @@
|
|||
/** @file format-obj.hpp
|
||||
** Simple functions to represent objects, for debugging and diagnostics.
|
||||
** The helpers provided here are rather commonplace, but written in a way
|
||||
** as to incur a rather modest header inclusion load. It should be OK to
|
||||
** use these even on widely used interface headers.
|
||||
** as to incur only modest header inclusion load. It should be OK to use
|
||||
** these even on widely used interface headers.
|
||||
** - util::toString() performs a failsafe to-String conversion, thereby preferring a
|
||||
** built-in conversion operator, falling back to a lexical conversion (boost)
|
||||
** or just a unmangled and simplified type string as default.
|
||||
** - util::typedString() combines this with a always visible type display
|
||||
** - lib::meta::demangleCxx() uses the built-in compiler support to translate a mangled
|
||||
** type-ID (as given by `typeid(TY).name()`) into a readable, fully qualified
|
||||
** C++ type name. This is only supported for GNU compatible compilers.
|
||||
**
|
||||
** @todo is it possible to stash away the `boost::lexical_cast` behind a custom facade,
|
||||
** the way we did it for `boost::format`? This would reduce inclusion cost...
|
||||
**
|
||||
** @see FormatHelper_test
|
||||
** @see [frontend for boost::format, printf-style](format-string.hpp)
|
||||
**
|
||||
|
|
|
|||
|
|
@ -254,8 +254,8 @@ namespace meta {
|
|||
/** compare unadorned types, disregarding const and references */
|
||||
template<typename T, typename U>
|
||||
struct is_basically
|
||||
: is_same <typename Strip<T>::TypeReferred
|
||||
,typename Strip<U>::TypeReferred>
|
||||
: is_same <typename Strip<T>::TypePlain
|
||||
,typename Strip<U>::TypePlain>
|
||||
{ };
|
||||
|
||||
/** detect various flavours of string / text data */
|
||||
|
|
|
|||
|
|
@ -47,12 +47,6 @@ return: 0
|
|||
END
|
||||
|
||||
|
||||
TEST "Helper to show demangled C++ names" TestHelperDemangling_test <<END
|
||||
out: .+lib.test.test.Space.+Outer.+Inner
|
||||
out-lit: lib::test::test::Space const* (*)(lib::test::test::Outer<lib::test::test::Space>::Inner const&&)
|
||||
END
|
||||
|
||||
|
||||
TEST "Testsuite option handling" TestOption_test <<END
|
||||
out: Testing invocation with cmdline: ...
|
||||
out: --> Testgroup=ALL
|
||||
|
|
|
|||
|
|
@ -320,6 +320,16 @@ out-lit: :.
|
|||
END
|
||||
|
||||
|
||||
TEST "Helper to show demangled C++ names" TypeDemangling_test <<END
|
||||
out: .+lib.test.test.Space.+Outer.+Inner
|
||||
out-lit: lib::test::test::Space const* (*)(lib::test::test::Outer<lib::test::test::Space>::Inner const&&)
|
||||
END
|
||||
|
||||
|
||||
TEST "Helper for human readable type display" TypeDisplay_test <<END
|
||||
END
|
||||
|
||||
|
||||
TEST "TypeList_test" TypeList_test <<END
|
||||
out: - 1 -- 2 -- 3 -- 5 -- 8 -- 13 -- The End -
|
||||
out: ..Size of = 1
|
||||
|
|
|
|||
|
|
@ -183,6 +183,10 @@ return: 0
|
|||
END
|
||||
|
||||
|
||||
TEST "formatting/string conversion in output" FormatCOUT_test <<END
|
||||
END
|
||||
|
||||
|
||||
TEST "formatting/diagnostics helpers" FormatHelper_test <<END
|
||||
out: Displaying some types....
|
||||
out: «util::test::Reticent»
|
||||
|
|
|
|||
|
|
@ -22,40 +22,6 @@
|
|||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
/** @file try.cpp
|
||||
** How to build generic string conversion into `ostream::operator<< `.
|
||||
**
|
||||
** This task is actually a conglomerate of several chores:
|
||||
** - sanitise and segregate the type-traits usage
|
||||
** - disentangle the existing toString conversion helper
|
||||
** - extract a basic form from this helper, which can be placed
|
||||
** into a header with minimal dependencies. After some consideration,
|
||||
** I decided to allow `<typeinfo>` in this category, which allows us
|
||||
** at least to show a type name as fallback
|
||||
** - distill an essential version of `enable_if`, which can be inlined.
|
||||
** This allows us to get rid of `boost::enable_if` finally.
|
||||
** - build a sensible `operator string()` for our `lib::P` based on this
|
||||
** - and _finally_, to come up with a templated version of the `ostream`
|
||||
** inserter `operator<<`, which does not cause too much havoc when
|
||||
** used by default. The greatest challenge here is to avoid ambiguous
|
||||
** overloads, yet also to deal with references, `void` and arrays.
|
||||
**
|
||||
** \par policy
|
||||
** What shall be expected from such a generic toString conversion?
|
||||
** It should be _minimal_, it should be _transparent_ and it should
|
||||
** always work and deliver a string, irrespective of the circumstances.
|
||||
** By extension, this means that we do not want to differentiate much
|
||||
** between values, references and pointers, which also means, we do
|
||||
** not want to indicate pointers explicitly (just signal NULL, when
|
||||
** encountered). The situation is slightly different for the `ostream`
|
||||
** inserter; in a modern GUI application, there isn't much use for
|
||||
** STDOUT and STDERR, beyond error messages and unit testing.
|
||||
** Thus, we can strive at building a more convenient flavour
|
||||
** here, which does indeed even shows the address of pointers.
|
||||
**
|
||||
*/
|
||||
|
||||
typedef unsigned int uint;
|
||||
|
||||
#include "lib/p.hpp"
|
||||
#include "lib/diff/gen-node.hpp"
|
||||
|
|
@ -69,105 +35,115 @@ typedef unsigned int uint;
|
|||
using lib::P;
|
||||
using lib::newP;
|
||||
using lib::diff::GenNode;
|
||||
using lib::meta::is_basically;
|
||||
using lib::meta::is_StringLike;
|
||||
using lib::meta::can_lexical2string;
|
||||
using lib::meta::can_convertToString;
|
||||
using lib::meta::use_StringConversion4Stream;
|
||||
|
||||
using std::string;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace util {
|
||||
namespace test {
|
||||
|
||||
class Reticent
|
||||
{
|
||||
uint neigh_ = 42;
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
using BasicallyString = is_basically<T, string>;
|
||||
template<typename T>
|
||||
using BasicallyChar = std::is_convertible<T, const char*>;
|
||||
|
||||
|
||||
void
|
||||
showTypes()
|
||||
{
|
||||
namespace { // test fixture
|
||||
|
||||
/** opaque class without string conversion */
|
||||
class Reticent
|
||||
{
|
||||
uint neigh_ = 42;
|
||||
};
|
||||
|
||||
|
||||
using lib::meta::is_basically;
|
||||
using lib::meta::is_StringLike;
|
||||
using lib::meta::can_lexical2string;
|
||||
using lib::meta::can_convertToString;
|
||||
using lib::meta::use_StringConversion4Stream;
|
||||
|
||||
template<typename T>
|
||||
using BasicallyString = is_basically<T, string>;
|
||||
template<typename T>
|
||||
using BasicallyCString = std::is_convertible<T, const char*>;
|
||||
|
||||
|
||||
#define SHOW_CHECK(_EXPR_) cout << STRINGIFY(_EXPR_) << "\t : " << (_EXPR_::value? "Yes":"No") << endl;
|
||||
#define ANALYSE(_TYPE_) \
|
||||
cout << "Type: " STRINGIFY(_TYPE_) " ......"<<endl; \
|
||||
SHOW_CHECK (is_StringLike<_TYPE_>); \
|
||||
SHOW_CHECK (BasicallyChar<_TYPE_>); \
|
||||
SHOW_CHECK (BasicallyString<_TYPE_>); \
|
||||
SHOW_CHECK (std::is_arithmetic<_TYPE_>); \
|
||||
SHOW_CHECK (can_lexical2string<_TYPE_>); \
|
||||
SHOW_CHECK (can_convertToString<_TYPE_>); \
|
||||
SHOW_CHECK (use_StringConversion4Stream<_TYPE_>);
|
||||
#define ANALYSE(_TYPE_) \
|
||||
cout << "Type: " STRINGIFY(_TYPE_) " ......"<<endl; \
|
||||
SHOW_CHECK (is_StringLike<_TYPE_>); \
|
||||
SHOW_CHECK (BasicallyString<_TYPE_>); \
|
||||
SHOW_CHECK (BasicallyCString<_TYPE_>); \
|
||||
SHOW_CHECK (std::is_arithmetic<_TYPE_>); \
|
||||
SHOW_CHECK (can_lexical2string<_TYPE_>); \
|
||||
SHOW_CHECK (can_convertToString<_TYPE_>); \
|
||||
SHOW_CHECK (use_StringConversion4Stream<_TYPE_>);
|
||||
|
||||
|
||||
using CharLit = typeof("bla");
|
||||
using CharPtr = typeof(const char*);
|
||||
using GenNodePtr = typeof(GenNode*);
|
||||
using GenNodeRef = typeof(GenNode&);
|
||||
|
||||
ANALYSE (string);
|
||||
ANALYSE (CharLit);
|
||||
ANALYSE (CharPtr)
|
||||
ANALYSE (Reticent)
|
||||
ANALYSE (P<Reticent>)
|
||||
ANALYSE (GenNode)
|
||||
ANALYSE (GenNodePtr)
|
||||
ANALYSE (GenNodeRef)
|
||||
ANALYSE (P<GenNode>)
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
showTraits()
|
||||
{
|
||||
using CharLit = typeof("literal");
|
||||
using CharPtr = typeof(const char*);
|
||||
using StrCRef = typeof(string const&);
|
||||
using GenNodePtr = typeof(GenNode*);
|
||||
using GenNodeRef = typeof(GenNode&);
|
||||
|
||||
ANALYSE (double);
|
||||
ANALYSE (string);
|
||||
ANALYSE (StrCRef);
|
||||
ANALYSE (CharLit);
|
||||
ANALYSE (CharPtr)
|
||||
ANALYSE (Reticent)
|
||||
ANALYSE (P<Reticent>)
|
||||
ANALYSE (GenNode)
|
||||
ANALYSE (GenNodePtr)
|
||||
ANALYSE (GenNodeRef)
|
||||
ANALYSE (P<GenNode>)
|
||||
cout << endl;
|
||||
}
|
||||
}//(end)fixture
|
||||
|
||||
|
||||
|
||||
|
||||
/***************************************************************************//**
|
||||
* @test verifies the proper working of some string-formatting helper functions.
|
||||
* - util::toString() provides a failsafe to-String conversion, preferring
|
||||
* an built-in conversion, falling back to just a type string.
|
||||
* - util::join() combines elements from arbitrary containers or iterators
|
||||
* into a string, relyint on aforementioned generic string conversion
|
||||
* @see format-util.hpp
|
||||
* @test How to build generic string conversion into `ostream::operator<< `.
|
||||
* This task (#985) was actually a conglomerate of several chores:
|
||||
* - sanitise and segregate the type-traits usage
|
||||
* - disentangle the existing util::str conversion helper
|
||||
* - extract a basic form from this helper, which can be placed
|
||||
* into a header with minimal dependencies. After some consideration,
|
||||
* I decided to allow `<typeinfo>` in this category, which allows us
|
||||
* at least to show a type name as fallback
|
||||
* - distill an essential version of `enable_if`, which can be inlined.
|
||||
* This allows us to get rid of `boost::enable_if` finally.
|
||||
* - build a sensible `operator string()` for our `lib::P` based on this
|
||||
* - and _finally_, to come up with a templated version of the `ostream`
|
||||
* inserter `operator<<`, which does not cause too much havoc when
|
||||
* used by default. The greatest challenge here is to avoid ambiguous
|
||||
* overloads, yet also to deal with references, `void` and arrays.
|
||||
*
|
||||
* @see format-cout.hpp
|
||||
* @see FormatHelper_test
|
||||
*/
|
||||
class FormatCOUT_test
|
||||
: public Test
|
||||
{
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
showTypes();
|
||||
|
||||
auto psss = newP<Reticent>();
|
||||
auto gnng = newP<GenNode>("Hui", "Buh");
|
||||
|
||||
cout << "mauu..." << psss <<endl;
|
||||
cout << "wauu..." << gnng <<endl;
|
||||
|
||||
cout << "mauuu.." << *psss <<endl;
|
||||
cout << "wauuu.." << *gnng <<endl;
|
||||
cout << "wauuup." << gnng.get() <<endl;
|
||||
|
||||
gnng.reset();
|
||||
cout << "aauu..." << gnng <<endl;
|
||||
cout << "aauu..." << gnng.get() <<endl;
|
||||
|
||||
cout << "\n.gulp.\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
{
|
||||
showTraits();
|
||||
|
||||
auto silent = newP<Reticent>();
|
||||
auto chatty = newP<GenNode>("Hui", "Buh");
|
||||
|
||||
cout << "smart-ptr, no string conv..." << silent <<endl;
|
||||
cout << "smart-ptr, custom conv......" << chatty <<endl;
|
||||
|
||||
cout << "reference, no string conv..." << *silent <<endl;
|
||||
cout << "reference, custom conv......" << *chatty <<endl;
|
||||
cout << "pointer, custom conv......" << chatty.get() <<endl;
|
||||
|
||||
chatty.reset();
|
||||
cout << "smart-ptr, NULL pointee....." << chatty <<endl;
|
||||
cout << "pointer, NULL pointee....." << chatty.get() <<endl;
|
||||
}
|
||||
};
|
||||
|
||||
LAUNCHER (FormatCOUT_test, "unit common");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
TestHelperDemangling(Test) - ensure a helper for C++ demangling works as expected
|
||||
TypeDemangling(Test) - ensure a helper for C++ demangling works as expected
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2014, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/test/test-helper.hpp"
|
||||
#include "lib/meta/util.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
|
@ -33,7 +33,7 @@ using std::endl;
|
|||
|
||||
|
||||
namespace lib {
|
||||
namespace test{
|
||||
namespace meta{
|
||||
namespace test{
|
||||
|
||||
template<class T>
|
||||
|
|
@ -42,7 +42,7 @@ namespace test{
|
|||
struct Inner { };
|
||||
|
||||
static const T*
|
||||
phantom (Inner const&&)
|
||||
cloak (Inner const&&)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
|
@ -62,13 +62,13 @@ namespace test{
|
|||
*
|
||||
* @see test-helper.hpp
|
||||
*/
|
||||
class TestHelperDemangling_test : public Test
|
||||
class TypeDemangling_test : public Test
|
||||
{
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
Outer<Space> ship;
|
||||
auto magic = &ship.phantom;
|
||||
auto magic = &ship.cloak;
|
||||
auto rawType = typeid(magic).name();
|
||||
|
||||
cout << rawType << endl;
|
||||
|
|
@ -76,7 +76,7 @@ namespace test{
|
|||
}
|
||||
};
|
||||
|
||||
LAUNCHER (TestHelperDemangling_test, "unit common");
|
||||
LAUNCHER (TypeDemangling_test, "unit common");
|
||||
|
||||
|
||||
}}} // namespace lib::test::test
|
||||
}}} // namespace lib::meta::test
|
||||
109
tests/library/meta/type-display-test.cpp
Normal file
109
tests/library/meta/type-display-test.cpp
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
TypeDisplay(Test) - human readable simplified display of C++ types
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2016, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
|
||||
//#include "lib/p.hpp"
|
||||
//#include "lib/diff/gen-node.hpp"
|
||||
|
||||
#include "lib/meta/util.hpp"
|
||||
|
||||
#include <iostream> //////////TODO
|
||||
#include <string>
|
||||
|
||||
//using lib::P;
|
||||
//using lib::newP;
|
||||
//using lib::diff::GenNode;
|
||||
|
||||
using std::string;
|
||||
using std::cout; /////////////TODO
|
||||
using std::endl; /////////////TODO
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace meta{
|
||||
namespace test{
|
||||
|
||||
namespace { // test fixture
|
||||
|
||||
template<class T>
|
||||
struct Outer
|
||||
{
|
||||
struct Inner { };
|
||||
|
||||
static const T*
|
||||
cloak (Inner const&&)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
struct Space { };
|
||||
|
||||
auto CHALLENGE_1 = "some::arbitrary::BullShit<oh::RLY>";
|
||||
auto CHALLENGE_2 = "lib::BullShit<const std::string& (const std::vector<proc::mobject::oh::RLY* const>)>";
|
||||
auto CHALLENGE_3 = "std::function<special::(anonymous namespace)::Shit(lib::P<asset::Clip, std::shared_ptr<asset::Clip>>)>";
|
||||
|
||||
}//(end)fixture
|
||||
|
||||
|
||||
|
||||
|
||||
/***************************************************************************//**
|
||||
* @test verify post processing of demangled C++ type names.
|
||||
* The purpose of those pretty printing functions is to support diagnostics
|
||||
* and unit testing by making type names easier to digest. But of course
|
||||
* we do not want to pick the right primary type for shortened display
|
||||
* and we do not want mess up the semantic structure.
|
||||
*
|
||||
* @see format-cout.hpp
|
||||
* @see FormatHelper_test
|
||||
*/
|
||||
class TypeDisplay_test
|
||||
: public Test
|
||||
{
|
||||
void
|
||||
run (Arg)
|
||||
{
|
||||
cout << "-----human-readable-------------"<<endl;
|
||||
cout << humanReadableTypeID (CHALLENGE_1) <<endl;
|
||||
cout << humanReadableTypeID (CHALLENGE_2) <<endl;
|
||||
cout << humanReadableTypeID (CHALLENGE_3) <<endl;
|
||||
|
||||
cout << "-----primary-component----------" <<endl;
|
||||
cout << primaryTypeComponent (CHALLENGE_1) <<endl;
|
||||
cout << primaryTypeComponent (CHALLENGE_2) <<endl;
|
||||
cout << primaryTypeComponent (CHALLENGE_3) <<endl;
|
||||
|
||||
cout << "-----sanitised-ID---------------" <<endl;
|
||||
cout << sanitisedFullTypeName (CHALLENGE_1) <<endl;
|
||||
cout << sanitisedFullTypeName (CHALLENGE_2) <<endl;
|
||||
cout << sanitisedFullTypeName (CHALLENGE_3) <<endl;
|
||||
}
|
||||
};
|
||||
|
||||
LAUNCHER (TypeDisplay_test, "unit common");
|
||||
|
||||
|
||||
}}} // namespace lib::meta::test
|
||||
|
||||
Loading…
Reference in a new issue