Skip to content

Commit

Permalink
Add some unit tests for the C1 parsing mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
j4james committed Nov 6, 2021
1 parent e17edc5 commit 5bd12dd
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
42 changes: 40 additions & 2 deletions src/terminal/adapter/ut_adapter/adapterTest.cpp
Expand Up @@ -394,10 +394,14 @@ class TestGetSet final : public ConGetSet
return FALSE;
}

bool SetConsoleOutputCP(const unsigned int /*codepage*/) override
bool SetConsoleOutputCP(const unsigned int codepage) override
{
Log::Comment(L"SetConsoleOutputCP MOCK called...");
return TRUE;
if (_setConsoleOutputCPResult)
{
VERIFY_ARE_EQUAL(_expectedOutputCP, codepage);
}
return _setConsoleOutputCPResult;
}

bool GetConsoleOutputCP(unsigned int& codepage) override
Expand Down Expand Up @@ -737,6 +741,7 @@ class TestGetSet final : public ConGetSet
CursorType _expectedCursorStyle;
bool _setCursorColorResult = false;
COLORREF _expectedCursorColor = 0;
bool _setConsoleOutputCPResult = false;
bool _getConsoleOutputCPResult = false;
bool _moveToBottomResult = false;

Expand Down Expand Up @@ -2634,6 +2639,39 @@ class AdapterTest
VERIFY_IS_TRUE(decdld(CellMatrix::Default, 0, FontSet::Size132x24, FontUsage::FullCell, bitmapOf6x18));
}

TEST_METHOD(TogglingC1ParserMode)
{
Log::Comment(L"1. Accept C1 controls");
_testGetSet->_setParserModeResult = true;
_testGetSet->_expectedParserMode = StateMachine::Mode::AcceptC1;
_testGetSet->_expectedParserModeEnabled = true;
VERIFY_IS_TRUE(_pDispatch.get()->AcceptC1Controls(true));

Log::Comment(L"2. Don't accept C1 controls");
_testGetSet->_setParserModeResult = true;
_testGetSet->_expectedParserMode = StateMachine::Mode::AcceptC1;
_testGetSet->_expectedParserModeEnabled = false;
VERIFY_IS_TRUE(_pDispatch.get()->AcceptC1Controls(false));

Log::Comment(L"3. Designate ISO-2022 coding system");
// Code page should be set to ISO-8859-1 and C1 parsing enabled
_testGetSet->_setConsoleOutputCPResult = true;
_testGetSet->_expectedOutputCP = 28591;
_testGetSet->_setParserModeResult = true;
_testGetSet->_expectedParserMode = StateMachine::Mode::AcceptC1;
_testGetSet->_expectedParserModeEnabled = true;
VERIFY_IS_TRUE(_pDispatch.get()->DesignateCodingSystem(DispatchTypes::CodingSystem::ISO2022));

Log::Comment(L"4. Designate UTF-8 coding system");
// Code page should be set to UTF-8 and C1 parsing disabled
_testGetSet->_setConsoleOutputCPResult = true;
_testGetSet->_expectedOutputCP = CP_UTF8;
_testGetSet->_setParserModeResult = true;
_testGetSet->_expectedParserMode = StateMachine::Mode::AcceptC1;
_testGetSet->_expectedParserModeEnabled = false;
VERIFY_IS_TRUE(_pDispatch.get()->DesignateCodingSystem(DispatchTypes::CodingSystem::UTF8));
}

private:
TestGetSet* _testGetSet; // non-ownership pointer
std::unique_ptr<AdaptDispatch> _pDispatch;
Expand Down
58 changes: 56 additions & 2 deletions src/terminal/parser/ut_parser/OutputEngineTest.cpp
Expand Up @@ -990,12 +990,14 @@ class StatefulDispatch final : public TermDispatch
{
}

virtual void Print(const wchar_t /*wchPrintable*/) override
virtual void Print(const wchar_t wchPrintable) override
{
_printString += wchPrintable;
}

virtual void PrintString(const std::wstring_view /*string*/) override
virtual void PrintString(const std::wstring_view string) override
{
_printString += string;
}

StatefulDispatch() :
Expand Down Expand Up @@ -1483,6 +1485,7 @@ class StatefulDispatch final : public TermDispatch
return true;
}

std::wstring _printString;
size_t _cursorDistance;
size_t _line;
size_t _column;
Expand Down Expand Up @@ -3382,4 +3385,55 @@ class StateMachineExternalTest final

pDispatch->ClearState();
}

TEST_METHOD(TestC1ParserMode)
{
auto dispatch = std::make_unique<StatefulDispatch>();
auto pDispatch = dispatch.get();
auto engine = std::make_unique<OutputStateMachineEngine>(std::move(dispatch));
StateMachine mach(std::move(engine));

Log::Comment(L"C1 parsing disabled: CSI control ignored and rest of sequence printed");
mach.SetParserMode(StateMachine::Mode::AcceptC1, false);
mach.ProcessString(L"\u009b"
L"123A");
VERIFY_IS_FALSE(pDispatch->_cursorUp);
VERIFY_ARE_EQUAL(pDispatch->_printString, L"123A");

pDispatch->ClearState();

Log::Comment(L"C1 parsing enabled: CSI interpreted and CUP sequence executed");
mach.SetParserMode(StateMachine::Mode::AcceptC1, true);
mach.ProcessString(L"\u009b"
L"123A");
VERIFY_IS_TRUE(pDispatch->_cursorUp);
VERIFY_ARE_EQUAL(pDispatch->_cursorDistance, 123u);

pDispatch->ClearState();

Log::Comment(L"C1 parsing disabled: NEL has no effect within a sequence");
mach.SetParserMode(StateMachine::Mode::AcceptC1, false);
mach.ProcessString(L"\x1b[12"
L"\u0085"
L";34H");
VERIFY_IS_FALSE(pDispatch->_lineFeed);
VERIFY_IS_TRUE(pDispatch->_cursorPosition);
VERIFY_ARE_EQUAL(pDispatch->_line, 12u);
VERIFY_ARE_EQUAL(pDispatch->_column, 34u);
VERIFY_ARE_EQUAL(pDispatch->_printString, L"");

pDispatch->ClearState();

Log::Comment(L"C1 parsing enabled: NEL aborts sequence and executes line feed");
mach.SetParserMode(StateMachine::Mode::AcceptC1, true);
mach.ProcessString(L"\x1b[12"
L"\u0085"
L";34H");
VERIFY_IS_TRUE(pDispatch->_lineFeed);
VERIFY_ARE_EQUAL(DispatchTypes::LineFeedType::WithReturn, pDispatch->_lineFeedType);
VERIFY_IS_FALSE(pDispatch->_cursorPosition);
VERIFY_ARE_EQUAL(pDispatch->_printString, L";34H");

pDispatch->ClearState();
}
};

0 comments on commit 5bd12dd

Please sign in to comment.