|
| 1 | +#include <Arduino.h> |
| 2 | +#include "DIAG.h" |
| 3 | +#include "Ztest.h" |
| 4 | +#include "DCCEXParser.h" |
| 5 | +#include "StringFormatter.h" |
| 6 | + |
| 7 | +RingStream * Ztest::ring = new RingStream(128); |
| 8 | + |
| 9 | +void Ztest::parse(const FSH * command, const FSH * expect, bool (*test)() ) { |
| 10 | + |
| 11 | + DIAG(F("ZTEST %S"), command); |
| 12 | + |
| 13 | + // create copy of command in RAM |
| 14 | + auto commandLength= STRLEN_P((char *)command) + 1; |
| 15 | + char commandBuffer[commandLength+1]; |
| 16 | + STRNCPY_P(commandBuffer, (PGM_P)command,commandLength+1); |
| 17 | + |
| 18 | + if (expect) { |
| 19 | + // create response buffer to collect comparison |
| 20 | + ring->flush(); |
| 21 | + ring->mark(0); // mark the start of the response |
| 22 | + DCCEXParser::parseOne(ring, (byte *)commandBuffer, ring); |
| 23 | + ring->commit(); |
| 24 | + } |
| 25 | + else { |
| 26 | + // run without output test |
| 27 | + DCCEXParser::parseOne(& USB_SERIAL, (byte *)commandBuffer,nullptr); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + // test the assert of side effects |
| 32 | + if (test) { |
| 33 | + auto result=test(); |
| 34 | + DIAG(F("ZTEST assert %S"), result ? F("OK") : F("FAILED")); |
| 35 | + } |
| 36 | + |
| 37 | + if (expect) { |
| 38 | + // Copy output to serial and check with expected |
| 39 | + ring->read(); // read redundant client id |
| 40 | + auto responseLength=ring->count(); |
| 41 | + if (responseLength < 1) { |
| 42 | + DIAG(F("!!....(no resp) ZTEST")); |
| 43 | + return; // no response |
| 44 | + } |
| 45 | + |
| 46 | + // show output while comparing |
| 47 | + char output[responseLength+1]; |
| 48 | + for (int16_t i=0;i<responseLength;i++) output[i]=ring->read(); |
| 49 | + output[responseLength]=0; |
| 50 | + DIAG(F("ZTEST response:%s"), output); |
| 51 | + DIAG(F("ZTEST expect :%S"), (char *)expect); |
| 52 | + if (STRCMP_P(output, (char *)expect) != 0) |
| 53 | + DIAG(F("ZTEST expect failed\n")); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments