Skip to content

Commit

Permalink
add - Added some tests
Browse files Browse the repository at this point in the history
---

We've added unit tests for a new string extension, GetIndexOfEnclosedWordFromIndex().

---

Type: add
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 9, 2024
1 parent 7e8a16d commit 1aa407b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
28 changes: 28 additions & 0 deletions Textify.Tests/General/TextToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,34 @@ public void TestUnescape(string text, string expected)
string actual = text.Unescape();
actual.ShouldBe(expected);
}

/// <summary>
/// Tests getting the index of the enclosed word from index that represents a start of a substring (without symbols)
/// </summary>
[TestMethod]
[DataRow("", 0, -1)]
[DataRow("!Hello world!", 2, 1)]
[DataRow("Hello world!", 8, 6)]
[Description("Querying")]
public void TestGetIndexOfEnclosedWordFromIndex(string text, int idx, int expected)
{
int actual = text.GetIndexOfEnclosedWordFromIndex(idx);
actual.ShouldBe(expected);
}

/// <summary>
/// Tests getting the index of the enclosed word from index that represents a start of a substring (with symbols)
/// </summary>
[TestMethod]
[DataRow("", 0, -1)]
[DataRow("!Hello world!", 2, 0)]
[DataRow("Hello world!", 8, 6)]
[Description("Querying")]
public void TestGetIndexOfEnclosedWordFromIndexSymbols(string text, int idx, int expected)
{
int actual = text.GetIndexOfEnclosedWordFromIndex(idx, true);
actual.ShouldBe(expected);
}
}

}
24 changes: 11 additions & 13 deletions Textify/General/TextTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,16 +786,10 @@ public static string GetEnclosedWordFromIndex(this string target, int sourceIdx,
throw new TextifyException("The target may not be null");

// Calculate the distance between two word spaces
int distance = 0, idx;
for (idx = sourceIdx; idx > 0; idx--)
{
var character = target[idx];
if ((!includeSymbols && !char.IsLetterOrDigit(character)) || (includeSymbols && char.IsWhiteSpace(character)))
{
idx++;
break;
}
}
int distance = 0;
int idx = GetIndexOfEnclosedWordFromIndex(target, sourceIdx, includeSymbols);
if (idx < 0)
return "";
for (int distanceIdx = idx; distanceIdx < target.Length; distanceIdx++)
{
var character = target[distanceIdx];
Expand Down Expand Up @@ -838,20 +832,22 @@ public static string Unescape(this string target)
}

/// <summary>
/// Gets enclosed word from index that represents a start of a substring
/// Gets the index of the enclosed word from index that represents a start of a substring
/// </summary>
/// <param name="target">Target string that contains a substring</param>
/// <param name="sourceIdx">Target string index that usually starts a substring</param>
/// <param name="includeSymbols">Whether to include symbols, such as punctuation, in the search or not</param>
/// <returns>The enclosed word from the specified index</returns>
/// <returns>The index of the enclosed enclosed word from the specified index, or -1 if the string is empty</returns>
public static int GetIndexOfEnclosedWordFromIndex(this string target, int sourceIdx, bool includeSymbols = false)
{
if (target is null)
throw new TextifyException("The target may not be null");
if (string.IsNullOrEmpty(target))
return -1;

// Calculate the word begin index
int idx;
for (idx = sourceIdx; idx > 0; idx--)
for (idx = sourceIdx; idx >= 0; idx--)
{
var character = target[idx];
if ((!includeSymbols && !char.IsLetterOrDigit(character)) || (includeSymbols && char.IsWhiteSpace(character)))
Expand All @@ -860,6 +856,8 @@ public static int GetIndexOfEnclosedWordFromIndex(this string target, int source
break;
}
}
if (idx < 0)
idx = 0;
return idx;
}
}
Expand Down

0 comments on commit 1aa407b

Please sign in to comment.