Skip to content
Snippets Groups Projects
Commit 261e4817 authored by Tomáš Pecka's avatar Tomáš Pecka
Browse files

Unit tests: CPPUNIT -> Catch2: cli

parent 21edcf2e
No related branches found
No related tags found
1 merge request!62Dev tp
#include "CliTest.h"
#include <catch2/catch.hpp>
 
#include <alib/iostream>
 
......@@ -9,320 +9,311 @@
#include <registry/AlgorithmRegistry.hpp>
#include <registration/AlgoRegistration.hpp>
 
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( CliTest, "common" );
CPPUNIT_TEST_SUITE_REGISTRATION ( CliTest );
void CliTest::setUp ( ) {
}
void CliTest::tearDown ( ) {
}
class One {
public:
static int one ( ) {
return 1;
}
};
class Two {
public:
static int two ( ) {
return 2;
}
};
class Add {
public:
static int add ( int a, const int & b ) {
return a + b;
}
static int add2 ( const int & a, int b ) {
return a + b;
}
};
class Neg {
public:
static int neg ( int a ) {
return - a;
class Foo {
int m_base;
public:
Foo ( int base ) : m_base ( base ) {
}
};
 
class IntToDouble {
public:
static double cast ( int a ) {
return a;
int bar ( int value ) {
return m_base + value;
}
};
 
class Divide {
public:
static double divide ( double a, double b ) {
return a / b;
int base ( ) {
return m_base;
}
 
static int divide2 ( int a, int b ) {
return a / b;
static Foo make_foo ( int base ) {
return Foo ( base );
}
};
 
void CliTest::testCreateUnique ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < One > ( One::one, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Two > ( Two::two, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Add > ( Add::add, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Add > ( Add::add2, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Neg > ( Neg::neg, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Divide > ( Divide::divide, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Divide > ( Divide::divide2, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
mkdir ( "local", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
namespace {
auto fooBar = registration::MethodRegister < Foo, int, Foo, int > ( & Foo::bar, "bar" );
} /* namespace */
 
// execute automaton::Determinize < aaa.xml <( parseString aaa.text ) <(decodeTree aaa.raw) > res.xml // determinizuj, parametry podle ocekavaneho typu z xml, string z textove reprezentace, strom z raw reprezentace; zapis jako xml do souboru
// execute automaton::RandomAutomaton DFA 2 2 2 // nahodny automat DFA s danymi vlastnostmi, vypis na konzoli
// execute string::Normalize < str.xml | string::PatternMatch - < pattern.xml > match_result.xml // - je predchozi vysledek
 
cli::Environment environment;
environment.setBinding ( "1", "1" );
cli::Parser parser ( cli::Lexer ( "execute One | Add <( Add (int) #1 <(One) ) - | Neg - > local/xxx.xml" ) );
parser.parse ( )->run ( environment );
TEST_CASE ( "Cli", "[unit][cli]" ) {
class One {
public:
static int one ( ) {
return 1;
}
};
class Two {
public:
static int two ( ) {
return 2;
}
};
class Add {
public:
static int add ( int a, const int & b ) {
return a + b;
}
static int add2 ( const int & a, int b ) {
return a + b;
}
};
class Neg {
public:
static int neg ( int a ) {
return - a;
}
};
class IntToDouble {
public:
static double cast ( int a ) {
return a;
}
};
class Divide {
public:
static double divide ( double a, double b ) {
return a / b;
}
static int divide2 ( int a, int b ) {
return a / b;
}
};
SECTION ( "Test Create Unique" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < One > ( One::one, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Two > ( Two::two, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Add > ( Add::add, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Add > ( Add::add2, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Neg > ( Neg::neg, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Divide > ( Divide::divide, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Divide > ( Divide::divide2, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 2 > ( ) );
mkdir ( "local", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
// execute automaton::Determinize < aaa.xml <( parseString aaa.text ) <(decodeTree aaa.raw) > res.xml // determinizuj, parametry podle ocekavaneho typu z xml, string z textove reprezentace, strom z raw reprezentace; zapis jako xml do souboru
// execute automaton::RandomAutomaton DFA 2 2 2 // nahodny automat DFA s danymi vlastnostmi, vypis na konzoli
// execute string::Normalize < str.xml | string::PatternMatch - < pattern.xml > match_result.xml // - je predchozi vysledek
 
environment.setBinding ( "2", "local/xxx.xml" );
parser = cli::Parser ( cli::Lexer ( "execute One | Add <( Add (int) <#2 <(One) ) - | Neg (double) - | Divide (double) - <(One | (double) Add <(One) - )" ) );
parser.parse ( )->run ( environment );
cli::Environment environment;
environment.setBinding ( "1", "1" );
cli::Parser parser ( cli::Lexer ( "execute One | Add <( Add (int) #1 <(One) ) - | Neg - > local/xxx.xml" ) );
parser.parse ( )->run ( environment );
 
parser = cli::Parser ( cli::Lexer ( "execute <:int #2" ) );
parser.parse ( )->run ( environment );
environment.setBinding ( "2", "local/xxx.xml" );
parser = cli::Parser ( cli::Lexer ( "execute One | Add <( Add (int) <#2 <(One) ) - | Neg (double) - | Divide (double) - <(One | (double) Add <(One) - )" ) );
parser.parse ( )->run ( environment );
 
parser = cli::Parser ( cli::Lexer ( "execute One > $res" ) );
parser.parse ( )->run ( environment );
parser = cli::Parser ( cli::Lexer ( "execute <:int #2" ) );
parser.parse ( )->run ( environment );
 
parser = cli::Parser ( cli::Lexer ( "execute $res" ) );
parser.parse ( )->run ( environment );
parser = cli::Parser ( cli::Lexer ( "execute One > $res" ) );
parser.parse ( )->run ( environment );
 
parser = cli::Parser ( cli::Lexer ( "execute Divide <(One) <(One)" ) );
CPPUNIT_ASSERT_NO_THROW ( parser.parse ( )->run ( environment ) );
parser = cli::Parser ( cli::Lexer ( "execute $res" ) );
parser.parse ( )->run ( environment );
 
abstraction::AlgorithmRegistry::unregisterAlgorithm < Divide, double, double > ( abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT );
abstraction::AlgorithmRegistry::unregisterAlgorithm < Divide, int, int > ( abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT );
parser = cli::Parser ( cli::Lexer ( "execute Divide <(One) <(One)" ) );
CHECK_NOTHROW ( parser.parse ( )->run ( environment ) );
 
CPPUNIT_ASSERT_THROW ( parser.parse ( )->run ( environment ), exception::CommonException );
}
abstraction::AlgorithmRegistry::unregisterAlgorithm < Divide, double, double > ( abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT );
abstraction::AlgorithmRegistry::unregisterAlgorithm < Divide, int, int > ( abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT );
 
class Source {
public:
static std::unique_ptr < int > source ( ) {
return std::make_unique < int > ( 1 );
CHECK_THROWS_AS ( parser.parse ( )->run ( environment ), exception::CommonException );
}
};
 
class Sink {
public:
static void sink ( std::unique_ptr < int > val ) {
std::cout << * val << std::endl;
}
};
class Source {
public:
static std::unique_ptr < int > source ( ) {
return std::make_unique < int > ( 1 );
}
};
 
void CliTest::testMove ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < Source > ( Source::source, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Sink > ( Sink::sink, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
class Sink {
public:
static void sink ( std::unique_ptr < int > val ) {
std::cout << * val << std::endl;
}
};
 
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute Source | Sink ^ - >" ) );
parser.parse ( )->run ( environment );
}
SECTION ( "Test Move" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < Source > ( Source::source, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < Sink > ( Sink::sink, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
 
static std::unique_ptr < int > source;
static std::unique_ptr < int > target;
class RvalueReferenceProvider {
public:
static std::unique_ptr < int > && foo ( ) {
return std::move ( source );
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute Source | Sink ^ - >" ) );
parser.parse ( )->run ( environment );
}
};
 
class RvalueReferenceAcceptor {
public:
static void bar ( std::unique_ptr < int > && out ) {
target = std::move ( out );
static std::unique_ptr < int > source;
static std::unique_ptr < int > target;
class RvalueReferenceProvider {
public:
static std::unique_ptr < int > && foo ( ) {
return std::move ( source );
}
};
class RvalueReferenceAcceptor {
public:
static void bar ( std::unique_ptr < int > && out ) {
target = std::move ( out );
}
};
SECTION ( "test Rvalue Reference Passing" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < RvalueReferenceProvider > ( RvalueReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < RvalueReferenceAcceptor > ( RvalueReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
{
source = std::make_unique < int > ( 1 );
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute RvalueReferenceProvider | RvalueReferenceAcceptor - >" ) );
CHECK_THROWS ( parser.parse ( )->run ( environment ) );
}
{
source = std::make_unique < int > ( 1 );
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute RvalueReferenceProvider | RvalueReferenceAcceptor ^ - >" ) );
CHECK_NOTHROW ( parser.parse ( )->run ( environment ) );
}
CHECK ( * target == 1 );
CHECK ( source == nullptr ); /* implementation specific */
}
};
 
void CliTest::testRvalueReferencePassing ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < RvalueReferenceProvider > ( RvalueReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < RvalueReferenceAcceptor > ( RvalueReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
class ConstReferenceProvider {
public:
static const std::string & foo ( ) {
static std::string dummy = "dummy";
return dummy;
}
};
class ConstReferenceAcceptor {
public:
static void bar ( const std::string & str ) {
std::cout << str << std::endl;
}
};
SECTION ( "Test Const Reference Passing" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ConstReferenceProvider > ( ConstReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < ConstReferenceAcceptor > ( ConstReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
 
try {
source = std::make_unique < int > ( 1 );
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute RvalueReferenceProvider | RvalueReferenceAcceptor - >" ) );
cli::Parser parser ( cli::Lexer ( "execute ConstReferenceProvider | ConstReferenceAcceptor - >" ) );
parser.parse ( )->run ( environment );
CPPUNIT_ASSERT ( false );
} catch ( ... ) {
CPPUNIT_ASSERT ( true );
}
source = std::make_unique < int > ( 1 );
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute RvalueReferenceProvider | RvalueReferenceAcceptor ^ - >" ) );
parser.parse ( )->run ( environment );
CPPUNIT_ASSERT ( * target == 1 );
CPPUNIT_ASSERT ( source == nullptr ); /* implementation specific */
}
class ConstReferenceProvider {
public:
static const std::string & foo ( ) {
static std::string dummy = "dummy";
return dummy;
}
};
class ConstReferenceAcceptor {
public:
static void bar ( const std::string & str ) {
std::cout << str << std::endl;
}
};
 
void CliTest::testConstReferencePassing ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ConstReferenceProvider > ( ConstReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < ConstReferenceAcceptor > ( ConstReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
class ReferenceProvider {
public:
static std::ostream & foo ( ) {
return std::cout;
}
};
 
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute ConstReferenceProvider | ConstReferenceAcceptor - >" ) );
parser.parse ( )->run ( environment );
}
class ReferenceAcceptor {
public:
static void bar ( std::ostream & out ) {
out << "yay" << std::endl;
}
};
 
class ReferenceProvider {
public:
static std::ostream & foo ( ) {
return std::cout;
}
};
SECTION ( "Test Reference Passing" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ReferenceProvider > ( ReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < ReferenceAcceptor > ( ReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
 
class ReferenceAcceptor {
public:
static void bar ( std::ostream & out ) {
out << "yay" << std::endl;
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute ReferenceProvider | ReferenceAcceptor - >" ) );
parser.parse ( )->run ( environment );
}
};
void CliTest::testReferencePassing ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ReferenceProvider > ( ReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < ReferenceAcceptor > ( ReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
 
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute ReferenceProvider | ReferenceAcceptor - >" ) );
parser.parse ( )->run ( environment );
}
class ConstRvalueReferenceProvider {
public:
static const std::string && foo ( ) {
static std::string dummy = "dummy";
return std::move ( dummy );
}
};
class ConstRvalueReferenceAcceptor {
public:
static void bar ( const std::string && str ) {
std::cout << str << std::endl;
}
};
SECTION ( "Test Const Rvalue Reference Passing" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ConstRvalueReferenceProvider > ( ConstRvalueReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < ConstRvalueReferenceAcceptor > ( ConstRvalueReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
 
class ConstRvalueReferenceProvider {
public:
static const std::string && foo ( ) {
static std::string dummy = "dummy";
return std::move ( dummy );
}
};
class ConstRvalueReferenceAcceptor {
public:
static void bar ( const std::string && str ) {
std::cout << str << std::endl;
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute ConstRvalueReferenceProvider | ConstRvalueReferenceAcceptor ^ - >" ) );
parser.parse ( )->run ( environment );
}
};
 
void CliTest::testConstRvalueReferencePassing ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < ConstRvalueReferenceProvider > ( ConstRvalueReferenceProvider::foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 0 > ( ) );
abstraction::AlgorithmRegistry::registerAlgorithm < ConstRvalueReferenceAcceptor > ( ConstRvalueReferenceAcceptor::bar, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
class Print {
public:
static void print ( ext::set < int > theSet ) {
for ( int value : theSet )
std::cout << value << ", ";
std::cout << std::endl;
}
};
 
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute ConstRvalueReferenceProvider | ConstRvalueReferenceAcceptor ^ - >" ) );
parser.parse ( )->run ( environment );
}
SECTION ( "Test Set Construction " ) {
abstraction::AlgorithmRegistry::registerAlgorithm < Print > ( Print::print, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
 
class Print {
public:
static void print ( ext::set < int > theSet ) {
for ( int value : theSet )
std::cout << value << ", ";
std::cout << std::endl;
}
};
void CliTest::testSetConstruction ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < Print > ( Print::print, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
cli::Environment environment;
 
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute { :int 1 2 3 } > $set" ) );
parser.parse ( )->run ( environment );
std::cout << environment.getVariable ( "set" )->getReturnType ( ) << std::endl;
parser = cli::Parser ( cli::Lexer ( "execute $set | Print -" ) );
parser.parse ( )->run ( environment );
cli::Parser parser ( cli::Lexer ( "execute { :int 1 2 3 } > $set" ) );
parser.parse ( )->run ( environment );
CHECK_THAT ( environment.getVariable ( "set" )->getReturnType ( ), Catch::Matchers::StartsWith ( "ext::set<int," ) );
 
parser = cli::Parser ( cli::Lexer ( "execute $set >lo\\cal/yyy.xml" ) );
parser.parse ( )->run ( environment );
parser = cli::Parser ( cli::Lexer ( "execute $set | Print -" ) );
parser.parse ( )->run ( environment );
 
parser = cli::Parser ( cli::Lexer ( "execute < :set @int \"local/yyy.xml\" > $set2" ) );
parser.parse ( )->run ( environment );
std::cout << environment.getVariable ( "set2" )->getReturnType ( ) << std::endl;
parser = cli::Parser ( cli::Lexer ( "execute $set2 | Print -" ) );
parser.parse ( )->run ( environment );
}
parser = cli::Parser ( cli::Lexer ( "execute $set >lo\\cal/yyy.xml" ) );
parser.parse ( )->run ( environment );
 
class Foo {
int m_base;
public:
Foo ( int base ) : m_base ( base ) {
}
parser = cli::Parser ( cli::Lexer ( "execute < :set @int \"local/yyy.xml\" > $set2" ) );
parser.parse ( )->run ( environment );
CHECK_THAT ( environment.getVariable ( "set2" )->getReturnType ( ), Catch::Matchers::StartsWith ( "ext::set<int," ) );
 
int bar ( int value ) {
return m_base + value;
parser = cli::Parser ( cli::Lexer ( "execute $set2 | Print -" ) );
parser.parse ( )->run ( environment );
}
 
int base ( ) {
return m_base;
}
SECTION ( "Test Member" ) {
abstraction::AlgorithmRegistry::registerAlgorithm < Foo > ( Foo::make_foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
abstraction::AlgorithmRegistry::registerMethod < Foo > ( & Foo::base, "base", std::array < std::string, 0 > ( ) );
 
static Foo make_foo ( int base ) {
return Foo ( base );
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute Foo 3 | Foo::base -" ) );
parser.parse ( )->run ( environment );
parser = cli::Parser ( cli::Lexer ( "execute Foo 3 | Foo::bar - 2" ) );
parser.parse ( )->run ( environment );
}
};
namespace {
 
auto fooBar = registration::MethodRegister < Foo, int, Foo, int > ( & Foo::bar, "bar" );
SECTION ( "Test XML IO" ) {
std::string in = "<Integer>1</Integer>";
 
} /* namespace */
void CliTest::testMember ( ) {
abstraction::AlgorithmRegistry::registerAlgorithm < Foo > ( Foo::make_foo, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, std::array < std::string, 1 > ( ) );
abstraction::AlgorithmRegistry::registerMethod < Foo > ( & Foo::base, "base", std::array < std::string, 0 > ( ) );
cli::Environment environment;
cli::Parser parser ( cli::Lexer ( "execute Foo 3 | Foo::base -" ) );
parser.parse ( )->run ( environment );
parser = cli::Parser ( cli::Lexer ( "execute Foo 3 | Foo::bar - 2" ) );
parser.parse ( )->run ( environment );
}
void CliTest::testXmlIO ( ) {
std::string in = "<Integer>1</Integer>";
cli::Environment environment;
environment.setVariable ( "in", in );
cli::Parser parser ( cli::Lexer ( "execute sax::SaxParseInterface $in | xml::Parse ^ - | Add <(One) - | xml::Compose - | sax::SaxComposeInterface - > $out" ) );
parser.parse ( )->run ( environment );
std::string out = environment.getVariable < std::string > ( "out" );
std::string ref = "<?xml version=\"1.0\"?>\n<Integer>2</Integer>\n";
cli::Environment environment;
environment.setVariable ( "in", in );
cli::Parser parser ( cli::Lexer ( "execute sax::SaxParseInterface $in | xml::Parse ^ - | Add <(One) - | xml::Compose - | sax::SaxComposeInterface - > $out" ) );
parser.parse ( )->run ( environment );
std::string out = environment.getVariable < std::string > ( "out" );
 
std::cout << out << std::endl;
std::cout << ref << std::endl;
std::string ref = "<?xml version=\"1.0\"?>\n<Integer>2</Integer>\n";
 
CPPUNIT_ASSERT ( out == ref );
CAPTURE ( out, ref );
CHECK ( out == ref );
}
}
#ifndef CLI_TEST_H_
#define CLI_TEST_H_
#include <cppunit/extensions/HelperMacros.h>
class CliTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE ( CliTest );
CPPUNIT_TEST ( testCreateUnique );
CPPUNIT_TEST ( testMove );
CPPUNIT_TEST ( testRvalueReferencePassing );
CPPUNIT_TEST ( testConstReferencePassing );
CPPUNIT_TEST ( testReferencePassing );
CPPUNIT_TEST ( testConstRvalueReferencePassing );
CPPUNIT_TEST ( testSetConstruction );
CPPUNIT_TEST ( testMember );
CPPUNIT_TEST ( testXmlIO );
CPPUNIT_TEST_SUITE_END ( );
public:
void setUp ( );
void tearDown ( );
void testCreateUnique ( );
void testMove ( );
void testRvalueReferencePassing ( );
void testConstReferencePassing ( );
void testReferencePassing ( );
void testConstRvalueReferencePassing ( );
void testSetConstruction ( );
void testMember ( );
void testXmlIO ( );
};
#endif // CLI_TEST_H_
#include <version.hpp>
#include <tclap/CmdLine.h>
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/TestResult.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/Test.h>
#include <cppunit/TestFailure.h>
#include <cppunit/portability/Stream.h>
#include <cppunit/TestListener.h>
#include <cppunit/SourceLine.h>
#include <cppunit/Exception.h>
#include <exception/CommonException.h>
CPPUNIT_NS_BEGIN
class CPPUNIT_API TestProgressListener : public TestListener
{
public:
TestProgressListener();
virtual ~TestProgressListener();
void startTest( Test *test );
void addFailure( const TestFailure &failure );
void endTest( Test *test );
int getResult() const;
void printResults() const;
private:
TestProgressListener( const TestProgressListener &copy );
void operator =( const TestProgressListener &copy );
private:
int m_Failures;
int m_Tests;
int m_Assertions;
bool m_lastTestFailed;
};
TestProgressListener::TestProgressListener() : m_Failures( 0 ), m_Tests(0), m_Assertions(0), m_lastTestFailed( false )
{
}
TestProgressListener::~TestProgressListener()
{
}
void TestProgressListener::startTest( Test * test )
{
stdCOut() << test->getName() << ":" << "\n";
stdCOut().flush();
m_lastTestFailed = false;
m_Tests++;
}
void TestProgressListener::addFailure( const TestFailure &failure )
{
stdCOut() << (failure.isError() ? "error" : "assertion") << " : " << failure.failedTestName() << " : " << failure.sourceLine().lineNumber() << "\n";
stdCOut() << "Exception " << failure.thrownException()->message().details();
m_lastTestFailed = true;
if(failure.isError()) m_Failures++; else m_Assertions++;
}
void TestProgressListener::endTest( Test * test)
{
stdCOut() << "Result (" << test->getName() << ")";
stdCOut().flush();
if ( !m_lastTestFailed )
stdCOut() << " : OK";
else
stdCOut() << " : Fail";
stdCOut() << "\n\n";
}
int TestProgressListener::getResult() const {
return m_Failures + m_Assertions;
}
void TestProgressListener::printResults() const {
stdCOut() << "Overal result: Tests: " << m_Tests << " Assertions: " << m_Assertions << " Failures: " << m_Failures << "\n";
}
CPPUNIT_NS_END
int main(int argc, char* argv[]) {
try {
TCLAP::CmdLine cmd("Main test binary.", ' ', ALIB_VERSION_INFO);
TCLAP::MultiArg<std::string> testPathSegments("p", "path", "test path", false, "string" );
cmd.add( testPathSegments );
cmd.parse(argc, argv);
CppUnit::TestResult controller;
CppUnit::TestResultCollector result;
controller.addListener( &result );
CppUnit::TestProgressListener progressListener;
controller.addListener( &progressListener );
CppUnit::Test *suite = NULL;
std::string testPath = "";
if(testPathSegments.getValue().size() == 0) {
// Get the top level suite from the registry
suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
} else if(testPathSegments.getValue().size() == 1) {
suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest();
} else {
suite = CppUnit::TestFactoryRegistry::getRegistry(testPathSegments.getValue()[0]).makeTest();
bool first = true;
for(const std::string& path : testPathSegments.getValue()) {
if(first) {
first = false;
continue;
}
testPath += path + "/";
}
testPath.pop_back();
}
// Adds the test to the list of test to run
CppUnit::TextUi::TestRunner runner;
runner.addTest( suite );
// Change the default outputter to a compiler error format outputter
runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) );
// Run the tests.
runner.run( controller, testPath );
progressListener.printResults();
std::ofstream xmlFileResults("CppUnitTestResults.xml");
CppUnit::XmlOutputter xmlOut(&result, xmlFileResults);
xmlOut.write();
return progressListener.getResult();
} catch(const exception::CommonException& exception) {
std::cerr << exception.getCause() << std::endl;
return 1;
} catch(const TCLAP::ArgException& exception) {
std::cerr << exception.error() << std::endl;
return 2;
} catch (const std::exception& exception) {
std::cerr << "Exception caught: " << exception.what() << std::endl;
return 3;
} catch(...) {
std::cerr << "Unknown exception caught." << std::endl;
return 127;
}
}
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment