Skip to content

Commit

Permalink
Fixes #21
Browse files Browse the repository at this point in the history
Issue with glob pattern not matching when single wildcard used in a segment, and followed by additional segments.
Also increased test coverage so that Match and IsMatch are inlign.
  • Loading branch information
dazinator committed Mar 6, 2017
1 parent 289b206 commit 6e709cf
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/DotNet.Glob.Benchmarks/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"dependencies": {
"NETStandard.Library": "1.6.1",
"DotNet.Glob": "1.0.0-*",
"Glob": "0.3.1",
"BenchmarkDotNet": "0.10.1"
"Glob": "0.3.2",
"BenchmarkDotNet": "0.10.3"
},
"frameworks": {
"netcoreapp1.1": {
Expand Down
74 changes: 29 additions & 45 deletions src/DotNet.Glob.Tests/GlobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,20 @@ public void Does_Not_Match(string pattern, params string[] testStrings)
[InlineData("/**/file.*", "/file.txt")]
[InlineData("**/file.*", "/file.txt")]
[InlineData("/*file.txt", "/file.txt")]
public void Can_IsMatch(string pattern, params string[] testStrings)
[InlineData("C:\\THIS_IS_A_DIR\\*", "C:\\THIS_IS_A_DIR\\somefile")] // Regression Test for https://github.com/dazinator/DotNet.Glob/issues/20
[InlineData("/DIR1/*/*", "/DIR1/DIR2/file.txt")] // Regression Test for https://github.com/dazinator/DotNet.Glob/issues/21
[InlineData("~/*~3", "~/abc123~3")] // Regression Test for https://github.com/dazinator/DotNet.Glob/pull/15
public void IsMatch(string pattern, params string[] testStrings)
{
var glob = Glob.Parse(pattern);
foreach (var testString in testStrings)
{
var match = glob.IsMatch(testString);
Assert.True(match);
Assert.True(match);

}
}

[Fact]
public void To_String_Returns_Pattern()
{
var pattern = "p?th/*a[bcd]b[e-g]/**/a[1-4][!wxyz][!a-c][!1-3].*";
var glob = Glob.Parse(pattern);
var resultPattern = glob.ToString();
Assert.Equal(pattern, resultPattern);
}


[Theory]
[InlineData("literal", "literal")]
[InlineData("a/literal", "a/literal")]
Expand All @@ -78,47 +73,36 @@ public void To_String_Returns_Pattern()
[InlineData("path/**/somefile.txt", "path/foo/bar/baz/somefile.txt")]
[InlineData("p?th/*a[bcd]b[e-g]a[1-4][!wxyz][!a-c][!1-3].*", "pGth/yGKNY6acbea3rm8.")]
[InlineData("/**/file.*", "/folder/file.csv")]
//[InlineData("/**/file.*", "/file.txt")]
//[InlineData("**/file.*", "/file.txt")]
//[InlineData("/**/file.*", "/file.txt")]
//[InlineData("/**/f~le.*", "/f~le.txt")]
public void Glob_IsMatch(string pattern, params string[] testStrings)
[InlineData("/**/file.*", "/file.txt")]
[InlineData("/**/file.*", "/file.txt")]
[InlineData("**/file.*", "/file.txt")]
[InlineData("/*file.txt", "/file.txt")]
[InlineData("C:\\THIS_IS_A_DIR\\*", "C:\\THIS_IS_A_DIR\\somefile")] // Regression Test for https://github.com/dazinator/DotNet.Glob/issues/20
[InlineData("/DIR1/*/*", "/DIR1/DIR2/file.txt")] // Regression Test for https://github.com/dazinator/DotNet.Glob/issues/21
[InlineData("~/*~3", "~/abc123~3")] // Regression Test for https://github.com/dazinator/DotNet.Glob/pull/15
public void Match(string pattern, params string[] testStrings)
{
// This is a different glob library, I am seeing if it matches the same patterns as my library.
// The tests above commented out show it has some limitations, that I have addressed in this library.
var glob = new global::Glob.Glob(pattern);
var glob = Glob.Parse(pattern);
foreach (var testString in testStrings)
{
var match = glob.IsMatch(testString);
Assert.True(match);
var match = glob.Match(testString);
Assert.True(match.Success);
}
}


/// <summary>
/// Regression Test for https://github.com/dazinator/DotNet.Glob/pull/15
/// </summary>
[Fact]
public void BugFix_Can_Read_Tilde()
public void To_String_Returns_Pattern()
{
// This is a different glob library, I am seeing if it matches the same patterns as my library.
// The three tests above commented out show it currently has some limitations, that this library doesn't.
var glob = Glob.Parse("~/*~3");
var isMatch = glob.IsMatch("~/abc123~3");
Assert.True(isMatch);
}
var pattern = "p?th/*a[bcd]b[e-g]/**/a[1-4][!wxyz][!a-c][!1-3].*";
var glob = Glob.Parse(pattern);
var resultPattern = glob.ToString();
Assert.Equal(pattern, resultPattern);
}

/// <summary>
/// Regression Test for https://github.com/dazinator/DotNet.Glob/pull/15
/// </summary>
[Fact]
public void BugFix_Issue_20_Can_Read_Colon_And_Underscore()
{
// This is a different glob library, I am seeing if it matches the same patterns as my library.
// The three tests above commented out show it currently has some limitations, that this library doesn't.
var glob = Glob.Parse("C:\\THIS_IS_A_DIR\\*");
var isMatch = glob.IsMatch("C:\\THIS_IS_A_DIR\\somefile");
Assert.True(isMatch);
}





}
}
42 changes: 42 additions & 0 deletions src/DotNet.Glob.Tests/OtherGlobLibraryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

namespace DotNet.Globbing.Tests
{
public class OtherGlobLibraryTests
{

[Theory]
[InlineData("literal", "literal")]
[InlineData("a/literal", "a/literal")]
[InlineData("path/*atstand", "path/fooatstand")]
[InlineData("path/hats*nd", "path/hatsforstand")]
[InlineData("path/?atstand", "path/hatstand")]
[InlineData("path/?atstand?", "path/hatstands")]
[InlineData("p?th/*a[bcd]", "pAth/fooooac")]
[InlineData("p?th/*a[bcd]b[e-g]a[1-4]", "pAth/fooooacbfa2")]
[InlineData("p?th/*a[bcd]b[e-g]a[1-4][!wxyz]", "pAth/fooooacbfa2v")]
[InlineData("p?th/*a[bcd]b[e-g]a[1-4][!wxyz][!a-c][!1-3].*", "pAth/fooooacbfa2vd4.txt")]
[InlineData("path/**/somefile.txt", "path/foo/bar/baz/somefile.txt")]
[InlineData("p?th/*a[bcd]b[e-g]a[1-4][!wxyz][!a-c][!1-3].*", "pGth/yGKNY6acbea3rm8.")]
[InlineData("/**/file.*", "/folder/file.csv")]
//[InlineData("/**/file.*", "/file.txt")]
//[InlineData("**/file.*", "/file.txt")]
//[InlineData("/**/file.*", "/file.txt")]
//[InlineData("/**/f~le.*", "/f~le.txt")]
public void Glob_IsMatch(string pattern, params string[] testStrings)
{
// This is a different glob library, I am seeing if it matches the same patterns as my library.
// The tests above commented out show it has some limitations, that I have addressed in this library.
var glob = new global::Glob.Glob(pattern);
foreach (var testString in testStrings)
{
var match = glob.IsMatch(testString);
Assert.True(match);
}
}
}
}
4 changes: 2 additions & 2 deletions src/DotNet.Glob.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"dependencies": {
"NETStandard.Library": "1.6.0",
"dotnet-test-xunit": "2.2.0-*",
"xunit": "2.2.0-beta4-build3444",
"xunit": "2.2.0",
"DotNet.Glob": "1.0.0-*",
"Glob": "0.3.1"
"Glob": "0.3.2"
},
"frameworks": {
"netcoreapp1.0": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void Visit(WildcardToken token)
var bestMatchText = new StringBuilder(endOfSegmentPos);
IList<GlobTokenMatch> bestMatches = null; // the most tokens that were matched.

for (int i = 0; i < endOfSegmentPos; i++)
for (int i = 0; i <= endOfSegmentPos; i++)
{
var matchInfo = nestedEval.Evaluate(remainingText);
if (matchInfo.Success)
Expand Down
10 changes: 6 additions & 4 deletions src/DotNet.Glob/Evaluation/WildcardTokenEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ public bool IsMatch(string allChars, int currentPosition, out int newPosition)
for (int i = currentPosition; i <= maxPos; i++)
{
var currentChar = allChars[i];
if (currentChar == '/' || currentChar == '\\')
{
return false;
}



//int newSubPosition;
Expand All @@ -92,6 +89,11 @@ public bool IsMatch(string allChars, int currentPosition, out int newPosition)
{
return true;
}

if (currentChar == '/' || currentChar == '\\')
{
return false;
}
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/DotNet.Glob/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"net46": {
"dependencies": {
"System.IO": "4.0.10.0",
"System.IO": "4.0.10",
"System.Runtime.Extensions": "4.3.0"
}
}
Expand Down

0 comments on commit 6e709cf

Please sign in to comment.