Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cpp interface and unit tests #36

Open
wants to merge 29 commits into
base: master
Choose a base branch
from

Conversation

xqp
Copy link
Contributor

@xqp xqp commented Sep 25, 2017

this is a PR for the future work of #34

what we have worked on?

  • add a callback interface to notify changes into the application (see Listener.hpp and FileWatcher.h)
  • add transformation classes to exclude (or filter) events from the library (like chokidar cli regex filter system)
  • add a c++11 unit test framework (see catch)
  • add basic tests for the filewatcher component and the event transformer classes
  • add the test executables to ctest
  • add a nsfw namespace because namespace pollution
  • add a installation command in cmake to install this lib on a system
  • change the include pathes to a relative path independent include path
  • rename the "includes" directory to "include" because library convenience (includes in something like /usr/local/include/nsfw/NSFW.h instead of /usr/local/includes/NSFW.h)
  • rename the javascript c++ interface

With this changes we can use and test the library with c++.

After the installation of cmake you can run the following command
cmake . && make -j

@@ -16,9 +16,13 @@ enum EventType {

struct Event {
Event(const EventType type, const std::string &directory, const std::string &fileA, const std::string &fileB) :
type(type), directory(directory), fileA(fileA), fileB(fileB) {}
type(type), directory(directory), fileA(fileA), fileB(fileB) {
timePoint = std::chrono::high_resolution_clock::now();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename timePoint to the more idiomatic name timestamp, please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with forgiveness I think it is a time point because the std::chrono libraries doesn't know any 'timestamps' just a lot of time points

see: http://en.cppreference.com/w/cpp/chrono/time_point
"Class template std::chrono::time_point represents a point in time. It is implemented as if it stores a value of type Duration indicating the time interval from the start of the Clock's epoch."

if we rename this as "timestamp" there is confusion with some known timestamps

return transform(std::move(vecEvents));
}

virtual VecEvents transform(VecEvents vecEvents) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: NSFW uses two-spaced tabs.

[this](std::unique_ptr<Event> &event){
std::regex self_regex(regex,
std::regex_constants::ECMAScript
| std::regex_constants::icase);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about the detailed semantics here. Though, my feeling is that you should set up self_regex outside the lambda and capture it. Otherwise it would recompile the regex several times, no?

ExcludeDirectories(const std::string &regex)
: regex(regex) {}

VecEvents transform(VecEvents vecEvents) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please implement that in a *.cpp file.

return vecEvents;
}

virtual ~ExcludeDirectories() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

std::string regex = "/bar$";
ExcludeDirectories exDir(regex);

SECTION("directory name does match") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does NOT match, no?

auto transformed = exDir(std::move(vec));
REQUIRE(transformed->size() == 0);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another test with both matching and non-matching dir names would be great.

std::string regex = "~[a-zA-Z_][a-zA-Z_0-9]*\\.tmp";
ExcludeFiles exFiles(regex);

SECTION("file name does match") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does NOT match, again?


#include "test_helper.h"

std::string getDirectoryFromFile(const std::string &path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be static (i.e. not part of the public interface of this compilation unit)

std::string executionPath(getDirectoryFromFile(programmName));
TestFileSystemAdapter testWatcher(executionPath,
std::chrono::milliseconds(10));
auto comparation = [](const Event &lhs, const Event &rhs) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparison

void notify(Args&&... args) {
std::lock_guard<std::mutex> lock(mListenersMutex);
for(const auto &func : mListeners) {
func.second(std::move(std::forward<Args>(args)...));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrofitting this into our private code base I noticed that the template parameter unpacking is wrong here. It doesn't work for multi-argument Listener<> callbacks. Needs to look like this:

// move closing paren of std::move _before_ param unpacking
func.second(std::move(std::forward<Args>(args))...);


template <class CallbackType>
class Listener
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two-spaced tabs, please.

}

VecEvents ExcludeDirectories::transform(VecEvents vecEvents) {
std::regex self_regex(mRegex,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two-spaced tabs, please.

@reneme
Copy link
Contributor

reneme commented Oct 4, 2017

@implausible @maxkorp Any thoughts on that? :-)

@implausible
Copy link
Contributor

Hello again! @reneme I will get to this over the weekend.

Copy link
Contributor

@implausible implausible left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test framework should run on travis/appveyor.

@xqp
Copy link
Contributor Author

xqp commented Nov 14, 2017

okay :) the test framework run on travis and appveyor..

except the mac implementation..
the macos implementation creates a ADD and Delete action for the std::rename() command o.O

@implausible
Copy link
Contributor

That's a little strange. There's also some inconsistency on windows as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants