/*
 * MIT License
 *
 * Copyright (c) 2022 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@gmail.com>
 *
 * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
 * software and associated documentation files (the "Software"), to deal in the Software
 * without restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#ifndef QT_PROTOBUF_BASIC_MULTI_TEST
#include "syntax.qpb.h"
#endif

#include <QMetaProperty>
#include <QSignalSpy>
#include <QtTest/QtTest>

#include <qtprotobuftestscommon.h>

class QtProtobufSyntaxTest : public QObject
{
    Q_OBJECT
public:
    QtProtobufSyntaxTest()
    {
        qRegisterProtobufTypes();
    }
private slots:
    void UnderscoresTest();
    void UpperCaseTest();
    void ReservedTest();
    void ReservedUpperCaseTest();
    void ReservedEnumTest();
    void LowerCaseEnumTest();
};

using namespace qtprotobufnamespace::tests;

void QtProtobufSyntaxTest::UnderscoresTest()
{
    //Sanity compilation checks
    Message_Uderscore_name msg1;
    MessageUderscorename msg2;
    MessageUnderscoreField msg3;
    PriorMessageUnderscoreField msg4;
    FollowingMessageUnderscoreField msg5;
    CombinedMessageUnderscoreField msg6;

    qProtobufAssertMessagePropertyRegistered<MessageUnderscoreField, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
    qProtobufAssertMessagePropertyRegistered<PriorMessageUnderscoreField, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
    qProtobufAssertMessagePropertyRegistered<PriorMessageUnderscoreField, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
    qProtobufAssertMessagePropertyRegistered<FollowingMessageUnderscoreField , QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
    qProtobufAssertMessagePropertyRegistered<CombinedMessageUnderscoreField , QtProtobuf::sint32>(1, "QtProtobuf::sint32", "underScoreMessageField");
}

void QtProtobufSyntaxTest::UpperCaseTest()
{
    qProtobufAssertMessagePropertyRegistered<MessageUpperCase, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "testField");
}

void QtProtobufSyntaxTest::ReservedTest()
{
    qProtobufAssertMessagePropertyRegistered<MessageReserved, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "import_proto");
    qProtobufAssertMessagePropertyRegistered<MessageReserved, QtProtobuf::sint32>(2, "QtProtobuf::sint32", "property_proto");
    qProtobufAssertMessagePropertyRegistered<MessageReserved, QtProtobuf::sint32>(3, "QtProtobuf::sint32", "id_proto");
}

void QtProtobufSyntaxTest::ReservedUpperCaseTest()
{
    qProtobufAssertMessagePropertyRegistered<MessageUpperCaseReserved, QtProtobuf::sint32>(1, "QtProtobuf::sint32", "import_proto");
    qProtobufAssertMessagePropertyRegistered<MessageUpperCaseReserved, QtProtobuf::sint32>(2, "QtProtobuf::sint32", "property_proto");
    qProtobufAssertMessagePropertyRegistered<MessageUpperCaseReserved, QtProtobuf::sint32>(3, "QtProtobuf::sint32", "id_proto");
}

void QtProtobufSyntaxTest::ReservedEnumTest()
{
    QVERIFY(MessageEnumReserved::staticMetaObject.enumeratorCount() > 0);
    QMetaEnum simpleEnum;
    for (int i = 0; i < MessageEnumReserved::staticMetaObject.enumeratorCount(); i++) {
        QMetaEnum tmp = MessageEnumReserved::staticMetaObject.enumerator(i);
        if (QString(tmp.name()) == QString("ReservedEnum")) {
            simpleEnum = tmp;
            break;
        }
    }
    QVERIFY(simpleEnum.isValid());
    QCOMPARE(simpleEnum.key(0), "Import");
    QCOMPARE(simpleEnum.key(1), "Property");
    QCOMPARE(simpleEnum.key(2), "Id");

    QCOMPARE(simpleEnum.value(0), 0);
    QCOMPARE(simpleEnum.value(1), 1);
    QCOMPARE(simpleEnum.value(2), 2);
}

void QtProtobufSyntaxTest::LowerCaseEnumTest()
{
    QVERIFY(MessageEnumReserved::staticMetaObject.enumeratorCount() > 0);
    QMetaEnum simpleEnum;
    for (int i = 0; i < MessageEnumReserved::staticMetaObject.enumeratorCount(); i++) {
        QMetaEnum tmp = MessageEnumReserved::staticMetaObject.enumerator(i);
        if (QString(tmp.name()) == QString("LowerCaseEnum")) {
            simpleEnum = tmp;
            break;
        }
    }
    QVERIFY(simpleEnum.isValid());
    QCOMPARE(simpleEnum.key(0), "EnumValue0");
    QCOMPARE(simpleEnum.key(1), "EnumValue1");
    QCOMPARE(simpleEnum.key(2), "EnumValue2");
}

QTEST_MAIN(QtProtobufSyntaxTest)
#include "syntaxtest.moc"
