Newer
Older
libj / modules / j / base / test / src / ut_throwable.cpp
#include <iostream>
#include <j/cppunit/cppunit.hpp>
#include <j/cppunit/assert.hpp>
#include <j/lang/throwable.hpp>
#include <j/lang/errno.hpp>

using namespace j;
using namespace j::lang;
using namespace j::cppunit;

class ThrowableChild : public Throwable
{
public:
    ThrowableChild() noexcept {};
    ~ThrowableChild() noexcept {};
    std::unique_ptr<Object> clone() const noexcept
    {
        return Throwable::clone();
    }
};

class ThrowableTest : public TestCase
{
public:
    ThrowableTest() {}
    ~ThrowableTest() {}
    void setUp() {}
    void tearDown() {}

    void testThrowable()
    {
        Throwable e;
        String exp = j::lang::Errno::message(j::lang::Errno::get());
        String act = e.getMessage();
        assertEquals(exp, act);
    }

    void testThrowableMsg()
    {
        String msg = "ERROR MESSAGE";
        Throwable e(msg);
        assertEquals(msg, e.getMessage());
    }

    void testThrowableCopy()
    {
        String msg = "ERROR MESSAGE 2";

        Throwable e(msg);
        Throwable ee = e;
        assertEquals(msg, e.getMessage());
        assertTrue(e != ee);
        assertFalse(e == ee);
    }

    void testThrowableMove()
    {
        String msg = "ERROR MESSAGE 3";

        Throwable e(msg);
        Throwable ee = std::move(e);
        assertEquals(msg, ee.getMessage());
    }

    void testThrowableToString()
    {
        String msg = "ERROR MESSAGE 3";
        Throwable e(msg);
        assertEquals(msg, e.toString());
    }

    void testOpEq()
    {
        String msg = "ERROR MESSAGE";
        String msg2 = "ERROR MESSAGE 2";

        Throwable e(msg);
        Throwable ee(msg2);
        ee = e;
        assertEquals(msg, ee.getMessage());

        ee = ee;
        assertEquals(msg, ee.getMessage());
    }

    void testOpEqMove()
    {
        String msg = "ERROR MESSAGE";
        String msg2 = "ERROR MESSAGE 2";

        Throwable e(msg);
        Throwable ee(msg2);
        ee = std::move(e);
        assertEquals(msg, ee.getMessage());

        ee = std::move(ee);
        assertEquals(msg, ee.getMessage());
    }

    void testClone()
    {
        ThrowableChild ch;
        std::unique_ptr<Object> ptr = ch.clone();
        String str = ptr->toString();
        assertTrue(str.length() > 0);
    }

    void suite()
    {
        RUN_TEST("new Throwable()", testThrowable);
        RUN_TEST("new Throwable(msg)", testThrowableMsg);
        RUN_TEST("copy", testThrowableCopy);
        RUN_TEST("move", testThrowableMove);
        RUN_TEST("toString()", testThrowableToString);
        RUN_TEST("operator=(const Throwable&)", testOpEq);
        RUN_TEST("operator=(const Throwable&&)", testOpEqMove);
        RUN_TEST("clone", testClone);
    }
};