@@ -31,6 +31,30 @@ public void Constructor(string testString, params string[] expectedCodepoints)
3131 }
3232 #endregion
3333
34+ #region CharLength tests
35+ private static IEnumerable < object [ ] > CharLengthTestData =>
36+ [
37+ [ "test" , 4 ] ,
38+ [ "" , 0 ] ,
39+ [ "This is\r \n a test!" , 16 ] ,
40+ [ "العربية" , 7 ] ,
41+ [ "😁🤔😮" , 6 ] ,
42+ [ "test😮" , 6 ] ,
43+ ] ;
44+
45+ [ TestMethod ]
46+ [ DynamicData ( nameof ( CharLengthTestData ) ) ]
47+ public void CharLength ( string str , int expectedResult )
48+ {
49+ UString us = new ( str ) ;
50+ Assert . AreEqual ( expectedResult , us . CharLength ) ;
51+
52+ // Test making sure that a substring results in the correct result
53+ us = CreateTestSubstring ( str ) ;
54+ Assert . AreEqual ( expectedResult , us . CharLength ) ;
55+ }
56+ #endregion
57+
3458 #region Equals tests
3559 private static IEnumerable < object [ ] > EqualsTestData =>
3660 [
@@ -542,6 +566,79 @@ public void Concat_StringList(string?[] strings, string expectedResults)
542566 Assert . AreEqual ( UString . Concat ( strings . Select ( CreateTestSubstring ) . ToArray ( ) ) , expectedUs ) ;
543567 }
544568 #endregion
569+
570+ #region Split tests
571+ private static IEnumerable < object ? [ ] > SplitExceptionTestData =>
572+ [
573+ [ "bla" , null , 1 , typeof ( ArgumentException ) ] ,
574+ [ "bla" , Array . Empty < UCodepoint > ( ) , 1 , typeof ( ArgumentException ) ] ,
575+ [ "bla" , new UCodepoint [ ] { ' ' } , - 1 , typeof ( ArgumentOutOfRangeException ) ] ,
576+ [ "bla" , new UCodepoint [ ] { ' ' } , 0 , typeof ( ArgumentOutOfRangeException ) ] ,
577+ ] ;
578+
579+ [ TestMethod ]
580+ [ DynamicData ( nameof ( SplitExceptionTestData ) ) ]
581+ public void Split_InvalidParameters ( string testString , UCodepoint [ ] ? separators , int maxCount ,
582+ Type expectedExceptionType )
583+ {
584+ UString us = new ( testString ) ;
585+ Assert . That . ThrowsException ( expectedExceptionType , ( ) => us . Split ( separators , maxCount ) ) ;
586+ }
587+
588+ private static IEnumerable < object [ ] > SplitTestData =>
589+ [
590+ [ "" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "" } ] ,
591+ [ "" , new UCodepoint [ ] { ' ' } , 1 , new [ ] { "" } ] ,
592+ [ "This is a test!" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "This" , "is" , "a" , "test!" } ] ,
593+ [ "This is a test!" , new UCodepoint [ ] { ' ' } , 2 , new [ ] { "This" , "is a test!" } ] ,
594+ [ " This is a test! " , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "" , "This" , "" , "is" , "a" , "test!" , "" , "" } ] ,
595+ [ "This is\r \n a test!" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "This" , "is\r \n a" , "test!" } ] ,
596+ [ "This is\r \n a test!" , new UCodepoint [ ] { ' ' , '\r ' , '\n ' } , int . MaxValue , new [ ] { "This" , "is" , "" , "a" , "test!" } ] ,
597+ [ "العربية" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "العربية" } ] ,
598+ [ "😁🤔😮" , new [ ] { UCodepoint . ReadFromStr ( "🤔" , 0 ) } , int . MaxValue , new [ ] { "😁" , "😮" } ] ,
599+ ] ;
600+
601+ [ TestMethod ]
602+ [ DynamicData ( nameof ( SplitTestData ) ) ]
603+ public void Split ( string testString , UCodepoint [ ] ? separators , int maxCount , string [ ] expectedResults )
604+ {
605+ UString us = new ( testString ) ;
606+ UString [ ] expectedUss = expectedResults . Select ( er => new UString ( er ) ) . ToArray ( ) ;
607+
608+ Assert . That . SequenceEqual ( expectedUss , us . Split ( separators , maxCount ) ) ;
609+
610+ // Test making sure that a substring results in the correct result
611+ us = CreateTestSubstring ( testString ) ;
612+ Assert . That . SequenceEqual ( expectedUss , us . Split ( separators , maxCount ) ) ;
613+ }
614+
615+ private static IEnumerable < object [ ] > SplitIgnoreEmptyTestData =>
616+ [
617+ [ "" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "" } ] ,
618+ [ "" , new UCodepoint [ ] { ' ' } , 1 , new [ ] { "" } ] ,
619+ [ "This is a test!" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "This" , "is" , "a" , "test!" } ] ,
620+ [ "This is a test!" , new UCodepoint [ ] { ' ' } , 2 , new [ ] { "This" , "is a test!" } ] ,
621+ [ " This is a test! " , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "This" , "is" , "a" , "test!" } ] ,
622+ [ "This is\r \n a test!" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "This" , "is\r \n a" , "test!" } ] ,
623+ [ "This is\r \n a test!" , new UCodepoint [ ] { ' ' , '\r ' , '\n ' } , int . MaxValue , new [ ] { "This" , "is" , "a" , "test!" } ] ,
624+ [ "العربية" , new UCodepoint [ ] { ' ' } , int . MaxValue , new [ ] { "العربية" } ] ,
625+ [ "😁🤔😮" , new [ ] { UCodepoint . ReadFromStr ( "🤔" , 0 ) } , int . MaxValue , new [ ] { "😁" , "😮" } ] ,
626+ ] ;
627+
628+ [ TestMethod ]
629+ [ DynamicData ( nameof ( SplitIgnoreEmptyTestData ) ) ]
630+ public void Split_IgnoreEmpty ( string testString , UCodepoint [ ] ? separators , int maxCount , string [ ] expectedResults )
631+ {
632+ UString us = new ( testString ) ;
633+ UString [ ] expectedUss = expectedResults . Select ( er => new UString ( er ) ) . ToArray ( ) ;
634+
635+ Assert . That . SequenceEqual ( expectedUss , us . Split ( separators , maxCount , StringSplitOptions . RemoveEmptyEntries ) ) ;
636+
637+ // Test making sure that a substring results in the correct result
638+ us = CreateTestSubstring ( testString ) ;
639+ Assert . That . SequenceEqual ( expectedUss , us . Split ( separators , maxCount , StringSplitOptions . RemoveEmptyEntries ) ) ;
640+ }
641+ #endregion
545642
546643 #region Private helper methods
547644 private static UString CreateTestSubstring ( string ? str )
0 commit comments