<commit_before>///
/// @file      retdec/internal/utilities/os_tests.cpp
/// @copyright (c) 2015 by <NAME> (<EMAIL>) and contributors
/// @license   MIT, see the @c LICENSE file for more details
/// @brief     Tests for operating-system-related utilities.
///

#include <gtest/gtest.h>

#include "retdec/exceptions.h"
#include "retdec/internal/utilities/os.h"
#include "retdec/test_utilities/tmp_file.h"

using namespace testing;
using namespace retdec::tests;

namespace retdec {
namespace internal {
namespace tests {

///
/// Tests for fileNameFromPath().
///
class FileNameFromPathTests: public Test {};

TEST_F(FileNameFromPathTests,
FileNameFromPathReturnsCorrectNameForPathWithSeparators) {
#ifdef RETDEC_OS_WINDOWS
	EXPECT_EQ("file.txt", fileNameFromPath(R"(C:\\path\to\file.txt)"));
#else
	EXPECT_EQ("file.txt", fileNameFromPath("/path/to/file.txt"));
#endif
}

TEST_F(FileNameFromPathTests,
FileNameFromPathContainingJustFileNameReturnsCorrectFileName) {
	EXPECT_EQ("file.txt", fileNameFromPath("file.txt"));
}

TEST_F(FileNameFromPathTests,
FileNameFromPathContainingNoFileNameReturnsEmptyString) {
#ifdef RETDEC_OS_WINDOWS
	EXPECT_EQ("", fileNameFromPath(R"(C:\\path\to\dir\)"));
#else
	EXPECT_EQ("", fileNameFromPath("/path/to/dir/"));
#endif
}

///
/// Tests for readFile().
///
class ReadFileTests: public Test {};

TEST_F(ReadFileTests,
ReturnsCorrectContentWhenFileExists) {
	auto tmpFile = TmpFile::createWithContent("content");

	EXPECT_EQ("content", readFile(tmpFile->getPath()));
}

TEST_F(ReadFileTests,
ThrowsFilesystemErrorWhenFileDoesNotExist) {
	EXPECT_THROW(readFile("nonexisting-file"), FilesystemError);
}

///
/// Tests for writeFile().
///
class WriteFileTests: public Test {};

///
/// Tests for copyFile().
///
class CopyFileTests: public Test {};

TEST_F(CopyFileTests,
ThrowsFilesystemErrorWhenSourceFileDoesNotExist) {
	EXPECT_THROW(copyFile("nonexisting-file", "any-file"), FilesystemError);
}

} // namespace tests
} // namespace internal
} // namespace retdec<commit_msg>Add tests for writeFile() and copyFile().<commit_after>///
/// @file      retdec/internal/utilities/os_tests.cpp
/// @copyright (c) 2015 by <NAME> (<EMAIL>) and contributors
/// @license   MIT, see the @c LICENSE file for more details
/// @brief     Tests for operating-system-related utilities.
///

#include <gtest/gtest.h>

#include "retdec/exceptions.h"
#include "retdec/internal/utilities/os.h"
#include "retdec/test_utilities/tmp_file.h"

using namespace testing;
using namespace retdec::tests;

namespace retdec {
namespace internal {
namespace tests {

///
/// Tests for fileNameFromPath().
///
class FileNameFromPathTests: public Test {};

TEST_F(FileNameFromPathTests,
FileNameFromPathReturnsCorrectNameForPathWithSeparators) {
#ifdef RETDEC_OS_WINDOWS
	EXPECT_EQ("file.txt", fileNameFromPath(R"(C:\\path\to\file.txt)"));
#else
	EXPECT_EQ("file.txt", fileNameFromPath("/path/to/file.txt"));
#endif
}

TEST_F(FileNameFromPathTests,
FileNameFromPathContainingJustFileNameReturnsCorrectFileName) {
	EXPECT_EQ("file.txt", fileNameFromPath("file.txt"));
}

TEST_F(FileNameFromPathTests,
FileNameFromPathContainingNoFileNameReturnsEmptyString) {
#ifdef RETDEC_OS_WINDOWS
	EXPECT_EQ("", fileNameFromPath(R"(C:\\path\to\dir\)"));
#else
	EXPECT_EQ("", fileNameFromPath("/path/to/dir/"));
#endif
}

///
/// Tests for readFile().
///
class ReadFileTests: public Test {};

TEST_F(ReadFileTests,
ReturnsCorrectContentWhenFileExists) {
	auto tmpFile = TmpFile::createWithContent("content");

	EXPECT_EQ("content", readFile(tmpFile->getPath()));
}

TEST_F(ReadFileTests,
ThrowsFilesystemErrorWhenFileDoesNotExist) {
	EXPECT_THROW(readFile("nonexisting-file"), FilesystemError);
}

///
/// Tests for writeFile().
///
class WriteFileTests: public Test {};

TEST_F(WriteFileTests,
WritesCorrectContentToFile) {
	const std::string Content{"content"};
	auto tmpFile = TmpFile::createWithContent("");

	writeFile(tmpFile->getPath(), Content);

	ASSERT_EQ(Content, readFile(tmpFile->getPath()));
}

TEST_F(WriteFileTests,
ThrowsIoErrorWhenFileCannotBeOpenedForWriting) {
	ASSERT_THROW(writeFile("/", "content"), IoError);
}

///
/// Tests for copyFile().
///
class CopyFileTests: public Test {};

TEST_F(CopyFileTests,
WritesCorrectContentToFile) {
	const std::string Content{"content"};
	auto tmpInFile = TmpFile::createWithContent(Content);
	auto tmpOutFile = TmpFile::createWithContent("");

	copyFile(tmpInFile->getPath(), tmpOutFile->getPath());

	ASSERT_EQ(Content, readFile(tmpOutFile->getPath()));
}

TEST_F(CopyFileTests,
ThrowsFilesystemErrorWhenSourceFileDoesNotExist) {
	EXPECT_THROW(copyFile("nonexisting-file", "any-file"), FilesystemError);
}

} // namespace tests
} // namespace internal
} // namespace retdec
{'fraction_non_alphanumeric': 0.15032818124073682, 'fraction_numerical': 0.0016938386618674571, 'mean_word_length': 5.916544655929722, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1205842', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1540, 'n_words': 276}