mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 16:59:35 +01:00
Adjust code and fix all unit tests errors.
This commit is contained in:
@@ -34,55 +34,56 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal (1, a.ColumnWidth ());
|
||||
Assert.Equal ("a", a.ToString ());
|
||||
Assert.Equal (1, a.ToString ().Length);
|
||||
Assert.Equal (1, a.RuneLen ());
|
||||
Assert.Equal (1, a.Utf8SequenceLength);
|
||||
Assert.Equal (1, b.ColumnWidth ());
|
||||
Assert.Equal ("b", b.ToString ());
|
||||
Assert.Equal (1, b.ToString ().Length);
|
||||
Assert.Equal (1, b.RuneLen ());
|
||||
Assert.Equal (1, b.Utf8SequenceLength);
|
||||
var rl = a < b;
|
||||
Assert.True (rl);
|
||||
Assert.Equal (1, c.ColumnWidth ());
|
||||
Assert.Equal ("{", c.ToString ());
|
||||
Assert.Equal (1, c.ToString ().Length);
|
||||
Assert.Equal (1, c.RuneLen ());
|
||||
Assert.Equal (1, c.Utf8SequenceLength);
|
||||
Assert.Equal (2, d.ColumnWidth ());
|
||||
Assert.Equal ("ᅐ", d.ToString ());
|
||||
Assert.Equal (1, d.ToString ().Length);
|
||||
Assert.Equal (3, d.RuneLen ());
|
||||
Assert.Equal (3, d.Utf8SequenceLength);
|
||||
Assert.Equal (0, e.ColumnWidth ());
|
||||
string join = "\u1104\u1161";
|
||||
Assert.Equal ("따", join);
|
||||
Assert.Equal (2, join.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, join.EnumerateRunes().Sum (x => x.ColumnWidth ()));
|
||||
Assert.Equal (OperationStatus.Done, Rune.DecodeFromUtf16 (join.ToCharArray (), out Rune result, out int charsConsumed));
|
||||
Assert.False (join.ToRunes () [0].DecodeSurrogatePair (out char [] spair));
|
||||
Assert.False (join.DecodeSurrogatePair (out char [] spair));
|
||||
Assert.Equal (2, join.RuneCount ());
|
||||
Assert.Equal (2, join.Length);
|
||||
Assert.Equal ("ᅡ", e.ToString ());
|
||||
Assert.Equal (1, e.ToString ().Length);
|
||||
Assert.Equal (3, e.RuneLen ());
|
||||
Assert.Equal (3, e.Utf8SequenceLength);
|
||||
string joinNormalize = join.Normalize ();
|
||||
Assert.Equal ("따", joinNormalize);
|
||||
Assert.Equal (2, joinNormalize.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, joinNormalize.EnumerateRunes().Sum (x => x.ColumnWidth ()));
|
||||
Assert.Equal (OperationStatus.Done, Rune.DecodeFromUtf16 (joinNormalize.ToCharArray (), out result, out charsConsumed));
|
||||
Assert.False (joinNormalize.ToRunes () [0].DecodeSurrogatePair (out spair));
|
||||
Assert.False (joinNormalize.DecodeSurrogatePair (out spair));
|
||||
Assert.Equal (1, joinNormalize.RuneCount ());
|
||||
Assert.Equal (1, joinNormalize.Length);
|
||||
Assert.Equal (-1, f.ColumnWidth ());
|
||||
Assert.Equal (1, f.ToString ().Length);
|
||||
Assert.Equal (1, f.RuneLen ());
|
||||
Assert.Equal (1, f.Utf8SequenceLength);
|
||||
Assert.Equal (-1, g.ColumnWidth ());
|
||||
Assert.Equal (1, g.ToString ().Length);
|
||||
Assert.Equal (1, g.RuneLen ());
|
||||
Assert.Equal (1, g.Utf8SequenceLength);
|
||||
var uh = h;
|
||||
(var runeh, var sizeh) = uh.DecodeRune ();
|
||||
Assert.Equal (2, runeh.ColumnWidth ());
|
||||
Assert.Equal (1, runeh.ColumnWidth ());
|
||||
Assert.Equal ("🨁", h);
|
||||
Assert.Equal (2, runeh.ToString ().Length);
|
||||
Assert.Equal (4, runeh.RuneLen ());
|
||||
Assert.Equal (sizeh, runeh.RuneLen ());
|
||||
Assert.Equal (4, runeh.Utf8SequenceLength);
|
||||
Assert.Equal (sizeh, runeh.Utf8SequenceLength);
|
||||
for (int x = 0; x < uh.Length - 1; x++) {
|
||||
Assert.Equal (0, char.ConvertToUtf32 (uh [x], uh [x + 1]));
|
||||
Assert.False (RuneExtensions.EncodeSurrogatePair (uh [x], uh [x + 1], out _));
|
||||
Assert.Equal (0x1FA01, char.ConvertToUtf32 (uh [x], uh [x + 1]));
|
||||
Assert.True (RuneExtensions.EncodeSurrogatePair (uh [x], uh [x + 1], out result));
|
||||
Assert.Equal (0x1FA01, result.Value);
|
||||
}
|
||||
Assert.True (Rune.IsValid (runeh.Value));
|
||||
Assert.True (RuneExtensions.IsValid (uh.ToByteArray ()));
|
||||
@@ -90,37 +91,38 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal (1, uh.RuneCount ());
|
||||
(var runelh, var sizelh) = uh.DecodeLastRune ();
|
||||
|
||||
Assert.Equal (2, runelh.ColumnWidth ());
|
||||
Assert.Equal (1, runelh.ColumnWidth ());
|
||||
Assert.Equal (2, runelh.ToString ().Length);
|
||||
Assert.Equal (4, runelh.RuneLen ());
|
||||
Assert.Equal (sizelh, runelh.RuneLen ());
|
||||
Assert.Equal (4, runelh.Utf8SequenceLength);
|
||||
Assert.Equal (sizelh, runelh.Utf8SequenceLength);
|
||||
Assert.True (Rune.IsValid (runelh.Value));
|
||||
|
||||
var ui = i;
|
||||
(var runei, var sizei) = ui.DecodeRune ();
|
||||
Assert.Equal (2, runei.ColumnWidth ());
|
||||
Assert.Equal (1, runei.ColumnWidth ());
|
||||
Assert.Equal ("", i);
|
||||
Assert.Equal (2, runei.ToString ().Length);
|
||||
Assert.Equal (4, runei.RuneLen ());
|
||||
Assert.Equal (sizei, runei.RuneLen ());
|
||||
Assert.Equal (4, runei.Utf8SequenceLength);
|
||||
Assert.Equal (sizei, runei.Utf8SequenceLength);
|
||||
for (int x = 0; x < ui.Length - 1; x++) {
|
||||
Assert.Equal (0, char.ConvertToUtf32 (uh [x], uh [x + 1]));
|
||||
Assert.False (RuneExtensions.EncodeSurrogatePair (ui [x], ui [x + 1], out _));
|
||||
Assert.Equal (0xE0FE1, char.ConvertToUtf32 (ui [x], ui [x + 1]));
|
||||
Assert.True (RuneExtensions.EncodeSurrogatePair (ui [x], ui [x + 1], out result));
|
||||
Assert.Equal (0xE0FE1, result.Value);
|
||||
}
|
||||
Assert.True (Rune.IsValid (runei.Value));
|
||||
Assert.True (RuneExtensions.IsValid (ui.ToByteArray ()));
|
||||
//Assert.True (Rune.FullRune (ui.ToByteArray ()));
|
||||
(var runeli, var sizeli) = ui.DecodeLastRune ();
|
||||
Assert.Equal (2, runeli.ColumnWidth ());
|
||||
Assert.Equal (1, runeli.ColumnWidth ());
|
||||
Assert.Equal (2, runeli.ToString ().Length);
|
||||
Assert.Equal (4, runeli.RuneLen ());
|
||||
Assert.Equal (sizeli, runeli.RuneLen ());
|
||||
Assert.Equal (4, runeli.Utf8SequenceLength);
|
||||
Assert.Equal (sizeli, runeli.Utf8SequenceLength);
|
||||
Assert.True (Rune.IsValid (runeli.Value));
|
||||
|
||||
Assert.Equal (runeh.ColumnWidth (), runei.ColumnWidth ());
|
||||
Assert.NotEqual (h, i);
|
||||
Assert.Equal (runeh.ToString ().Length, runei.ToString ().Length);
|
||||
Assert.Equal (runeh.RuneLen (), runei.RuneLen ());
|
||||
Assert.Equal (runeh.Utf8SequenceLength, runei.Utf8SequenceLength);
|
||||
var uj = j.ToString ();
|
||||
(var runej, var sizej) = uj.DecodeRune ();
|
||||
Assert.Equal (0, j.ColumnWidth ());
|
||||
@@ -130,51 +132,51 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal ("⃐", uj);
|
||||
Assert.Equal (1, j.ToString ().Length);
|
||||
Assert.Equal (1, runej.ToString ().Length);
|
||||
Assert.Equal (3, j.RuneLen ());
|
||||
Assert.Equal (sizej, runej.RuneLen ());
|
||||
Assert.Equal (3, j.Utf8SequenceLength);
|
||||
Assert.Equal (sizej, runej.Utf8SequenceLength);
|
||||
Assert.Equal (1, k.ColumnWidth ());
|
||||
Assert.Equal ("■", k.ToString ());
|
||||
Assert.Equal (1, k.ToString ().Length);
|
||||
Assert.Equal (3, k.RuneLen ());
|
||||
Assert.Equal (3, k.Utf8SequenceLength);
|
||||
Assert.Equal (1, l.ColumnWidth ());
|
||||
Assert.Equal ("□", l.ToString ());
|
||||
Assert.Equal (1, l.ToString ().Length);
|
||||
Assert.Equal (3, l.RuneLen ());
|
||||
Assert.Equal (3, l.Utf8SequenceLength);
|
||||
Assert.Equal (1, m.ColumnWidth ());
|
||||
Assert.Equal ("", m.ToString ());
|
||||
Assert.Equal (1, m.ToString ().Length);
|
||||
Assert.Equal (3, m.RuneLen ());
|
||||
var rn = StringExtensions.Make (n).DecodeRune ().rune;
|
||||
Assert.Equal (3, m.Utf8SequenceLength);
|
||||
var rn = StringExtensions.Make (n).DecodeRune ().Rune;
|
||||
Assert.Equal (2, rn.ColumnWidth ());
|
||||
Assert.Equal ("🍕", rn.ToString ());
|
||||
Assert.Equal (2, rn.ToString ().Length);
|
||||
Assert.Equal (4, rn.RuneLen ());
|
||||
Assert.Equal (4, rn.Utf8SequenceLength);
|
||||
Assert.Equal (2, o.ColumnWidth ());
|
||||
Assert.Equal ("🍕", o.ToString ());
|
||||
Assert.Equal (2, o.ToString ().Length);
|
||||
Assert.Equal (4, o.RuneLen ());
|
||||
var rp = p.DecodeRune ().rune;
|
||||
Assert.Equal (4, o.Utf8SequenceLength);
|
||||
var rp = p.DecodeRune ().Rune;
|
||||
Assert.Equal (2, rp.ColumnWidth ());
|
||||
Assert.Equal ("🍕", p);
|
||||
Assert.Equal (2, p.Length);
|
||||
Assert.Equal (4, rp.RuneLen ());
|
||||
Assert.Equal (4, rp.Utf8SequenceLength);
|
||||
Assert.Equal (1, q.ColumnWidth ());
|
||||
Assert.Equal ("℃", q.ToString ());
|
||||
Assert.Equal (1, q.ToString ().Length);
|
||||
Assert.Equal (3, q.RuneLen ());
|
||||
var rq = q.ToString ().DecodeRune ().rune;
|
||||
Assert.Equal (3, q.Utf8SequenceLength);
|
||||
var rq = q.ToString ().DecodeRune ().Rune;
|
||||
Assert.Equal (1, rq.ColumnWidth ());
|
||||
Assert.Equal ("℃", rq.ToString ());
|
||||
Assert.Equal (1, rq.ToString ().Length);
|
||||
Assert.Equal (3, rq.RuneLen ());
|
||||
Assert.Equal (3, rq.Utf8SequenceLength);
|
||||
Assert.Equal (2, r.ColumnWidth ());
|
||||
Assert.Equal ("ᄀ", r.ToString ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (3, r.RuneLen ());
|
||||
Assert.Equal (3, r.Utf8SequenceLength);
|
||||
Assert.Equal (1, s.ColumnWidth ());
|
||||
Assert.Equal ("━", s.ToString ());
|
||||
Assert.Equal (1, s.ToString ().Length);
|
||||
Assert.Equal (3, s.RuneLen ());
|
||||
Assert.Equal (3, s.Utf8SequenceLength);
|
||||
var buff = new byte [4];
|
||||
var sb = ((Rune)'\u2503').EncodeRune (buff);
|
||||
Assert.Equal (1, ((Rune)'\u2503').ColumnWidth ());
|
||||
@@ -194,49 +196,49 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal ('\u1100', (uint)rune.Value);
|
||||
string str = "\u2615";
|
||||
Assert.Equal ("☕", str);
|
||||
Assert.Equal (2, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, str.EnumerateRunes().Sum(x => x.ColumnWidth()));
|
||||
Assert.Equal (2, str.ConsoleWidth ());
|
||||
Assert.Equal (1, str.RuneCount ());
|
||||
Assert.Equal (1, str.Length);
|
||||
str = "\u2615\ufe0f"; // Identical but \ufe0f forces it to be rendered as a colorful image as compared to a monochrome text variant.
|
||||
Assert.Equal ("☕️", str);
|
||||
Assert.Equal (2, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, str.EnumerateRunes().Sum(x => x.ColumnWidth()));
|
||||
Assert.Equal (2, str.ConsoleWidth ());
|
||||
Assert.Equal (2, str.RuneCount ());
|
||||
Assert.Equal (2, str.Length);
|
||||
str = "\u231a";
|
||||
Assert.Equal ("⌚", str);
|
||||
Assert.Equal (2, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, str.EnumerateRunes().Sum(x => x.ColumnWidth()));
|
||||
Assert.Equal (2, str.ConsoleWidth ());
|
||||
Assert.Equal (1, str.RuneCount ());
|
||||
Assert.Equal (1, str.Length);
|
||||
str = "\u231b";
|
||||
Assert.Equal ("⌛", str);
|
||||
Assert.Equal (2, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, str.EnumerateRunes().Sum(x => x.ColumnWidth()));
|
||||
Assert.Equal (2, str.ConsoleWidth ());
|
||||
Assert.Equal (1, str.RuneCount ());
|
||||
Assert.Equal (1, str.Length);
|
||||
str = "\u231c";
|
||||
Assert.Equal ("⌜", str);
|
||||
Assert.Equal (1, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (1, str.EnumerateRunes().Sum(x => x.ColumnWidth()));
|
||||
Assert.Equal (1, str.ConsoleWidth ());
|
||||
Assert.Equal (1, str.RuneCount ());
|
||||
Assert.Equal (1, str.Length);
|
||||
str = "\u1dc0";
|
||||
Assert.Equal ("᷀", str);
|
||||
Assert.Equal (0, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (0, str.EnumerateRunes().Sum(x => x.ColumnWidth()));
|
||||
Assert.Equal (0, str.ConsoleWidth ());
|
||||
Assert.Equal (1, str.RuneCount ());
|
||||
Assert.Equal (1, str.Length);
|
||||
str = "\ud83e\udd16";
|
||||
Assert.Equal ("🤖", str);
|
||||
Assert.Equal (2, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, str.EnumerateRunes ().Sum (x => x.ColumnWidth ()));
|
||||
Assert.Equal (2, str.ConsoleWidth ());
|
||||
Assert.Equal (1, str.RuneCount ()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
|
||||
Assert.Equal (2, str.Length); // String always preserves the originals values of each surrogate pair
|
||||
str = "\U0001f9e0";
|
||||
Assert.Equal ("🧠", str);
|
||||
Assert.Equal (2, str.Sum (x => ((Rune)x).ColumnWidth ()));
|
||||
Assert.Equal (2, str.EnumerateRunes ().Sum (x => x.ColumnWidth ()));
|
||||
Assert.Equal (2, str.ConsoleWidth ());
|
||||
Assert.Equal (1, str.RuneCount ());
|
||||
Assert.Equal (2, str.Length);
|
||||
@@ -258,16 +260,16 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal (1, c.ToString ().Length);
|
||||
Assert.Equal ("a", c.ToString ());
|
||||
Rune d = new Rune (0x10421);
|
||||
Assert.Equal (2, d.ColumnWidth ());
|
||||
Assert.Equal (1, d.ColumnWidth ()); // Many surrogate pairs only occupies 1 column
|
||||
Assert.Equal (2, d.ToString ().Length);
|
||||
Assert.Equal ("𐐡", d.ToString ());
|
||||
Assert.False (RuneExtensions.EncodeSurrogatePair ('\ud799', '\udc21', out Rune rune));
|
||||
Assert.Throws<ArgumentOutOfRangeException> (() => new Rune ('\ud799', '\udc21'));
|
||||
Rune e = new Rune ('\ud801', '\udc21');
|
||||
Assert.Equal (2, e.ColumnWidth ());
|
||||
Assert.Equal (1, e.ColumnWidth ());
|
||||
Assert.Equal (2, e.ToString ().Length);
|
||||
Assert.Equal ("𐐡", e.ToString ());
|
||||
Assert.False (Rune.IsValid (new Rune ('\ud801').Value));
|
||||
Assert.Throws<ArgumentOutOfRangeException> (() => Assert.False (Rune.IsValid (new Rune ('\ud801').Value)));
|
||||
Rune f = new Rune ('\ud83c', '\udf39');
|
||||
Assert.Equal (2, f.ColumnWidth ());
|
||||
Assert.Equal (2, f.ToString ().Length);
|
||||
@@ -276,8 +278,8 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Null (exception);
|
||||
Rune g = new Rune (0x10ffff);
|
||||
string s = "\U0010ffff";
|
||||
Assert.Equal (2, g.ColumnWidth ());
|
||||
Assert.Equal (2, s.ConsoleWidth ());
|
||||
Assert.Equal (1, g.ColumnWidth ());
|
||||
Assert.Equal (1, s.ConsoleWidth ());
|
||||
Assert.Equal (2, g.ToString ().Length);
|
||||
Assert.Equal (2, s.Length);
|
||||
Assert.Equal ("", g.ToString ());
|
||||
@@ -301,11 +303,11 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal (2, k.ToString ().Length);
|
||||
Assert.Equal ("🐂", k.ToString ());
|
||||
var l = new Rune ('\ud801', '\udcbb');
|
||||
Assert.Equal (2, l.ColumnWidth ());
|
||||
Assert.Equal (1, l.ColumnWidth ());
|
||||
Assert.Equal (2, l.ToString ().Length);
|
||||
Assert.Equal ("𐒻", l.ToString ());
|
||||
var m = new Rune ('\ud801', '\udccf');
|
||||
Assert.Equal (2, m.ColumnWidth ());
|
||||
Assert.Equal (1, m.ColumnWidth ());
|
||||
Assert.Equal (2, m.ToString ().Length);
|
||||
Assert.Equal ("𐓏", m.ToString ());
|
||||
var n = new Rune ('\u00e1');
|
||||
@@ -324,26 +326,27 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal (2, q.ColumnWidth ());
|
||||
Assert.Equal (1, q.ToString ().Length);
|
||||
Assert.Equal ("〉", q.ToString ());
|
||||
var r = "\U0000232a".DecodeRune ().rune;
|
||||
var r = "\U0000232a".DecodeRune ().Rune;
|
||||
Assert.Equal (2, r.ColumnWidth ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal ("〉", r.ToString ());
|
||||
|
||||
PrintTextElementCount ('\u00e1'.ToString (), "á", 1, 1, 1, 1);
|
||||
PrintTextElementCount (new string ('\u0061', '\u0301'), "á", 1, 2, 2, 1);
|
||||
PrintTextElementCount (new string ('\u0065', '\u0301'), "é", 1, 2, 2, 1);
|
||||
PrintTextElementCount (new string (new char [] { '\u0061', '\u0301' }), "á", 1, 2, 2, 1);
|
||||
PrintTextElementCount (StringExtensions.Make ('\u0061', '\u0301'), "á", 1, 2, 2, 1);
|
||||
PrintTextElementCount (StringExtensions.Make ('\u0065', '\u0301'), "é", 1, 2, 2, 1);
|
||||
PrintTextElementCount (StringExtensions.Make (new Rune [] { new Rune (0x1f469), new Rune (0x1f3fd), new Rune ('\u200d'), new Rune (0x1f692) }),
|
||||
"👩🏽🚒", 6, 4, 7, 1);
|
||||
PrintTextElementCount (StringExtensions.Make (new Rune [] { new Rune (0x1f469), new Rune (0x1f3fd), new Rune ('\u200d'), new Rune (0x1f692) }),
|
||||
"\U0001f469\U0001f3fd\u200d\U0001f692", 6, 4, 7, 1);
|
||||
PrintTextElementCount (StringExtensions.Make (new Rune ('\ud801', '\udccf')),
|
||||
"\ud801\udccf", 2, 1, 2, 1);
|
||||
"𐓏", 1, 1, 2, 1);
|
||||
}
|
||||
|
||||
void PrintTextElementCount (string us, string s, int consoleWidth, int runeCount, int stringCount, int txtElementCount)
|
||||
{
|
||||
Assert.NotEqual (us.Length, s.Length);
|
||||
Assert.Equal (us.ToString (), s);
|
||||
Assert.Equal (us.Length, s.Length);
|
||||
Assert.Equal (us, s);
|
||||
Assert.Equal (consoleWidth, us.ConsoleWidth ());
|
||||
Assert.Equal (runeCount, us.RuneCount ());
|
||||
Assert.Equal (stringCount, s.Length);
|
||||
@@ -368,7 +371,7 @@ namespace Terminal.Gui.TextTests {
|
||||
int CountLettersInString (string s)
|
||||
{
|
||||
int letterCount = 0;
|
||||
foreach (Rune rune in s) {
|
||||
foreach (Rune rune in s.EnumerateRunes ()) {
|
||||
if (Rune.IsLetter (rune)) { letterCount++; }
|
||||
}
|
||||
|
||||
@@ -387,27 +390,37 @@ namespace Terminal.Gui.TextTests {
|
||||
|
||||
bool ProcessTestStringUseChar (string s)
|
||||
{
|
||||
char surrogateChar = default;
|
||||
for (int i = 0; i < s.Length; i++) {
|
||||
Rune r = new Rune (s [i]);
|
||||
if (!char.IsSurrogate (s [i])) {
|
||||
Rune r;
|
||||
if (char.IsSurrogate (s [i])) {
|
||||
if (surrogateChar != default && char.IsSurrogate (surrogateChar)) {
|
||||
r = new Rune (surrogateChar, s [i]);
|
||||
Assert.True (r.IsSurrogatePair ());
|
||||
int codePoint = char.ConvertToUtf32 (surrogateChar, s [i]);
|
||||
RuneExtensions.EncodeSurrogatePair (surrogateChar, s [i], out Rune rune);
|
||||
Assert.Equal (codePoint, rune.Value);
|
||||
string sp = new string (new char [] { surrogateChar, s [i] });
|
||||
r = (Rune)codePoint;
|
||||
Assert.Equal (sp, r.ToString ());
|
||||
Assert.True (r.IsSurrogatePair ());
|
||||
|
||||
surrogateChar = default;
|
||||
} else if (i < s.Length - 1) {
|
||||
surrogateChar = s [i];
|
||||
continue;
|
||||
} else {
|
||||
Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (s [i]));
|
||||
throw new Exception ("String was not well-formed UTF-16.");
|
||||
}
|
||||
} else {
|
||||
r = new Rune (s [i]);
|
||||
var buff = new byte [4];
|
||||
((Rune)s [i]).EncodeRune (buff);
|
||||
Assert.Equal ((int)s [i], buff [0]);
|
||||
Assert.Equal (s [i], r.Value);
|
||||
Assert.True (Rune.IsValid (r.Value));
|
||||
Assert.False (r.IsSurrogatePair ());
|
||||
} else if (i + 1 < s.Length && char.IsSurrogatePair (s [i], s [i + 1])) {
|
||||
int codePoint = char.ConvertToUtf32 (s [i], s [i + 1]);
|
||||
RuneExtensions.EncodeSurrogatePair (s [i], s [i + 1], out Rune rune);
|
||||
Assert.Equal (codePoint, rune.Value);
|
||||
string sp = new string (new char [] { s [i], s [i + 1] });
|
||||
r = (Rune)codePoint;
|
||||
Assert.Equal (sp, r.ToString ());
|
||||
Assert.True (r.IsSurrogatePair ());
|
||||
i++; // Increment the iterator by the number of surrogate pair
|
||||
} else {
|
||||
Assert.False (Rune.IsValid (r.Value));
|
||||
throw new Exception ("String was not well-formed UTF-16.");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -422,14 +435,16 @@ namespace Terminal.Gui.TextTests {
|
||||
int colWidth = 0;
|
||||
|
||||
for (int i = 0; i < s.Length; i++) {
|
||||
Rune rune = new Rune (s [i]);
|
||||
if (Rune.IsValid (rune.Value)) {
|
||||
Rune rune = default;
|
||||
if (Rune.IsValid (s [i])) {
|
||||
rune = new Rune (s [i]);
|
||||
Assert.True (Rune.IsValid (rune.Value));
|
||||
runes.Add (rune);
|
||||
Assert.Equal (s [i], rune.Value);
|
||||
Assert.False (rune.IsSurrogatePair ());
|
||||
} else if (i + 1 < s.Length && (RuneExtensions.EncodeSurrogatePair (s [i], s [i + 1], out codePoint))) {
|
||||
Assert.False (Rune.IsValid (rune.Value));
|
||||
Assert.Equal (0, rune.Value);
|
||||
Assert.False (Rune.IsValid (s [i]));
|
||||
rune = codePoint;
|
||||
runes.Add (rune);
|
||||
string sp = new string (new char [] { s [i], s [i + 1] });
|
||||
@@ -437,7 +452,7 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.True (codePoint.IsSurrogatePair ());
|
||||
i++; // Increment the iterator by the number of surrogate pair
|
||||
} else {
|
||||
Assert.False (Rune.IsValid (rune.Value));
|
||||
Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (s [i]));
|
||||
throw new Exception ("String was not well-formed UTF-16.");
|
||||
}
|
||||
colWidth += rune.ColumnWidth (); // Increment the column width of this Rune
|
||||
@@ -466,7 +481,7 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.True (Rune.IsValid (new Rune ('\ud83c', '\udf39').Value));
|
||||
Assert.False (Rune.IsValid ('\ud801'));
|
||||
Assert.False (Rune.IsValid ((uint)'\ud801'));
|
||||
Assert.False (Rune.IsValid (((Rune)'\ud801').Value));
|
||||
Assert.Throws<ArgumentOutOfRangeException> (() => Assert.False (Rune.IsValid (((Rune)'\ud801').Value)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -477,15 +492,18 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal (4, rune1.EncodeRune (buff1));
|
||||
Assert.True (RuneExtensions.IsValid (buff1));
|
||||
Assert.Equal (2, rune1.ToString ().Length);
|
||||
Assert.Equal (4, rune1.RuneLen ());
|
||||
var rune2 = (Rune)'\ud801'; // To avoid throwing an exception set as uint instead a Rune instance.
|
||||
var buff2 = new byte [4];
|
||||
Assert.Equal (3, rune2.EncodeRune (buff2));
|
||||
Assert.False (RuneExtensions.IsValid (buff2)); // To avoid throwing an exception pass as uint parameter instead Rune.
|
||||
Assert.Equal (5, rune2.ToString ().Length); // Invalid string. It returns the decimal 55297 representation of the 0xd801 hexadecimal.
|
||||
Assert.Equal (-1, rune2.RuneLen ());
|
||||
Assert.Equal (3, new Rune ('\ud801').EncodeRune (buff2)); // error
|
||||
Assert.Equal (new byte [] { 0xef, 0x3f, 0x3d, 0 }, buff2); // error
|
||||
Assert.Equal (4, rune1.Utf8SequenceLength);
|
||||
var c = '\ud801';
|
||||
var buff2 = Encoding.UTF8.GetBytes (new char [] { c });
|
||||
//var buff2 = new byte [4];
|
||||
Assert.Equal (3, buff2.Length);
|
||||
var str = Encoding.UTF8.GetString (buff2);
|
||||
Assert.Equal ("<22>", str);
|
||||
Assert.False (RuneExtensions.IsValid (buff2));
|
||||
Assert.Equal (1, str.Length);
|
||||
Assert.Throws<ArgumentOutOfRangeException> (() => (Rune)'\ud801');
|
||||
Assert.Equal (new byte [] { 0xef, 0xbf, 0xbd }, buff2);
|
||||
Assert.Equal (Rune.ReplacementChar.ToString (), str);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -493,17 +511,17 @@ namespace Terminal.Gui.TextTests {
|
||||
{
|
||||
Rune l = (Rune)'\u0370';
|
||||
Assert.False (l.IsNonSpacingChar ());
|
||||
Assert.Equal (1, l.ColumnWidth());
|
||||
Assert.Equal (1, StringExtensions.Make (l).ConsoleWidth());
|
||||
Assert.Equal (1, l.ColumnWidth ());
|
||||
Assert.Equal (1, StringExtensions.Make (l).ConsoleWidth ());
|
||||
Rune ns = (Rune)'\u302a';
|
||||
Assert.False (ns.IsNonSpacingChar());
|
||||
Assert.Equal (2, ns.ColumnWidth());
|
||||
Assert.Equal (2, StringExtensions.Make(ns).ConsoleWidth());
|
||||
Assert.False (ns.IsNonSpacingChar ());
|
||||
Assert.Equal (2, ns.ColumnWidth ());
|
||||
Assert.Equal (2, StringExtensions.Make (ns).ConsoleWidth ());
|
||||
l = (Rune)'\u006f';
|
||||
ns = (Rune)'\u0302';
|
||||
var s = "\u006f\u0302";
|
||||
Assert.Equal (1, l.ColumnWidth());
|
||||
Assert.Equal (0, ns.ColumnWidth());
|
||||
Assert.Equal (1, l.ColumnWidth ());
|
||||
Assert.Equal (0, ns.ColumnWidth ());
|
||||
var ul = StringExtensions.Make (l);
|
||||
Assert.Equal ("o", ul);
|
||||
var uns = StringExtensions.Make (ns);
|
||||
@@ -511,8 +529,8 @@ namespace Terminal.Gui.TextTests {
|
||||
var f = $"{l}{ns}";
|
||||
Assert.Equal ("ô", f);
|
||||
Assert.Equal (f, s);
|
||||
Assert.Equal (1, f.ConsoleWidth());
|
||||
Assert.Equal (1, s.Sum (c => ((Rune)c).ColumnWidth ()));
|
||||
Assert.Equal (1, f.ConsoleWidth ());
|
||||
Assert.Equal (1, s.EnumerateRunes().Sum (c => c.ColumnWidth ()));
|
||||
Assert.Equal (2, s.Length);
|
||||
(var rune, var size) = f.DecodeRune ();
|
||||
Assert.Equal (rune, l);
|
||||
@@ -520,8 +538,8 @@ namespace Terminal.Gui.TextTests {
|
||||
l = (Rune)'\u0041';
|
||||
ns = (Rune)'\u0305';
|
||||
s = "\u0041\u0305";
|
||||
Assert.Equal (1, l.ColumnWidth());
|
||||
Assert.Equal (0, ns.ColumnWidth());
|
||||
Assert.Equal (1, l.ColumnWidth ());
|
||||
Assert.Equal (0, ns.ColumnWidth ());
|
||||
ul = StringExtensions.Make (l);
|
||||
Assert.Equal ("A", ul);
|
||||
uns = StringExtensions.Make (ns);
|
||||
@@ -529,8 +547,8 @@ namespace Terminal.Gui.TextTests {
|
||||
f = $"{l}{ns}";
|
||||
Assert.Equal ("A̅", f);
|
||||
Assert.Equal (f, s);
|
||||
Assert.Equal (1, f.ConsoleWidth());
|
||||
Assert.Equal (1, s.Sum (c => ((Rune)c).ColumnWidth ()));
|
||||
Assert.Equal (1, f.ConsoleWidth ());
|
||||
Assert.Equal (1, s.EnumerateRunes().Sum(c => c.ColumnWidth()));
|
||||
Assert.Equal (2, s.Length);
|
||||
(rune, size) = f.DecodeRune ();
|
||||
Assert.Equal (rune, l);
|
||||
@@ -539,7 +557,7 @@ namespace Terminal.Gui.TextTests {
|
||||
ns = (Rune)'\u0308';
|
||||
s = "\u0061\u0308";
|
||||
Assert.Equal (1, l.ColumnWidth ());
|
||||
Assert.Equal (0, ns.ColumnWidth());
|
||||
Assert.Equal (0, ns.ColumnWidth ());
|
||||
ul = StringExtensions.Make (l);
|
||||
Assert.Equal ("a", ul);
|
||||
uns = StringExtensions.Make (ns);
|
||||
@@ -547,8 +565,8 @@ namespace Terminal.Gui.TextTests {
|
||||
f = $"{l}{ns}";
|
||||
Assert.Equal ("ä", f);
|
||||
Assert.Equal (f, s);
|
||||
Assert.Equal (1, f.ConsoleWidth());
|
||||
Assert.Equal (1, s.Sum (c => ((Rune)c).ColumnWidth()));
|
||||
Assert.Equal (1, f.ConsoleWidth ());
|
||||
Assert.Equal (1, s.EnumerateRunes().Sum(c => c.ColumnWidth()));
|
||||
Assert.Equal (2, s.Length);
|
||||
(rune, size) = f.DecodeRune ();
|
||||
Assert.Equal (rune, l);
|
||||
@@ -556,8 +574,8 @@ namespace Terminal.Gui.TextTests {
|
||||
l = (Rune)'\u4f00';
|
||||
ns = (Rune)'\u302a';
|
||||
s = "\u4f00\u302a";
|
||||
Assert.Equal (2, l.ColumnWidth());
|
||||
Assert.Equal (2, ns.ColumnWidth());
|
||||
Assert.Equal (2, l.ColumnWidth ());
|
||||
Assert.Equal (2, ns.ColumnWidth ());
|
||||
ul = StringExtensions.Make (l);
|
||||
Assert.Equal ("伀", ul);
|
||||
uns = StringExtensions.Make (ns);
|
||||
@@ -565,8 +583,8 @@ namespace Terminal.Gui.TextTests {
|
||||
f = $"{l}{ns}";
|
||||
Assert.Equal ("伀〪", f); // Occupies 4 columns.
|
||||
Assert.Equal (f, s);
|
||||
Assert.Equal (4, f.ConsoleWidth());
|
||||
Assert.Equal (4, s.Sum (c => ((Rune)c).ColumnWidth()));
|
||||
Assert.Equal (4, f.ConsoleWidth ());
|
||||
Assert.Equal (4, s.EnumerateRunes().Sum(c => c.ColumnWidth()));
|
||||
Assert.Equal (2, s.Length);
|
||||
(rune, size) = f.DecodeRune ();
|
||||
Assert.Equal (rune, l);
|
||||
@@ -577,8 +595,8 @@ namespace Terminal.Gui.TextTests {
|
||||
public void Test_IsWideChar ()
|
||||
{
|
||||
Assert.True (((Rune)0x115e).IsWideChar ());
|
||||
Assert.Equal (2, ((Rune)0x115e).ColumnWidth());
|
||||
Assert.False (((Rune)0x116f).IsWideChar());
|
||||
Assert.Equal (2, ((Rune)0x115e).ColumnWidth ());
|
||||
Assert.False (((Rune)0x116f).IsWideChar ());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -595,32 +613,32 @@ namespace Terminal.Gui.TextTests {
|
||||
const int end = 0x10ffff;
|
||||
|
||||
for (int i = start; i <= end; i++) {
|
||||
Rune r = new Rune ((uint)i);
|
||||
if (!r.IsValid()) {
|
||||
if (char.IsSurrogate ((char)i)) {
|
||||
continue;
|
||||
}
|
||||
Rune r = new Rune ((uint)i);
|
||||
string us = StringExtensions.Make (r);
|
||||
string hex = i.ToString ("x6");
|
||||
int v = int.Parse (hex, System.Globalization.NumberStyles.HexNumber);
|
||||
string s = char.ConvertFromUtf32 (v);
|
||||
|
||||
if (!r.IsSurrogatePair()) {
|
||||
if (!r.IsSurrogatePair ()) {
|
||||
Assert.Equal (r.ToString (), us);
|
||||
Assert.Equal (us, s);
|
||||
if (r.ColumnWidth () < 0) {
|
||||
Assert.NotEqual (r.ColumnWidth (), us.ConsoleWidth());
|
||||
Assert.NotEqual (s.Sum (c => ((Rune)c).ColumnWidth ()), us.ConsoleWidth());
|
||||
Assert.NotEqual (r.ColumnWidth (), us.ConsoleWidth ());
|
||||
Assert.NotEqual (s.EnumerateRunes().Sum (c => c.ColumnWidth ()), us.ConsoleWidth ());
|
||||
} else {
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth());
|
||||
Assert.Equal (s.Sum (c => ((Rune)c).ColumnWidth ()), us.ConsoleWidth());
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth ());
|
||||
Assert.Equal (s.EnumerateRunes().Sum (c => c.ColumnWidth ()), us.ConsoleWidth ());
|
||||
}
|
||||
Assert.Equal (us.RuneCount(), s.Length);
|
||||
Assert.Equal (us.RuneCount (), s.Length);
|
||||
} else {
|
||||
Assert.Equal (r.ToString (), us.ToString ());
|
||||
Assert.Equal (us.ToString (), s);
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth());
|
||||
Assert.Equal (s.Sum (c => ((Rune)c).ColumnWidth ()), us.ConsoleWidth());
|
||||
Assert.Equal (1, us.RuneCount()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
|
||||
Assert.Equal (r.ToString (), us);
|
||||
Assert.Equal (us, s);
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth ());
|
||||
Assert.Equal (s.ConsoleWidth (), us.ConsoleWidth ());
|
||||
Assert.Equal (1, us.RuneCount ()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
|
||||
Assert.Equal (2, s.Length); // String always preserves the originals values of each surrogate pair
|
||||
}
|
||||
}
|
||||
@@ -635,17 +653,17 @@ namespace Terminal.Gui.TextTests {
|
||||
Rune r9b = (Rune)0x02009b;
|
||||
|
||||
Assert.Equal (2, r0.ColumnWidth ());
|
||||
Assert.Equal (2, r7.ColumnWidth());
|
||||
Assert.Equal (2, r1b.ColumnWidth());
|
||||
Assert.Equal (2, r9b.ColumnWidth());
|
||||
Assert.Equal (2, r7.ColumnWidth ());
|
||||
Assert.Equal (2, r1b.ColumnWidth ());
|
||||
Assert.Equal (2, r9b.ColumnWidth ());
|
||||
|
||||
"𐨁".DecodeSurrogatePair (out char [] chars);
|
||||
"𠀀".DecodeSurrogatePair (out char [] chars);
|
||||
var rtl = new Rune (chars [0], chars [1]);
|
||||
var rtlp = new Rune ('\ud802', '\ude01');
|
||||
var s = "\U00010a01";
|
||||
var rtlp = new Rune ('\ud840', '\udc00');
|
||||
var s = "\U00020000";
|
||||
|
||||
Assert.Equal (2, rtl.ColumnWidth());
|
||||
Assert.Equal (2, rtlp.ColumnWidth());
|
||||
Assert.Equal (2, rtl.ColumnWidth ());
|
||||
Assert.Equal (2, rtlp.ColumnWidth ());
|
||||
Assert.Equal (2, s.Length);
|
||||
}
|
||||
|
||||
@@ -662,45 +680,47 @@ namespace Terminal.Gui.TextTests {
|
||||
int v = int.Parse (hex, System.Globalization.NumberStyles.HexNumber);
|
||||
string s = char.ConvertFromUtf32 (v);
|
||||
|
||||
if (!r.IsSurrogatePair()) {
|
||||
if (!r.IsSurrogatePair ()) {
|
||||
Assert.Equal (r.ToString (), us);
|
||||
Assert.Equal (us, s);
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth());
|
||||
Assert.Equal (us.RuneCount(), s.Length); // For not surrogate pairs string.RuneCount is always equal to String.Length
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth ());
|
||||
Assert.Equal (us.RuneCount (), s.Length); // For not surrogate pairs string.RuneCount is always equal to String.Length
|
||||
} else {
|
||||
Assert.Equal (r.ToString (), us);
|
||||
Assert.Equal (us, s);
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth());
|
||||
Assert.Equal (1, us.RuneCount()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth ());
|
||||
Assert.Equal (1, us.RuneCount ()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
|
||||
Assert.Equal (2, s.Length); // String always preserves the originals values of each surrogate pair
|
||||
}
|
||||
Assert.Equal (s.Sum (c => ((Rune)c).ColumnWidth ()), us.ConsoleWidth());
|
||||
Assert.Equal (s.ConsoleWidth (), us.ConsoleWidth ());
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_IsSurrogate ()
|
||||
{
|
||||
Rune r = (Rune)'\ue0fd';
|
||||
Assert.False (r.IsSurrogate());
|
||||
Assert.False (char.IsSurrogate (r.ToString (), 0));
|
||||
r = (Rune)0x927C0;
|
||||
Assert.False (r.IsSurrogate());
|
||||
Assert.False (char.IsSurrogate (r.ToString (), 0));
|
||||
|
||||
r = (Rune)'\ud800';
|
||||
Assert.True (r.IsSurrogate());
|
||||
char c = '\ue0fd';
|
||||
Assert.False (char.IsSurrogate (c.ToString (), 0));
|
||||
Rune r = (Rune)0x927C0;
|
||||
Assert.True (r.IsSurrogatePair ());
|
||||
Assert.True (r.IsSurrogatePair ());
|
||||
Assert.True (char.IsSurrogate (r.ToString (), 0));
|
||||
r = (Rune)'\udfff';
|
||||
Assert.True (r.IsSurrogate());
|
||||
Assert.True (char.IsSurrogate(r.ToString(), 0));
|
||||
Assert.True (char.IsSurrogate (r.ToString (), 1));
|
||||
Assert.True (char.IsSurrogatePair (r.ToString (), 0));
|
||||
|
||||
c = '\ud800';
|
||||
Assert.True (char.IsSurrogate (c.ToString (), 0));
|
||||
c = '\udfff';
|
||||
Assert.True (char.IsSurrogate (c.ToString (), 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_EncodeSurrogatePair ()
|
||||
{
|
||||
Assert.False (RuneExtensions.EncodeSurrogatePair (unchecked((char)0x40D7C0), (char)0xDC20, out Rune rune));
|
||||
Assert.False (RuneExtensions.EncodeSurrogatePair ((char)0x0065, (char)0x0301, out _));
|
||||
Assert.Equal (0, rune.Value);
|
||||
Assert.False (RuneExtensions.EncodeSurrogatePair ((char)0x0065, (char)0x0301, out rune));
|
||||
Assert.Equal (0, rune.Value);
|
||||
Assert.True (RuneExtensions.EncodeSurrogatePair ('\ud83c', '\udf56', out rune));
|
||||
Assert.Equal (0x1F356, rune.Value);
|
||||
Assert.Equal ("🍖", rune.ToString ());
|
||||
@@ -732,38 +752,21 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.True (v >= 0x10000 && v <= RuneExtensions.MaxRune.Value);
|
||||
Assert.Equal (r.ToString (), us);
|
||||
Assert.Equal (us, s);
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth());
|
||||
Assert.Equal (s.Sum (c => ((Rune)c).ColumnWidth ()), us.ConsoleWidth());
|
||||
Assert.Equal (1, us.RuneCount()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
|
||||
Assert.Equal (r.ColumnWidth (), us.ConsoleWidth ());
|
||||
Assert.Equal (s.ConsoleWidth (), us.ConsoleWidth ());
|
||||
Assert.Equal (1, us.RuneCount ()); // Here returns 1 because is a valid surrogate pair resulting in only rune >=U+10000..U+10FFFF
|
||||
Assert.Equal (2, s.Length); // String always preserves the originals values of each surrogate pair
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//[Fact]
|
||||
//public void Test_ExpectedSizeFromFirstByte ()
|
||||
//{
|
||||
// Assert.Equal (-1, Rune.ExpectedSizeFromFirstByte (255));
|
||||
// Assert.Equal (1, Rune.ExpectedSizeFromFirstByte (127));
|
||||
// Assert.Equal (4, Rune.ExpectedSizeFromFirstByte (240));
|
||||
//}
|
||||
|
||||
[Fact]
|
||||
public void Test_FullRune_Extension ()
|
||||
{
|
||||
string us = "Hello, 世界";
|
||||
//Assert.True (us.FullRune ());
|
||||
us = $"Hello, {StringExtensions.Make (new byte [] { 228 })}界";
|
||||
//Assert.False (us.FullRune ());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_DecodeRune_Extension ()
|
||||
{
|
||||
string us = "Hello, 世界";
|
||||
List<Rune> runes = new List<Rune> ();
|
||||
int tSize = 0;
|
||||
for (int i = 0; i < us.RuneCount(); i++) {
|
||||
for (int i = 0; i < us.RuneCount (); i++) {
|
||||
(Rune rune, int size) = us.Substring (i, 1).DecodeRune ();
|
||||
runes.Add (rune);
|
||||
tSize += size;
|
||||
@@ -779,7 +782,7 @@ namespace Terminal.Gui.TextTests {
|
||||
string us = "Hello, 世界";
|
||||
List<Rune> runes = new List<Rune> ();
|
||||
int tSize = 0;
|
||||
for (int i = us.RuneCount() - 1; i >= 0; i--) {
|
||||
for (int i = us.RuneCount () - 1; i >= 0; i--) {
|
||||
(Rune rune, int size) = us.Substring (i, 1).DecodeLastRune ();
|
||||
runes.Add (rune);
|
||||
tSize += size;
|
||||
@@ -789,37 +792,17 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal (13, tSize);
|
||||
}
|
||||
|
||||
//[Fact]
|
||||
//public void Test_InvalidIndex_Extension ()
|
||||
//{
|
||||
// string us = "Hello, 世界";
|
||||
// Assert.Equal (-1, us.InvalidIndex ());
|
||||
// us = string.Make (new byte [] { 0xff, 0xfe, 0xfd });
|
||||
// Assert.Equal (0, us.InvalidIndex ());
|
||||
//}
|
||||
|
||||
[Fact]
|
||||
public void Test_Valid_Extension ()
|
||||
{
|
||||
string us = "Hello, 世界";
|
||||
Assert.True (RuneExtensions.IsValid (us.ToByteArray()));
|
||||
Assert.True (RuneExtensions.IsValid (us.ToByteArray ()));
|
||||
us = StringExtensions.Make (new byte [] { 0xff, 0xfe, 0xfd });
|
||||
Assert.False (RuneExtensions.IsValid(us.ToByteArray()));
|
||||
Assert.False (RuneExtensions.IsValid (us.ToByteArray ()));
|
||||
}
|
||||
|
||||
//[Fact]
|
||||
//public void Test_ExpectedSizeFromFirstByte_Extension ()
|
||||
//{
|
||||
// string us = StringExtensions.Make (255);
|
||||
// Assert.Equal (-1, us.ExpectedSizeFromFirstByte ());
|
||||
// us = StringExtensions.Make (127);
|
||||
// Assert.Equal (1, us.ExpectedSizeFromFirstByte ());
|
||||
// us = StringExtensions.Make (240);
|
||||
// Assert.Equal (4, us.ExpectedSizeFromFirstByte ());
|
||||
//}
|
||||
|
||||
[Fact]
|
||||
public void Equals_Tests ()
|
||||
public void Equals_ToRuneList ()
|
||||
{
|
||||
var a = new List<List<Rune>> () { "First line.".ToRuneList () };
|
||||
var b = new List<List<Rune>> () { "First line.".ToRuneList (), "Second line.".ToRuneList () };
|
||||
@@ -866,7 +849,7 @@ namespace Terminal.Gui.TextTests {
|
||||
int sumConsoleWidth = 0;
|
||||
for (uint i = 0; i < 32; i++) {
|
||||
sumRuneWidth += ((Rune)i).ColumnWidth ();
|
||||
sumConsoleWidth += StringExtensions.Make (i).ConsoleWidth();
|
||||
sumConsoleWidth += StringExtensions.Make (i).ConsoleWidth ();
|
||||
}
|
||||
|
||||
Assert.Equal (-32, sumRuneWidth);
|
||||
@@ -878,33 +861,139 @@ namespace Terminal.Gui.TextTests {
|
||||
{
|
||||
string us = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
|
||||
Assert.Equal (200, us.Length);
|
||||
Assert.Equal (200, us.RuneCount());
|
||||
Assert.Equal (200, us.ConsoleWidth());
|
||||
int sumRuneWidth = us.Sum (x => ((Rune)x).ColumnWidth ());
|
||||
Assert.Equal (200, us.RuneCount ());
|
||||
Assert.Equal (200, us.ConsoleWidth ());
|
||||
int sumRuneWidth = us.EnumerateRunes().Sum (x => x.ColumnWidth ());
|
||||
Assert.Equal (200, sumRuneWidth);
|
||||
|
||||
us = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n";
|
||||
Assert.Equal (201, us.Length);
|
||||
Assert.Equal (201, us.RuneCount());
|
||||
Assert.Equal (200, us.ConsoleWidth());
|
||||
sumRuneWidth = us.Sum (x => ((Rune)x).ColumnWidth ());
|
||||
Assert.Equal (201, us.RuneCount ());
|
||||
Assert.Equal (200, us.ConsoleWidth ());
|
||||
sumRuneWidth = us.EnumerateRunes().Sum(x => x.ColumnWidth());
|
||||
Assert.Equal (199, sumRuneWidth);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rune_IsHighSurrogate_IsLowSurrogate ()
|
||||
{
|
||||
Rune r = (Rune)'\ud800';
|
||||
Assert.True (r.IsHighSurrogate());
|
||||
char c = '\ud800';
|
||||
Assert.True (char.IsHighSurrogate (c));
|
||||
|
||||
r = (Rune)'\udbff';
|
||||
Assert.True (r.IsHighSurrogate());
|
||||
c = '\udbff';
|
||||
Assert.True (char.IsHighSurrogate (c));
|
||||
|
||||
r = (Rune)'\udc00';
|
||||
Assert.True (r.IsLowSurrogate());
|
||||
c = '\udc00';
|
||||
Assert.True (char.IsLowSurrogate (c));
|
||||
|
||||
r = (Rune)'\udfff';
|
||||
Assert.True (r.IsLowSurrogate());
|
||||
c = '\udfff';
|
||||
Assert.True (char.IsLowSurrogate (c));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rune_ToRunes ()
|
||||
{
|
||||
var str = "First line.";
|
||||
var runes = str.ToRunes ();
|
||||
for (int i = 0; i < runes.Length; i++) {
|
||||
Assert.Equal (str [i], runes [i].Value);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void System_Rune_ColumnWidth ()
|
||||
{
|
||||
var r = new Rune ('a');
|
||||
Assert.Equal (1, r.ColumnWidth ());
|
||||
Assert.Equal (1, r.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new Rune ('b');
|
||||
Assert.Equal (1, r.ColumnWidth ());
|
||||
Assert.Equal (1, r.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new Rune (123);
|
||||
Assert.Equal (1, r.ColumnWidth ());
|
||||
Assert.Equal (1, r.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new Rune ('\u1150');
|
||||
Assert.Equal (2, r.ColumnWidth ()); // 0x1150 ᅐ Unicode Technical Report #11
|
||||
Assert.Equal (2, r.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (3, r.Utf8SequenceLength);
|
||||
|
||||
r = new Rune ('\u1161');
|
||||
Assert.Equal (0, r.ColumnWidth ()); // 0x1161 ᅡ column width of 0
|
||||
Assert.Equal (0, r.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (3, r.Utf8SequenceLength);
|
||||
|
||||
r = new Rune (31);
|
||||
Assert.Equal (-1, r.ColumnWidth ()); // non printable character
|
||||
Assert.Equal (0, r.ToString ().ConsoleWidth ());// ConsoleWidth only returns zero or greater than zero
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new Rune (127);
|
||||
Assert.Equal (-1, r.ColumnWidth ()); // non printable character
|
||||
Assert.Equal (0, r.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, r.ToString ().Length);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new Rune (0x16fe0);
|
||||
Assert.Equal (2, r.ColumnWidth ()); // non printable character
|
||||
Assert.Equal (2, r.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (2, r.ToString ().Length);
|
||||
Assert.Equal (2, r.Utf16SequenceLength);
|
||||
Assert.Equal (4, r.Utf8SequenceLength);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void System_Text_Rune ()
|
||||
{
|
||||
var r = new System.Text.Rune ('a');
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new System.Text.Rune ('b');
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new System.Text.Rune (123);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength);
|
||||
|
||||
r = new System.Text.Rune ('\u1150');
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (3, r.Utf8SequenceLength); // 0x1150 ᅐ Unicode Technical Report #11
|
||||
|
||||
r = new System.Text.Rune ('\u1161');
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (3, r.Utf8SequenceLength); // 0x1161 ᅡ column width of 0
|
||||
|
||||
r = new System.Text.Rune (31);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength); // non printable character
|
||||
|
||||
r = new System.Text.Rune (127);
|
||||
Assert.Equal (1, r.Utf16SequenceLength);
|
||||
Assert.Equal (1, r.Utf8SequenceLength); // non printable character
|
||||
|
||||
r = new System.Text.Rune (0x16fe0);
|
||||
Assert.Equal (2, r.Utf16SequenceLength);
|
||||
Assert.Equal (4, r.Utf8SequenceLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1565,7 +1565,7 @@ namespace Terminal.Gui.TextTests {
|
||||
width = text.RuneCount () - 1;
|
||||
wrappedLines = TextFormatter.WordWrapText (text, width);
|
||||
Assert.Equal (2, wrappedLines.Count);
|
||||
Assert.Equal (text[..(text.RuneCount () - 1)], wrappedLines [0]);
|
||||
Assert.Equal (text [..(text.RuneCount () - 1)], wrappedLines [0]);
|
||||
Assert.Equal ("e", wrappedLines [1]);
|
||||
|
||||
width = text.RuneCount () - 2;
|
||||
@@ -3524,70 +3524,6 @@ namespace Terminal.Gui.TextTests {
|
||||
Assert.Equal ("\u2460\u2461\u2462\u2463\u2464", list [1]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void System_Rune_ColumnWidth ()
|
||||
{
|
||||
var c = new Rune ('a');
|
||||
Assert.Equal (1, c.ColumnWidth ());
|
||||
Assert.Equal (1, c.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, c.ToString ().Length);
|
||||
|
||||
c = new Rune ('b');
|
||||
Assert.Equal (1, c.ColumnWidth ());
|
||||
Assert.Equal (1, c.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, c.ToString ().Length);
|
||||
|
||||
c = new Rune (123);
|
||||
Assert.Equal (1, c.ColumnWidth ());
|
||||
Assert.Equal (1, c.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, c.ToString ().Length);
|
||||
|
||||
c = new Rune ('\u1150');
|
||||
Assert.Equal (2, c.ColumnWidth ()); // 0x1150 ᅐ Unicode Technical Report #11
|
||||
Assert.Equal (2, c.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (3, c.ToString ().Length);
|
||||
|
||||
c = new Rune ('\u1161');
|
||||
Assert.Equal (0, c.ColumnWidth ()); // 0x1161 ᅡ column width of 0
|
||||
Assert.Equal (0, c.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (3, c.ToString ().Length);
|
||||
|
||||
c = new Rune (31);
|
||||
Assert.Equal (-1, c.ColumnWidth ()); // non printable character
|
||||
Assert.Equal (0, c.ToString ().ConsoleWidth ());// ConsoleWidth only returns zero or greater than zero
|
||||
Assert.Equal (1, c.ToString ().Length);
|
||||
|
||||
c = new Rune (127);
|
||||
Assert.Equal (-1, c.ColumnWidth ()); // non printable character
|
||||
Assert.Equal (0, c.ToString ().ConsoleWidth ());
|
||||
Assert.Equal (1, c.ToString ().Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void System_Text_Rune ()
|
||||
{
|
||||
var c = new System.Text.Rune ('a');
|
||||
Assert.Equal (1, c.Utf8SequenceLength);
|
||||
|
||||
c = new System.Text.Rune ('b');
|
||||
Assert.Equal (1, c.Utf8SequenceLength);
|
||||
|
||||
c = new System.Text.Rune (123);
|
||||
Assert.Equal (1, c.Utf8SequenceLength);
|
||||
|
||||
c = new System.Text.Rune ('\u1150');
|
||||
Assert.Equal (3, c.Utf8SequenceLength); // 0x1150 ᅐ Unicode Technical Report #11
|
||||
|
||||
c = new System.Text.Rune ('\u1161');
|
||||
Assert.Equal (3, c.Utf8SequenceLength); // 0x1161 ᅡ column width of 0
|
||||
|
||||
c = new System.Text.Rune (31);
|
||||
Assert.Equal (1, c.Utf8SequenceLength); // non printable character
|
||||
|
||||
c = new System.Text.Rune (127);
|
||||
Assert.Equal (1, c.Utf8SequenceLength); // non printable character
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Format_WordWrap_PreserveTrailingSpaces ()
|
||||
{
|
||||
@@ -3877,29 +3813,27 @@ namespace Terminal.Gui.TextTests {
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Ustring_Array_Is_Not_Equal_ToRunes_Array_And_String_Array ()
|
||||
public void String_Array_Is_Not_Always_Equal_ToRunes_Array ()
|
||||
{
|
||||
var text = "New Test 你";
|
||||
string us = text;
|
||||
string s = text;
|
||||
Assert.Equal (10, us.RuneCount ());
|
||||
Assert.Equal (10, s.Length);
|
||||
// The reason is string index is related to byte length and not rune length
|
||||
Assert.Equal (12, us.Length);
|
||||
Assert.NotEqual (20320, us [9]);
|
||||
Assert.Equal (20320, s [9]);
|
||||
Assert.Equal (228, us [9]);
|
||||
Assert.Equal ("ä", us [9].ToString ());
|
||||
Assert.Equal ("你", s [9].ToString ());
|
||||
|
||||
// Rune array is equal to string array
|
||||
var usToRunes = us.ToRunes ();
|
||||
string s = "New Test 你";
|
||||
// Rune array length is equal to string array
|
||||
var usToRunes = s.ToRunes ();
|
||||
Assert.Equal (10, usToRunes.Length);
|
||||
Assert.Equal (10, s.Length);
|
||||
Assert.Equal (20320, usToRunes [9].Value);
|
||||
Assert.Equal (20320, s [9]);
|
||||
Assert.Equal ("你", usToRunes [9].ToString ());
|
||||
Assert.Equal ("你", s [9].ToString ());
|
||||
|
||||
s = "New Test \U0001d539";
|
||||
// Rune array length isn't equal to string array
|
||||
usToRunes = s.ToRunes ();
|
||||
Assert.Equal (10, usToRunes.Length);
|
||||
Assert.Equal (11, s.Length);
|
||||
Assert.Equal (120121, usToRunes [9].Value);
|
||||
Assert.Equal (55349, s [9]);
|
||||
Assert.Equal ("𝔹", usToRunes [9].ToString ());
|
||||
Assert.Equal ("𝔹", new string (new char [] { s [9], s [10] }));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user