mudlet/test/TMxpSendTagHandlerTest.cpp
Gustavo Sousa 102491a743
Refactoring/reimplementation of MXP support (#3625)
Refactoring/reimplementation of MXP protocol
        Extracts MXP support from TBuffer
        Separates input parsing from protocol processing: input is parsed into MxpTag objects by the MxpNodeBuilder and are passed to be handled by the MxpTagProcessor
        Class hierarchy of MxpTagHandler for implementing support for each tag: allows for a clear separation of the implementation of each tag and makes it easier for adding support for new tags
        Defines a clear interface (MxpClient) separating the MXP implementation from other parts of the code
        Solves some limitations of previous implementation such as supporting element definitions not only for<SEND> tags, interpolating named and positional attributes and interpolating &text; placeholder by tag content
        Includes support for COLOR tag
        Handles FONT, U, I, B, VAR and !ENTITY tags, but so far no defined behavior (can be implemented in MxpMudlet)
        Attributes that hold MXP state are now in the Host class to avoid needless copies when TBuffer is copied around
        Introduces QtTests for some of the classes
    Other refactorings intended to simplify TBuffer class, extracting responbilities that aren't directly related to the buffer mgmt
        Extracted TEncodingTable from TBuffer, this class is responsible for mapping encoding names to tables. It also removes some bloat from TBuffer.
        TLinkStore to manage links and associated hints to be displayed.
        TEntityResolver to map character entities, such as &gt; &#32; &#x20; to their associated string values; supports interpolating strings replacing the entity placeholders by their values
2020-05-03 19:09:42 +02:00

112 lines
3.2 KiB
C++

#include <QTest>
#include <TMxpTagParser.h>
#include "TMxpStubClient.h"
#include "TMxpSendTagHandler.h"
class TMxpSendTagHandlerTest : public QObject {
Q_OBJECT
private:
private slots:
void testStaticText()
{
// <SEND "tell Zugg " PROMPT>Zugg</SEND>
TMxpStubContext ctx;
TMxpStubClient stub;
TMxpTagParser parser;
MxpStartTag* startTag = parser.parseStartTag("<SEND \"tell Zugg \" PROMPT>");
MxpEndTag* endTag = parser.parseEndTag("</SEND>");
TMxpSendTagHandler sendTagHandler;
TMxpTagHandler& tagHandler = sendTagHandler;
tagHandler.handleTag(ctx, stub, startTag);
tagHandler.handleContent("Zugg");
tagHandler.handleTag(ctx, stub, endTag);
QCOMPARE(stub.mHrefs.size(), 1);
QCOMPARE(stub.mHrefs[0], "printCmdLine([[tell Zugg ]])");
QCOMPARE(stub.mHints.size(), 1);
QCOMPARE(stub.mHints[0], "tell Zugg ");
}
void testSimpleSend()
{
// <SEND>north</SEND>
TMxpStubContext ctx;
TMxpStubClient stub;
MxpStartTag startTag("SEND");
MxpEndTag endTag("SEND");
TMxpSendTagHandler sendTagHandler;
TMxpTagHandler& tagHandler = sendTagHandler;
tagHandler.handleTag(ctx, stub, &startTag);
tagHandler.handleContent("north");
tagHandler.handleTag(ctx, stub, &endTag);
QCOMPARE(stub.mHrefs.size(), 1);
QCOMPARE(stub.mHrefs[0], "send([[north]])");
QCOMPARE(stub.mHints.size(), 1);
QCOMPARE(stub.mHints[0], "north");
}
void testSendPrompt()
{
// <SEND href="&text;" PROMPT>north</SEND>
TMxpStubContext ctx;
TMxpStubClient stub;
TMxpTagParser parser;
MxpStartTag* startTag = parser.parseStartTag("<SEND href=\"&text;\" PROMPT>");
MxpEndTag* endTag = parser.parseEndTag("</SEND>");
TMxpSendTagHandler sendTagHandler;
TMxpTagHandler& tagHandler = sendTagHandler;
tagHandler.handleTag(ctx, stub, startTag);
tagHandler.handleContent("north");
tagHandler.handleTag(ctx, stub, endTag);
QCOMPARE(stub.mHrefs.size(), 1);
QCOMPARE(stub.mHrefs[0], "printCmdLine([[north]])");
QCOMPARE(stub.mHints.size(), 1);
QCOMPARE(stub.mHints[0], "north");
}
void testResolvingEntity()
{
// <SEND href="&text;" PROMPT>north</SEND>
TMxpStubContext ctx;
TMxpStubClient stub;
ctx.getEntityResolver().registerEntity("&charName;", "Gandalf");
TMxpTagParser parser;
MxpStartTag* startTag = parser.parseStartTag("<SEND href=\"say I am &charName;\">");
MxpEndTag* endTag = parser.parseEndTag("</SEND>");
TMxpSendTagHandler sendTagHandler;
TMxpTagHandler& tagHandler = sendTagHandler;
tagHandler.handleTag(ctx, stub, startTag);
tagHandler.handleContent("TAG CONTENT");
tagHandler.handleTag(ctx, stub, endTag);
QCOMPARE(stub.mHrefs.size(), 1);
QCOMPARE(stub.mHrefs[0], "send([[say I am Gandalf]])");
QCOMPARE(stub.mHints.size(), 1);
QCOMPARE(stub.mHints[0], "say I am Gandalf");
}
};
#include "TMxpSendTagHandlerTest.moc"
QTEST_MAIN(TMxpSendTagHandlerTest)