163 lines
4.5 KiB
C++
163 lines
4.5 KiB
C++
#include <string>
|
|
#include "../src/ro.h"
|
|
#include <functional>
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
using namespace ro;
|
|
|
|
#define assert(v,fmt,...) if(!(v)) { \
|
|
throw Error(ro::sfmt("ASSERT ERROR %s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)); \
|
|
}
|
|
|
|
|
|
void test(const char* desc, const std::function<void()>& lambda)
|
|
{
|
|
std::cout << "Run test: " << desc << "..........";
|
|
try {
|
|
lambda();
|
|
std::cout << "OK" << std::endl;
|
|
}
|
|
catch(ro::Error e)
|
|
{
|
|
std::cout << "FAILED" << std::endl;
|
|
std::cout << e.what() << std::endl;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
test("Test Option", [](){
|
|
Option<string> opt;
|
|
assert(opt.is_none(), "Option is not empty");
|
|
assert(! opt.is_some(), "Option has some value");
|
|
string text = "This is some data";
|
|
opt = text;
|
|
assert(opt.is_some(), "Option shall has some value");
|
|
assert(opt.unwrap() == text,"Value mistmatch");
|
|
});
|
|
|
|
test("Option -> Result convert", [](){
|
|
Option<string> opt;
|
|
Result<string, Error> ret = opt.ok_or(ERR("Option is null"));
|
|
assert(ret.is_err(), "Object should containe error object");
|
|
string err = ret.err().unwrap().what();
|
|
assert( err.find("Option is null") != string::npos, "Error message mistmatch");
|
|
|
|
opt = "abc";
|
|
ret = opt.ok_or(ERR("Option is null"));
|
|
assert(ret.is_ok(), "Object should contain data");
|
|
assert(ret.unwrap() == "abc", "Data mistmatch");
|
|
});
|
|
|
|
test("Option map", [](){
|
|
auto fn = [](int x){
|
|
return "Hello";
|
|
};
|
|
|
|
Option<int> opt;
|
|
Option<string> opt1 = opt.map<string>(fn);
|
|
assert(opt.is_none() == opt1.is_none(), "Nothing shall be changed");
|
|
|
|
opt = 2;
|
|
opt1 = opt.map<string>(fn);
|
|
assert(opt1.unwrap() == "Hello", "Value mismatch");
|
|
});
|
|
|
|
|
|
test("Option map or", [](){
|
|
auto fn = [](int x){
|
|
return ++x;
|
|
};
|
|
|
|
Option<int> opt;
|
|
int v = opt.map_or<int>(0, fn);
|
|
assert(v == 0, "Value mismatch");
|
|
|
|
opt = 2;
|
|
v = opt.map_or<int>(0, fn);
|
|
assert(v == 3, "Value mismatch %d",v);
|
|
});
|
|
|
|
test("Test Result", []{
|
|
Result<string, Error> ret = ERR("Error");
|
|
assert(ret.is_err(), "Object should containe error object");
|
|
ret = string("Hello");
|
|
assert(ret.is_ok(), "Object should contain data");
|
|
assert(ret.unwrap() == "Hello", "Data mistmatch");
|
|
});
|
|
|
|
test("Result->option convert",[](){
|
|
Result<string, Error> ret = ERR("Error");
|
|
Option<string> opt_v = ret.ok();
|
|
assert(opt_v.is_none(), "Value shall not be present");
|
|
Option<Error> opt_err = ret.err();
|
|
assert(opt_err.is_some(), "Erreur shall not be none");
|
|
|
|
ret = string("Hello");
|
|
opt_v = ret.ok();
|
|
assert(opt_v.is_some(), "Value shall be present");
|
|
opt_err = ret.err();
|
|
assert(opt_err.is_none(), "Erreur shall be none");
|
|
});
|
|
|
|
test("Result expect", [](){
|
|
try{
|
|
Result<string, Error> ret = ERR("Error");
|
|
auto s = ret.expect(ERR("Expect message"));
|
|
assert(false, "Should not happend");
|
|
}
|
|
catch(Error e)
|
|
{
|
|
assert(true, "%s", e.what());
|
|
}
|
|
});
|
|
|
|
test("Result map", [](){
|
|
auto err = ERR("Error");
|
|
auto fn = [](string data){
|
|
return 1;
|
|
};
|
|
Result<string, Error> ret = err;
|
|
Result<int, Error> ret1 = ret.map<int>(fn);
|
|
assert(ret1.is_err(), "mapped result shall be an error");
|
|
assert(ret1.err() == ret.err(), "Error object mistmatch");
|
|
|
|
ret = string("Hello");
|
|
ret1 = ret.map<int>(fn);
|
|
assert(ret1.is_ok(), "mapped result shall not be an error");
|
|
assert(ret1.unwrap() == 1, "Value mistmatch");
|
|
});
|
|
|
|
|
|
test("Result map err", [](){
|
|
auto fn = [](int x){
|
|
return "Hello";
|
|
};
|
|
Result<bool, int> ret = true;
|
|
Result<bool, string> ret1 = ret.map_err<string>(fn);
|
|
assert(ret1.unwrap() == true, "Value mistmatch");
|
|
|
|
ret = 10;
|
|
ret1 = ret.map_err<string>(fn);
|
|
assert(ret1.err().unwrap() == "Hello", "Value mistmatch");
|
|
});
|
|
|
|
test("Result map or", [](){
|
|
auto fn = [](int x){
|
|
return ++x;
|
|
};
|
|
|
|
Result<int, Error> ret = ERR("Error");
|
|
int v = ret.map_or<int>(0, fn);
|
|
assert(v == 0, "Value mismatch");
|
|
|
|
ret = 2;
|
|
v = ret.map_or<int>(0, fn);
|
|
assert(v == 3, "Value mismatch %d",v);
|
|
});
|
|
|
|
}
|