Skip to content

Commit 34425c3

Browse files
committed
Tests for readFileAsString()
1 parent 8d5eaf4 commit 34425c3

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ detect_stray_source_files("${contracts_sources}" "contracts/")
3333
set(libsolutil_sources
3434
libsolutil/Checksum.cpp
3535
libsolutil/CommonData.cpp
36+
libsolutil/CommonIO.cpp
3637
libsolutil/FixedHash.cpp
3738
libsolutil/IndentedWriter.cpp
3839
libsolutil/IpfsHash.cpp

test/libsolutil/CommonData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
// SPDX-License-Identifier: GPL-3.0
1818
/**
19-
* Unit tests for the StringUtils routines.
19+
* Unit tests for the CommonData routines.
2020
*/
2121

2222
#include <libsolutil/Common.h>

test/libsolutil/CommonIO.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
// SPDX-License-Identifier: GPL-3.0
18+
19+
/// Unit tests for the CommonIO routines.
20+
21+
#include <libsolutil/CommonIO.h>
22+
23+
#include <test/Common.h>
24+
#include <test/FilesystemUtils.h>
25+
#include <test/TemporaryDirectory.h>
26+
#include <test/libsolidity/util/SoltestErrors.h>
27+
28+
#include <boost/test/unit_test.hpp>
29+
#include <boost/filesystem.hpp>
30+
31+
#include <fstream>
32+
#include <string>
33+
34+
using namespace std;
35+
using namespace solidity::test;
36+
37+
namespace solidity::util::test
38+
{
39+
40+
BOOST_AUTO_TEST_SUITE(CommonIOTest)
41+
42+
BOOST_AUTO_TEST_CASE(readFileAsString_regular_file)
43+
{
44+
TemporaryDirectory tempDir("common-io-test-");
45+
createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n");
46+
47+
BOOST_TEST(readFileAsString((tempDir.path() / "test.txt").string()) == "ABC\ndef\n");
48+
}
49+
50+
BOOST_AUTO_TEST_CASE(readFileAsString_directory)
51+
{
52+
TemporaryDirectory tempDir("common-io-test-");
53+
BOOST_CHECK_THROW(readFileAsString(tempDir.path().string()), NotAFile);
54+
}
55+
56+
BOOST_AUTO_TEST_CASE(readFileAsString_symlink)
57+
{
58+
TemporaryDirectory tempDir("common-io-test-");
59+
createFileWithContent(tempDir.path() / "test.txt", "ABC\ndef\n");
60+
61+
if (!createSymlinkIfSupportedByFilesystem("test.txt", tempDir.path() / "symlink.txt"))
62+
return;
63+
64+
BOOST_TEST(readFileAsString((tempDir.path() / "symlink.txt").string()) == "ABC\ndef\n");
65+
}
66+
67+
BOOST_AUTO_TEST_SUITE_END()
68+
69+
} // namespace solidity::util::test

0 commit comments

Comments
 (0)