#include #include "../src/nerr.h" #include #include using namespace std; using namespace nerr; #define assert(v,fmt,...) if(!(v)) { \ throw Error(nerr::sfmt("ASSERT ERROR %s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)); \ } void test(const char* desc, const std::function& lambda) { std::cout << "Run test: " << desc << ".........."; try { lambda(); std::cout << "OK" << std::endl; } catch(nerr::Error e) { std::cout << "FAILED" << std::endl; std::cout << e.what() << std::endl; } } Result test_return() { return string("Error"); } int main(int argc, char const *argv[]) { test("Test Option", [](){ Option 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 opt; Result 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 opt; Option opt1 = opt.map(fn); assert(opt.is_none() == opt1.is_none(), "Nothing shall be changed"); opt = 2; opt1 = opt.map(fn); assert(opt1.unwrap() == "Hello", "Value mismatch"); }); test("Option map or", [](){ auto fn = [](int x){ return ++x; }; Option opt; int v = opt.map_or(0, fn); assert(v == 0, "Value mismatch"); opt = 2; v = opt.map_or(0, fn); assert(v == 3, "Value mismatch %d",v); }); test("Test Result", []{ Result 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 ret = ERR("Error"); Option opt_v = ret.ok(); assert(opt_v.is_none(), "Value shall not be present"); Option 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 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){ cout << data << endl; return 1; }; Result ret = err; Result ret1 = ret.map(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(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){ cout << "Error code is " << x << endl; return "Hello"; }; Result ret = true; Result ret1 = ret.map_err(fn); assert(ret1.unwrap() == true, "Value mistmatch"); ret = 10; ret1 = ret.map_err(fn); assert(ret1.err().unwrap() == "Hello", "Value mistmatch"); }); test("Result map or", [](){ auto fn = [](int x){ return ++x; }; Result ret = ERR("Error"); int v = ret.map_or(0, fn); assert(v == 0, "Value mismatch"); ret = 2; v = ret.map_or(0, fn); assert(v == 3, "Value mismatch %d",v); }); }