Unit Tests

- Updated test class to conform to coding standards.
- Implemented TAP.py to allow Python unit tests.
This commit is contained in:
Paul Beckingham 2013-04-07 00:09:16 -04:00
parent d72c6567b0
commit 61fdc0da52
4 changed files with 230 additions and 97 deletions

View file

@ -1,7 +1,7 @@
////////////////////////////////////////////////////////////////////////////////
// taskwarrior - a command line task list manager.
//
// Copyright 2006-2012, Paul Beckingham, Federico Hernandez.
// Copyright 2006-2013, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ -33,55 +33,55 @@
///////////////////////////////////////////////////////////////////////////////
UnitTest::UnitTest ()
: mPlanned (0)
, mCounter (0)
, mPassed (0)
, mFailed (0)
, mSkipped (0)
: _planned (0)
, _counter (0)
, _passed (0)
, _failed (0)
, _skipped (0)
{
}
///////////////////////////////////////////////////////////////////////////////
UnitTest::UnitTest (int planned)
: mPlanned (planned)
, mCounter (0)
, mPassed (0)
, mFailed (0)
, mSkipped (0)
: _planned (planned)
, _counter (0)
, _passed (0)
, _failed (0)
, _skipped (0)
{
std::cout << "1.." << mPlanned << "\n";
std::cout << "1.." << _planned << "\n";
}
///////////////////////////////////////////////////////////////////////////////
UnitTest::~UnitTest ()
{
float percentPassed = 0.0;
if (mPlanned > 0)
percentPassed = (100.0 * mPassed) / std::max (mPlanned, mPassed + mFailed + mSkipped);
if (_planned > 0)
percentPassed = (100.0 * _passed) / std::max (_planned, _passed + _failed + _skipped);
if (mCounter < mPlanned)
if (_counter < _planned)
{
std::cout << "# Only "
<< mCounter
<< _counter
<< " tests, out of a planned "
<< mPlanned
<< _planned
<< " were run.\n";
mSkipped += mPlanned - mCounter;
_skipped += _planned - _counter;
}
else if (mCounter > mPlanned)
else if (_counter > _planned)
std::cout << "# "
<< mCounter
<< _counter
<< " tests were run, but only "
<< mPlanned
<< _planned
<< " were planned.\n";
std::cout << "# "
<< mPassed
<< _passed
<< " passed, "
<< mFailed
<< _failed
<< " failed, "
<< mSkipped
<< _skipped
<< " skipped. "
<< std::setprecision (3) << percentPassed
<< "% passed.\n";
@ -90,41 +90,41 @@ UnitTest::~UnitTest ()
///////////////////////////////////////////////////////////////////////////////
void UnitTest::plan (int planned)
{
mPlanned = planned;
mCounter = 0;
mPassed = 0;
mFailed = 0;
mSkipped = 0;
_planned = planned;
_counter = 0;
_passed = 0;
_failed = 0;
_skipped = 0;
std::cout << "1.." << mPlanned << "\n";
std::cout << "1.." << _planned << "\n";
}
///////////////////////////////////////////////////////////////////////////////
void UnitTest::planMore (int extra)
{
mPlanned += extra;
std::cout << "1.." << mPlanned << "\n";
_planned += extra;
std::cout << "1.." << _planned << "\n";
}
///////////////////////////////////////////////////////////////////////////////
void UnitTest::ok (bool expression, const std::string& name)
{
++mCounter;
++_counter;
if (expression)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
@ -134,22 +134,22 @@ void UnitTest::ok (bool expression, const std::string& name)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::notok (bool expression, const std::string& name)
{
++mCounter;
++_counter;
if (!expression)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
@ -159,21 +159,21 @@ void UnitTest::notok (bool expression, const std::string& name)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::is (bool actual, bool expected, const std::string& name)
{
++mCounter;
++_counter;
if (actual == expected)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: "
@ -187,21 +187,21 @@ void UnitTest::is (bool actual, bool expected, const std::string& name)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::is (size_t actual, size_t expected, const std::string& name)
{
++mCounter;
++_counter;
if (actual == expected)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: "
@ -215,21 +215,21 @@ void UnitTest::is (size_t actual, size_t expected, const std::string& name)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::is (int actual, int expected, const std::string& name)
{
++mCounter;
++_counter;
if (actual == expected)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: "
@ -243,21 +243,21 @@ void UnitTest::is (int actual, int expected, const std::string& name)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::is (double actual, double expected, const std::string& name)
{
++mCounter;
++_counter;
if (actual == expected)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: "
@ -271,21 +271,21 @@ void UnitTest::is (double actual, double expected, const std::string& name)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::is (double actual, double expected, double tolerance, const std::string& name)
{
++mCounter;
++_counter;
if (fabs (actual - expected) <= tolerance)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: "
@ -299,21 +299,21 @@ void UnitTest::is (double actual, double expected, double tolerance, const std::
///////////////////////////////////////////////////////////////////////////////
void UnitTest::is (unsigned char actual, unsigned char expected, const std::string& name)
{
++mCounter;
++_counter;
if (actual == expected)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: "
@ -330,21 +330,21 @@ void UnitTest::is (
const std::string& expected,
const std::string& name)
{
++mCounter;
++_counter;
if (actual == expected)
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: '"
@ -362,21 +362,21 @@ void UnitTest::is (
const char* expected,
const std::string& name)
{
++mCounter;
++_counter;
if (! strcmp (actual, expected))
{
++mPassed;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n";
}
else
{
++mFailed;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " - "
<< name
<< "\n# expected: '"
@ -399,10 +399,10 @@ void UnitTest::diag (const std::string& text)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::pass (const std::string& text)
{
++mCounter;
++mPassed;
++_counter;
++_passed;
std::cout << "ok "
<< mCounter
<< _counter
<< " "
<< text
<< "\n";
@ -411,10 +411,10 @@ void UnitTest::pass (const std::string& text)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::fail (const std::string& text)
{
++mCounter;
++mFailed;
++_counter;
++_failed;
std::cout << "not ok "
<< mCounter
<< _counter
<< " "
<< text
<< "\n";
@ -423,10 +423,10 @@ void UnitTest::fail (const std::string& text)
///////////////////////////////////////////////////////////////////////////////
void UnitTest::skip (const std::string& text)
{
++mCounter;
++mSkipped;
++_counter;
++_skipped;
std::cout << "skip "
<< mCounter
<< _counter
<< " "
<< text
<< "\n";