Files
Terminal.Gui/UnitTests/Text/RuneTests.cs
Tig 4430fe2cc6 Fixes #3192. Improve correctness / clarity of existing View.AutoSize functionality/unit tests (#3202)
* Removed resharper settings from editorconfig

* Remove constructors with frame parameters from Button class.

* Remove constructors with frame parameters from CheckBox class.

* Cleanup code.

* Remove constructors with frame parameters from ComboBox class.

* @BDisp
Remove constructors with frame parameters from FrameView class.

* Remove constructors with frame parameters from Label class.

* Remove constructors with frame parameters from ListView class.

* Remove constructors with frame parameters from ScrollBarView class.

* Remove constructors with frame parameters from ScrollView class.

* Remove namespace braces.

* Cleanup code.

* Cleanup code.

* Cleanup code.

* Remove constructors with frame parameters from TextField class.

* Remove constructors with frame parameters from TimeField class.

* Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty.

* Remove constructors with frame parameters from Toplevel class.

* Remove constructors with frame parameters from Window class.

* Fix merge errors.

* Revert "Fixes #3182. OnResizeNeeded returns int.MaxValue and int.MaxValue when Application.Top is null, should return Size.Empty."

This reverts commit cf9c24b846.

* Revert unit test.

* Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame (#2980)

* Fixes #2882. TabView: 'Frame.DrawFrame(Rect, bool)' is obsolete: 'This method is obsolete in v2. Use use LineCanvas or Frame

* Trying fix this unit test that sometimes fail.

* Fixes #2983. View need a alternative DrawFrame for the v2.

* Use new DrawFrame method.

* Change _lines field to Lines property.

* Add TabWindow unit test.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Revert "Change _lines field to Lines property."

This reverts commit ab6c5f3094.

* Add DrawIncompleteFrame method and unit tests.

* Add more unit tests to LineCanvas.

* Fix newline conflict errors.

* Force render immediately instead of join.

* I will never rely on zero-location-based unit test again.

* Fix TestTreeViewColor unit test fail.

* Using location of 3 to avoid be divisible by 2 and so avoiding bugs.

* Revert "Using location of 3 to avoid be divisible by 2 and so avoiding bugs."

This reverts commit dd3df135d8.

* Revert "I will never rely on zero-location-based unit test again."

This reverts commit 62adf6f285.

* Revert "Fix newline conflict errors."

This reverts commit 4acf72612d.

* Revert "Add more unit tests to LineCanvas."

This reverts commit 66bc6f514e.

* Revert "Add DrawIncompleteFrame method and unit tests."

This reverts commit 680ba264e1.

* Resolving merge conflicts.

* Revert "Use new DrawFrame method."

This reverts commit 69a7f17f19.

* Revert "Fixes #2983. View need a alternative DrawFrame for the v2."

This reverts commit dade9fd767.

* Reverting this changes to start a new one.

* Add horizontal and vertical support for combining glyphs.

* Fix text and auto size behavior.

* Add TabWidth property.

* Add unit test for WordWrap.

* Fixes #3017. View TextDirection returns incorrect size on a vertical direction instance with AutoSize as false.

* Using Frame to force read from the get method.

* Fix some issues with AutoSize and ForceValidatePosDim.

* Fixing broken unit tests.

* Restoring code I've broken.

* Removing forgotten code.

* Only LayoutStyle.Computed can change the Frame.

* DateField and TimeField depends on LayoutStyle.Computed.

* Fix unit tests related with LayoutStyle.

* Implements tabs, left and right arrows as View.

* Draws a minimum full border.

* Adds missing XML parameter.

* Adds assert tests for Frame.

* Removes duplicates InlineData.

* Adds more unit tests for minimum full border without Left and Right thickness.

* Trying to fix the TestTreeViewColor unit test fail.

* Prevents a user to set TextDirection to -1.

* Prevents any invalid TextDirection value.

* Removes (TextDirection)(-1).

* Removes unnecessary TextDirection initialization.

* Removes LayoutStyle.

* Fixing unit tests with border.

* Trying to fix TestTreeViewColor again.

* Revert "Trying to fix TestTreeViewColor again."

This reverts commit c2efa8e42e.

* Trying to fix TestTreeViewColor again.

* Fix merge errors.

* Fix merge errors.

* Restoring unit test.

* Restores the right XML comment.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Fix Disposing unit tests that sometimes throws because some instances aren't cleared on others unit tests classes.

* Only call OnResizeNeeded if it's LayoutStyle.Computed.

* Fix merge errors.

* Fix merge errors.

* Fix unit tests fail.

* Reformat.

* Again.

* Rename to OnDrawAdornments.

* Fix failing unit tests.

* Reduces indentation and cleanup code.

* Cleanup code.

* Fix bug done when cleanup.

* Replace FrameHandledMouseEvent to AdornmentHandledMouseEvent.

* Removes Tab constructor parameters.

---------

Co-authored-by: Tig <tig@users.noreply.github.com>

* Fix merge errors.

* Remove constructors with parameters from Button.

* Remove parenthesis on objects initializers from Button.

* Remove constructors with parameters from CheckBox.

* Remove parenthesis on objects initializers from CheckBox.

* Remove constructors with parameters from ComboBox.

* Remove constructors with parameters from FrameView.

* Remove parenthesis on objects initializers from FrameView.

* Initial commit

* Renamed Direction enum for clarity in refactoring unit tests

* Moved nav tests to NavigationTests

* Moved view tests around

* Cleaning up TextFormatter and View.AutoSize code

* Fixed latent TextFormatter bug with \n

* removed Application dependency on some autosize unit tests

* Fixed Label tests to deal with auotsize overriding height/width

* Fixed more label tests. WIP

* Fixed all places where AutoSize = happend after setting Dims

* Started adding new primitive View.Text tests

* Code comments

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* Remove frame set from the View constructor and prevent SetRelativeLayout running if not yet initialized.

* Changes needed for unit tests pass on remove parameters constructors from the Label class.

* Remove constructors with parameters from Label.

* Remove parenthesis on objects initializers from Label.

* Prefix private fields with underscore.

* Renamed to MaxLength.

* Remove constructors with parameters from ListView.

* MakeWrapper not needed anymore.

* Remove parenthesis on objects initializers from ListView.

* WIP: Enforce that it makes no sense to set Width/Height if AutoSize = true. Update Unit tests to match.

* Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat.

* Massive code cleanup - use parameterless constructors and ensure AutoSize is set properly. Code reformat.

* Fixed messagebox

* Remove constructors with parameters from ScrollBarView and ScrollView.

* Remove parenthesis on objects initializers from ScrollBarView and ScrollView.

* Cleanup code.

* Fix merge errors.

* Add empty dotsettings for solution and projects.

* Set ReSharper language analysis level for projects to  C#12

* Make ReSharper consider itself the boss for style

* Add rule to enforce property backing fields above the property

* Disable auto-detection of naming rules so ReSharper doesn't change them by itself

* Don't let someone's VS settings override the indent settings

* Explicitly set tab width to 4 spaces and force spaces.

* Rules to keep various multi-line constructs aligned within themselves

* Curly brace rules (Using K&R style, per current project spec)

* Blank line rules

Mostly to add breaks in various situations, and also to enforce max of 1 blank line.

* Increase auto-wrap to 160 from default of 120

* Line break at end of all files, to make Unixy systems happy

* Keep attributes on their own lines except for methods and records that are themselves single-line

* Increase attribute auto-wrap to 60 from default of 38

* Wrap/chop rules for long method signatures and record declarations

Chop if either already multi-line or if over 8 parameters.
Chop AFTER the left paren and BEFORE the first parameter.

* Chop rules for generics

For generics with multi-line type parameters or multiple type parameter constraints, chop in a way that has consistent alignment.

* Always enforce enum members on their own lines

* One-line functions completely on one line

They're usually expression-bodied anyway, here, so this is almost irrelevant

* Keep control flow statements on their own lines

* Follow same chop rules for method invocation as method declaration

* Chop long or multi-line method chains or patterns

* Wrap rules for binary operators

Operator at beginning of new lines
Auto-chop if long or already multi-line

* Spaces between keywords and their opening parentheses

* Add file layout rules for organization of reorderable items like fields, properties, etc.

Ugly XML, so load it up in the UI to look at it
It's MOSTLY the ReSharper defaults, but with more aggressive sorting, generally by access modifier and name, within each grouping.

* Deconstructors use per-member types

* Use keywords for built-in types and increase severity of inspection for violations

Also apply to IntPtr and similar, which should now be nint and similar

* Apply syntax style on completion.

* Use var when evident for built-in and simple types, but not elsewhere

* Increase severity for some minor redundancy and clarity inspections

* Enforce braces always required for blocks, and treat as error

* Warn if a local function is not statement-bodied

* Increase various inspection severities

IF Condition => Severity:
Attributes not wrapped property => Suggestion
Constructors expression-bodied => Error
default doesn't have type when it isn't clear => suggestion
Namespaces not file-scoped => error
Methods not statement-bodied => hint
Null check pattern not the object property pattern => Error

* Adjust preference order of null check patterns to make object pattern highest

* XmlDoc rules to keep tags and contents aligned and wrapped

* Add a few custom profiles for code cleanup and make the full profile default

* Remove parts for languages not used in this solution

* copied in v2_develop changes

* Merged v2_develop

* Added Begin/Init unit tests. Removed Application dependencey from AutoSizeFalse tests

* TextFormatter.Lines -> GetLines ()

* Let ReSharper know we intend to localize things

* TextFormatter code cleanup

* copied in v2_develop changes

* Merged v2_develop

* Spaces aren't wanted here either.

* Fix merge errors.

* Fixes ContentBottomRightCorner related with https://github.com/gui-cs/Terminal.Gui/issues/3211#issue-2098878820

* Remove constructors with parameters from ContextMenu.

* Remove commented code.

* Remove constructors with parameters from OpenDialog.

* Remove constructors with parameters from SaveDialog.

* Remove constructors with parameters from TextField.

* Remove constructors with parameters from TimeField.

* Fix unit test.

* Remove unnecessary SetInitialProperties method.

* Remove unnecessary SetInitialProperties method.

* Remove parenthesis on objects initializers from Toplevel and Window.

* Remove constructors with parameters from RadioGroup.

* Remove constructors with parameters from TextView.

* Remove constructors with parameters from MenuBar.

* TEMPORARY: Turn everything that was set to error down to warning or lower

* Fixes #3219. MenuBar is opened by call OpenMenu even it's disabled.

* Remove constructors with parameters from Menu.

* Remove constructors with parameters from View.

* Change constructor to internal because is mainly useful for testing.

* ReSharper Cleanup Code.

* Added format only r# config

* Fixes #3224. TextFormatter.Lines should return a single string.Empty list even with Width or Height equal to zero.

* Remove constructors with parameters from Dialog.

* Remove constructors with parameters from TextValidateField.

* Fixes https://github.com/gui-cs/Terminal.Gui/issues/3224#issuecomment-1924096038

* Fixes #3225. Press CursorDown on TabView doesn't move to the next view.

* Fixes #3229. TextFormatter should have a FillRemaining property.

* Testing formatting merge in bdisp

* Testing formatting merge

* Testing formatting merge bdisp

* Testing formatting merge 2

* xmldoc format

* R# Full Code Cleanup

* R# Full Code Cleanup2

* R# Full Code Cleanup2

* Merged! But broke tests

* Refixing...

* Refixed DrawTests

* Refixed ViewTests

* Refixed Text Tests

* Refixed more unit tests

* Refixed scenarios

* Refixed rest of scenarios

* Refixed ViewsTests

* Refixed rest of tests. All unit tests pass again!

* Fixed warnings

* Updated R# version. Added new code cleanup settings

* Applied latest code cleanup to solution

* Another code cleanup pass

* Tweaked r# settings. .editorconfig now matches.

* r# else on separate line

* r# - update

* r# - full solution

* test commit

* test commit

* test commit

* Removed extra profiles

* Full cleanup following cleaning up profiles

* Null checking pattern rules/inspections

* Tabs and extra whitespace are evil

* Attributes on their own lines

* Code layout rules to put fields first and to put backing fields with their properties

* Merged v2_develop

* Full cleanup following dodexahedron's PR

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
Co-authored-by: Brandon Thetford <github@snapsinazfs.com>
2024-02-11 15:54:03 -07:00

1046 lines
38 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Buffers;
using System.Globalization;
using System.Text;
namespace Terminal.Gui.TextTests;
public class RuneTests
{
[Fact]
public void Cast_To_Char_Durrogate_Pair_Return_UTF16 ()
{
Assert.NotEqual ("𝔹", $"{new Rune (unchecked ((char)0x1d539))}");
Assert.Equal ("픹", $"{new Rune (unchecked ((char)0x1d539))}");
Assert.Equal ("픹", $"{new Rune (0xd539)}");
Assert.Equal ("𝔹", $"{new Rune (0x1d539)}");
}
[Fact]
public void Equals_ToRuneList ()
{
List<List<Rune>> a = new () { "First line.".ToRuneList () };
List<List<Rune>> b = new () { "First line.".ToRuneList (), "Second line.".ToRuneList () };
List<Rune> c = new (a [0]);
List<Rune> d = a [0];
Assert.Equal (a [0], b [0]);
// Not the same reference
Assert.False (a [0] == b [0]);
Assert.NotEqual (a [0], b [1]);
Assert.False (a [0] == b [1]);
Assert.Equal (c, a [0]);
Assert.False (c == a [0]);
Assert.Equal (c, b [0]);
Assert.False (c == b [0]);
Assert.NotEqual (c, b [1]);
Assert.False (c == b [1]);
Assert.Equal (d, a [0]);
// Is the same reference
Assert.True (d == a [0]);
Assert.Equal (d, b [0]);
Assert.False (d == b [0]);
Assert.NotEqual (d, b [1]);
Assert.False (d == b [1]);
Assert.True (a [0].SequenceEqual (b [0]));
Assert.False (a [0].SequenceEqual (b [1]));
Assert.True (c.SequenceEqual (a [0]));
Assert.True (c.SequenceEqual (b [0]));
Assert.False (c.SequenceEqual (b [1]));
Assert.True (d.SequenceEqual (a [0]));
Assert.True (d.SequenceEqual (b [0]));
Assert.False (d.SequenceEqual (b [1]));
}
[Fact]
public void GetColumns_GetRuneCount ()
{
PrintTextElementCount ('\u00e1'.ToString (), "á", 1, 1, 1, 1);
PrintTextElementCount ("\u0061\u0301", "á", 1, 2, 2, 1);
PrintTextElementCount ("\u0061\u0301", "á", 1, 2, 2, 1);
PrintTextElementCount ("\u0065\u0301", "é", 1, 2, 2, 1);
PrintTextElementCount ("\U0001f469\U0001f3fd\u200d\U0001f692", "👩🏽‍🚒", 6, 4, 7, 1);
PrintTextElementCount ("\ud801\udccf", "𐓏", 1, 1, 2, 1);
}
[Theory]
[InlineData (
"\u2615\ufe0f",
"☕️",
2,
2,
2
)] // \ufe0f forces it to be rendered as a colorful image as compared to a monochrome text variant.
[InlineData (
"\u1107\u1165\u11b8",
"법",
3,
2,
1
)] // the letters 법 join to form the Korean word for "rice:" U+BC95 법 (read from top left to bottom right)
[InlineData ("\U0001F468\u200D\U0001F469\u200D\U0001F467", "👨‍👩‍👧", 8, 6, 8)] // Man, Woman and Girl emoji.
[InlineData ("\u0915\u093f", "कि", 2, 2, 2)] // Hindi कि with DEVANAGARI LETTER KA and DEVANAGARI VOWEL SIGN I
[InlineData (
"\u0e4d\u0e32",
"ํา",
2,
1,
2
)] // Decomposition: ํ (U+0E4D)- า (U+0E32) = U+0E33 ำ Thai Character Sara Am
[InlineData ("\u0e33", "ำ", 1, 1, 1)] // Decomposition: ํ (U+0E4D)- า (U+0E32) = U+0E33 ำ Thai Character Sara Am
public void GetColumns_String_Without_SurrogatePair (
string code,
string str,
int codeLength,
int columns,
int stringLength
)
{
Assert.Equal (str, code.Normalize ());
Assert.Equal (codeLength, code.Length);
//Assert.Equal (columns, code.EnumerateRunes ().Sum (x => x.GetColumns ()));
Assert.Equal (columns, str.GetColumns ());
Assert.Equal (stringLength, str.Length);
}
[Theory]
[InlineData (new [] { '\ud83e', '\ude01' }, "🨁", 1, 2, 4)] // Neutral Chess Queen
[InlineData (new [] { '\udb43', '\udfe1' }, "󠿡", 1, 2, 4)] // Undefined Character
[InlineData (new [] { '\ud83c', '\udf55' }, "🍕", 2, 2, 4)] // 🍕 Slice of Pizza
[InlineData (new [] { '\ud83e', '\udd16' }, "🤖", 2, 2, 4)] // 🤖 Robot Face
[InlineData (new [] { '\ud83e', '\udde0' }, "🧠", 2, 2, 4)] // 🧠 Brain
[InlineData (new [] { '\ud801', '\udc21' }, "𐐡", 1, 2, 4)] // 𐐡 Deseret Capital Letter Er
[InlineData (new [] { '\ud83c', '\udf39' }, "🌹", 2, 2, 4)] // 🌹 Rose
[InlineData (new [] { '\uD83D', '\uDC7E' }, "👾", 2, 2, 4)] // U+1F47E alien monster (CodepointWidth::Wide)
[InlineData (
new [] { '\uD83D', '\uDD1C' },
"🔜",
2,
2,
4
)] // 🔜 Soon With Rightwards Arrow Above (CodepointWidth::Wide)
public void GetColumns_Utf16_Encode (char [] code, string str, int columns, int stringLength, int utf8Length)
{
var rune = new Rune (code [0], code [1]);
Assert.Equal (str, rune.ToString ());
Assert.Equal (columns, rune.GetColumns ());
Assert.Equal (stringLength, rune.ToString ().Length);
Assert.Equal (utf8Length, rune.Utf8SequenceLength);
Assert.True (Rune.IsValid (rune.Value));
}
[Theory]
[InlineData ("\U0001fa01", "🨁", 1, 2)] // Neutral Chess Queen
[InlineData ("\U000e0fe1", "󠿡", 1, 2)] // Undefined Character
[InlineData ("\U0001F355", "🍕", 2, 2)] // 🍕 Slice of Pizza
[InlineData ("\U0001F916", "🤖", 2, 2)] // 🤖 Robot Face
[InlineData ("\U0001f9e0", "🧠", 2, 2)] // 🧠 Brain
[InlineData ("\U00010421", "𐐡", 1, 2)] // 𐐡 Deseret Capital Letter Er
[InlineData ("\U0001f339", "🌹", 2, 2)] // 🌹 Rose
//[InlineData ("\uFE20FE21", "", 1, 1)] // Combining Ligature Left Half - U+fe20 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
// Combining Ligature Right Half - U+fe21 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
public void GetColumns_Utf32_Encode (string code, string str, int columns, int stringLength)
{
OperationStatus operationStatus = Rune.DecodeFromUtf16 (code, out Rune rune, out int charsConsumed);
Assert.Equal (OperationStatus.Done, operationStatus);
Assert.Equal (str, rune.ToString ());
Assert.Equal (columns, rune.GetColumns ());
Assert.Equal (stringLength, rune.ToString ().Length);
Assert.Equal (charsConsumed, rune.Utf16SequenceLength);
Assert.True (Rune.IsValid (rune.Value));
// with DecodeRune
(Rune nrune, int size) = code.DecodeRune ();
Assert.Equal (str, nrune.ToString ());
Assert.Equal (columns, nrune.GetColumns ());
Assert.Equal (stringLength, nrune.ToString ().Length);
Assert.Equal (size, nrune.Utf8SequenceLength);
for (var x = 0; x < code.Length - 1; x++)
{
Assert.Equal (nrune.Value, char.ConvertToUtf32 (code [x], code [x + 1]));
Assert.True (RuneExtensions.EncodeSurrogatePair (code [x], code [x + 1], out Rune result));
Assert.Equal (rune, result);
}
Assert.True (Rune.IsValid (nrune.Value));
}
[Theory]
[InlineData (new byte [] { 0xf0, 0x9f, 0xa8, 0x81 }, "🨁", 1, 2)] // Neutral Chess Queen
[InlineData (new byte [] { 0xf3, 0xa0, 0xbf, 0xa1 }, "󠿡", 1, 2)] // Undefined Character
[InlineData (new byte [] { 0xf0, 0x9f, 0x8d, 0x95 }, "🍕", 2, 2)] // 🍕 Slice of Pizza
[InlineData (new byte [] { 0xf0, 0x9f, 0xa4, 0x96 }, "🤖", 2, 2)] // 🤖 Robot Face
[InlineData (new byte [] { 0xf0, 0x90, 0x90, 0xa1 }, "𐐡", 1, 2)] // 𐐡 Deseret Capital Letter Er
[InlineData (new byte [] { 0xf0, 0x9f, 0x8c, 0xb9 }, "🌹", 2, 2)] // 🌹 Rose
public void GetColumns_Utf8_Encode (byte [] code, string str, int columns, int stringLength)
{
OperationStatus operationStatus = Rune.DecodeFromUtf8 (code, out Rune rune, out int bytesConsumed);
Assert.Equal (OperationStatus.Done, operationStatus);
Assert.Equal (str, rune.ToString ());
Assert.Equal (columns, rune.GetColumns ());
Assert.Equal (stringLength, rune.ToString ().Length);
Assert.Equal (bytesConsumed, rune.Utf8SequenceLength);
Assert.True (Rune.IsValid (rune.Value));
}
[Theory]
[InlineData (0, "\0", 0, 1, 1)]
[InlineData ('\u1dc0', "᷀", 0, 1, 3)] // ◌᷀ Combining Dotted Grave Accent
[InlineData ('\u20D0', "⃐", 0, 1, 3)] // ◌⃐ Combining Left Harpoon Above
[InlineData (1, "\u0001", -1, 1, 1)]
[InlineData (2, "\u0002", -1, 1, 1)]
[InlineData (31, "\u001f", -1, 1, 1)] // non printable character - Information Separator One
[InlineData (127, "\u007f", -1, 1, 1)] // non printable character - Delete
[InlineData (32, " ", 1, 1, 1)] // space
[InlineData ('a', "a", 1, 1, 1)]
[InlineData ('b', "b", 1, 1, 1)]
[InlineData (123, "{", 1, 1, 1)] // { Left Curly Bracket
[InlineData ('\u231c', "⌜", 1, 1, 3)] // ⌜ Top Left Corner
// BUGBUG: These are CLEARLY wide glyphs, but GetColumns() returns 1
// However, most terminals treat these as narrow and they overlap the next cell when drawn (including Windows Terminal)
[InlineData (
'\u1161',
"ᅡ",
1,
1,
3
)] // ᅡ Hangul Jungseong A - Unicode Hangul Jamo for join with column width equal to 0 alone.
[InlineData ('\u2103', "℃", 1, 1, 3)] // ℃ Degree Celsius
[InlineData ('\u2501', "━", 1, 1, 3)] // ━ Box Drawings Heavy Horizontal
[InlineData ('\u25a0', "■", 1, 1, 3)] // ■ Black Square
[InlineData ('\u25a1', "□", 1, 1, 3)] // □ White Square
[InlineData ('\u277f', "❿", 1, 1, 3)] //Dingbat Negative Circled Number Ten - ❿ U+277f
[InlineData (
'\u4dc0',
"䷀",
1,
1,
3
)] // ䷀Hexagram For The Creative Heaven - U+4dc0 - https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
[InlineData ('\ud7b0', "ힰ", 1, 1, 3)] // ힰ ┤Hangul Jungseong O-Yeo - ힰ U+d7b0')]
[InlineData ('\uf61e', "", 1, 1, 3)] // Private Use Area
[InlineData ('\u23f0', "⏰", 2, 1, 3)] // Alarm Clock - ⏰ U+23f0
[InlineData ('\u1100', "ᄀ", 2, 1, 3)] // ᄀ Hangul Choseong Kiyeok
[InlineData ('\u1150', "ᅐ", 2, 1, 3)] // ᅐ Hangul Choseong Ceongchieumcieuc
[InlineData ('\u2615', "☕", 2, 1, 3)] // ☕ Hot Beverage
[InlineData ('\u231a', "⌚", 2, 1, 3)] // ⌚ Watch
[InlineData ('\u231b', "⌛", 2, 1, 3)] // ⌛ Hourglass
// From WindowsTerminal's CodepointWidthDetector tests (https://github.com/microsoft/terminal/blob/main/src/types/CodepointWidthDetector.cpp)
//static constexpr std::wstring_view emoji = L"\xD83E\xDD22"; // U+1F922 nauseated face
//static constexpr std::wstring_view ambiguous = L"\x414"; // U+0414 cyrillic capital de
//{ 0x414, L"\x414", CodepointWidth::Narrow }, // U+0414 cyrillic capital de
[InlineData ('\u0414', "Д", 1, 1, 2)] // U+0414 cyrillic capital de
//{ 0x1104, L"\x1104", CodepointWidth::Wide }, // U+1104 hangul choseong ssangtikeut
[InlineData ('\u1104', "ᄄ", 2, 1, 3)]
//{ 0x306A, L"\x306A", CodepointWidth::Wide }, // U+306A hiragana na な
[InlineData (0x306A, "な", 2, 1, 3)]
//{ 0x30CA, L"\x30CA", CodepointWidth::Wide }, // U+30CA katakana na ナ
[InlineData (0x30CA, "ナ", 2, 1, 3)]
//{ 0x72D7, L"\x72D7", CodepointWidth::Wide }, // U+72D7
[InlineData (0x72D7, "狗", 2, 1, 3)]
public void GetColumns_With_Single_Code (int code, string str, int columns, int stringLength, int utf8Length)
{
var rune = new Rune (code);
Assert.Equal (str, rune.ToString ());
Assert.Equal (columns, rune.GetColumns ());
Assert.Equal (stringLength, rune.ToString ().Length);
Assert.Equal (utf8Length, rune.Utf8SequenceLength);
Assert.True (Rune.IsValid (rune.Value));
}
// IsCombiningMark tests
[Theory]
[InlineData (0x0338, true)] // Combining Long Solidus Overlay (U+0338) (e.g. ≠)
[InlineData (0x0300, true)] // Combining Grave Accent
[InlineData (0x0301, true)] // Combining acute accent (é)
[InlineData (0x0302, true)] // Combining Circumflex Accent
[InlineData (0x0328, true)] // Combining ogonek (a small hook or comma shape) U+0328
[InlineData (0x00E9, false)] // Latin Small Letter E with Acute, Unicode U+00E9 é
[InlineData (0x0061, false)] // Latin Small Letter A is U+0061.
[InlineData (
'\uFE20',
true
)] // Combining Ligature Left Half - U+fe20 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
[InlineData (
'\uFE21',
true
)] // Combining Ligature Right Half - U+fe21 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
public void IsCombiningMark (int codepoint, bool expected)
{
var rune = new Rune (codepoint);
Assert.Equal (expected, rune.IsCombiningMark ());
}
[Theory]
[InlineData (0x0338)] // Combining Long Solidus Overlay (U+0338) (e.g. ≠)
[InlineData (0x0300)] // Combining Grave Accent
[InlineData (0x0301)] // Combining acute accent (é)
[InlineData (0x0302)] // Combining Circumflex Accent
[InlineData (0x0061)] // Combining ogonek (a small hook or comma shape)
[InlineData (
'\uFE20'
)] // Combining Ligature Left Half - U+fe20 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
[InlineData (
'\uFE21'
)] // Combining Ligature Right Half - U+fe21 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
public void MakePrintable_Combining_Character_Is_Not_Printable (int code)
{
var rune = new Rune (code);
Rune actual = rune.MakePrintable ();
Assert.Equal (code, actual.Value);
}
[Theory]
[InlineData (0x0000001F, 0x241F)]
[InlineData (0x0000007F, 0x247F)]
[InlineData (0x0000009F, 0x249F)]
[InlineData (0x0001001A, 0x1001A)]
public void MakePrintable_Converts_Control_Chars_To_Proper_Unicode (int code, int expected)
{
Rune actual = ((Rune)code).MakePrintable ();
Assert.Equal (expected, actual.Value);
}
[Theory]
[InlineData (0x20)]
[InlineData (0x7E)]
[InlineData (0xA0)]
[InlineData (0x010020)]
public void MakePrintable_Does_Not_Convert_Ansi_Chars_To_Unicode (int code)
{
Rune actual = ((Rune)code).MakePrintable ();
Assert.Equal (code, actual.Value);
}
[Theory]
[InlineData (
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
200,
200,
200
)]
[InlineData (
"01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n",
201,
200,
199
)] // has a '\n' newline
[InlineData (
"\t01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n",
202,
200,
198
)] // has a '\t' and a '\n' newline
public void Rune_ColumnWidth_Versus_String_ConsoleWidth (string text, int stringLength, int strCols, int runeCols)
{
Assert.Equal (stringLength, text.Length);
Assert.Equal (stringLength, text.GetRuneCount ());
Assert.Equal (strCols, text.GetColumns ());
int sumRuneWidth = text.EnumerateRunes ().Sum (x => x.GetColumns ());
Assert.Equal (runeCols, sumRuneWidth);
}
[Theory]
[InlineData (0x12345678)]
[InlineData ('\ud801')]
public void Rune_Exceptions_Integers (int code) { Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (code)); }
[Theory]
[InlineData (new [] { '\ud799', '\udc21' })]
public void Rune_Exceptions_Utf16_Encode (char [] code)
{
Assert.False (RuneExtensions.EncodeSurrogatePair (code [0], code [1], out Rune rune));
Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (code [0], code [1]));
}
/// <summary>
/// Shows the difference between using Wcwidth.UnicodeCalculator and our own port of wcwidth. Specifically, the
/// UnicodeCalculator is more accurate to spec where null has a width of 0, and our port says it's -1.
/// </summary>
/// <param name="expectedColumns"></param>
/// <param name="scalar"></param>
[Theory]
[InlineData (0, 0)]
[InlineData (-1, 1)]
[InlineData (-1, 2)]
[InlineData (-1, 3)]
[InlineData (-1, 4)]
[InlineData (-1, 5)]
[InlineData (-1, 6)]
[InlineData (-1, 7)]
[InlineData (-1, 8)]
[InlineData (-1, 9)]
[InlineData (-1, 10)]
[InlineData (-1, 11)]
[InlineData (-1, 12)]
[InlineData (-1, 13)]
[InlineData (-1, 14)]
[InlineData (-1, 15)]
[InlineData (-1, 16)]
[InlineData (-1, 17)]
[InlineData (-1, 18)]
[InlineData (-1, 19)]
[InlineData (-1, 20)]
[InlineData (-1, 21)]
[InlineData (-1, 22)]
[InlineData (-1, 23)]
[InlineData (-1, 24)]
[InlineData (-1, 25)]
[InlineData (-1, 26)]
[InlineData (-1, 27)]
[InlineData (-1, 28)]
[InlineData (-1, 29)]
[InlineData (-1, 30)]
[InlineData (-1, 31)]
public void Rune_GetColumns_Non_Printable (int expectedColumns, int scalar)
{
var rune = new Rune (scalar);
Assert.Equal (expectedColumns, rune.GetColumns ());
Assert.Equal (0, rune.ToString ().GetColumns ());
}
[Fact]
public void Rune_GetColumns_Versus_String_GetColumns_With_Non_Printable_Characters ()
{
var sumRuneWidth = 0;
var sumConsoleWidth = 0;
for (uint i = 0; i < 32; i++)
{
sumRuneWidth += ((Rune)i).GetColumns ();
sumConsoleWidth += ((Rune)i).ToString ().GetColumns ();
}
Assert.Equal (-31, sumRuneWidth);
Assert.Equal (0, sumConsoleWidth);
}
[Theory]
[InlineData ('\ud800', true)]
[InlineData ('\udbff', true)]
[InlineData ('\udc00', false)]
[InlineData ('\udfff', false)]
[InlineData ('\uefff', null)]
public void Rune_IsHighSurrogate_IsLowSurrogate (char code, bool? isHighSurrogate)
{
if (isHighSurrogate == true)
{
Assert.True (char.IsHighSurrogate (code));
}
else if (isHighSurrogate == false)
{
Assert.True (char.IsLowSurrogate (code));
}
else
{
Assert.False (char.IsHighSurrogate (code));
Assert.False (char.IsLowSurrogate (code));
}
}
[Theory]
[InlineData (true, '\u1100')]
[InlineData (true, '\ud83c', '\udf39')]
[InlineData (true, '\udbff', '\udfff')]
[InlineData (false, '\ud801')]
[InlineData (false, '\ud83e')]
public void Rune_IsValid (bool valid, params char [] chars)
{
Rune rune = default;
var isValid = true;
if (chars.Length == 1)
{
try
{
rune = new Rune (chars [0]);
}
catch (Exception)
{
isValid = false;
}
}
else
{
rune = new Rune (chars [0], chars [1]);
}
if (valid)
{
Assert.NotEqual (default (Rune), rune);
Assert.True (Rune.IsValid (rune.Value));
Assert.True (valid);
}
else
{
Assert.False (valid);
Assert.False (isValid);
}
}
[Theory]
[InlineData ("First line.")]
[InlineData ("Hello, 𝔹𝕆𝔹")]
public void Rune_ToRunes (string text)
{
Rune [] runes = text.ToRunes ();
for (var i = 0; i < runes.Length; i++)
{
Assert.Equal (text.EnumerateRunes ().ToArray () [i].Value, runes [i].Value);
}
}
[Fact]
public void Sum_Of_Rune_GetColumns_Is_Not_Always_Equal_To_String_GetColumns ()
{
const int start = 0x000000;
const int end = 0x10ffff;
for (int i = start; i <= end; i++)
{
if (char.IsSurrogate ((char)i))
{
continue;
}
var r = new Rune ((uint)i);
var us = r.ToString ();
var hex = i.ToString ("x6");
int v = int.Parse (hex, NumberStyles.HexNumber);
string s = char.ConvertFromUtf32 (v);
if (!r.IsSurrogatePair ())
{
Assert.Equal (r.ToString (), us);
Assert.Equal (us, s);
if (r.GetColumns () < 0)
{
Assert.NotEqual (r.GetColumns (), us.GetColumns ());
Assert.NotEqual (s.EnumerateRunes ().Sum (c => c.GetColumns ()), us.GetColumns ());
}
else
{
Assert.Equal (r.GetColumns (), us.GetColumns ());
Assert.Equal (s.EnumerateRunes ().Sum (c => c.GetColumns ()), us.GetColumns ());
}
Assert.Equal (us.GetRuneCount (), s.Length);
}
else
{
Assert.Equal (r.ToString (), us);
Assert.Equal (us, s);
Assert.Equal (r.GetColumns (), us.GetColumns ());
Assert.Equal (s.GetColumns (), us.GetColumns ());
Assert.Equal (
1,
us.GetRuneCount ()
); // 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
}
}
}
[Theory]
[InlineData ('a', 1, 1)]
[InlineData (31, 1, 1)]
[InlineData (123, 1, 1)]
[InlineData (127, 1, 1)]
[InlineData ('\u1150', 1, 3)]
[InlineData ('\u1161', 1, 3)]
[InlineData (0x16fe0, 2, 4)]
public void System_Text_Rune_SequenceLength (int code, int utf16Length, int utf8Length)
{
var r = new Rune (code);
Assert.Equal (utf16Length, r.Utf16SequenceLength);
Assert.Equal (utf8Length, r.Utf8SequenceLength);
}
[Fact]
public void Test_All_Surrogate_Pairs_Range ()
{
for (uint h = 0xd800; h <= 0xdbff; h++)
{
for (uint l = 0xdc00; l <= 0xdfff; l++)
{
var r = new Rune ((char)h, (char)l);
var us = r.ToString ();
var hex = r.Value.ToString ("x6");
int v = int.Parse (hex, NumberStyles.HexNumber);
string s = char.ConvertFromUtf32 (v);
Assert.True (v >= 0x10000 && v <= RuneExtensions.MaxUnicodeCodePoint);
Assert.Equal (r.ToString (), us);
Assert.Equal (us, s);
Assert.Equal (r.GetColumns (), us.GetColumns ());
Assert.Equal (s.GetColumns (), us.GetColumns ());
Assert.Equal (
1,
us.GetRuneCount ()
); // 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
}
}
}
[Theory]
[InlineData ("<22><><EFBFBD>", false)]
[InlineData ("Hello, 世界", true)]
[InlineData (new byte [] { 0xff, 0xfe, 0xfd }, false)]
[InlineData (new byte [] { 0xf0, 0x9f, 0x8d, 0x95 }, true)]
public void Test_CanBeEncodedAsRune_Extension (object text, bool canBeEncodedAsRune)
{
string str;
if (text is string)
{
str = (string)text;
if (canBeEncodedAsRune)
{
Assert.True (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
}
else
{
Assert.False (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
}
}
else if (text is byte [])
{
str = StringExtensions.ToString ((byte [])text);
if (canBeEncodedAsRune)
{
Assert.True (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
}
else
{
Assert.False (RuneExtensions.CanBeEncodedAsRune (Encoding.Unicode.GetBytes (str.ToCharArray ())));
}
}
}
[Theory]
[InlineData ("Hello, 世界", 13, 11, 9, "界世 ,olleH")] // Without Surrogate Pairs
[InlineData ("Hello, 𝔹𝕆𝔹", 19, 10, 13, "𝔹𝕆𝔹 ,olleH")] // With Surrogate Pairs
public void Test_DecodeLastRune_Extension (
string text,
int bytesLength,
int colsLength,
int textLength,
string encoded
)
{
List<Rune> runes = new ();
var tSize = 0;
for (int i = text.GetRuneCount () - 1; i >= 0; i--)
{
(Rune rune, int size) = text.DecodeLastRune (i);
runes.Add (rune);
tSize += size;
}
var result = StringExtensions.ToString (runes);
Assert.Equal (encoded, result);
Assert.Equal (bytesLength, tSize);
Assert.Equal (colsLength, result.GetColumns ());
Assert.Equal (textLength, result.Length);
}
[Theory]
[InlineData ("Hello, 世界", 13, 11, 9)] // Without Surrogate Pairs
[InlineData ("Hello, 𝔹𝕆𝔹", 19, 10, 13)] // With Surrogate Pairs
public void Test_DecodeRune_Extension (string text, int bytesLength, int colsLength, int textLength)
{
List<Rune> runes = new ();
var tSize = 0;
for (var i = 0; i < text.GetRuneCount (); i++)
{
(Rune rune, int size) = text.DecodeRune (i);
runes.Add (rune);
tSize += size;
}
var result = StringExtensions.ToString (runes);
Assert.Equal (text, result);
Assert.Equal (bytesLength, tSize);
Assert.Equal (colsLength, result.GetColumns ());
Assert.Equal (textLength, result.Length);
}
[Theory]
[InlineData ('\uea85', null, "", false)] // Private Use Area
[InlineData (0x1F356, new [] { '\ud83c', '\udf56' }, "🍖", true)] // 🍖 Meat On Bone
public void Test_DecodeSurrogatePair (int code, char [] charsValue, string runeString, bool isSurrogatePair)
{
var rune = new Rune (code);
char [] chars;
if (isSurrogatePair)
{
Assert.True (rune.DecodeSurrogatePair (out chars));
Assert.Equal (2, chars.Length);
Assert.Equal (charsValue [0], chars [0]);
Assert.Equal (charsValue [1], chars [1]);
Assert.Equal (runeString, new Rune (chars [0], chars [1]).ToString ());
}
else
{
Assert.False (rune.DecodeSurrogatePair (out chars));
Assert.Null (chars);
Assert.Equal (runeString, rune.ToString ());
}
Assert.Equal (chars, charsValue);
}
[Theory]
[InlineData (unchecked ((char)0x40D7C0), (char)0xDC20, 0, "\0", false)]
[InlineData ((char)0x0065, (char)0x0301, 0, "\0", false)]
[InlineData ('\ud83c', '\udf56', 0x1F356, "🍖", true)] // 🍖 Meat On Bone
public void Test_EncodeSurrogatePair (
char highSurrogate,
char lowSurrogate,
int runeValue,
string runeString,
bool isSurrogatePair
)
{
Rune rune;
if (isSurrogatePair)
{
Assert.True (RuneExtensions.EncodeSurrogatePair ('\ud83c', '\udf56', out rune));
}
else
{
Assert.False (RuneExtensions.EncodeSurrogatePair (highSurrogate, lowSurrogate, out rune));
}
Assert.Equal (runeValue, rune.Value);
Assert.Equal (runeString, rune.ToString ());
}
[Theory]
[InlineData ('\ue0fd', false)]
[InlineData ('\ud800', true)]
[InlineData ('\udfff', true)]
public void Test_IsSurrogate (char code, bool isSurrogate)
{
if (isSurrogate)
{
Assert.True (char.IsSurrogate (code.ToString (), 0));
}
else
{
Assert.False (char.IsSurrogate (code.ToString (), 0));
}
}
[Theory]
[InlineData (500000000)]
[InlineData (0xf801, 0xdfff)]
public void Test_MaxRune (params int [] codes)
{
if (codes.Length == 1)
{
Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (codes [0]));
}
else
{
Assert.Throws<ArgumentOutOfRangeException> (() => new Rune ((char)codes [0], (char)codes [1]));
}
}
[Theory]
[InlineData (
'\u006f',
'\u0302',
"\u006f\u0302",
1,
0,
2,
"o",
"̂",
"ô",
1,
2
)]
[InlineData (
'\u0065',
'\u0301',
"\u0065\u0301",
1,
0,
2,
"e",
"́",
"é",
1,
2
)]
public void Test_NonSpacingChar (
int code1,
int code2,
string code,
int rune1Length,
int rune2Length,
int codeLength,
string code1String,
string code2String,
string joinString,
int joinLength,
int bytesLength
)
{
var rune = new Rune (code1);
var nsRune = new Rune (code2);
Assert.Equal (rune1Length, rune.GetColumns ());
Assert.Equal (rune2Length, nsRune.GetColumns ());
var ul = rune.ToString ();
Assert.Equal (code1String, ul);
var uns = nsRune.ToString ();
Assert.Equal (code2String, uns);
string f = $"{rune}{nsRune}".Normalize ();
Assert.Equal (f, joinString);
Assert.Equal (f, code.Normalize ());
Assert.Equal (joinLength, f.GetColumns ());
Assert.Equal (joinLength, code.EnumerateRunes ().Sum (c => c.GetColumns ()));
Assert.Equal (codeLength, code.Length);
(Rune nrune, int size) = f.DecodeRune ();
Assert.Equal (f.ToRunes () [0], nrune);
Assert.Equal (bytesLength, size);
}
[Theory]
[InlineData (0x20D0, 0x20EF)]
[InlineData (0x2310, 0x231F)]
[InlineData (0x1D800, 0x1D80F)]
public void Test_Range (int start, int end)
{
for (int i = start; i <= end; i++)
{
var r = new Rune ((uint)i);
var us = r.ToString ();
var hex = i.ToString ("x6");
int v = int.Parse (hex, NumberStyles.HexNumber);
string s = char.ConvertFromUtf32 (v);
if (!r.IsSurrogatePair ())
{
Assert.Equal (r.ToString (), us);
Assert.Equal (us, s);
Assert.Equal (r.GetColumns (), us.GetColumns ());
Assert.Equal (
us.GetRuneCount (),
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.GetColumns (), us.GetColumns ());
Assert.Equal (
1,
us.GetRuneCount ()
); // 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.GetColumns (), us.GetColumns ());
}
}
[Fact]
public void Test_SurrogatePair_From_String ()
{
Assert.True (ProcessTestStringUseChar ("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟"));
Assert.Throws<Exception> (() => ProcessTestStringUseChar ("\ud801"));
Assert.True (ProcessStringUseRune ("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟"));
Assert.Throws<Exception> (() => ProcessStringUseRune ("\ud801"));
}
[Fact]
public void TestRuneIsLetter ()
{
Assert.Equal (5, CountLettersInString ("Hello"));
Assert.Equal (8, CountLettersInString ("𐓏𐓘𐓻𐓘𐓻𐓟 𐒻𐓟"));
}
[Fact]
public void TestSplit ()
{
var inputString = "🐂, 🐄, 🐆";
string [] splitOnSpace = inputString.Split (' ');
string [] splitOnComma = inputString.Split (',');
Assert.Equal (3, splitOnSpace.Length);
Assert.Equal (3, splitOnComma.Length);
}
private int CountLettersInString (string s)
{
var letterCount = 0;
foreach (Rune rune in s.EnumerateRunes ())
{
if (Rune.IsLetter (rune))
{
letterCount++;
}
}
return letterCount;
}
private void PrintTextElementCount (
string us,
string s,
int consoleWidth,
int runeCount,
int stringCount,
int txtElementCount
)
{
Assert.Equal (us.Length, s.Length);
Assert.Equal (us, s);
Assert.Equal (consoleWidth, us.GetColumns ());
Assert.Equal (runeCount, us.GetRuneCount ());
Assert.Equal (stringCount, s.Length);
TextElementEnumerator enumerator = StringInfo.GetTextElementEnumerator (s);
var textElementCount = 0;
while (enumerator.MoveNext ())
{
textElementCount++; // For versions prior to Net5.0 the StringInfo class might handle some grapheme clusters incorrectly.
}
Assert.Equal (txtElementCount, textElementCount);
}
private bool ProcessStringUseRune (string s)
{
string us = s;
var rs = "";
Rune codePoint;
List<Rune> runes = new ();
var colWidth = 0;
for (var i = 0; i < s.Length; i++)
{
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.Equal (0, rune.Value);
Assert.False (Rune.IsValid (s [i]));
rune = codePoint;
runes.Add (rune);
var sp = new string (new [] { s [i], s [i + 1] });
Assert.Equal (sp, codePoint.ToString ());
Assert.True (codePoint.IsSurrogatePair ());
i++; // Increment the iterator by the number of surrogate pair
}
else
{
Assert.Throws<ArgumentOutOfRangeException> (() => new Rune (s [i]));
throw new Exception ("String was not well-formed UTF-16.");
}
colWidth += rune.GetColumns (); // Increment the column width of this Rune
rs += rune.ToString ();
}
Assert.Equal (us.GetColumns (), colWidth);
Assert.Equal (s, rs);
Assert.Equal (s, StringExtensions.ToString (runes));
return true;
}
private bool ProcessTestStringUseChar (string s)
{
char surrogateChar = default;
for (var i = 0; i < s.Length; i++)
{
Rune r;
if (char.IsSurrogate (s [i]))
{
if (surrogateChar != default (int) && 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);
var sp = new string (new [] { surrogateChar, s [i] });
r = (Rune)codePoint;
Assert.Equal (sp, r.ToString ());
Assert.True (r.IsSurrogatePair ());
surrogateChar = default (char);
}
else if (i < s.Length - 1)
{
surrogateChar = s [i];
}
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]).Encode (buff);
Assert.Equal ((int)s [i], buff [0]);
Assert.Equal (s [i], r.Value);
Assert.True (Rune.IsValid (r.Value));
Assert.False (r.IsSurrogatePair ());
}
}
return true;
}
}