Skip to content

Commit

Permalink
test: convert to section
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Oct 6, 2024
1 parent 1e4220f commit d2c81eb
Showing 1 changed file with 96 additions and 87 deletions.
183 changes: 96 additions & 87 deletions tests/filesystem_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,98 +63,107 @@ static void filesystem_test_group( int serial, const std::string &s1, const std:
s << writebuf2;
};

// Creating/removing directory; not confusing it with a file
REQUIRE( !dir_exist( dir1 ) );
REQUIRE( !file_exist( dir1 ) );
REQUIRE( assure_dir_exist( dir1 ) );
REQUIRE( dir_exist( dir1 ) );
REQUIRE( !file_exist( dir1 ) );
REQUIRE( remove_directory( dir1 ) );
REQUIRE( !remove_directory( dir1 ) );
REQUIRE( !dir_exist( dir1 ) );

// Creating/reading file, renaming it, removing, not confusing with a directory
REQUIRE( assure_dir_exist( dir1 ) );
REQUIRE( !file_exist( file1_1 ) );
REQUIRE( write_to_file( file1_1, writer, nullptr ) );
REQUIRE( file_exist( file1_1 ) );
REQUIRE( read_from_file( file1_1, reader ) );
CHECK( readbuf == writebuf );
REQUIRE( rename_file( file1_1, file1_2 ) );
REQUIRE( !rename_file( file1_1, file1_2 ) );
REQUIRE( file_exist( file1_2 ) );
REQUIRE( !rename_file( file1_2, dir1 ) );
REQUIRE( file_exist( file1_2 ) );
REQUIRE( !dir_exist( file1_2 ) );
REQUIRE( remove_file( file1_2 ) );
REQUIRE( !remove_file( file1_2 ) );

// Copying file
REQUIRE( write_to_file( file1_1, writer, nullptr ) );
REQUIRE( copy_file( file1_1, file1_2 ) );
REQUIRE( file_exist( file1_2 ) );
REQUIRE( copy_file( file1_1, file1_2 ) );
REQUIRE( remove_file( file1_2 ) );
REQUIRE( write_to_file( file1_2, writer2, nullptr ) );
REQUIRE( copy_file( file1_2, file1_1 ) );
REQUIRE( remove_file( file1_2 ) );
REQUIRE( read_from_file( file1_1, reader ) );
CHECK( readbuf == writebuf2 );
REQUIRE( assure_dir_exist( dir2 ) );
REQUIRE( !copy_file( file1_2, file1_1 ) );
REQUIRE( !copy_file( file1_1, dir2 ) );
REQUIRE( !copy_file( dir2, file1_2 ) );
REQUIRE( remove_file( file1_1 ) );
REQUIRE( remove_directory( dir2 ) );
REQUIRE( !copy_file( file1_1, file1_2 ) );

// Checking if can write to dir
REQUIRE( can_write_to_dir( dir1 ) );
REQUIRE( remove_directory( dir1 ) );
REQUIRE( !can_write_to_dir( dir1 ) );

// Listing files from dir; paths should stay valid utf-8 strings
REQUIRE( assure_dir_exist( dir1 ) );
REQUIRE( write_to_file( file1_1, writer, nullptr ) );
REQUIRE( write_to_file( file1_2, writer, nullptr ) );
REQUIRE( assure_dir_exist( dir2 ) );
REQUIRE( write_to_file( file2_1, writer, nullptr ) );
{
std::vector<std::string> got = get_files_from_path(
".json",
base,
true,
true
);
std::vector<std::string> exp = {
file1_1,
file1_2,
file2_1,
};
const auto comparator = []( const std::string & a, const std::string & b ) {
// We don't care about lexicographic order here, just the data order
// NOLINTNEXTLINE(cata-use-localized-sorting)
return a > b;
};
std::sort( exp.begin(), exp.end(), comparator );
std::sort( got.begin(), got.end(), comparator );
CHECK( got == exp );
SECTION( "Creating/removing directory; not confusing it with a file" ) {
REQUIRE( !dir_exist( dir1 ) );
REQUIRE( !file_exist( dir1 ) );
REQUIRE( assure_dir_exist( dir1 ) );
REQUIRE( dir_exist( dir1 ) );
REQUIRE( !file_exist( dir1 ) );
REQUIRE( remove_directory( dir1 ) );
REQUIRE( !remove_directory( dir1 ) );
REQUIRE( !dir_exist( dir1 ) );
}

// Can't delete directory with files
REQUIRE( !remove_directory( dir1 ) );
REQUIRE( dir_exist( dir1 ) );
SECTION( "Creating/reading file, renaming it, removing, not confusing with a directory" ) {
REQUIRE( assure_dir_exist( dir1 ) );
REQUIRE( !file_exist( file1_1 ) );
REQUIRE( write_to_file( file1_1, writer, nullptr ) );
REQUIRE( file_exist( file1_1 ) );
REQUIRE( read_from_file( file1_1, reader ) );
CHECK( readbuf == writebuf );
REQUIRE( rename_file( file1_1, file1_2 ) );
REQUIRE( !rename_file( file1_1, file1_2 ) );
REQUIRE( file_exist( file1_2 ) );
REQUIRE( !rename_file( file1_2, dir1 ) );
REQUIRE( file_exist( file1_2 ) );
REQUIRE( !dir_exist( file1_2 ) );
REQUIRE( remove_file( file1_2 ) );
REQUIRE( !remove_file( file1_2 ) );
}

SECTION( "Copying file" ) {
REQUIRE( write_to_file( file1_1, writer, nullptr ) );
REQUIRE( copy_file( file1_1, file1_2 ) );
REQUIRE( file_exist( file1_2 ) );
REQUIRE( copy_file( file1_1, file1_2 ) );
REQUIRE( remove_file( file1_2 ) );
REQUIRE( write_to_file( file1_2, writer2, nullptr ) );
REQUIRE( copy_file( file1_2, file1_1 ) );
REQUIRE( remove_file( file1_2 ) );
REQUIRE( read_from_file( file1_1, reader ) );
CHECK( readbuf == writebuf2 );
REQUIRE( assure_dir_exist( dir2 ) );
REQUIRE( !copy_file( file1_2, file1_1 ) );
REQUIRE( !copy_file( file1_1, dir2 ) );
REQUIRE( !copy_file( dir2, file1_2 ) );
REQUIRE( remove_file( file1_1 ) );
REQUIRE( remove_directory( dir2 ) );
REQUIRE( !copy_file( file1_1, file1_2 ) );
}

SECTION( "Checking if can write to dir" ) {
REQUIRE( can_write_to_dir( dir1 ) );
REQUIRE( remove_directory( dir1 ) );
REQUIRE( !can_write_to_dir( dir1 ) );
}

SECTION( "Listing files from dir; paths should stay valid utf-8 strings" ) {
REQUIRE( assure_dir_exist( dir1 ) );
REQUIRE( write_to_file( file1_1, writer, nullptr ) );
REQUIRE( write_to_file( file1_2, writer, nullptr ) );
REQUIRE( assure_dir_exist( dir2 ) );
REQUIRE( write_to_file( file2_1, writer, nullptr ) );
{
std::vector<std::string> got = get_files_from_path(
".json",
base,
true,
true
);
std::vector<std::string> exp = {
file1_1,
file1_2,
file2_1,
};
const auto comparator = []( const std::string & a, const std::string & b ) {
// We don't care about lexicographic order here, just the data order
// NOLINTNEXTLINE(cata-use-localized-sorting)
return a > b;
};
std::sort( exp.begin(), exp.end(), comparator );
std::sort( got.begin(), got.end(), comparator );
CHECK( got == exp );
}
}

SECTION( "Can't delete directory with files" ) {
REQUIRE( !remove_directory( dir1 ) );
REQUIRE( dir_exist( dir1 ) );
}

// Unless we use remove_tree
REQUIRE( remove_tree( dir1 ) );
REQUIRE( !dir_exist( dir1 ) );
SECTION( "Unless we use remove_tree" ) {
REQUIRE( remove_tree( dir1 ) );
REQUIRE( !dir_exist( dir1 ) );
}

// Removing non-existent tree is not an error
REQUIRE( remove_tree( dir1 ) );
REQUIRE( remove_tree( dir2 ) );
SECTION( "Removing non-existent tree is not an error" ) {
REQUIRE( remove_tree( dir1 ) );
REQUIRE( remove_tree( dir2 ) );
}

// Clean up
REQUIRE( remove_directory( base ) );
SECTION( "Clean up" ) {
REQUIRE( remove_directory( base ) );
}
}

// HACK: Need to rework std::u8string
Expand Down

0 comments on commit d2c81eb

Please sign in to comment.