tests: Compile gtest from source, add a few new tests

GTest does not ship precompiled binaries anymore. They are now compiled
using the CMakelists.txt from gtest.

Add basic unit tests for C4Value, DirectExec and C4StringTable.
scancodes-fix
Oliver Schneider 2013-01-28 16:48:04 +01:00 committed by Günther Brammer
parent e45a569aa3
commit c43a682785
6 changed files with 220 additions and 17 deletions

View File

@ -1406,19 +1406,7 @@ if(HAVE_UPNP)
target_link_libraries(openclonk ${UPNP_LIBRARIES})
endif()
# GTest
include(FindGTest)
if(GTEST_FOUND)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(tests EXCLUDE_FROM_ALL
tests/UnicodeHandlingTest.cpp
tests/main.cpp
)
target_link_libraries(tests ${GTEST_LIBRARIES})
if(HAVE_PTHREAD)
target_link_libraries(tests pthread)
endif()
endif()
add_subdirectory(tests EXCLUDE_FROM_ALL)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)

View File

@ -0,0 +1,52 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2013 Oliver Schneider
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#include <C4Include.h>
#include "script/C4StringTable.h"
#include <gtest/gtest.h>
TEST(C4StringTableTest, SanityTests)
{
EXPECT_FALSE(Strings.FindString("Apfelmus"));
C4String * str = Strings.RegString("Apfelmus");
ASSERT_TRUE(str);
EXPECT_TRUE(Strings.FindString("Apfelmus"));
str->IncRef();
str->DecRef();
EXPECT_FALSE(Strings.FindString("Apfelmus"));
}
class C4ValueNumbers;
class C4PropList;
class C4AulFunc;
class C4Object;
class C4ValueArray;
class C4Def;
class C4Effect;
class C4AulParSet;
class C4AulScript;
class C4AulScriptFunc;
#include "script/C4Value.h"
TEST(C4StringTableTest, ComparisonOperators)
{
C4String * str1 = Strings.RegString("Käsebrot");
C4String * str2 = Strings.RegString("Apfelmus");
C4Value v1(str1);
C4Value v2(str2);
C4Value v3(str1);
EXPECT_NE(v1, v2);
EXPECT_EQ(v1, v3);
}

View File

@ -0,0 +1,35 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2013 Oliver Schneider
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#include <C4Include.h>
#include "script/C4Value.h"
#include <gtest/gtest.h>
TEST(C4ValueTest, SanityTests)
{
srand(time(nullptr));
int rnd = rand();
// two C4Values constructed with same parameter are equal
EXPECT_TRUE(C4Value(rnd) == C4Value(rnd));
EXPECT_EQ(C4Value(rnd), C4Value(rnd));
EXPECT_FALSE(C4Value(rnd) != C4Value(rnd));
int rnd2 = rand();
while(rnd2 == rnd) rnd2 = rand();
EXPECT_FALSE(C4Value(rnd) == C4Value(rnd2));
EXPECT_TRUE(C4Value(42));
EXPECT_FALSE(C4Value(0));
EXPECT_TRUE(C4Value(true));
EXPECT_FALSE(C4Value(false));
}

View File

@ -0,0 +1,42 @@
# OpenClonk, http://www.openclonk.org
#
# Copyright (c) 2013 Oliver Schneider
#
# Portions might be copyrighted by other authors who have contributed
# to OpenClonk.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# See isc_license.txt for full license and disclaimer.
#
# "Clonk" is a registered trademark of Matthes Bender.
# See clonk_trademark_license.txt for full license.
cmake_minimum_required (VERSION 2.6.0)
project(openclonk_unittest)
# used by c4script and by the unit tests
set(C4SCRIPT_SOURCES
../include/c4script/c4script.h
../src/lib/C4SimpleLog.cpp
../src/C4Include.cpp
../src/script/C4ScriptStandalone.cpp
)
# GTest
find_path(GTEST_INCLUDE_DIR gtest/gtest.h REQUIRED)
find_path(GTEST_SOURCE_DIR gtest/CMakeLists.txt HINTS /usr/src REQUIRED)
add_subdirectory(${GTEST_SOURCE_DIR}/gtest gtest)
include_directories(${GTEST_INCLUDE_DIR})
set(GTEST_LIBRARIES gtest gtest_main)
AUX_SOURCE_DIRECTORY(. TESTS_SOURCES)
add_executable(tests ${TESTS_SOURCES} ${C4SCRIPT_SOURCES})
target_link_libraries(tests ${GTEST_LIBRARIES} libc4script libmisc)
if(UNIX AND NOT APPLE)
target_link_libraries(tests rt)
endif()
if(HAVE_PTHREAD)
target_link_libraries(tests pthread)
endif()

View File

@ -0,0 +1,90 @@
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2013 Oliver Schneider
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
#include <C4Include.h>
#include "script/C4Aul.h"
#include <gtest/gtest.h>
TEST(DirectExecTest, SanityTests)
{
C4AulScript * pScript = new C4AulScript();
ASSERT_TRUE(pScript);
C4Value rVal(pScript->DirectExec(nullptr, "5*8", "unit test script", false, nullptr));
EXPECT_EQ(rVal, C4Value(5*8));
delete pScript;
}
template<typename T>
bool operator==(const C4Set<T>& lhs, const C4Set<T>& rhs)
{
if (lhs.GetSize() != rhs.GetSize()) return false;
auto lit = lhs.First();
auto rit = rhs.First();
while(lit != nullptr) {
if (*lit != *rit) return false;
lit = lhs.Next(lit);
rit = lhs.Next(rit);
}
return true;
}
#include "script/C4ScriptHost.h"
class TestHost : public C4ScriptHost
{
public:
void test_equality(const TestHost& rhs) const
{
// C4ScriptHost
EXPECT_EQ(Includes, rhs.Includes);
EXPECT_EQ(Appends, rhs.Appends);
EXPECT_EQ(Script, rhs.Script);
EXPECT_EQ(Resolving, rhs.Resolving);
EXPECT_EQ(IncludesResolved, rhs.IncludesResolved);
EXPECT_EQ(LocalNamed.iSize, rhs.LocalNamed.iSize);
if (LocalNamed.iSize == rhs.LocalNamed.iSize)
EXPECT_TRUE(std::equal(LocalNamed.pNames, LocalNamed.pNames+LocalNamed.iSize, rhs.LocalNamed.pNames));
EXPECT_EQ(LocalValues, rhs.LocalValues);
EXPECT_EQ(SourceScripts, rhs.SourceScripts);
// C4AulScript
EXPECT_EQ(ScriptName, rhs.ScriptName);
}
virtual bool Parse() { ADD_FAILURE() << "tried to call Parse()"; return false; }
virtual void UnLink() { FAIL() << "tried to call UnLink()"; }
virtual bool Load(C4Group &hGroup, const char *szFilename,
const char *szLanguage, C4LangStringTable *pLocalTable)
{ ADD_FAILURE() << "tried to call Load()"; return false; }
virtual void Clear() { FAIL() << "tried to call Clear()";}
virtual void ResetProfilerTimes() { FAIL() << "tried to call ResetProfilerTimes()"; }
virtual void CollectProfilerTimes(class C4AulProfiler &rProfiler) { FAIL() << "tried to call CollectProfilerTimes()"; }
virtual bool ReloadScript(const char *szPath, const char *szLanguage){ ADD_FAILURE() << "tried to call ReloadScript()"; return false; }
virtual bool ResolveIncludes(C4DefList *rDefs){ ADD_FAILURE() << "tried to call ResolveIncludes()"; return false; }
virtual bool ResolveAppends(C4DefList *rDefs){ ADD_FAILURE() << "tried to call ResolveAppends()"; return false; }
};
TEST(DirectExecTest, HostUnmodifedByParseTest)
{
TestHost host;
TestHost host2 = host;
host.test_equality(host2);
char szScript[] = "8*5";
C4AulScriptFunc *pFunc = new C4AulScriptFunc(&host, host.GetScriptHost(), 0, szScript);
host.test_equality(host2);
pFunc->ParseFn();
host.test_equality(host2);
delete pFunc;
}

View File

@ -15,7 +15,6 @@
/* Verify correct behavior of UTF-8 handling code. */
#include "lib/Standard.h"
#include "lib/Standard.cpp"
#include <gtest/gtest.h>
TEST(UnicodeHandlingTest, AcceptsEmptyString)
@ -188,8 +187,6 @@ TEST(UnicodeHandlingTest, RejectsInvalidMultiByteUtf8)
}
#include "lib/StdBuf.h"
#include "lib/StdBuf.cpp"
size_t FileSize(int) { return 0; }
#ifdef _WIN32
TEST(UnicodeHandlingTest, WideStringConversion)
@ -209,7 +206,6 @@ TEST(UnicodeHandlingTest, WideStringConversion)
#ifdef _WIN32
#include "platform/StdRegistry.h"
#include "platform/StdRegistry.cpp"
char StdCompiler::SeparatorToChar(enum StdCompiler::Sep) { return ' '; }
TEST(UnicodeHandlingTest, RegistryAccess)
{