Skip to content

Commit

Permalink
# 664 Handle Trailing Whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Sampson committed Jun 4, 2020
1 parent 84edd18 commit 69d5a09
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
56 changes: 56 additions & 0 deletions src/Caliburn.Micro.Platform.Tests/StringSplitterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Xunit;

namespace Caliburn.Micro.Platform.Tests
{
public class StringSplitterTests
{
[Fact]
public void SplitSimpleString()
{
var output = StringSplitter.Split("MyMethodName", ';');

Assert.Collection(output, o => Assert.Equal("MyMethodName", o));
}

[Fact]
public void SplitSeparatedString()
{
var output = StringSplitter.Split("First;Second", ';');

Assert.Collection(output,
o => Assert.Equal("First", o),
o => Assert.Equal("Second", o));
}

[Fact]
public void TrimsSplitSimpleString()
{
var output = StringSplitter.Split("MyMethodName ", ';');

Assert.Collection(output, o => Assert.Equal("MyMethodName", o));
}

[Fact]
public void RemovesEmptySplitsSeparatedString()
{
var output = StringSplitter.Split("First;Second;", ';');

Assert.Collection(output,
o => Assert.Equal("First", o),
o => Assert.Equal("Second", o));
}

[Fact]
public void HandlesNewLinesInSeparatedString()
{
var output = StringSplitter.Split(@"
First;
Second;
", ';');

Assert.Collection(output,
o => Assert.Equal("First", o),
o => Assert.Equal("Second", o));
}
}
}
11 changes: 7 additions & 4 deletions src/Caliburn.Micro.Platform/StringSplitter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Caliburn.Micro
Expand Down Expand Up @@ -45,7 +46,7 @@ public static string[] Split(string message, char separator)
str = builder.ToString();
if (!string.IsNullOrEmpty(str))
{
list.Add(builder.ToString());
list.Add(builder.ToString().Trim());
}

builder.Length = 0;
Expand All @@ -59,10 +60,12 @@ public static string[] Split(string message, char separator)
str = builder.ToString();
if (!string.IsNullOrEmpty(str))
{
list.Add(builder.ToString());
list.Add(builder.ToString().Trim());
}

return list.ToArray();
return list
.Where(s => !string.IsNullOrEmpty(s))
.ToArray();
}

/// <summary>
Expand Down Expand Up @@ -140,4 +143,4 @@ public static string[] SplitParameters(string parameters)
return list.ToArray();
}
}
}
}

0 comments on commit 69d5a09

Please sign in to comment.