// SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
// SPDX-FileCopyrightText: 2024 Arjen Hiemstra <ahiemstra@heimr.nl>
//
// This file is automatically generated from autotest.cpp.j2.
// To regenerate, run `tools/propertygenerator/generate_properties.py`.

#include <QtTest>

#include <properties/OffsetProperty.h>

#include "CreateTestInstances.h"

using namespace Union::Properties;
using namespace Qt::StringLiterals;

class TestOffsetProperty : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void testNull()
    {
        auto property = std::make_unique<OffsetProperty>();

        // A null instance should not have any values for its properties.
        QVERIFY(!property->horizontal().has_value());
        QVERIFY(!property->vertical().has_value());
    }

    void testEmpty()
    {
        auto property = OffsetProperty::empty();

        // An empty instance should only have values that are considered "empty".
        QCOMPARE(property->horizontal().value(), emptyValue<qreal>());
        QCOMPARE(property->vertical().value(), emptyValue<qreal>());
    }

    void testHasAnyValue()
    {
        auto property = std::make_unique<OffsetProperty>();

        // An empty instance should not have any values for its properties.
        QVERIFY(!property->hasAnyValue());

        {
            qreal value;
            property->setHorizontal(value);
            QVERIFY(property->hasAnyValue());
            property->setHorizontal(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
        {
            qreal value;
            property->setVertical(value);
            QVERIFY(property->hasAnyValue());
            property->setVertical(std::nullopt);
            QVERIFY(!property->hasAnyValue());
        }
    }

    void testResolveProperties()
    {
        auto source = std::make_unique<OffsetProperty>();
        auto destination = std::make_unique<OffsetProperty>();

        QVERIFY(!source->hasAnyValue());
        QVERIFY(!destination->hasAnyValue());

        // Calling resolve on empty source and destination should have no effect.
        OffsetProperty::resolveProperties(source.get(), destination.get());

        QVERIFY(!destination->hasAnyValue());

        source->setHorizontal(qreal{});
        source->setVertical(qreal{});

        QVERIFY(source->hasAnyValue());
        QVERIFY(!destination->hasAnyValue());

        OffsetProperty::resolveProperties(source.get(), destination.get());

        QVERIFY(destination->hasAnyValue());

        QCOMPARE(destination->horizontal(), source->horizontal());
        QCOMPARE(destination->vertical(), source->vertical());
    }

    void testDataStream()
    {
        auto group = testOffsetPropertyInstance();

        QByteArray data;
        QDataStream writeStream(&data, QIODevice::WriteOnly);
        writeStream << group.get();

        QCOMPARE(writeStream.status(), QDataStream::Status::Ok);
        QVERIFY(data.size() > 0);

        QDataStream readStream(data);
        auto readGroup = std::make_unique<OffsetProperty>();
        readStream >> readGroup;

        QCOMPARE(readStream.status(), QDataStream::Status::Ok);
        QCOMPARE(*readGroup, *group);
    }
};

QTEST_MAIN(TestOffsetProperty)

#include "TestOffsetProperty.moc"