LCOV - code coverage report
Current view: top level - source/simulation2/components - CCmpTest.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 103 118 87.3 %
Date: 2023-01-19 00:18:29 Functions: 72 79 91.1 %

          Line data    Source code
       1             : /* Copyright (C) 2022 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "simulation2/system/Component.h"
      21             : #include "ICmpTest.h"
      22             : 
      23             : #include "simulation2/scripting/ScriptComponent.h"
      24             : #include "simulation2/MessageTypes.h"
      25             : 
      26          57 : class CCmpTest1A : public ICmpTest1
      27             : {
      28             : public:
      29         116 :     static void ClassInit(CComponentManager& componentManager)
      30             :     {
      31         116 :         componentManager.SubscribeToMessageType(MT_TurnStart);
      32         116 :         componentManager.SubscribeToMessageType(MT_Interpolate);
      33         116 :         componentManager.SubscribeToMessageType(MT_Destroy);
      34         116 :     }
      35             : 
      36          46 :     DEFAULT_COMPONENT_ALLOCATOR(Test1A)
      37             : 
      38             :     int32_t m_x;
      39             : 
      40         116 :     static std::string GetSchema()
      41             :     {
      42         116 :         return "<a:component type='test'/><ref name='anything'/>";
      43             :     }
      44             : 
      45          17 :     void Init(const CParamNode& paramNode) override
      46             :     {
      47          17 :         if (paramNode.GetChild("x").IsOk())
      48           9 :             m_x = paramNode.GetChild("x").ToInt();
      49             :         else
      50           8 :             m_x = 11000;
      51          17 :     }
      52             : 
      53          19 :     void Deinit() override
      54             :     {
      55          19 :     }
      56             : 
      57           6 :     void Serialize(ISerializer& serialize) override
      58             :     {
      59           6 :         serialize.NumberI32_Unbounded("x", m_x);
      60           6 :     }
      61             : 
      62           2 :     void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) override
      63             :     {
      64           2 :         deserialize.NumberI32_Unbounded("x", m_x);
      65           2 :     }
      66             : 
      67          31 :     int GetX() override
      68             :     {
      69          31 :         return m_x;
      70             :     }
      71             : 
      72          11 :     void HandleMessage(const CMessage& msg, bool UNUSED(global)) override
      73             :     {
      74          11 :         switch (msg.GetType())
      75             :         {
      76           3 :         case MT_Destroy:
      77           3 :             GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_RenderSubmit, this, false);
      78           3 :             break;
      79           5 :         case MT_TurnStart:
      80           5 :             m_x += 1;
      81           5 :             break;
      82           3 :         case MT_Interpolate:
      83           3 :             m_x += 2;
      84           3 :             break;
      85           0 :         default:
      86           0 :             m_x = 0;
      87           0 :             break;
      88             :         }
      89          11 :     }
      90             : };
      91             : 
      92         116 : REGISTER_COMPONENT_TYPE(Test1A)
      93             : 
      94           6 : class CCmpTest1B : public ICmpTest1
      95             : {
      96             : public:
      97         116 :     static void ClassInit(CComponentManager& componentManager)
      98             :     {
      99         116 :         componentManager.SubscribeToMessageType(MT_Update);
     100         116 :         componentManager.SubscribeGloballyToMessageType(MT_Interpolate);
     101         116 :     }
     102             : 
     103           4 :     DEFAULT_COMPONENT_ALLOCATOR(Test1B)
     104             : 
     105             :     int32_t m_x;
     106             : 
     107         116 :     static std::string GetSchema()
     108             :     {
     109         116 :         return "<a:component type='test'/><empty/>";
     110             :     }
     111             : 
     112           2 :     void Init(const CParamNode&) override
     113             :     {
     114           2 :         m_x = 12000;
     115           2 :     }
     116             : 
     117           2 :     void Deinit() override
     118             :     {
     119           2 :     }
     120             : 
     121           0 :     void Serialize(ISerializer& serialize) override
     122             :     {
     123           0 :         serialize.NumberI32_Unbounded("x", m_x);
     124           0 :     }
     125             : 
     126           0 :     void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) override
     127             :     {
     128           0 :         deserialize.NumberI32_Unbounded("x", m_x);
     129           0 :     }
     130             : 
     131           6 :     int GetX() override
     132             :     {
     133           6 :         return m_x;
     134             :     }
     135             : 
     136           3 :     void HandleMessage(const CMessage& msg, bool UNUSED(global)) override
     137             :     {
     138           3 :         switch (msg.GetType())
     139             :         {
     140           1 :         case MT_Update:
     141           1 :             m_x += 10;
     142           1 :             break;
     143           2 :         case MT_Interpolate:
     144           2 :             m_x += 20;
     145           2 :             break;
     146           0 :         default:
     147           0 :             m_x = 0;
     148           0 :             break;
     149             :         }
     150           3 :     }
     151             : };
     152             : 
     153         116 : REGISTER_COMPONENT_TYPE(Test1B)
     154             : 
     155          27 : class CCmpTest2A : public ICmpTest2
     156             : {
     157             : public:
     158         116 :     static void ClassInit(CComponentManager& componentManager)
     159             :     {
     160         116 :         componentManager.SubscribeToMessageType(MT_TurnStart);
     161         116 :         componentManager.SubscribeToMessageType(MT_Update);
     162         116 :     }
     163             : 
     164          26 :     DEFAULT_COMPONENT_ALLOCATOR(Test2A)
     165             : 
     166             :     int32_t m_x;
     167             : 
     168         116 :     static std::string GetSchema()
     169             :     {
     170         116 :         return "<a:component type='test'/><empty/>";
     171             :     }
     172             : 
     173           8 :     void Init(const CParamNode&) override
     174             :     {
     175           8 :         m_x = 21000;
     176           8 :     }
     177             : 
     178           9 :     void Deinit() override
     179             :     {
     180           9 :     }
     181             : 
     182           4 :     void Serialize(ISerializer& serialize) override
     183             :     {
     184           4 :         serialize.NumberI32_Unbounded("x", m_x);
     185           4 :     }
     186             : 
     187           1 :     void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) override
     188             :     {
     189           1 :         deserialize.NumberI32_Unbounded("x", m_x);
     190           1 :     }
     191             : 
     192          18 :     int GetX() override
     193             :     {
     194          18 :         return m_x;
     195             :     }
     196             : 
     197           6 :     void HandleMessage(const CMessage& msg, bool UNUSED(global)) override
     198             :     {
     199           6 :         switch (msg.GetType())
     200             :         {
     201           2 :         case MT_TurnStart:
     202           2 :             m_x += 50;
     203           2 :             break;
     204           4 :         case MT_Update:
     205           4 :             m_x += static_cast<const CMessageUpdate&> (msg).turnLength.ToInt_RoundToZero();
     206           4 :             break;
     207           0 :         default:
     208           0 :             m_x = 0;
     209           0 :             break;
     210             :         }
     211           6 :     }
     212             : };
     213             : 
     214         116 : REGISTER_COMPONENT_TYPE(Test2A)
     215             : 
     216             : ////////////////////////////////////////////////////////////////
     217             : 
     218          74 : class CCmpTest1Scripted : public ICmpTest1
     219             : {
     220             : public:
     221         440 :     DEFAULT_SCRIPT_WRAPPER(Test1Scripted)
     222             : 
     223          46 :     int GetX() override
     224             :     {
     225          46 :         return m_Script.Call<int> ("GetX");
     226             :     }
     227             : };
     228             : 
     229         116 : REGISTER_COMPONENT_SCRIPT_WRAPPER(Test1Scripted)
     230             : 
     231             : ////////////////////////////////////////////////////////////////
     232             : 
     233          20 : class CCmpTest2Scripted : public ICmpTest2
     234             : {
     235             : public:
     236         284 :     DEFAULT_SCRIPT_WRAPPER(Test2Scripted)
     237             : 
     238          14 :     int GetX() override
     239             :     {
     240          14 :         return m_Script.Call<int> ("GetX");
     241             :     }
     242             : };
     243             : 
     244         119 : REGISTER_COMPONENT_SCRIPT_WRAPPER(Test2Scripted)

Generated by: LCOV version 1.13