[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] (no subject)
From: |
Greg Chicares |
Subject: |
[lmi-commits] (no subject) |
Date: |
Sat, 11 Jun 2016 00:02:29 +0000 (UTC) |
branch: master
commit 32375f948c6cc2c4885b1043e16036a5576187bf
Author: Gregory W. Chicares <address@hidden>
Date: Fri Jun 10 21:25:02 2016 +0000
Add unit tests for validate_directory() and validate_filepath() [432]
Update documentation. Remove one comment calling for unit tests to be
written, and another suggesting a different function name that seems no
better.
---
path_utility.cpp | 11 +++-------
path_utility_test.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 8 deletions(-)
diff --git a/path_utility.cpp b/path_utility.cpp
index 5695837..3b585d8 100644
--- a/path_utility.cpp
+++ b/path_utility.cpp
@@ -297,8 +297,6 @@ fs::path unique_filepath
// TODO ?? CALCULATION_SUMMARY Refactor duplication:
// validate_directory()
// validate_filepath()
-// and add unit tests for both.
-// Also rename 'validate_directory' --> 'validate_directory_path',
/// Throw an informative exception if the 'directory' argument does
/// not name a valid directory.
@@ -308,16 +306,13 @@ fs::path unique_filepath
/// 'context': semantic description of the directory to be named;
/// used in the exception report.
///
-/// Although a std::invalid_argument exception would seem more
-/// fitting in the context of this function, in the global context
-/// 'd' may be specified by users, so std::runtime_error is
-/// preferable.
+/// Although a std::invalid_argument exception might at first seem
+/// appropriate here, std::runtime_error is chosen because the
+/// 'directory' argument may be specified by users.
///
/// Exceptions thrown from the boost filesystem library on path
/// assignment are caught in order to rethrow with 'context'
/// prepended.
-///
-/// TODO ?? Need unit tests.
void validate_directory
(std::string const& directory
diff --git a/path_utility_test.cpp b/path_utility_test.cpp
index 3a06b57..f1c557a 100644
--- a/path_utility_test.cpp
+++ b/path_utility_test.cpp
@@ -249,6 +249,59 @@ void test_path_inserter()
BOOST_TEST_EQUAL(z, oss.str());
}
+void test_path_validation()
+{
+ std::string context("Unit test file");
+
+ // Create a file and a directory to test.
+ fs::create_directory("path_utility_test_dir");
+ write_dummy_file("path_utility_test_file");
+
+ // All right.
+ validate_directory("path_utility_test_dir", context);
+ validate_filepath("path_utility_test_file", context);
+ validate_filepath("./path_utility_test_file", context);
+
+ // Not well formed.
+ BOOST_TEST_THROW
+ (validate_filepath("<|>", context)
+ ,std::runtime_error
+ ,lmi_test::what_regex("invalid name \"<|>\" in path")
+ );
+
+ // Not empty.
+ BOOST_TEST_THROW
+ (validate_filepath("", context)
+ ,std::runtime_error
+ ,"Unit test file must not be empty."
+ );
+
+ // Must exist.
+ BOOST_TEST_THROW
+ (validate_filepath("no_such_file", context)
+ ,std::runtime_error
+ ,"Unit test file 'no_such_file' not found."
+ );
+
+ // Must be a directory.
+ BOOST_TEST_THROW
+ (validate_directory("path_utility_test_file", context)
+ ,std::runtime_error
+ ,"Unit test file 'path_utility_test_file' is not a directory."
+ );
+
+ // Must not be a directory.
+ BOOST_TEST_THROW
+ (validate_filepath("path_utility_test_dir", context)
+ ,std::runtime_error
+ ,"Unit test file 'path_utility_test_dir' is a directory."
+ );
+
+ // Remove file and directory created for this test.
+ fs::remove("path_utility_test_dir");
+ fs::remove("path_utility_test_file");
+}
+
int test_main(int, char*[])
{
test_orthodox_filename();
@@ -256,6 +309,7 @@ int test_main(int, char*[])
test_unique_filepath_with_normal_filenames();
test_unique_filepath_with_ludicrous_filenames();
test_path_inserter();
+ test_path_validation();
return EXIT_SUCCESS;
}