From fda4a515dec8d223118f94756def0197aefe050e Mon Sep 17 00:00:00 2001 From: heapwolf Date: Fri, 12 Apr 2019 11:23:23 +0700 Subject: [PATCH] cleanup --- .gitignore | 2 +- .travis.yml | 2 +- LICENSE | 2 +- test/index.cxx | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 test/index.cxx diff --git a/.gitignore b/.gitignore index 76e579a..2d9044d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -test +/test/index diff --git a/.travis.yml b/.travis.yml index 87298e5..fcf503b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: cpp -script: make && make test +script: build run test compiler: clang diff --git a/LICENSE b/LICENSE index 9dbe777..8d86b5a 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/test/index.cxx b/test/index.cxx new file mode 100644 index 0000000..efb8248 --- /dev/null +++ b/test/index.cxx @@ -0,0 +1,89 @@ +#include +#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) +}