Skip to content

Commit

Permalink
Merge pull request #31 from PromoFaux/stringsplitter-exceptions
Browse files Browse the repository at this point in the history
Throw exceptions when invalid params passed to StringSplitter
  • Loading branch information
PromoFaux authored Oct 17, 2021
2 parents 21905dc + ce8475d commit 6857524
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
6 changes: 6 additions & 0 deletions ManualTests/ManualTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
<ProjectReference Include="..\Matterhook.NET.MatterhookClient\Matterhook.NET.MatterhookClient.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions Matterhook.NET.MatterhookClient.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using Xunit;

namespace Matterhook.NET.MatterhookClient.Tests
{
public class MiscTests
{

[Fact]
public void StringSplitterThrowsExceptionWhenNullStringPassed()
{
Assert.Throws<ArgumentException>(() => StringSplitter.SplitTextIntoChunks(null, 250, false));
}

[Fact]
public void StringSplitterThrowsExceptionWhenChunkSizeOfLessThan1()
{
Assert.Throws<ArgumentException>(() => StringSplitter.SplitTextIntoChunks("A message", 0, false));
}

}
}
2 changes: 1 addition & 1 deletion Matterhook.NET.MatterhookClient/MatterhookClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task<HttpResponseMessage> PostAsync(MattermostMessage inMessage, in

foreach (var msg in outMessages)
{
var msgJson = msg.SerializeToJson();
var msgJson = msg.SerializeToJson();
response = await _httpClient.PostAsync(_webhookUrl,
new StringContent(msgJson, Encoding.UTF8, "application/json")).ConfigureAwait(false);
}
Expand Down
1 change: 0 additions & 1 deletion Matterhook.NET.MatterhookClient/MattermostMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ internal MattermostMessage Clone()
};
}


public string SerializeToJson()
{
return JsonConvert.SerializeObject(this, new MattermostJsonSerializerSettings());
Expand Down
44 changes: 17 additions & 27 deletions Matterhook.NET.MatterhookClient/StringSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,46 @@ public static IEnumerable<string> SplitTextIntoChunks(string str, int maxChunkSi
{
if (string.IsNullOrEmpty(str)) throw new ArgumentException("Text can't be null or empty.", nameof(str));
if (maxChunkSize < 1) throw new ArgumentException("Max. chunk size must be at least 1 char.", nameof(maxChunkSize));

return SplitTextIntoChunks2();

IEnumerable<string> SplitTextIntoChunks2()
if (str.Length < maxChunkSize) return new List<string> { str };
if (preserveWords)
{
if (str.Length < maxChunkSize)
{
yield return str;
}
else if (preserveWords)
{
//Less Simple
foreach (var chunk in SplitTextBySizePreservingWords(str, maxChunkSize))
yield return chunk;
}
else
{
//Simple
foreach (var chunk in SplitTextBySize(str, maxChunkSize))
yield return chunk;
}
return SplitTextBySizePreservingWords(str, maxChunkSize);
}
else
{
return SplitTextBySize(str, maxChunkSize);
}
}

private static IEnumerable<string> SplitTextBySize(string str, int maxChunkSize)
{
if (str.Length < maxChunkSize) yield return str;
if (str.Length < maxChunkSize) return new List<string> { str };
var list = new List<string>();
for (var i = 0; i < str.Length; i += maxChunkSize)
{
yield return str.Substring(i, Math.Min(maxChunkSize, str.Length - i));
list.Add(str.Substring(i, Math.Min(maxChunkSize, str.Length - i)));
}
return list;
}

private static IEnumerable<string> SplitTextBySizePreservingWords(string str, int maxChunkSize)
{
if (str.Length < maxChunkSize) yield return str;
if (str.Length < maxChunkSize) return new List<string> { str };
var words = str.Split(' ');
var tempString = new StringBuilder("");

var list = new List<string>();
foreach (var word in words)
{
if (word.Length + tempString.Length + 1 > maxChunkSize)
{
yield return tempString.ToString();
list.Add(tempString.ToString());
tempString.Clear();
}

tempString.Append(tempString.Length > 0 ? " " + word : word);
}
yield return tempString.ToString();
if (tempString.Length >= 1)
list.Add(tempString.ToString());
return list;
}
}
}

0 comments on commit 6857524

Please sign in to comment.