New Xboard unit tests in Cute Chess

In my previous Cute Chess 2021 hackathon post I mentioned how on the final days of the hackathon I started making Cute Chess Qt 6 clean. For the past couple of weeks I’ve been working towards that goal.

In order to make Cute Chess Qt 6 clean, we need to get rid of the compatibility layers that we’re currently using. That requires rewriting code including the Xboard and UCI protocol parsing. These two parser rely heavily on QStringRef, a read-only subset of QString API. In Qt 6 QStringRef is deprecated in favor of QStringView.

Replacing QStringRef with QStringView is not a simple search-and-replace operation because the two classes aim to do different things: QStringView is designed to be an interface type whereas QStringRef was intended to be used in situation where every bit of performance matters. Our current parser code also operates on the internal QString pointer of QStringRef which to me looks a bit error prone.

Rewriting the protocol parsing to use QStringView would be simpler if we had unit tests — which we don’t. Cute Chess has extensive unit tests for the chess game itself but not for protocol parsing.

I started by writing unit tests for the XboardEngine class which is responsible for parsing the Xboard protocol. The design of the class was not (and is still not) very testable. There are dependencies between different internal methods of the class and business logic is interleaved with protocol parsing. I wanted to keep the changes to a minimum before tests are in place, so I extracted routines responsible for protocol parsing to their own methods.

I derived the unit test class from the XboardEngine class so that protected methods could be tested. There were some private signals that had to be changed to protected so that they wouldn’t conflict with the unit testing framework.

The initial tests covered parsing of engine features and principal valuation. Finally, I added tests to cover parsing of options. None of the changes I introduced require any Qt 6 features.

The next steps are to extend the tests to cover parsing of ChessEngine methods and protocol parsing tests for the UciEngine class.