-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support escaping , in Test filter (#1374)
* Support for Escaping test cases with comma
- Loading branch information
Nitin Gurram
authored
Jan 17, 2018
1 parent
25fa1b6
commit 46f1cae
Showing
4 changed files
with
318 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.VisualStudio.TestPlatform.Utilities | ||
{ | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
public static class StringExtensions | ||
{ | ||
public static IEnumerable<string> Tokenize(this string input, char separator, char escape) | ||
{ | ||
if (string.IsNullOrEmpty(input)) yield break; | ||
|
||
var buffer = new StringBuilder(); | ||
var escaping = false; | ||
foreach (var c in input) | ||
{ | ||
if (escaping) | ||
{ | ||
buffer.Append(c); | ||
escaping = false; | ||
} | ||
else if (c == escape) | ||
{ | ||
escaping = true; | ||
} | ||
else if (c == separator) | ||
{ | ||
yield return buffer.Flush(); | ||
} | ||
else | ||
{ | ||
buffer.Append(c); | ||
} | ||
} | ||
if (buffer.Length > 0 || input[input.Length - 1] == separator) yield return buffer.Flush(); | ||
} | ||
|
||
private static string Flush(this StringBuilder stringBuilder) | ||
{ | ||
var result = stringBuilder.ToString(); | ||
stringBuilder.Clear(); | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
test/Microsoft.TestPlatform.Utilities.UnitTests/StringUtilitiesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Linq; | ||
|
||
namespace Microsoft.TestPlatform.Utilities.UnitTests | ||
{ | ||
using Castle.Core.Internal; | ||
using Microsoft.VisualStudio.TestPlatform.Utilities; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class StringUtilitiesTests | ||
{ | ||
[TestMethod] | ||
public void SplitShouldReturnWhenStringisNullOrEmpty() | ||
{ | ||
var argsList = string.Empty.Tokenize(SplitChar, EscapeChar); | ||
|
||
Assert.IsTrue(argsList.IsNullOrEmpty()); | ||
} | ||
|
||
[TestMethod] | ||
public void SplitShouldReturnWhenStringDoesntContainSplitChar() | ||
{ | ||
var data = "foobar"; | ||
var argsList = data.Tokenize(SplitChar, EscapeChar); | ||
var enumerable = argsList as string[] ?? argsList.ToArray(); | ||
|
||
Assert.IsTrue(enumerable.Length == 1); | ||
Assert.IsTrue(enumerable.First().Equals(data)); | ||
} | ||
|
||
[TestMethod] | ||
public void SplitShouldSplitWhenStringContainsSplitChar() | ||
{ | ||
var data = "foo,bar"; | ||
var argsList = data.Tokenize(SplitChar, EscapeChar); | ||
var enumerable = argsList as string[] ?? argsList.ToArray(); | ||
|
||
Assert.IsTrue(enumerable.Length == 2); | ||
} | ||
|
||
[TestMethod] | ||
public void SplitShouldSplitWhenStringWithSplitCharStartEnd() | ||
{ | ||
var data = ",foo,bar,"; | ||
var argsList = data.Tokenize(SplitChar, EscapeChar); | ||
var enumerable = argsList as string[] ?? argsList.ToArray(); | ||
|
||
Assert.IsTrue(enumerable.Length == 4); | ||
} | ||
|
||
[TestMethod] | ||
public void SplitShouldEscapeSplitCharWhenEscapedCharPresent() | ||
{ | ||
var data = "foo\\,bar"; | ||
var argsList = data.Tokenize(SplitChar, EscapeChar); | ||
var enumerable = argsList as string[] ?? argsList.ToArray(); | ||
|
||
Assert.IsTrue(enumerable.Length == 1); | ||
Assert.IsTrue(enumerable.First().Equals("foo,bar")); | ||
} | ||
|
||
[TestMethod] | ||
public void SplitShouldEscapeSplitCharWhenEscapedNonEscapedCharPresent() | ||
{ | ||
var data = "foo\\,,bar"; | ||
var argsList = data.Tokenize(SplitChar, EscapeChar); | ||
var enumerable = argsList as string[] ?? argsList.ToArray(); | ||
Assert.IsTrue(enumerable.Length == 2); | ||
Assert.IsTrue(enumerable.First().Equals("foo,")); | ||
} | ||
|
||
[TestMethod] | ||
public void SplitShouldSplitWhenOnlySplitCharPresent() | ||
{ | ||
var data = ","; | ||
var argsList = data.Tokenize(SplitChar, EscapeChar); | ||
var enumerable = argsList as string[] ?? argsList.ToArray(); | ||
|
||
Assert.IsTrue(enumerable.Length == 2); | ||
} | ||
|
||
[TestMethod] | ||
public void SplitShouldNotSplitWhenNoSplitCharPresent() | ||
{ | ||
var data = "foo\\bar"; | ||
var argsList = data.Tokenize(SplitChar, EscapeChar); | ||
var enumerable = argsList as string[] ?? argsList.ToArray(); | ||
|
||
Assert.IsTrue(enumerable.Length == 1); | ||
} | ||
|
||
private const char SplitChar = ','; | ||
private const char EscapeChar = '\\'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters