/*
* Author:          Patrick-Christopher Mattulat
* Co-Author:       Claude Sonnet 4.6 (LLM)
* Company:         Lynar Studios
* E-Mail:          webmaster@lynarstudios.com
* Created:         2023-02-11
* Changed:         2026-06-23
*
* */

#include <gtest/gtest.h>
#include <ls-std/ls-std-core.hpp>
#include <ls-std/ls-std-io.hpp>
#include <memory>

using ls::standard::core::IllegalArgumentException;
using ls::standard::core::type::byte_field;
using ls::standard::io::NewLine;
using ls::standard::io::SectionPairRowSingleValue;
using ls::standard::io::SerializableSectionPairParameter;
using ls::standard::io::SerializableSectionPairRowSingleValue;
using std::dynamic_pointer_cast;
using std::make_shared;
using std::shared_ptr;
using std::string;
using testing::Test;
using testing::TestWithParam;
using testing::Values;

namespace
{
  class SerializableSectionPairRowSingleValueTest : public Test
  {
    public:

      SerializableSectionPairRowSingleValueTest() = default;
      ~SerializableSectionPairRowSingleValueTest() override = default;
  };

  class SerializableSectionPairRowSingleValueTest_LineBreakTest : public TestWithParam<string>
  {
    public:

      SerializableSectionPairRowSingleValueTest_LineBreakTest() = default;
      ~SerializableSectionPairRowSingleValueTest_LineBreakTest() override = default;
  };

  TEST_F(SerializableSectionPairRowSingleValueTest, constructor_no_reference)
  {
    const SerializableSectionPairParameter parameter{};

    EXPECT_THROW(
        {
          try
          {
            SerializableSectionPairRowSingleValue serializable(parameter);
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  TEST_F(SerializableSectionPairRowSingleValueTest, getClassName)
  {
    SerializableSectionPairParameter parameter{};
    parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));

    ASSERT_STREQ("SerializableSectionPairRowSingleValue", SerializableSectionPairRowSingleValue{parameter}.getClassName().c_str());
  }

  TEST_F(SerializableSectionPairRowSingleValueTest, getValue)
  {
    SerializableSectionPairParameter parameter{};
    const auto value = make_shared<SectionPairRowSingleValue>("empty");
    parameter.setValue(value);
    const SerializableSectionPairRowSingleValue serializable(parameter);

    ASSERT_TRUE(value == serializable.getValue());
  }

  TEST_P(SerializableSectionPairRowSingleValueTest_LineBreakTest, marshal)
  {
    const string& newLine = GetParam();
    SerializableSectionPairParameter parameter{};
    parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));
    parameter.setNewLine(newLine);
    SerializableSectionPairRowSingleValue serializable(parameter);

    const byte_field expected = "empty" + newLine;
    const byte_field actual = serializable.marshal();

    ASSERT_STREQ(expected.c_str(), actual.c_str());
  }

  TEST_F(SerializableSectionPairRowSingleValueTest, unmarshal)
  {
    SerializableSectionPairParameter parameter{};
    parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));
    SerializableSectionPairRowSingleValue serializable(parameter);
    serializable.unmarshal("blue");

    ASSERT_STREQ("blue", dynamic_pointer_cast<SectionPairRowSingleValue>(parameter.getValue())->get().c_str());
  }

  TEST_F(SerializableSectionPairRowSingleValueTest, unmarshal_empty_string)
  {
    SerializableSectionPairParameter parameter{};
    parameter.setValue(make_shared<SectionPairRowSingleValue>("empty"));
    SerializableSectionPairRowSingleValue serializable(parameter);

    EXPECT_THROW(
        {
          try
          {
            serializable.unmarshal("");
          }
          catch (const IllegalArgumentException &_exception)
          {
            throw;
          }
        },
        IllegalArgumentException);
  }

  INSTANTIATE_TEST_SUITE_P(LineBreakTest, SerializableSectionPairRowSingleValueTest_LineBreakTest, Values(NewLine::getUnixNewLine(), NewLine::getWindowsNewLine()));
}
