Skip to content

Commit 6857524

Browse files
authored
Merge pull request #31 from PromoFaux/stringsplitter-exceptions
Throw exceptions when invalid params passed to StringSplitter
2 parents 21905dc + ce8475d commit 6857524

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

ManualTests/ManualTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
<ProjectReference Include="..\Matterhook.NET.MatterhookClient\Matterhook.NET.MatterhookClient.csproj" />
1010
</ItemGroup>
1111

12+
<ItemGroup>
13+
<None Update="config.json">
14+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
15+
</None>
16+
</ItemGroup>
17+
1218
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Xunit;
5+
6+
namespace Matterhook.NET.MatterhookClient.Tests
7+
{
8+
public class MiscTests
9+
{
10+
11+
[Fact]
12+
public void StringSplitterThrowsExceptionWhenNullStringPassed()
13+
{
14+
Assert.Throws<ArgumentException>(() => StringSplitter.SplitTextIntoChunks(null, 250, false));
15+
}
16+
17+
[Fact]
18+
public void StringSplitterThrowsExceptionWhenChunkSizeOfLessThan1()
19+
{
20+
Assert.Throws<ArgumentException>(() => StringSplitter.SplitTextIntoChunks("A message", 0, false));
21+
}
22+
23+
}
24+
}

Matterhook.NET.MatterhookClient/MatterhookClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task<HttpResponseMessage> PostAsync(MattermostMessage inMessage, in
108108

109109
foreach (var msg in outMessages)
110110
{
111-
var msgJson = msg.SerializeToJson();
111+
var msgJson = msg.SerializeToJson();
112112
response = await _httpClient.PostAsync(_webhookUrl,
113113
new StringContent(msgJson, Encoding.UTF8, "application/json")).ConfigureAwait(false);
114114
}

Matterhook.NET.MatterhookClient/MattermostMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ internal MattermostMessage Clone()
5151
};
5252
}
5353

54-
5554
public string SerializeToJson()
5655
{
5756
return JsonConvert.SerializeObject(this, new MattermostJsonSerializerSettings());

Matterhook.NET.MatterhookClient/StringSplitter.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,46 @@ public static IEnumerable<string> SplitTextIntoChunks(string str, int maxChunkSi
2020
{
2121
if (string.IsNullOrEmpty(str)) throw new ArgumentException("Text can't be null or empty.", nameof(str));
2222
if (maxChunkSize < 1) throw new ArgumentException("Max. chunk size must be at least 1 char.", nameof(maxChunkSize));
23-
24-
return SplitTextIntoChunks2();
25-
26-
IEnumerable<string> SplitTextIntoChunks2()
23+
if (str.Length < maxChunkSize) return new List<string> { str };
24+
if (preserveWords)
2725
{
28-
if (str.Length < maxChunkSize)
29-
{
30-
yield return str;
31-
}
32-
else if (preserveWords)
33-
{
34-
//Less Simple
35-
foreach (var chunk in SplitTextBySizePreservingWords(str, maxChunkSize))
36-
yield return chunk;
37-
}
38-
else
39-
{
40-
//Simple
41-
foreach (var chunk in SplitTextBySize(str, maxChunkSize))
42-
yield return chunk;
43-
}
26+
return SplitTextBySizePreservingWords(str, maxChunkSize);
27+
}
28+
else
29+
{
30+
return SplitTextBySize(str, maxChunkSize);
4431
}
4532
}
4633

4734
private static IEnumerable<string> SplitTextBySize(string str, int maxChunkSize)
4835
{
49-
if (str.Length < maxChunkSize) yield return str;
36+
if (str.Length < maxChunkSize) return new List<string> { str };
37+
var list = new List<string>();
5038
for (var i = 0; i < str.Length; i += maxChunkSize)
5139
{
52-
yield return str.Substring(i, Math.Min(maxChunkSize, str.Length - i));
40+
list.Add(str.Substring(i, Math.Min(maxChunkSize, str.Length - i)));
5341
}
42+
return list;
5443
}
5544

5645
private static IEnumerable<string> SplitTextBySizePreservingWords(string str, int maxChunkSize)
5746
{
58-
if (str.Length < maxChunkSize) yield return str;
47+
if (str.Length < maxChunkSize) return new List<string> { str };
5948
var words = str.Split(' ');
6049
var tempString = new StringBuilder("");
61-
50+
var list = new List<string>();
6251
foreach (var word in words)
6352
{
6453
if (word.Length + tempString.Length + 1 > maxChunkSize)
6554
{
66-
yield return tempString.ToString();
55+
list.Add(tempString.ToString());
6756
tempString.Clear();
6857
}
69-
7058
tempString.Append(tempString.Length > 0 ? " " + word : word);
7159
}
72-
yield return tempString.ToString();
60+
if (tempString.Length >= 1)
61+
list.Add(tempString.ToString());
62+
return list;
7363
}
7464
}
7565
}

0 commit comments

Comments
 (0)