Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Apr 12, 2019
1 parent a5777c8 commit fda4a51
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
test
/test/index

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: cpp

script: make && make test
script: build run test

compiler: clang

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Paolo Fragomeni
Copyright (c) 2019 Paolo Fragomeni

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
89 changes: 89 additions & 0 deletions test/index.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <assert.h>
#include "../index.hxx"

#define ASSERT(message, ...) do { \
if(!(__VA_ARGS__)) { \
std::cerr << "FAIL: " << message << std::endl; \
exit(0);\
} \
else { \
std::cout << "OK: " << message << std::endl; \
testcount++; \
} \
} while(0);

int main (int argc, char* argv[]) {
int testcount = 0;
int testplan = 9;

//
// sanity test.
//
ASSERT("sanity: true is true", true == true);

EventEmitter ee;

ASSERT("maxListeners should be 10", ee.maxListeners == 10);

ee.on("event1", [&](int a, std::string b) {
ASSERT("first arg should be equal to 10", a == 10);
ASSERT("second arg should be foo", b == "foo");
});

ee.emit("event1", 10, (std::string) "foo");

try {
ee.on("event1", []() {});
}
catch(...) {
ASSERT("duplicate listener", true);
}

ee.on("event2", [&]() {
ASSERT("no params", true);
});

ee.emit("event2");

int count1 = 0;
ee.on("event3", [&]() {
if (++count1 == 10) {
ASSERT("event was emitted correct number of times", true);
}
});

for (int i = 0; i < 10; i++) {
ee.emit("event3");
}

int count2 = 0;
ee.on("event4", [&](){
count2++;
if (count2 > 1) {
ASSERT("event was fired after it was removed", false);
}
});

ee.emit("event4");
ee.off("event4");
ee.emit("event4");
ee.emit("event4");

int count3 = 0;
ee.on("event5", [&]() {
count3++;
ASSERT("events were fired even though all listeners were removed", false);
});

ASSERT("the correct number of listeners has been reported", ee.listeners() == 4);

ee.off();

ASSERT("no additional events fired after all listeners removed",
count1 == 10 &&
count2 == 1 &&
count3 == 0
);

ASSERT("testcount == plan", testcount == testplan)
}

0 comments on commit fda4a51

Please sign in to comment.