Skip to content

Commit

Permalink
Updated the tests to use mettle.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarum90 committed Mar 6, 2016
1 parent 1d6b54a commit d8dd832
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 95 deletions.
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -u
CXX=${CXX:-clang++-3.7}
OUT=build
INCLUDES="-Iinclude/"
#INCLUDES+=" -I${HOME}/cxxlibs/hana/include/"
INCLUDES+=" -Ideps/github.com/jimporter/mettle/include/"
INCLUDES+=" -Ideps/github.com/Naios/function2/include/"
OPTS="-std=c++1y -Wall -Werror"

Expand Down
228 changes: 134 additions & 94 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,106 +3,146 @@
#include "mhe/promise/util.hpp"
#include "mhe/promise/promise.hpp"
#include "mhe/promise/internal/callback_group.hpp"
#include "mettle/header_only.hpp"

using MHE_PROMISE_SCOPE::Promise;
using MHE_PROMISE_SCOPE::BlockingContextExitExecutor;
using MHE_PROMISE_SCOPE::util::unique_function;
using MHE_PROMISE_SCOPE::internal::CallbackGroup;

int main(int argc, char ** argv) {

std::pair<const char *, unique_function<bool()>> tests[] = {

// Simple test to verify the basic resolved promise use case.
std::make_pair("test_happy_case", [](){
bool success = false;
{
BlockingContextExitExecutor b;
auto p = Promise<int>(&b, [](auto resolve, auto reject) {
resolve(5);
});

p.Then([&success](auto i){success = (i == 5);});
}
return success;
}),

// Simple test to verify the basic rejected promise use case.
std::make_pair("test_sad_case", [](){
return true; // for now -- the test does not actually run.
bool success = false;
{
BlockingContextExitExecutor b;
auto p = Promise<int>(&b, [](auto resolve, auto reject) {
reject(5);
});

p.Catch([&success](auto i){success = (i == 5);});
}
return success;
}),

std::make_pair("test_callback_group", [](){
bool c1_called = false;
bool c2_called = false;
bool c3_not_called = true;
// TODO(sarum90): Before these proliferate, consider writing a wrapper around
// expect. I'm not crazy about the fact that it just raises an exception.
using mettle::suite;
using mettle::expect;
using mettle::equal_to;
using mettle::filter;
using mettle::skip;


// =============================
//
// Tiny utility object and matcher for verifying that some code was executed as
// part of the test.
//
class WillBeCalled {
public:
WillBeCalled(){}

~WillBeCalled(){
}

void Call() {
_called = true;
}

bool WasCalled() const {
return _called;
}

private:
bool _called = false;
};

auto was_called = []() {
return filter(
[](auto && x) {return x.WasCalled();},
equal_to(true),
"Callback was called: "
);
};

auto was_not_called = []() {
return filter(
[](auto && x) {return x.WasCalled();},
equal_to(false),
"Callback was called: "
);
};

// =============================


suite<> basic("Basic tests for Promises", [](auto &_) {

// Simple test to verify the basic resolved promise use case.
_.test("test_happy_case", [](){
WillBeCalled c;
{
BlockingContextExitExecutor b;
auto p = Promise<int>(&b, [](auto resolve, auto reject) {
resolve(5);
});

p.Then([&c](auto i){
c.Call();
expect(i, equal_to(5));
});
}
expect(c, was_called());
});


// Simple test to verify the basic rejected promise use case.
_.test("test_sad_case", {skip}, [](){
WillBeCalled c;
{
BlockingContextExitExecutor b;
auto p = Promise<int>(&b, [](auto resolve, auto reject) {
reject(5);
});

p.Catch([&c](auto i){
c.Call();
expect(i, equal_to(5));
});
}
expect(c, was_called());
});

_.test("test_callback_group", [](){
WillBeCalled c1;
WillBeCalled c2;
WillBeCalled c3;
CallbackGroup<int> cg;
cg.AddCallback([&](int i){c1.Call();});
cg.AddCallback([&](int i){
expect(i, equal_to(2));
c2.Call();
});
cg.Call(2);
cg.AddCallback([&](int i){ c3.Call(); });
expect(c1, was_called());
expect(c2, was_called());
expect(c3, was_not_called());
});

//Doesn't work, maybe should error nicer?
_.test("test_callback_group_movable_arg", {skip}, [](){
bool called = false;
{
typedef std::unique_ptr<bool *> arg_type;
arg_type boolarg = std::make_unique<bool *>(&called);
std::cout << boolarg.get() << std::endl;
CallbackGroup<arg_type> cg;
cg.AddCallback([&](arg_type i){ std::cout << i.get() << std::endl; }); // dummy
cg.AddCallback([&](arg_type i){ std::cout << i.get() << std::endl; **i = true; });
cg.Call(std::move(boolarg));
}
expect(called, equal_to(true));
});

_.test("test_callback_group_movable_function", [](){
bool called = false;
{
auto bool_closure = std::make_unique<bool *>(&called);
auto closure = [bool_closure=std::move(bool_closure)](int i){
**bool_closure = true;
};
CallbackGroup<int> cg;
cg.AddCallback([&](int i){ c1_called = true; });
cg.AddCallback([&](int i){ if(i == 2) { c2_called = true;} });
cg.AddCallback(std::move(closure));
cg.Call(2);
cg.AddCallback([&](int i){ c3_not_called = false; });
return c1_called && c2_called && c3_not_called;
}),

/* Doesn't work, maybe should error nicer?
std::make_pair("test_callback_group_movable_arg", [](){
bool called = false;
{
typedef std::unique_ptr<bool *> arg_type;
arg_type boolarg = std::make_unique<bool *>(&called);
std::cout << boolarg.get() << std::endl;
CallbackGroup<arg_type> cg;
cg.AddCallback([&](arg_type i){ std::cout << i.get() << std::endl; }); // dummy
cg.AddCallback([&](arg_type i){ std::cout << i.get() << std::endl; **i = true; });
cg.Call(std::move(boolarg));
}
return called;
}),
*/

std::make_pair("test_callback_group_movable_function", [](){
bool called = false;
{
auto bool_closure = std::make_unique<bool *>(&called);
auto closure = [bool_closure=std::move(bool_closure)](int i){
**bool_closure = true;
};
CallbackGroup<int> cg;
cg.AddCallback(std::move(closure));
cg.Call(2);
}
return called;
}),

};

bool success = true;

for (auto& test: tests) {
auto result = test.second();
if (result) {
std::cout << test.first << " PASSED" << std::endl;
} else {
std::cout << test.first << " FAILED" << std::endl;
}
success = result && success;
}

std::cout << std::endl;
if (success) {
std::cout << "ALL OK" << std::endl;
return 0;
}
std::cout << "FAILURE" << std::endl;
return -1;
}
expect(called, equal_to(true));
});

});

0 comments on commit d8dd832

Please sign in to comment.