tests: Update testing framework and begin reworking everything

Add in several new features like:
- Don't exit until the specific test function completes.
- Make it easier to run named tests from C++.
- Use a macro to find line number when calling test :: equal().
- Use a macro to do basic setup and cleanup between test functions.

Also update the version test while I'm at it.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-04-26 11:59:12 -04:00
parent 35f19d98ef
commit 8aa121f291
4 changed files with 48 additions and 35 deletions

View File

@ -14,14 +14,14 @@ for arg in sys.argv:
src = SConscript("src/Sconscript")
tests = [ "version", "file", "database", "index", "filter", "idle", "tag_db",
"queue" ]
tests = [ "version" ] #, "file", "database", "index", "filter", "idle", "tag_db",
# "queue" ]
#scripts = [ "playlist", "library", "deck", "audio", "gui" ]
prev = None
for test in tests:
t = Command("%s.out" % test, [], "./tests/%s" % test)
t = Command("%s.out" % test, [], "./tests/src/%s.run" % test)
if prev and (check_depends == True):
Depends(t, prev)

View File

@ -10,11 +10,13 @@ namespace test
static unsigned int test_num;
static unsigned int failed;
void new_test(const std::string &name)
{
std::cout << name << std::endl;
test_num = 0;
failed = 0;
}
void begin()
@ -26,25 +28,40 @@ namespace test
void end()
{
std::cout << std::endl;
}
template <class T>
void assert_equal(const T &lhs, const T &rhs)
{
if (lhs == rhs)
std::cout << "Success!" << std::endl;
else {
std::cout << "Failed! =(" << std::endl;
std::cout << " Actual: " << lhs << std::endl;
std::cout << " Expected: " << rhs << std::endl;
exit(1);
if (failed > 0) {
std::cout << failed << " tests failed =(" << std::endl;
std::cout << std::endl;
exit(failed);
}
}
template <class T>
void equal(const T &lhs, const T &rhs)
void assert_equal(const T &lhs, const T &rhs, unsigned int line)
{
begin();
assert_equal(lhs, rhs);
if (lhs == rhs)
std::cout << "Success!" << std::endl;
else {
std::cout << "Failed at line " << line << ":" << std::endl;
std::cout << " Actual: " << lhs << std::endl;
std::cout << " Expected: " << rhs << std::endl;
failed++;
}
}
#define equal(lhs, rhs) \
begin(); \
test :: assert_equal(lhs, rhs, __LINE__)
}
#define run_test(name, func, ...) \
do { \
test :: new_test(name); \
func(##__VA_ARGS__); \
test :: end(); \
} while (0)
#define test_equal(lhs, rhs) \
do { \
test :: begin(); \
test :: assert_equal(lhs, rhs, __LINE__); \
} while (0)

View File

@ -1,12 +1,22 @@
/*
* Copyright 2013 (c) Anna Schumaker.
* Prints out version info
*/
#include <print.h>
#include <version.h>
#include "test.h"
#ifdef CONFIG_DEBUG
const std::string expected = "6.1-debug";
#else
const std::string expected = "6.1";
#endif /* CONFIG_DEBUG */
static void test_version()
{
test_equal((std::string)get_version(), expected);
}
int main(int argc, char **argv)
{
print("%s\n", get_version());
run_test("Version Test", test_version);
return 0;
}

View File

@ -1,14 +0,0 @@
#!/bin/bash
# Copyright 2014 (c) Anna Schumaker.
. $(dirname $0)/_functions
# Find version
version=$(config_version)
[ $(config_debug) == "True" ] && version="$version-debug"
# Run test
echo -n "Version test ... "
assert_equal "$(./src/version.run)" "$version"