// docker-finance | modern accounting for the power-user
//
// Copyright (C) 2021-2025 Aaron Fiore (Founder, Evergreen Crypto LLC)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! \file
//! \author Aaron Fiore (Founder, Evergreen Crypto LLC)
//! \note File intended to be loaded into ROOT.cern framework / Cling interpreter
//! \since docker-finance 1.0.0

#ifndef CONTAINER_SRC_ROOT_TEST_UNIT_TYPE_HH_
#define CONTAINER_SRC_ROOT_TEST_UNIT_TYPE_HH_

#include <gtest/gtest.h>

#include <utility>

#include "../common/type.hh"

//! \namespace dfi
//! \since docker-finance 1.0.0
namespace dfi
{
//! \namespace dfi::tests
//! \brief docker-finance common test framework
//! \ingroup cpp_tests
//! \since docker-finance 1.0.0
namespace tests
{
//! \namespace dfi::tests::unit
//! \brief docker-finance unit test cases
//! \ingroup cpp_tests
//! \since docker-finance 1.0.0
namespace unit
{

namespace common = ::dfi::common;

//
// RuntimeError
//

//! \brief Exception fixture (RuntimeError)
//! \since docker-finance 1.0.0
struct RuntimeError : public Exception
{
};

TEST_F(RuntimeError, throw_ex_if)  // cppcheck-suppress syntaxError
{
  try
    {
      common::throw_ex_if<common::type::RuntimeError>(false, what);
    }
  catch (const type::Exception& ex)
    {
      ASSERT_EQ(ex.type(), t_type::RuntimeError);
      ASSERT_EQ(ex.what(), what);
    }
}

TEST_F(RuntimeError, throw_ex)
{
  try
    {
      common::throw_ex<common::type::RuntimeError>(what);
    }
  catch (const type::Exception& ex)
    {
      ASSERT_EQ(ex.type(), t_type::RuntimeError);
      // NOTE: no case for ex.what() because message is formatted within function
    }
}

TEST_F(RuntimeError, rethrow)
{
  ASSERT_THROW(
      {
        try
          {
            throw type::RuntimeError(what);
          }
        catch (const type::Exception& ex)
          {
            ASSERT_EQ(ex.type(), t_type::RuntimeError);
            ASSERT_EQ(ex.what(), what);
            throw;
          }
      },
      type::Exception);
}

TEST_F(RuntimeError, copy_assignment)
{
  type::RuntimeError one(what);
  type::RuntimeError two;

  two = one;

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STREQ(one.what(), two.what());
}

TEST_F(RuntimeError, copy_ctor)
{
  type::RuntimeError one(what);
  type::RuntimeError two(one);

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STREQ(one.what(), two.what());
}

TEST_F(RuntimeError, move_assignment)
{
  type::RuntimeError one(what);
  type::RuntimeError two;

  two = std::move(one);

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STRNE(one.what(), two.what());
}

TEST_F(RuntimeError, move_ctor)
{
  type::RuntimeError one(what);
  type::RuntimeError two(std::move(one));

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STRNE(one.what(), two.what());
}

//
// InvalidArgument
//

//! \brief Exception fixture (InvalidArgument)
//! \since docker-finance 1.0.0
struct InvalidArgument : public Exception
{
};

TEST_F(InvalidArgument, throw_ex_if)
{
  try
    {
      common::throw_ex_if<common::type::InvalidArgument>(false, what);
    }
  catch (const type::Exception& ex)
    {
      ASSERT_EQ(ex.type(), t_type::InvalidArgument);
      ASSERT_EQ(ex.what(), what);
    }
}

TEST_F(InvalidArgument, throw_ex)
{
  try
    {
      common::throw_ex<common::type::InvalidArgument>(what);
    }
  catch (const type::Exception& ex)
    {
      ASSERT_EQ(ex.type(), t_type::InvalidArgument);
      // NOTE: no case for ex.what() because message is formatted within function
    }
}

TEST_F(InvalidArgument, rethrow)
{
  ASSERT_THROW(
      {
        try
          {
            throw type::InvalidArgument(what);
          }
        catch (const type::Exception& ex)
          {
            ASSERT_EQ(ex.type(), t_type::InvalidArgument);
            ASSERT_EQ(ex.what(), what);
            throw;
          }
      },
      type::Exception);
}

TEST_F(InvalidArgument, copy_assignment)
{
  type::InvalidArgument one(what);
  type::InvalidArgument two;

  two = one;

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STREQ(one.what(), two.what());
}

TEST_F(InvalidArgument, copy_ctor)
{
  type::InvalidArgument one(what);
  type::InvalidArgument two(one);

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STREQ(one.what(), two.what());
}

TEST_F(InvalidArgument, move_assignment)
{
  type::InvalidArgument one(what);
  type::InvalidArgument two;

  two = std::move(one);

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STRNE(one.what(), two.what());
}

TEST_F(InvalidArgument, move_ctor)
{
  type::InvalidArgument one(what);
  type::InvalidArgument two(std::move(one));

  ASSERT_EQ(one.type(), two.type());
  ASSERT_STRNE(one.what(), two.what());
}

}  // namespace unit
}  // namespace tests
}  // namespace dfi

#endif  // CONTAINER_SRC_ROOT_TEST_UNIT_TYPE_HH_

// # vim: sw=2 sts=2 si ai et
