Exemplo n.º 1
0
TEST(WTF_Expected, Expected_void)
{
    typedef Expected<void, const char*> E;
    typedef Expected<void, const void*> EV;
    typedef Expected<void, std::string> String;
    {
        auto e = E();
        EXPECT_TRUE(e.hasValue());
        const auto e2(e);
        EXPECT_TRUE(e2.hasValue());
        EXPECT_EQ(e, e2);
        E e3;
        e3 = e2;
        EXPECT_TRUE(e3.hasValue());
        EXPECT_EQ(e, e3);
    }
    {
        constexpr E c;
        EXPECT_TRUE(c.hasValue());
        constexpr const auto c2(c);
        EXPECT_TRUE(c2.hasValue());
        EXPECT_EQ(c, c2);
    }
    {
        auto u = E(makeUnexpected(oops));
        EXPECT_FALSE(u.hasValue());
        EXPECT_EQ(u.error(), oops);
        EXPECT_EQ(u.getUnexpected().value(), oops);
    }
    {
        auto uv = EV(makeUnexpected(oops));
        EXPECT_FALSE(uv.hasValue());
        EXPECT_EQ(uv.error(), oops);
        EXPECT_EQ(uv.getUnexpected().value(), oops);
    }
    {
        E e = makeUnexpected(oops);
        EXPECT_FALSE(e.hasValue());
        EXPECT_EQ(e.error(), oops);
        EXPECT_EQ(e.getUnexpected().value(), oops);
    }
    {
        auto e = makeExpectedFromError<void, const char*>(oops);
        EXPECT_FALSE(e.hasValue());
        EXPECT_EQ(e.error(), oops);
        EXPECT_EQ(e.getUnexpected().value(), oops);
    }
    {
        auto e = makeExpectedFromError<void, const void*>(oops);
        EXPECT_FALSE(e.hasValue());
        EXPECT_EQ(e.error(), oops);
        EXPECT_EQ(e.getUnexpected().value(), oops);
    }
    {
        auto e0 = E();
        auto e1 = E();
        swap(e0, e1);
        EXPECT_EQ(e0, e1);
    }
    {
        auto e0 = E(makeUnexpected(oops));
        auto e1 = E(makeUnexpected(foof));
        swap(e0, e1);
        EXPECT_EQ(e0.error(), foof);
        EXPECT_EQ(e1.error(), oops);
    }
    {
        const char* message = "very long failure string, for very bad failure cases";
        String e0(makeUnexpected<std::string>(message));
        String e1(makeUnexpected<std::string>(message));
        String e2(makeUnexpected<std::string>(std::string()));
        EXPECT_EQ(e0.error(), std::string(message));
        EXPECT_EQ(e0, e1);
        EXPECT_NE(e0, e2);
        String* e4 = new String(makeUnexpected<std::string>(message));
        String* e5 = new String(*e4);
        EXPECT_EQ(e0, *e4);
        delete e4;
        EXPECT_EQ(e0, *e5);
        delete e5;
    }
}
Exemplo n.º 2
0
TEST(WTF_Expected, expected)
{
    typedef Expected<int, const char*> E;
    typedef Expected<int, const void*> EV;
    typedef Expected<foo, const char*> FooChar;
    typedef Expected<foo, std::string> FooString;
    {
        auto e = E();
        EXPECT_TRUE(e.hasValue());
        EXPECT_EQ(e.value(), 0);
        EXPECT_EQ(e.valueOr(3.14), 0);
    }
    {
        constexpr E e;
        EXPECT_TRUE(e.hasValue());
        EXPECT_EQ(e.value(), 0);
        EXPECT_EQ(e.valueOr(3.14), 0);
    }
    {
        auto e = E(42);
        EXPECT_TRUE(e.hasValue());
        EXPECT_EQ(e.value(), 42);
        EXPECT_EQ(e.valueOr(3.14), 42);
        const auto e2(e);
        EXPECT_TRUE(e2.hasValue());
        EXPECT_EQ(e2.value(), 42);
        EXPECT_EQ(e2.valueOr(3.14), 42);
        E e3;
        e3 = e2;
        EXPECT_TRUE(e3.hasValue());
        EXPECT_EQ(e3.value(), 42);
        EXPECT_EQ(e3.valueOr(3.14), 42);
        const E e4 = e2;
        EXPECT_TRUE(e4.hasValue());
        EXPECT_EQ(e4.value(), 42);
        EXPECT_EQ(e4.valueOr(3.14), 42);
    }
    {
        constexpr E c(42);
        EXPECT_TRUE(c.hasValue());
        EXPECT_EQ(c.value(), 42);
        EXPECT_EQ(c.valueOr(3.14), 42);
        constexpr const auto c2(c);
        EXPECT_TRUE(c2.hasValue());
        EXPECT_EQ(c2.value(), 42);
        EXPECT_EQ(c2.valueOr(3.14), 42);
    }
    {
        auto u = E(makeUnexpected(oops));
        EXPECT_FALSE(u.hasValue());
        EXPECT_EQ(u.error(), oops);
        EXPECT_EQ(u.getUnexpected().value(), oops);
        EXPECT_EQ(u.valueOr(3.14), 3);
    }
    {
        auto uv = EV(makeUnexpected(oops));
        EXPECT_FALSE(uv.hasValue());
        EXPECT_EQ(uv.error(), oops);
        EXPECT_EQ(uv.getUnexpected().value(), oops);
        EXPECT_EQ(uv.valueOr(3.14), 3);
    }
    {
        E e = makeUnexpected(oops);
        EXPECT_FALSE(e.hasValue());
        EXPECT_EQ(e.error(), oops);
        EXPECT_EQ(e.getUnexpected().value(), oops);
        EXPECT_EQ(e.valueOr(3.14), 3);
    }
    {
        auto e = makeExpectedFromError<int, const char*>(oops);
        EXPECT_FALSE(e.hasValue());
        EXPECT_EQ(e.error(), oops);
        EXPECT_EQ(e.getUnexpected().value(), oops);
        EXPECT_EQ(e.valueOr(3.14), 3);
    }
    {
        auto e = makeExpectedFromError<int, const void*>(oops);
        EXPECT_FALSE(e.hasValue());
        EXPECT_EQ(e.error(), oops);
        EXPECT_EQ(e.getUnexpected().value(), oops);
        EXPECT_EQ(e.valueOr(3.14), 3);
    }
    {
        auto e = FooChar(42);
        EXPECT_EQ(e->v, 42);
        EXPECT_EQ((*e).v, 42);
    }
    {
        auto e0 = E(42);
        auto e1 = E(1024);
        swap(e0, e1);
        EXPECT_EQ(e0.value(), 1024);
        EXPECT_EQ(e1.value(), 42);
    }
    {
        auto e0 = E(makeUnexpected(oops));
        auto e1 = E(makeUnexpected(foof));
        swap(e0, e1);
        EXPECT_EQ(e0.error(), foof);
        EXPECT_EQ(e1.error(), oops);
    }
    {
        FooChar c(foo(42));
        EXPECT_EQ(c->v, 42);
        EXPECT_EQ((*c).v, 42);
    }
    {
        FooString s(foo(42));
        EXPECT_EQ(s->v, 42);
        EXPECT_EQ((*s).v, 42);
        const char* message = "very long failure string, for very bad failure cases";
        FooString e0(makeUnexpected<std::string>(message));
        FooString e1(makeUnexpected<std::string>(message));
        FooString e2(makeUnexpected<std::string>(std::string()));
        EXPECT_EQ(e0.error(), std::string(message));
        EXPECT_EQ(e0, e1);
        EXPECT_NE(e0, e2);
        FooString* e4 = new FooString(makeUnexpected<std::string>(message));
        FooString* e5 = new FooString(*e4);
        EXPECT_EQ(e0, *e4);
        delete e4;
        EXPECT_EQ(e0, *e5);
        delete e5;
    }
}