//+------------------------------------------------------------------+
//|                                                      TestKit.mqh |
//| Report and setup helpers for tests with the fake broker.         |
//+------------------------------------------------------------------+

#ifndef TRADE_GATEWAY_TEST_KIT_MQH
#define TRADE_GATEWAY_TEST_KIT_MQH // include guard

#include <TradeGateway\FakeGateway.mqh>
#include <TradeGateway\BreakEvenRule.mqh>

#define TEST_PRICE_TOL 0.0000001 // tolerance when two prices or volumes are compared

//+------------------------------------------------------------------+
//| Counts the cases and prints one line per case                    |
//+------------------------------------------------------------------+
class CTestReport
  {
private:
   int               m_passed;  // cases that passed
   int               m_failed;  // cases that failed
   int               m_case;    // number of the current case
   string            m_name;    // name of the current case
   string            m_failure; // first failed check of the current case

public:
                     CTestReport(void);
   void              Begin(const string name);
   void              Check(const bool condition,const string what);
   void              CheckInt(const long actual,const long expected,const string what);
   void              CheckPrice(const double actual,const double expected,const string what);
   void              CheckRetcode(const uint actual,const uint expected,const string what);
   void              CheckState(const ENUM_BE_STATE actual,const ENUM_BE_STATE expected,const string what);
   void              End(void);
   void              PrintSummary(void) const;

private:
   void              Fail(const string text);
  };

//+------------------------------------------------------------------+
//| Starts with no cases                                             |
//+------------------------------------------------------------------+
CTestReport::CTestReport(void) : m_passed(0),
   m_failed(0),
   m_case(0),
   m_name(""),
   m_failure("")
  {
  }

//+------------------------------------------------------------------+
//| Starts a case                                                    |
//+------------------------------------------------------------------+
void CTestReport::Begin(const string name)
  {
   m_case++;
   m_name = name;
   m_failure = "";
  }

//+------------------------------------------------------------------+
//| Checks a condition, remembers the first failure                  |
//+------------------------------------------------------------------+
void CTestReport::Check(const bool condition,const string what)
  {
   if(!condition)
      Fail(what);
  }

//+------------------------------------------------------------------+
//| Checks an integer value                                          |
//+------------------------------------------------------------------+
void CTestReport::CheckInt(const long actual,const long expected,const string what)
  {
   if(actual != expected)
      Fail(StringFormat("%s (expected %I64d, actual %I64d)", what, expected, actual));
  }

//+------------------------------------------------------------------+
//| Checks a price or volume                                         |
//+------------------------------------------------------------------+
void CTestReport::CheckPrice(const double actual,const double expected,const string what)
  {
   if(MathAbs(actual - expected) > TEST_PRICE_TOL)
      Fail(StringFormat("%s (expected %.8g, actual %.8g)", what, expected, actual));
  }

//+------------------------------------------------------------------+
//| Checks a retcode                                                 |
//+------------------------------------------------------------------+
void CTestReport::CheckRetcode(const uint actual,const uint expected,const string what)
  {
   if(actual != expected)
      Fail(StringFormat("%s (expected %u %s, actual %u %s)", what, expected,
                        GatewayRetcodeText(expected), actual, GatewayRetcodeText(actual)));
  }

//+------------------------------------------------------------------+
//| Checks the state of a ticket                                     |
//+------------------------------------------------------------------+
void CTestReport::CheckState(const ENUM_BE_STATE actual,const ENUM_BE_STATE expected,const string what)
  {
   if(actual != expected)
      Fail(StringFormat("%s (expected %s, actual %s)", what, EnumToString(expected), EnumToString(actual)));
  }

//+------------------------------------------------------------------+
//| Ends the case and prints its line                                |
//+------------------------------------------------------------------+
void CTestReport::End(void)
  {
   if(m_failure == "")
     {
      m_passed++;
      PrintFormat("PASS  %02d  %s", m_case, m_name);
      return;
     }
   m_failed++;
   PrintFormat("FAIL  %02d  %s: %s", m_case, m_name, m_failure);
  }

//+------------------------------------------------------------------+
//| Prints the result of all cases                                   |
//+------------------------------------------------------------------+
void CTestReport::PrintSummary(void) const
  {
   if(m_failed == 0)
      PrintFormat("TradeGatewayTests: %d of %d cases passed", m_passed, m_passed + m_failed);
   else
      PrintFormat("TradeGatewayTests: %d of %d cases passed, %d FAILED",
                  m_passed, m_passed + m_failed, m_failed);
  }

//+------------------------------------------------------------------+
//| Keeps the first failure of the case                              |
//+------------------------------------------------------------------+
void CTestReport::Fail(const string text)
  {
   if(m_failure == "")
      m_failure = text;
  }

//--- one report for the whole script
CTestReport g_report;

//+------------------------------------------------------------------+
//| Sets a quote and checks that the fake accepted it                |
//+------------------------------------------------------------------+
void TestQuote(CFakeGateway *fake,const string symbol,const double bid,const double ask)
  {
   g_report.Check(fake.SetQuote(symbol, bid, ask), "SetQuote " + symbol);
  }

//+------------------------------------------------------------------+
//| Sets the levels and checks that the fake accepted them           |
//+------------------------------------------------------------------+
void TestLevels(CFakeGateway *fake,const string symbol,const int stops_level,const int freeze_level)
  {
   g_report.Check(fake.SetLevels(symbol, stops_level, freeze_level), "SetLevels " + symbol);
  }

//+------------------------------------------------------------------+
//| Adds a position and checks that the fake accepted it             |
//+------------------------------------------------------------------+
ulong TestOpen(CFakeGateway *fake,const string symbol,const ENUM_POSITION_TYPE type,const double volume,
               const double price_open,const double sl,const double tp,const long magic)
  {
   const ulong ticket = fake.AddPosition(symbol, type, volume, price_open, sl, tp, magic);
   g_report.Check(ticket != 0, "AddPosition " + symbol);
   return ticket;
  }

//+------------------------------------------------------------------+
//| Reads a position and checks that it is open                      |
//+------------------------------------------------------------------+
void TestRead(CFakeGateway *fake,const ulong ticket,SGatewayPosition &position)
  {
   g_report.Check(fake.PositionRead(ticket, position), "PositionRead " + (string)ticket);
  }

//+------------------------------------------------------------------+
//| Reads a logged request and checks that it exists                 |
//+------------------------------------------------------------------+
void TestRequest(CFakeGateway *fake,const int index,SGatewayRequest &request)
  {
   g_report.Check(fake.RequestAt(index, request), "RequestAt " + (string)index);
  }

//+------------------------------------------------------------------+
//| Checks the return value and the retcode of one request           |
//+------------------------------------------------------------------+
void ExpectAnswer(const bool result,const uint retcode,const uint expected,const string what)
  {
   g_report.Check(result == GatewayRetcodeIsSuccess(expected), what + ": return value");
   g_report.CheckRetcode(retcode, expected, what);
  }

//+------------------------------------------------------------------+
//| Sends a modify request and checks the answer                     |
//+------------------------------------------------------------------+
void ExpectModify(CFakeGateway *fake,const ulong ticket,const double sl,const double tp,
                  const uint expected,const string what)
  {
   const bool result = fake.ModifyStops(ticket, sl, tp);
   ExpectAnswer(result, fake.LastRetcode(), expected, what);
  }

//+------------------------------------------------------------------+
//| Sends a close request and checks the answer                      |
//+------------------------------------------------------------------+
void ExpectClose(CFakeGateway *fake,const ulong ticket,const uint expected,const string what)
  {
   const bool result = fake.ClosePosition(ticket);
   ExpectAnswer(result, fake.LastRetcode(), expected, what);
  }

//+------------------------------------------------------------------+
//| Sends a partial close request and checks the answer              |
//+------------------------------------------------------------------+
void ExpectPartial(CFakeGateway *fake,const ulong ticket,const double volume,
                   const uint expected,const string what)
  {
   const bool result = fake.ClosePartial(ticket, volume);
   ExpectAnswer(result, fake.LastRetcode(), expected, what);
  }

#endif // TRADE_GATEWAY_TEST_KIT_MQH
//+------------------------------------------------------------------+
