From 34425c351dc4bbf45f938bf4c98851c4f555999c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 25 Jun 2021 15:28:54 +0200 Subject: [PATCH] Tests for readFileAsString() --- test/CMakeLists.txt | 1 + test/libsolutil/CommonData.cpp | 2 +- test/libsolutil/CommonIO.cpp | 69 ++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/libsolutil/CommonIO.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e5c5b2c7c..2dc7a50dd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -33,6 +33,7 @@ detect_stray_source_files("${contracts_sources}" "contracts/") set(libsolutil_sources libsolutil/Checksum.cpp libsolutil/CommonData.cpp + libsolutil/CommonIO.cpp libsolutil/FixedHash.cpp libsolutil/IndentedWriter.cpp libsolutil/IpfsHash.cpp diff --git a/test/libsolutil/CommonData.cpp b/test/libsolutil/CommonData.cpp index 670a71d14..8be8cc3b0 100644 --- a/test/libsolutil/CommonData.cpp +++ b/test/libsolutil/CommonData.cpp @@ -16,7 +16,7 @@ */ // SPDX-License-Identifier: GPL-3.0 /** - * Unit tests for the StringUtils routines. + * Unit tests for the CommonData routines. */ #include diff --git a/test/libsolutil/CommonIO.cpp b/test/libsolutil/CommonIO.cpp new file mode 100644 index 000000000..e83bebece --- /dev/null +++ b/test/libsolutil/CommonIO.cpp @@ -0,0 +1,69 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +// SPDX-License-Identifier: GPL-3.0 + +/// Unit tests for the CommonIO routines. + +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include + +using namespace std; +using namespace solidity::test; + +namespace solidity::util::test +{ + +BOOST_AUTO_TEST_SUITE(CommonIOTest) + +BOOST_AUTO_TEST_CASE(readFileAsString_regular_file) +{ + TemporaryDirectory tempDir("common-io-test-"); + createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n"); + + BOOST_TEST(readFileAsString((tempDir.path() / "test.txt").string()) == "ABC\ndef\n"); +} + +BOOST_AUTO_TEST_CASE(readFileAsString_directory) +{ + TemporaryDirectory tempDir("common-io-test-"); + BOOST_CHECK_THROW(readFileAsString(tempDir.path().string()), NotAFile); +} + +BOOST_AUTO_TEST_CASE(readFileAsString_symlink) +{ + TemporaryDirectory tempDir("common-io-test-"); + createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n"); + + if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt")) + return; + + BOOST_TEST(readFileAsString((tempDir.path() / "symlink.txt").string()) == "ABC\ndef\n"); +} + +BOOST_AUTO_TEST_SUITE_END() + +} // namespace solidity::util::test