From 7d909cb13c977af39605069c2f80ea33df8ddc44 Mon Sep 17 00:00:00 2001 From: e Date: Fri, 16 Aug 2024 18:15:43 -0300 Subject: [PATCH] ags4: adds utf-8 and string split tests - utf-8 tests are added for CompareTo, EndsWith, IndexOf, LowerCase, Replace and ReplaceCharAt - Also adds String.Split tests --- ags4/auto-test/test-string.asc | 135 ++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 2 deletions(-) diff --git a/ags4/auto-test/test-string.asc b/ags4/auto-test/test-string.asc index c236547..1f7916f 100644 --- a/ags4/auto-test/test-string.asc +++ b/ags4/auto-test/test-string.asc @@ -1,7 +1,7 @@ // Test String Module Script int GetTestStringCount() { - return 23; + return 30 + 60 /* string split */; } void TestString() @@ -12,6 +12,12 @@ void TestString() String mytext = "Hello"; mytext = mytext.Append("World"); tap.is(mytext, "HelloWorld", "String.Append test"); + +// disabled because single utf-8 char errors the new compiler +// // UTF-8 specific test +// String utfText = "Hell"; +// utfText = utfText.AppendChar('ä'); // Append a UTF-8 character +// tap.is(utfText, "Hellä", "String.AppendChar UTF-8 test"); } // String.AppendChar @@ -26,6 +32,11 @@ void TestString() String mytext = "Hello"; tap.ok(mytext.CompareTo("hello",eCaseInsensitive) == 0, "String.CompareTo sensitivity off"); tap.ok(mytext.CompareTo("hello",eCaseSensitive) != 0, "String.CompareTo sensitivity on"); + + // UTF-8 specific test + String utfText = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞĀĂĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨĪÜ"; + tap.ok(utfText.CompareTo("àáâãäåçèéêëìíîïðñòóôõöøùúûüýþāăąćĉċčďđēĕėęěĝğġģĥħĩīü", eCaseInsensitive) == 0, "String.CompareTo UTF-8 sensitivity off"); + tap.ok(utfText.CompareTo("àáâãäåçèéêëìíîïðñòóôõöøùúûüýþāăąćĉċčďđēĕėęěĝğġģĥħĩīü", eCaseSensitive) != 0, "String.CompareTo UTF-8 sensitivity on"); } // String.Copy @@ -39,6 +50,10 @@ void TestString() { String myString = "Hello from the script!"; tap.ok(myString.EndsWith("script!"), "String.EndsWith test"); + + // UTF-8 specific test + String utfString = "Grüße von MÜNCHEN!"; + tap.ok(utfString.EndsWith("münchen!", eCaseInsensitive), "String.EndsWith UTF-8 test"); } // String.Format @@ -55,6 +70,11 @@ void TestString() String haystack = "The haystack had a needle in it somewhere."; int result = haystack.IndexOf("a needle"); tap.is_int(result, 17, "String.IndexOf test"); + + // UTF-8 specific test + String utfHaystack = "The haystack had a ĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨ and a needle in it somewhere.."; + int utfResult = utfHaystack.IndexOf("a needle"); + tap.is_int(utfResult, 43, "String.IndexOf UTF-8 test"); } // String.IsNullOrEmpty @@ -72,6 +92,11 @@ void TestString() String mystring = "THIS is a test string"; String lowercased = mystring.LowerCase(); tap.is(lowercased, "this is a test string", "String.LowerCase test"); + + // UTF-8 specific test + String utfString = "FUßGÄNGERÜBERGÄNGE"; + String utfLowercased = utfString.LowerCase(); + tap.is(utfLowercased, "fußgängerübergänge", "String.LowerCase UTF-8 test"); } // String.Replace @@ -79,13 +104,23 @@ void TestString() String original = "Hello from the script!"; String changed = original.Replace("hello", "goodbye"); tap.is(changed, "goodbye from the script!", "String.Replace test"); + + // UTF-8 specific test + String utfOriginal = "Grüße vom München!"; + String utfChanged = utfOriginal.Replace("München", "Berlin"); + tap.is(utfChanged, "Grüße vom Berlin!", "String.Replace UTF-8 test"); } // String.ReplaceCharAt { String mystring = "Hello"; String changed = mystring.ReplaceCharAt(2, 'm'); - tap.is(changed, "Hemlo", "String.ReplaceCharAt test"); + tap.is(changed, "Hemlo", "String.ReplaceCharAt test"); + + // UTF-8 specific test + String utfString = "ĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨ"; + String utfChanged = utfString.ReplaceCharAt(3, 's'); // Replacing 'Ċ' with 's' + tap.is(utfChanged, "ĄĆĈsČĎĐĒĔĖĘĚĜĞĠĢĤĦĨ", "String.ReplaceCharAt UTF-8 test"); } // String.StartsWith @@ -150,6 +185,102 @@ void TestString() String text = "This is my string."; tap.is_int(text.Length, 18, "String.Length test"); } + + // String.Split + { + { + String s = ",ONE,, TWO,, , THREE,,"; + String result[] = s.Split(",", eStrSplit_None); + tap.is_int(result.Length, 9, "String.Split: (1,length) by character delimiter without options"); + tap.is(result[0], "", "String.Split: (1,element check 0) by character delimiter without options"); + tap.is(result[1], "ONE", "String.Split: (1,element check 1) by character delimiter without options"); + tap.is(result[2], "", "String.Split: (1,element check 2) by character delimiter without options"); + tap.is(result[3], " TWO", "String.Split: (1,element check 3) by character delimiter without options"); + tap.is(result[4], "", "String.Split: (1,element check 4) by character delimiter without options"); + tap.is(result[5], " ", "String.Split: (1,element check 5) by character delimiter without options"); + tap.is(result[6], " THREE", "String.Split: (1,element check 6) by character delimiter without options"); + tap.is(result[7], "", "String.Split: (1,element check 7) by character delimiter without options"); + tap.is(result[8], "", "String.Split: (1,element check 8) by character delimiter without options"); + } + { + String s = ",ONE,, TWO,, , THREE,,"; + String result[] = s.Split(",", eStrSplit_Trim); + tap.is_int(result.Length, 9, "String.Split: (2,length) by character delimiter with Trim option"); + tap.is(result[0], "", "String.Split: (2,element check 0) by character delimiter with Trim option"); + tap.is(result[1], "ONE", "String.Split: (2,element check 1) by character delimiter with Trim option"); + tap.is(result[2], "", "String.Split: (2,element check 2) by character delimiter with Trim option"); + tap.is(result[3], "TWO", "String.Split: (2,element check 3) by character delimiter with Trim option"); + tap.is(result[4], "", "String.Split: (2,element check 4) by character delimiter with Trim option"); + tap.is(result[5], "", "String.Split: (2,element check 5) by character delimiter with Trim option"); + tap.is(result[6], "THREE", "String.Split: (2,element check 6) by character delimiter with Trim option"); + tap.is(result[7], "", "String.Split: (2,element check 7) by character delimiter with Trim option"); + tap.is(result[8], "", "String.Split: (2,element check 8) by character delimiter with Trim option"); + } + { + String s = ",ONE,, TWO,, , THREE,,"; + String result[] = s.Split(",", eStrSplit_RemoveEmpty); + tap.is_int(result.Length, 4, "String.Split: (3,length) by character delimiter with RemoveEmpty option"); + tap.is(result[0], "ONE", "String.Split: (3,element check 0) by character delimiter with RemoveEmpty option"); + tap.is(result[1], " TWO", "String.Split: (3,element check 1) by character delimiter with RemoveEmpty option"); + tap.is(result[2], " ", "String.Split: (3,element check 2) by character delimiter with RemoveEmpty option"); + tap.is(result[3], " THREE", "String.Split: (3,element check 3) by character delimiter with RemoveEmpty option"); + } + { + String s = ",ONE,, TWO,, , THREE ,,"; + String result[] = s.Split(",", eStrSplit_Trim | eStrSplit_RemoveEmpty); + tap.is_int(result.Length, 3, "String.Split: (4,length) by character delimiter with Trim and RemoveEmpty options"); + tap.is(result[0], "ONE", "String.Split: (4,element check 0) by character delimiter with Trim and RemoveEmpty options"); + tap.is(result[1], "TWO", "String.Split: (4,element check 1) by character delimiter with Trim and RemoveEmpty options"); + tap.is(result[2], "THREE", "String.Split: (4,element check 2) by character delimiter with Trim and RemoveEmpty options"); + } + { + String s = "[stop]ONE[stop] [stop]TWO [stop][stop] [stop]THREE[stop][stop] "; + String result[] = s.Split("[stop]", eStrSplit_None); + tap.is_int(result.Length, 9, "String.Split: (5,length) by string delimiter without options"); + tap.is(result[0], "", "String.Split: (5,element check 0) by string delimiter without options"); + tap.is(result[1], "ONE", "String.Split: (5,element check 1) by string delimiter without options"); + tap.is(result[2], " ", "String.Split: (5,element check 2) by string delimiter without options"); + tap.is(result[3], "TWO ", "String.Split: (5,element check 3) by string delimiter without options"); + tap.is(result[4], "", "String.Split: (5,element check 4) by string delimiter without options"); + tap.is(result[5], " ", "String.Split: (5,element check 5) by string delimiter without options"); + tap.is(result[6], "THREE", "String.Split: (5,element check 6) by string delimiter without options"); + tap.is(result[7], "", "String.Split: (5,element check 7) by string delimiter without options"); + tap.is(result[8], " ", "String.Split: (5,element check 8) by string delimiter without options"); + } + { + String s = "[stop]ONE[stop] [stop]TWO [stop][stop] [stop]THREE[stop][stop] "; + String result[] = s.Split("[stop]", eStrSplit_Trim); + tap.is_int(result.Length, 9, "String.Split: (6,length) by string delimiter with Trim option"); + tap.is(result[0], "", "String.Split: (6,element check 0) by string delimiter with Trim option"); + tap.is(result[1], "ONE", "String.Split: (6,element check 1) by string delimiter with Trim option"); + tap.is(result[2], "", "String.Split: (6,element check 2) by string delimiter with Trim option"); + tap.is(result[3], "TWO", "String.Split: (6,element check 3) by string delimiter with Trim option"); + tap.is(result[4], "", "String.Split: (6,element check 4) by string delimiter with Trim option"); + tap.is(result[5], "", "String.Split: (6,element check 5) by string delimiter with Trim option"); + tap.is(result[6], "THREE", "String.Split: (6,element check 6) by string delimiter with Trim option"); + tap.is(result[7], "", "String.Split: (6,element check 7) by string delimiter with Trim option"); + tap.is(result[8], "", "String.Split: (6,element check 8) by string delimiter with Trim option"); + } + { + String s = "[stop]ONE[stop] [stop]TWO [stop][stop] [stop]THREE[stop][stop] "; + String result[] = s.Split("[stop]", eStrSplit_RemoveEmpty); + tap.is_int(result.Length, 6, "String.Split: (7,length) by string delimiter with RemoveEmpty option"); + tap.is(result[0], "ONE", "String.Split: (7,element check 0) by string delimiter with RemoveEmpty option"); + tap.is(result[1], " ", "String.Split: (7,element check 1) by string delimiter with RemoveEmpty option"); + tap.is(result[2], "TWO ", "String.Split: (7,element check 2) by string delimiter with RemoveEmpty option"); + tap.is(result[3], " ", "String.Split: (7,element check 3) by string delimiter with RemoveEmpty option"); + tap.is(result[4], "THREE", "String.Split: (7,element check 4) by string delimiter with RemoveEmpty option"); + tap.is(result[5], " ", "String.Split: (7,element check 5) by string delimiter with RemoveEmpty option"); + } + { + String s = "[stop]ONE[stop] [stop]TWO [stop][stop] [stop]THREE[stop][stop] "; + String result[] = s.Split("[stop]", eStrSplit_Trim | eStrSplit_RemoveEmpty); + tap.is_int(result.Length, 3, "String.Split: (8,length) by string delimiter with Trim and RemoveEmpty options"); + tap.is(result[0], "ONE", "String.Split: (8,element check 0) by string delimiter with Trim and RemoveEmpty options"); + tap.is(result[1], "TWO", "String.Split: (8,element check 1) by string delimiter with Trim and RemoveEmpty options"); + tap.is(result[2], "THREE", "String.Split: (8,element check 2) by string delimiter with Trim and RemoveEmpty options"); + } + } tap.Comment("end String tests"); } \ No newline at end of file