I am trying to write unit tests for some string-formatting code. Formatted output is likely in some cases to contain bidirectional text, i.e. a mixture of both left-to-right and right-to-left.
I have verified empirically that the output looks correct, for all combinations of LTR and RTL input and output, when run on an Android device or emulator. I am struggling to capture this in unit tests, though; I'm not sure how I can correctly specify, in my test cases, the expected output.
For example, I want to assert that the returned string will be rendered like this:
-د.ك.123,456.78
That is, the glyphs should appear in this order from left to right:
-
1
2
3
,
4
5
6
.
7
8
.
ك
.
د
(You have no idea how hard it was even editing that into the right sequence in the SO edit box!)
I have tried using standard string comparison methods in my test cases, like this:
assertEquals("-د.ك.123,456.78", formattedOutput);
but that fails because the expected-output string in the test code is having its text reordered. In fact it even appears differently in the source depending on which tool I use to view the source (Android Studio vs. Github-with-Chrome), so I have no confidence that it's testing the right thing.
I have also tried building up the expected-output value in steps, to avoid confusion in the editor, although it results in the same string literal being constructed under the hood:
assertEquals("-123,456.78" + SYMBOL, formattedOutput);
How can I compare the visual order of the glyphs, rather than the logical order? Will Android's BidiFormatter
help me here?