//----------------------------------------------------------------------------
///
/// \file   main.cpp
///
/// \brief  main file for pinmux
///
/// \date   20180129
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#if defined ( CATCH2 )
#include <catch2/catch_test_macros.hpp>
#elif defined ( CATCH )
#include <catch/catch.hpp>
#else
#error "Either 'catch' or 'catch2' has to be installed"
#endif

//--- Own ----------------------------

#include "../src/board.h"
#include "../src/app.h"
#include "../src/pinmux.h"
#include "writerPlatform.h"

#define TEST_ALL
#define STOP_ON_FAIL

//---Implementation------------------------------------------------------------


TEST_CASE( "Overload periphery", "[default]" ) {
   CPinmux pinmux;
   CApp app;
   CBoard board;
   bool sta;
   CPeripheryPtr p;

   SECTION( "attributes" )
   {
      const char *boardString_OA=
      R"JSON(
      {
          "name": "board_alternate_function",
          "version": "1",
          "peripheries": [
             { "type": "GPIO", "periphery": "PA2", "status": "disabled", "pin": "#1" },
             { "type": "GPIO", "periphery": "PA2", "pin": "#2" },
             { "type": "GPIO", "periphery": "PA3", "pin": "#3", "status": "disabled" },
             { "type": "GPIO", "periphery": "PA3", "pin": "#3", "status": "disabled" },
             { "type": "GPIO", "periphery": "PA4" },
             { "type": "GPIO", "periphery": "PA4", "status": "disabled" }
          ],
          "constructors": [
               {
                  "type": "GPIO",
                  "constructorArgument": "eGpioPeripheral<PeripheryBlock>, eGpioPin<PeripheryNumber>",
                  "attributeName": "<unlikely>",
                  "arguments": [
                     {
                        "type": "fixed",
                        "value": "eGpioPeripheral<PeripheryBlock>"
                     },
                     {
                        "type": "fixed",
                        "value": "eGpioPin<PeripheryNumber>"
                     }
                  ],
                  "initFunction": "__HAL_RCC_GPIO<PeripheryBlock>_CLK_ENABLE"
               }
         ],
         "final": "yes"
      }
      )JSON";

      app.clear();
      board.clear();
      app.setBoardId("myboard");
      sta=board.loadByteArray( QByteArray(boardString_OA), "overload" );
      REQUIRE ( sta == true );
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "overload_attr",
                   "periphery": [
                      { "type": "GPIO", "name": "1" }
                    ]
               })JSON" ), "intern" );

      pinmux.setBoardApp(board, app);
      pinmux.resolveApp();

      p=board.findPeripheryByPeripheryName( QString("PA2") );
      REQUIRE ( p == true );
      REQUIRE( p->getEnabled() == true );
      REQUIRE( p->getAttribute( "pin" ) == "#2" );

      p=board.findPeripheryByPeripheryName( QString("PA3") );
      REQUIRE ( p == true );
      REQUIRE( p->getEnabled() == false );
      REQUIRE( p->getAttribute( "pin" ) == "#3" );

      p=board.findPeripheryByPeripheryName( QString("PA4") );
      REQUIRE ( p == true );
      REQUIRE( p->getEnabled() == true );
      REQUIRE( p->getAttribute( "pin" ) == "" );
   }
}


//---fin-----------------------------------------------------------------------
