157 lines
5.1 KiB
MQL5
157 lines
5.1 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| UnitTests.mq5 |
|
|
//| Unit Tests for Package riskgate-ea |
|
|
//| Organization: douglasrechia |
|
|
//+------------------------------------------------------------------+
|
|
|
|
#include "../knitpkg/build/BuildInfo.mqh"
|
|
|
|
#property copyright "Copyright (C) 2026 by " + MANIFEST_AUTHOR
|
|
#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"
|
|
#property description "Author: " + MANIFEST_AUTHOR
|
|
#property description "License: " + MANIFEST_LICENSE
|
|
#property description ""
|
|
#property description "Powered by KnitPkg for MetaTrader"
|
|
#property description "https://knitpkg.dev"
|
|
|
|
// Include the headers under test
|
|
#include "../knitpkg/include/douglasrechia/riskgate-ea/RiskGateClient.mqh"
|
|
|
|
#define TEST_MSG_SEPARATOR "\r\n\r\n"
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| TestRiskGateClientBasicUsage |
|
|
//+------------------------------------------------------------------+
|
|
bool TestRiskGateClientBasicUsage()
|
|
{
|
|
const ushort TEST_PORT = 15555;
|
|
|
|
// Create mock server
|
|
douglasrechia::ServerSocket* server = new douglasrechia::ServerSocket(TEST_PORT);
|
|
if(!server.Created())
|
|
{
|
|
Print("Failed to create test server: ", server.GetLastError());
|
|
delete server;
|
|
return false;
|
|
}
|
|
|
|
// Create client and connect
|
|
douglasrechia::RiskGateClient client("127.0.0.1", TEST_PORT, 1000);
|
|
if(!client.Connect())
|
|
{
|
|
Print("Client failed to connect");
|
|
delete server;
|
|
return false;
|
|
}
|
|
|
|
// Accept connection on server side
|
|
douglasrechia::ClientSocket* clientConn = NULL;
|
|
int attempts = 0;
|
|
while(clientConn == NULL && attempts < 100)
|
|
{
|
|
clientConn = server.Accept();
|
|
if(clientConn == NULL)
|
|
Sleep(10);
|
|
attempts++;
|
|
}
|
|
|
|
if(clientConn == NULL)
|
|
{
|
|
Print("Server failed to accept connection");
|
|
client.Disconnect();
|
|
delete server;
|
|
return false;
|
|
}
|
|
|
|
// Create test signal
|
|
CJAVal signal, response;
|
|
signal["symbol"] = "EURUSD";
|
|
signal["side"] = "BUY";
|
|
signal["stop_loss"] = 1.0850;
|
|
|
|
// Start async request (client sends signal)
|
|
bool requestStarted = true;
|
|
|
|
// Server receives signal
|
|
string receivedSignal = "";
|
|
attempts = 0;
|
|
while(receivedSignal == "" && attempts < 100)
|
|
{
|
|
receivedSignal = clientConn.Receive(TEST_MSG_SEPARATOR);
|
|
if(receivedSignal == "")
|
|
Sleep(10);
|
|
attempts++;
|
|
}
|
|
|
|
// Server sends mock response
|
|
CJAVal mockResponse;
|
|
mockResponse["approved"] = true;
|
|
mockResponse["lot"] = 0.05;
|
|
mockResponse["reason"] = "test_approved";
|
|
string responseStr = mockResponse.Serialize() + TEST_MSG_SEPARATOR;
|
|
clientConn.Send(responseStr);
|
|
|
|
// Client receives response
|
|
bool online = client.RequestPositionSize(signal, response);
|
|
|
|
// Verify response
|
|
bool testPassed = online &&
|
|
response["approved"].ToBool() == true &&
|
|
response["lot"].ToDbl() == 0.05 &&
|
|
response["reason"].ToStr() == "test_approved";
|
|
|
|
// Cleanup
|
|
client.Disconnect();
|
|
delete clientConn;
|
|
delete server;
|
|
|
|
return testPassed;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| DoTests |
|
|
//+------------------------------------------------------------------+
|
|
void DoTests(int &testsPerformed,int &testsPassed)
|
|
{
|
|
string testName="";
|
|
|
|
//--- TestRiskGateClientBasicUsage
|
|
testsPerformed++;
|
|
testName="TestRiskGateClientBasicUsage";
|
|
if(TestRiskGateClientBasicUsage())
|
|
{
|
|
testsPassed++;
|
|
PrintFormat("%s pass",testName);
|
|
}
|
|
else
|
|
PrintFormat("%s failed",testName);
|
|
|
|
//---
|
|
// 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-ea");
|
|
}
|
|
//+------------------------------------------------------------------+
|