Skip to content

Commit

Permalink
Add tests to modified file event
Browse files Browse the repository at this point in the history
  • Loading branch information
uilianries authored and berkus committed Mar 24, 2016
1 parent b647310 commit d090551
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
14 changes: 14 additions & 0 deletions tests/directory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/thread.hpp>
#include <fstream>

Expand Down Expand Up @@ -76,6 +77,19 @@ class directory
BOOST_REQUIRE(boost::filesystem::equivalent(boost::filesystem::current_path(), boost::filesystem::initial_path()));
}

void write_file(const char *file, const char *buffer)
{
boost::filesystem::current_path(full_path);
BOOST_REQUIRE(boost::filesystem::equivalent(full_path, boost::filesystem::current_path()));
BOOST_REQUIRE(boost::filesystem::exists(file));
boost::filesystem::ofstream ofs(file);
BOOST_CHECK(ofs);
ofs << buffer;
ofs.close();
boost::filesystem::current_path(boost::filesystem::initial_path());
BOOST_REQUIRE(boost::filesystem::equivalent(boost::filesystem::current_path(), boost::filesystem::initial_path()));
}

private:
boost::filesystem::path full_path;
};
Expand Down
32 changes: 22 additions & 10 deletions tests/test_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ void rename_file_handler_new(const boost::filesystem::path& expected_path, const
BOOST_CHECK_EQUAL(ev.type, boost::asio::dir_monitor_event::renamed_new_name);
}

void modify_file_handler(const boost::system::error_code &ec, const boost::asio::dir_monitor_event &ev)
{
boost::filesystem::path fullpath = boost::filesystem::canonical(TEST_DIR1);
fullpath /= TEST_FILE2;

BOOST_CHECK_EQUAL(ec, boost::system::error_code());
BOOST_CHECK_EQUAL(ev.path, fullpath);
BOOST_CHECK_EQUAL(ev.type, boost::asio::dir_monitor_event::modified);
}

BOOST_AUTO_TEST_CASE(rename_file)
{
directory dir(TEST_DIR1);
Expand All @@ -76,6 +66,28 @@ BOOST_AUTO_TEST_CASE(rename_file)
io_service.reset();
}

void modify_file_handler(const boost::filesystem::path& expected_path, const boost::system::error_code &ec, const boost::asio::dir_monitor_event &ev)
{
BOOST_CHECK_EQUAL(ec, boost::system::error_code());
BOOST_CHECK_THE_SAME_PATHS_RELATIVE(ev.path, expected_path);
BOOST_CHECK_EQUAL(ev.type, boost::asio::dir_monitor_event::modified);
}

BOOST_AUTO_TEST_CASE(modify_file)
{
directory dir(TEST_DIR1);
auto test_file2 = dir.create_file(TEST_FILE2);

boost::asio::dir_monitor dm(io_service);
dm.add_directory(TEST_DIR1);

dir.write_file(TEST_FILE2, TEST_FILE1);

dm.async_monitor(boost::bind(&modify_file_handler, boost::ref(test_file2), _1, _2));
io_service.run();
io_service.reset();
}

void remove_file_handler(const boost::filesystem::path& expected_path, const boost::system::error_code &ec, const boost::asio::dir_monitor_event &ev)
{
BOOST_CHECK_EQUAL(ec, boost::system::error_code());
Expand Down
16 changes: 16 additions & 0 deletions tests/test_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ BOOST_AUTO_TEST_CASE(remove_file)
BOOST_CHECK_EQUAL(ev.type, boost::asio::dir_monitor_event::removed);
}

BOOST_AUTO_TEST_CASE(modify_file)
{
directory dir(TEST_DIR1);
auto test_file1 = dir.create_file(TEST_FILE1);

boost::asio::dir_monitor dm(io_service);
dm.add_directory(TEST_DIR1);

dir.write_file(TEST_FILE1, TEST_FILE2);

boost::asio::dir_monitor_event ev = dm.monitor();

BOOST_CHECK_THE_SAME_PATHS_RELATIVE(ev.path, test_file1);
BOOST_CHECK_EQUAL(ev.type, boost::asio::dir_monitor_event::modified);
}

BOOST_AUTO_TEST_CASE(multiple_events)
{
directory dir(TEST_DIR1);
Expand Down

0 comments on commit d090551

Please sign in to comment.