Skip to content

Commit fda4a51

Browse files
committed
cleanup
1 parent a5777c8 commit fda4a51

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
test
1+
/test/index
22

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: cpp
22

3-
script: make && make test
3+
script: build run test
44

55
compiler: clang
66

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Paolo Fragomeni
3+
Copyright (c) 2019 Paolo Fragomeni
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

test/index.cxx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <assert.h>
2+
#include "../index.hxx"
3+
4+
#define ASSERT(message, ...) do { \
5+
if(!(__VA_ARGS__)) { \
6+
std::cerr << "FAIL: " << message << std::endl; \
7+
exit(0);\
8+
} \
9+
else { \
10+
std::cout << "OK: " << message << std::endl; \
11+
testcount++; \
12+
} \
13+
} while(0);
14+
15+
int main (int argc, char* argv[]) {
16+
int testcount = 0;
17+
int testplan = 9;
18+
19+
//
20+
// sanity test.
21+
//
22+
ASSERT("sanity: true is true", true == true);
23+
24+
EventEmitter ee;
25+
26+
ASSERT("maxListeners should be 10", ee.maxListeners == 10);
27+
28+
ee.on("event1", [&](int a, std::string b) {
29+
ASSERT("first arg should be equal to 10", a == 10);
30+
ASSERT("second arg should be foo", b == "foo");
31+
});
32+
33+
ee.emit("event1", 10, (std::string) "foo");
34+
35+
try {
36+
ee.on("event1", []() {});
37+
}
38+
catch(...) {
39+
ASSERT("duplicate listener", true);
40+
}
41+
42+
ee.on("event2", [&]() {
43+
ASSERT("no params", true);
44+
});
45+
46+
ee.emit("event2");
47+
48+
int count1 = 0;
49+
ee.on("event3", [&]() {
50+
if (++count1 == 10) {
51+
ASSERT("event was emitted correct number of times", true);
52+
}
53+
});
54+
55+
for (int i = 0; i < 10; i++) {
56+
ee.emit("event3");
57+
}
58+
59+
int count2 = 0;
60+
ee.on("event4", [&](){
61+
count2++;
62+
if (count2 > 1) {
63+
ASSERT("event was fired after it was removed", false);
64+
}
65+
});
66+
67+
ee.emit("event4");
68+
ee.off("event4");
69+
ee.emit("event4");
70+
ee.emit("event4");
71+
72+
int count3 = 0;
73+
ee.on("event5", [&]() {
74+
count3++;
75+
ASSERT("events were fired even though all listeners were removed", false);
76+
});
77+
78+
ASSERT("the correct number of listeners has been reported", ee.listeners() == 4);
79+
80+
ee.off();
81+
82+
ASSERT("no additional events fired after all listeners removed",
83+
count1 == 10 &&
84+
count2 == 1 &&
85+
count3 == 0
86+
);
87+
88+
ASSERT("testcount == plan", testcount == testplan)
89+
}

0 commit comments

Comments
 (0)