2026-03-13 15:43:54 -03:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| UnitTests.mq5 |
|
|
|
|
|
//| Unit Tests for Package riskgate |
|
|
|
|
|
//| Organization: douglasrechia |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
|
|
|
|
|
#include "../knitpkg/build/BuildInfo.mqh"
|
|
|
|
|
|
2026-03-30 11:38:29 -03:00
|
|
|
#property copyright "Copyright (C) 2026 by " + MANIFEST_AUTHOR
|
2026-03-13 15:43:54 -03:00
|
|
|
#property link "https://knitpkg.dev"
|
|
|
|
|
#property description ""
|
|
|
|
|
#property description "Version: " + MANIFEST_VERSION
|
|
|
|
|
#property description ""
|
|
|
|
|
#property description "Description: Unit tests for package riskgate"
|
|
|
|
|
#property description "Organization: douglasrechia"
|
2026-03-30 11:38:29 -03:00
|
|
|
#property description "Author: " + MANIFEST_AUTHOR
|
|
|
|
|
#property description "License: " + MANIFEST_LICENSE
|
2026-03-13 15:43:54 -03:00
|
|
|
#property description ""
|
|
|
|
|
#property description "Powered by KnitPkg for MetaTrader"
|
|
|
|
|
#property description "https://knitpkg.dev"
|
|
|
|
|
|
|
|
|
|
// Include the headers under test
|
|
|
|
|
#include "../knitpkg/include/douglasrechia/riskgate/RiskGateHandler.mqh"
|
|
|
|
|
#include "../knitpkg/include/douglasrechia/riskgate/RiskGateServer.mqh"
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Test handler for basic server functionality |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
class TestHandler : public douglasrechia::RiskGateHandler
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void OnSignal(CJAVal& signal, CJAVal& response) override
|
|
|
|
|
{
|
|
|
|
|
string symbol = signal["symbol"].ToStr();
|
|
|
|
|
double requestedLot = signal["lot"].ToDbl();
|
|
|
|
|
|
|
|
|
|
response["approved"] = true;
|
|
|
|
|
response["lot"] = requestedLot * 0.5;
|
|
|
|
|
response["reason"] = "test_handler";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string Name() override { return "TestHandler"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Test basic handler functionality |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
bool TestDumbHandler()
|
|
|
|
|
{
|
|
|
|
|
TestHandler handler;
|
|
|
|
|
|
|
|
|
|
CJAVal signal, response;
|
|
|
|
|
signal["symbol"] = "EURUSD";
|
|
|
|
|
signal["lot"] = 1.0;
|
|
|
|
|
signal["side"] = "BUY";
|
|
|
|
|
|
|
|
|
|
handler.OnSignal(signal, response);
|
|
|
|
|
|
|
|
|
|
bool approved = response["approved"].ToBool();
|
|
|
|
|
double lot = response["lot"].ToDbl();
|
|
|
|
|
string reason = response["reason"].ToStr();
|
|
|
|
|
|
|
|
|
|
if(!approved)
|
|
|
|
|
{
|
|
|
|
|
Print("Test failed: signal not approved");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(lot != 0.5)
|
|
|
|
|
{
|
|
|
|
|
PrintFormat("Test failed: expected lot=0.5, got lot=%.2f", lot);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(reason != "test_handler")
|
|
|
|
|
{
|
|
|
|
|
PrintFormat("Test failed: expected reason='test_handler', got reason='%s'", reason);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| DoTests |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void DoTests(int &testsPerformed,int &testsPassed)
|
|
|
|
|
{
|
|
|
|
|
string testDumbHandler="";
|
|
|
|
|
|
|
|
|
|
//--- TestDumbHandler
|
|
|
|
|
testsPerformed++;
|
|
|
|
|
testDumbHandler="TestDumbHandler";
|
|
|
|
|
if(TestDumbHandler())
|
|
|
|
|
{
|
|
|
|
|
testsPassed++;
|
|
|
|
|
PrintFormat("%s pass",testDumbHandler);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
PrintFormat("%s failed",testDumbHandler);
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
// Add more tests here as needed
|
|
|
|
|
}
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| UnitTests() |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void UnitTests(const string packageName)
|
|
|
|
|
{
|
|
|
|
|
PrintFormat("Unit tests for Package %s\n",packageName);
|
|
|
|
|
//--- initial values
|
|
|
|
|
int testsPerformed=0;
|
|
|
|
|
int testsPassed=0;
|
|
|
|
|
//--- test distributions
|
|
|
|
|
DoTests(testsPerformed,testsPassed);
|
|
|
|
|
//--- print statistics
|
|
|
|
|
PrintFormat("\n%d of %d passed",testsPassed,testsPerformed);
|
|
|
|
|
}
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Script program start function |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void OnStart()
|
|
|
|
|
{
|
|
|
|
|
UnitTests("riskgate");
|
|
|
|
|
}
|
|
|
|
|
//+------------------------------------------------------------------+
|