File indexing completed on 2024-04-28 05:50:36

0001 /*
0002     SPDX-FileCopyrightText: 2018 Kurt Hindenburg <kurt.hindenburg@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef VT102EMULATIONTEST_H
0008 #define VT102EMULATIONTEST_H
0009 
0010 #include <variant>
0011 #include <vector>
0012 
0013 #include <QObject>
0014 
0015 #include "Vt102Emulation.h"
0016 
0017 namespace Konsole
0018 {
0019 class TestEmulation;
0020 
0021 class Vt102EmulationTest : public QObject
0022 {
0023     Q_OBJECT
0024 
0025 private Q_SLOTS:
0026     void testTokenFunctions();
0027 
0028     void testParse();
0029     void testTokenizing_data();
0030     void testTokenizing();
0031 
0032     void testTokenizingVT52_data();
0033     void testTokenizingVT52();
0034 
0035 private:
0036     static void sendAndCompare(TestEmulation *em, const char *input, size_t inputLen, const QString &expectedPrint, const QByteArray &expectedSent);
0037 };
0038 
0039 class TestEmulation : public Vt102Emulation
0040 {
0041     Q_OBJECT
0042     // Give us access to protected functions
0043     friend class Vt102EmulationTest;
0044 
0045     QByteArray lastSent;
0046 
0047 public:
0048     struct ProcessToken {
0049         int code;
0050         int p;
0051         int q;
0052 
0053         bool operator==(const ProcessToken &rhs) const
0054         {
0055             return code == rhs.code && p == rhs.p && q == rhs.q;
0056         }
0057     };
0058     struct ProcessSessionAttributeRequest {
0059         std::vector<uint> chars;
0060 
0061         bool operator==(const ProcessSessionAttributeRequest &rhs) const
0062         {
0063             return chars == rhs.chars;
0064         }
0065     };
0066     struct ProcessChecksumRequest {
0067         std::vector<int> args;
0068 
0069         bool operator==(const ProcessChecksumRequest &rhs) const
0070         {
0071             return args == rhs.args;
0072         }
0073     };
0074     struct DecodingError {
0075         bool operator==(const DecodingError &) const
0076         {
0077             return true;
0078         }
0079     };
0080     using Item = std::variant<ProcessToken, ProcessSessionAttributeRequest, ProcessChecksumRequest, DecodingError>;
0081 
0082 private:
0083     std::vector<Item> items;
0084 
0085     bool blockFurtherProcessing = false;
0086 
0087 public:
0088     void receiveChars(const QVector<uint> &c) override
0089     {
0090         Vt102Emulation::receiveChars(c);
0091     }
0092 
0093 public:
0094     void sendString(const QByteArray &string) override
0095     {
0096         lastSent = string;
0097         Vt102Emulation::sendString(string);
0098     }
0099 
0100     void reportDecodingError(int /*token*/) override
0101     {
0102         items.push_back(DecodingError{});
0103     }
0104 
0105     void processToken(int code, int p, int q) override
0106     {
0107         items.push_back(ProcessToken{code, p, q});
0108         if (!blockFurtherProcessing) {
0109             Vt102Emulation::processToken(code, p, q);
0110         }
0111     }
0112 
0113     void processSessionAttributeRequest(const int tokenSize, const uint terminator) override
0114     {
0115         items.push_back(ProcessSessionAttributeRequest{std::vector<uint>(tokenBuffer, tokenBuffer + tokenSize)});
0116         if (!blockFurtherProcessing) {
0117             Vt102Emulation::processSessionAttributeRequest(tokenSize, terminator);
0118         }
0119     }
0120 
0121     void processChecksumRequest(int argc, int argv[]) override
0122     {
0123         ProcessChecksumRequest item;
0124         for (int i = 0; i < argc; i++) {
0125             item.args.push_back(argv[i]);
0126         }
0127         items.push_back(item);
0128         if (!blockFurtherProcessing) {
0129             Vt102Emulation::processChecksumRequest(argc, argv);
0130         }
0131     }
0132 };
0133 
0134 }
0135 
0136 #endif // VT102EMULATIONTEST_H