-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enable C++ unit testing * Have separate CMake project for C++ unit tests * Move include * Prefer GTest over Catch2
- Loading branch information
Showing
5 changed files
with
142 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# CMake configuration for SHP C++ unit tests | ||
|
||
project(${CMAKE_PROJECT_NAME}Tests CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
# Set up GoogleTest | ||
include(FetchContent) | ||
|
||
FetchContent_Declare( | ||
googletest | ||
GIT_REPOSITORY https://github.com/google/googletest.git | ||
GIT_TAG v1.14.0 | ||
) | ||
|
||
# For Windows: Prevent overriding the parent project's compiler/linker settings | ||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) | ||
|
||
FetchContent_MakeAvailable(googletest) | ||
|
||
foreach(executable dbf_test) | ||
add_executable(${executable} ${PROJECT_SOURCE_DIR}/${executable}.cc) | ||
target_link_libraries(${executable} PRIVATE ${PACKAGE} gtest) | ||
add_test( | ||
NAME ${executable} | ||
COMMAND ${executable} | ||
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" | ||
) | ||
set_target_properties(${executable} PROPERTIES FOLDER "tests") | ||
endforeach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <filesystem> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
#include <string_view> | ||
|
||
#include <gtest/gtest.h> | ||
#include "shapefil.h" | ||
|
||
namespace fs = std::filesystem; | ||
|
||
namespace | ||
{ | ||
|
||
static const auto kTestData = fs::path{"shape_eg_data"}; | ||
|
||
static bool SetContents(const fs::path &file_name, std::string_view content) | ||
{ | ||
std::ofstream file(file_name); | ||
if (!file.is_open()) | ||
return false; | ||
file << content; | ||
file.close(); | ||
return true; | ||
} | ||
|
||
TEST(DBFOpenTest, OpenDoesNotExist_rb) | ||
{ | ||
const auto handle = DBFOpen("/does/not/exist.dbf", "rb"); | ||
EXPECT_EQ(nullptr, handle); | ||
} | ||
|
||
TEST(DBFOpenTest, OpenDoesNotExist_rb_plus) | ||
{ | ||
const auto handle = DBFOpen("/does/not/exist2.dbf", "rb+"); | ||
EXPECT_EQ(nullptr, handle); | ||
} | ||
|
||
TEST(DBFOpenTest, OpenUnexpectedFormat) | ||
{ | ||
const auto filename = kTestData / "README.md"; | ||
const auto handle = DBFOpen(filename.string().c_str(), "rb"); | ||
EXPECT_EQ(nullptr, handle); | ||
} | ||
|
||
TEST(DBFOpenTest, OpenExisting) | ||
{ | ||
const auto filename = kTestData / "anno.dbf"; | ||
const auto handle = DBFOpen(filename.string().c_str(), "rb"); | ||
EXPECT_NE(nullptr, handle); | ||
DBFClose(handle); | ||
} | ||
|
||
TEST(DBFCreateTest, CreateDoesNotExist) | ||
{ | ||
const auto handle = DBFCreate("/does/not/exist"); | ||
EXPECT_EQ(nullptr, handle); | ||
} | ||
|
||
TEST(DBFCreateTest, CreateAlreadyExisting) | ||
{ | ||
const auto filename = kTestData / "in-the-way.dbf"; | ||
fs::copy(kTestData / "anno.dbf", filename); | ||
EXPECT_TRUE(SetContents(filename, "some content")); | ||
const auto handle = DBFCreate(filename.string().c_str()); | ||
// TODO(schwehr): Seems like a bug to overwrite an existing. | ||
EXPECT_NE(nullptr, handle); | ||
DBFClose(handle); | ||
const auto size = fs::file_size(filename); | ||
EXPECT_EQ(34, size); | ||
fs::remove(filename); | ||
} | ||
|
||
TEST(DBFCreateTest, CreateAndClose) | ||
{ | ||
const auto filename = kTestData / "empty.dbf"; | ||
const auto handle = DBFCreate(filename.string().c_str()); | ||
DBFClose(handle); | ||
const auto size = fs::file_size(filename); | ||
EXPECT_EQ(34, size); | ||
fs::remove(filename); | ||
} | ||
|
||
} // namespace | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |