Browse Source
* feature: Add AvaloniaNameGeneratorFilterByPath option * Extract common interface, introduce pattern groupspull/10407/head
committed by
GitHub
9 changed files with 103 additions and 3 deletions
@ -0,0 +1,32 @@ |
|||||
|
using Avalonia.NameGenerator.Generator; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.NameGenerator.Tests |
||||
|
{ |
||||
|
public class GlobPatternTests |
||||
|
{ |
||||
|
[Theory] |
||||
|
[InlineData("*", "anything", true)] |
||||
|
[InlineData("", "anything", false)] |
||||
|
[InlineData("Views/*", "Views/SignUpView.xaml", true)] |
||||
|
[InlineData("Views/*", "Extensions/SignUpView.xaml", false)] |
||||
|
[InlineData("*SignUpView*", "Extensions/SignUpView.xaml", true)] |
||||
|
[InlineData("*SignUpView.paml", "Extensions/SignUpView.xaml", false)] |
||||
|
[InlineData("*.xaml", "Extensions/SignUpView.xaml", true)] |
||||
|
public void Should_Match_Glob_Expressions(string pattern, string value, bool matches) |
||||
|
{ |
||||
|
Assert.Equal(matches, new GlobPattern(pattern).Matches(value)); |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("Views/SignUpView.xaml", true, new[] { "*.xaml", "Views/*" })] |
||||
|
[InlineData("Views/SignUpView.paml", false, new[] { "*.xaml", "Views/*" })] |
||||
|
[InlineData("Extensions/SignUpView.paml", false, new[] { "*.xaml", "Views/*" })] |
||||
|
[InlineData("anything", true, new[] { "*", "*" })] |
||||
|
[InlineData("anything", false, new[] { "", "" })] |
||||
|
public void Should_Match_Glob_Pattern_Groups(string value, bool matches, string[] patterns) |
||||
|
{ |
||||
|
Assert.Equal(matches, new GlobPatternGroup(patterns).Matches(value)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Avalonia.NameGenerator.Domain |
||||
|
{ |
||||
|
public interface IGlobPattern |
||||
|
{ |
||||
|
bool Matches(string str); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
using System.Text.RegularExpressions; |
||||
|
using Avalonia.NameGenerator.Domain; |
||||
|
|
||||
|
namespace Avalonia.NameGenerator.Generator |
||||
|
{ |
||||
|
public class GlobPattern : IGlobPattern |
||||
|
{ |
||||
|
private const RegexOptions GlobOptions = RegexOptions.IgnoreCase | RegexOptions.Singleline; |
||||
|
private readonly Regex _regex; |
||||
|
|
||||
|
public GlobPattern(string pattern) |
||||
|
{ |
||||
|
var expression = "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$"; |
||||
|
_regex = new Regex(expression, GlobOptions); |
||||
|
} |
||||
|
|
||||
|
public bool Matches(string str) => _regex.IsMatch(str); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Avalonia.NameGenerator.Domain; |
||||
|
|
||||
|
namespace Avalonia.NameGenerator.Generator |
||||
|
{ |
||||
|
public class GlobPatternGroup : IGlobPattern |
||||
|
{ |
||||
|
private readonly GlobPattern[] _patterns; |
||||
|
|
||||
|
public GlobPatternGroup(IEnumerable<string> patterns) => |
||||
|
_patterns = patterns |
||||
|
.Select(pattern => new GlobPattern(pattern)) |
||||
|
.ToArray(); |
||||
|
|
||||
|
public bool Matches(string str) => _patterns.All(pattern => pattern.Matches(str)); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue