コード例 #1
0
        public void EarlierRulesTakePrecedence_ExcludeBeforeInclude()
        {
            var hierarchy = new TestHierarchy(
                new SimpleMatchable("root",
                                    new SimpleMatchable("dev",
                                                        new SimpleMatchable("hda"),
                                                        new SimpleMatchable("hda1"),
                                                        new SimpleMatchable("hdb"),
                                                        new SimpleMatchable("hdb1"),
                                                        new SimpleMatchable("hdb2")),
                                    new SimpleMatchable("home",
                                                        new SimpleMatchable("me",
                                                                            new SimpleMatchable("hdmi")),
                                                        new SimpleMatchable("you")),
                                    new SimpleMatchable("var")));

            var parser = new GlobParser();

            var enumerator = new MultiGlobMatchEnumerator().Include(parser.Parse("**"));
            var filter     = new MultiGlobMatchFilter(hierarchy.CaseSensitive)
                             .Exclude(parser.Parse("dev/**"))
                             .Include(parser.Parse("**/hd[am]?"));

            var matches = enumerator.EnumerateMatches(hierarchy)
                          .Where(filter.Filter)
                          .ToArray();

            Assert.That(matches.Select(m => m.Item.Name).ToArray(),
                        Is.EquivalentTo(new []
            {
                "hdmi",
            }));
        }
コード例 #2
0
        public void FindsAllUniqueMatchesOfIncludesNotMatchedByExcludes()
        {
            var hierarchy = new TestHierarchy(
                new SimpleMatchable("root",
                                    new SimpleMatchable("dev",
                                                        new SimpleMatchable("hda"),
                                                        new SimpleMatchable("hda1"),
                                                        new SimpleMatchable("hdb"),
                                                        new SimpleMatchable("hdb1"),
                                                        new SimpleMatchable("hdb2")),
                                    new SimpleMatchable("home",
                                                        new SimpleMatchable("me",
                                                                            new SimpleMatchable("hdmi")),
                                                        new SimpleMatchable("you")),
                                    new SimpleMatchable("var")));

            var parser = new GlobParser();

            var matches = new MultiGlobMatchEnumerator()
                          .Exclude(parser.Parse("home/*/**"))
                          .Include(parser.Parse("**/h*"))
                          .EnumerateMatches(hierarchy);

            Assert.That(matches.Select(m => m.Item.Name).ToArray(),
                        Is.EquivalentTo(new []
            {
                "hda",
                "hda1",
                "hdb",
                "hdb1",
                "hdb2",
                "home"
            }));
        }
コード例 #3
0
        public void MatcherWithOnlyExclusionRulesMatchingInitially_MatchesNothing()
        {
            var hierarchy = new TestHierarchy(
                new SimpleMatchable("root",
                                    new SimpleMatchable("dev",
                                                        new SimpleMatchable("hda"),
                                                        new SimpleMatchable("hda1"),
                                                        new SimpleMatchable("hdb"),
                                                        new SimpleMatchable("hdb1"),
                                                        new SimpleMatchable("hdb2")),
                                    new SimpleMatchable("home",
                                                        new SimpleMatchable("me",
                                                                            new SimpleMatchable("hdmi")),
                                                        new SimpleMatchable("you")),
                                    new SimpleMatchable("var")));

            var parser = new GlobParser();

            var matches = new MultiGlobMatchEnumerator()
                          .Exclude(parser.Parse("dev/**"))
                          .Exclude(parser.Parse("**/hd[am]?"))
                          .Include(parser.Parse("nothing/**"))
                          .EnumerateMatches(hierarchy).ToArray();

            Assert.That(matches.Select(m => m.Item.Name).ToArray(), Is.Empty);
        }
コード例 #4
0
        public void MatcherWithOnlyRecursiveExclusionRulesMatchingWithinASubtree_DoesNotMatchWithinThatSubtree()
        {
            var hierarchy = new TestHierarchy(
                new SimpleMatchable("root",
                                    new SimpleMatchable("dev",
                                                        new SimpleMatchable("hda"),
                                                        new SimpleMatchable("hda1"),
                                                        new SimpleMatchable("hdb"),
                                                        new SimpleMatchable("hdb1"),
                                                        new SimpleMatchable("hdb2")),
                                    new SimpleMatchable("home",
                                                        new SimpleMatchable("me",
                                                                            new SimpleMatchable("hdmi")),
                                                        new SimpleMatchable("you")),
                                    new SimpleMatchable("var")));

            var parser = new GlobParser();

            var matches = new MultiGlobMatchEnumerator()
                          .Exclude(parser.Parse("d*/**"))
                          .Include(parser.Parse("d*"))
                          .EnumerateMatches(hierarchy).ToArray();

            Assert.That(matches.Select(m => m.Item.Name).ToArray(),
                        Is.EquivalentTo(new []
            {
                "dev",
            }));
        }
コード例 #5
0
        public void ExerciseMultiGlobMatchEnumerator()
        {
            TestContext.WriteLine($"CLR Version: {Environment.Version}");
            TestContext.WriteLine($"Runtime: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}");

            var parser   = new GlobParser();
            var includes = new []
            {
                parser.Parse("**/Microsoft*/**/*.dll"),
                parser.Parse("Program*/**/*.exe"),
            };
            var excludes = new []
            {
                parser.Parse("**/SQL*/**")
            };

            var hierarchy  = new FileSystemHierarchy(new DirectoryInfo("C:\\"), false);
            var enumerator = new MultiGlobMatchEnumerator()
                             .Exclude(excludes)
                             .Include(includes);

            var count = enumerator.EnumerateMatches(hierarchy).Count();

            TestContext.WriteLine($"{count} matches");
        }
コード例 #6
0
        public void FindsAllMatches()
        {
            var hierarchy = new TestHierarchy(
                new SimpleMatchable("root",
                                    new SimpleMatchable("dev",
                                                        new SimpleMatchable("hda"),
                                                        new SimpleMatchable("hda1"),
                                                        new SimpleMatchable("hdb"),
                                                        new SimpleMatchable("hdb1"),
                                                        new SimpleMatchable("hdb2")),
                                    new SimpleMatchable("home",
                                                        new SimpleMatchable("me",
                                                                            new SimpleMatchable("hdmi")),
                                                        new SimpleMatchable("you")),
                                    new SimpleMatchable("var")));

            var glob = new GlobParser().Parse("**/hd??");

            var matches = new GlobMatchEnumerator(glob).EnumerateMatches(hierarchy);

            Assert.That(matches.Select(m => m.Item.Name).ToArray(),
                        Is.EquivalentTo(new []
            {
                "hda1",
                "hdb1",
                "hdb2",
                "hdmi"
            }));
        }
コード例 #7
0
        public void AcceptsAnything(string value, bool caseSensitive)
        {
            var segment = new GlobParser().ParseSingleSegment <WildcardMultiSegment>("**");

            var match = segment.Match(value, caseSensitive);

            Assert.That(match.Success, Is.True);
        }
コード例 #8
0
        public void ParsesValidPatterns(Case testCase)
        {
            var glob = new GlobParser().Parse(testCase.Pattern !);

            Assert.That(glob.Root, Is.EqualTo(testCase.Root).Using(rootSegmentComparer));
            Assert.That(glob.Segments, Is.EqualTo(testCase.Segments).Using(segmentComparer));
            Assert.That(glob, Is.EqualTo(new Glob(testCase.Root, testCase.Segments.ToArray())).Using(globComparer));
        }
コード例 #9
0
        public void AcceptsMatchingCaseSensitivePathSegment()
        {
            var segment = new GlobParser().ParseSingleSegment <WildcardSegment>("dir*ory");

            var match = segment.Match("directory", true);

            Assert.That(match.Success, Is.True);
        }
コード例 #10
0
        public void AcceptsMatchingCaseInsensitivePathSegment()
        {
            var segment = new GlobParser().ParseSingleSegment <FixedSegment>("directory");

            var match = segment.Match("DIRectoRy", false);

            Assert.That(match.Success, Is.True);
        }
コード例 #11
0
        public void RejectsNonMatchingCaseInsensitivePathSegment()
        {
            var segment = new GlobParser().ParseSingleSegment <WildcardSegment>("dir*ory");

            var match = segment.Match("something", false);

            Assert.That(match.Success, Is.False);
        }
コード例 #12
0
        public void RejectsNonMatchingCaseSensitivePathSegment()
        {
            var segment = new GlobParser().ParseSingleSegment <FixedSegment>("directory");

            var match = segment.Match("DIRectoRy", true);

            Assert.That(match.Success, Is.False);
        }
コード例 #13
0
        public void WildcardSegmentYieldsPrefix()
        {
            var glob  = new GlobParser().Parse("dir*ory");
            var start = new GlobMatchFactory(true).Start(glob);

            var prefix = start.GetPrefixFilter();

            Assert.That(prefix, Is.EqualTo("dir"));
        }
コード例 #14
0
        public void WildcardMultiSegmentDoesNotYieldPrefix()
        {
            var glob  = new GlobParser().Parse("**");
            var start = new GlobMatchFactory(true).Start(glob);

            var prefix = start.GetPrefixFilter();

            Assert.That(prefix, Is.EqualTo(""));
        }
コード例 #15
0
        public void WildcardSegmentMatchesOnlyOnce()
        {
            var glob  = new GlobParser().Parse("dir*ory");
            var start = new GlobMatchFactory(true).Start(glob);

            var child = ApplyToHierarchy(start, "directory", "directory");

            Assert.That(child.IsMatch, Is.False);
            Assert.That(child.CanContinue, Is.False);
        }
コード例 #16
0
        public void NotEqual(string a, string b)
        {
            var parser = new GlobParser();
            var aRoot  = parser.Parse(a).Root;
            var bRoot  = parser.Parse(b).Root;

            Assume.That(aRoot, Is.Not.Null);
            Assume.That(bRoot, Is.Not.Null);

            Assert.That(aRoot, Is.Not.EqualTo(bRoot).Using(comparer));
        }
コード例 #17
0
        public void FixedSegmentMatches()
        {
            var glob  = new GlobParser().Parse("directory");
            var start = new GlobMatchFactory(true).Start(glob);

            var child = ApplyToHierarchy(start, "directory");

            Assert.That(child.IsMatch, Is.True);
            Assert.That(child.CanContinue, Is.False);
            Assert.That(child.GetPathSegments().ToArray(), Is.EqualTo(new [] { "directory" }));
        }
コード例 #18
0
        public void WildcardMultiSegmentCanMatchNoSegments()
        {
            var glob  = new GlobParser().Parse("a/**/b");
            var start = new GlobMatchFactory(true).Start(glob);

            var child = ApplyToHierarchy(start, "a", "b");

            Assert.That(child.IsMatch, Is.True);
            Assert.That(child.CanContinue, Is.True);
            Assert.That(child.GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "b" }));
        }
コード例 #19
0
ファイル: Globber.cs プロジェクト: EnoughTea/essentions
        /// <summary>
        /// Initializes a new instance of the <see cref="Globber"/> class.
        /// </summary>
        /// <param name="env">The environment.</param>
        /// <exception cref="ArgumentNullException"><paramref name="env"/> is <see langword="null"/></exception>
        public Globber(IFileSystemEnvironment env)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }

            _env      = env;
            _parser   = new GlobParser(env);
            _visitor  = new GlobVisitor(env);
            _comparer = new PathComparer(env.IsUnix());
        }
コード例 #20
0
        public void RejectsUndefinedVariableCaptures()
        {
            var parser = new GlobParser
            {
                Variables =
                {
                    new GlobVariable("MM", @"\d{2}"),
                    new GlobVariable("dd", @"\d{2}")
                }
            };

            Assert.That(() => parser.Parse("./logs-{missing}/{MM}{dd}/*.log"), Throws.InstanceOf <GlobFormatException>());
        }
コード例 #21
0
ファイル: Globber.cs プロジェクト: nirav72/spectre.io
        /// <summary>
        /// Initializes a new instance of the <see cref="Globber"/> class.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="environment">The environment.</param>
        public Globber(IFileSystem fileSystem, IEnvironment environment)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            if (environment is null)
            {
                throw new ArgumentNullException(nameof(environment));
            }

            _parser  = new GlobParser(environment);
            _visitor = new GlobVisitor(fileSystem, environment);
        }
コード例 #22
0
        public void CoalescesAdjacentWildcardMultiSegments()
        {
            var glob = new GlobParser().Parse("a/**/**/**/b/**/c");

            var expected = new Glob(null,
                                    new FixedSegment("a"),
                                    new WildcardMultiSegment(),
                                    new FixedSegment("b"),
                                    new WildcardMultiSegment(),
                                    new FixedSegment("c"));

            Assert.That(glob.Root, Is.EqualTo(expected.Root).Using(rootSegmentComparer));
            Assert.That(glob.Segments, Is.EqualTo(expected.Segments).Using(segmentComparer));
            Assert.That(glob, Is.EqualTo(expected).Using(globComparer));
        }
コード例 #23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Globber"/> class.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="environment">The environment.</param>
        public Globber(IFileSystem fileSystem, ICakeEnvironment environment)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException("fileSystem");
            }
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }

            _environment = environment;
            _parser      = new GlobParser(environment);
            _visitor     = new GlobVisitor(fileSystem, environment);
            _comparer    = new PathComparer(environment.IsUnix());
        }
コード例 #24
0
        public void ContainerExclusionDoesNotApplyRecursively()
        {
            var hierarchy = new TestHierarchy(
                new SimpleMatchable("root",
                                    new SimpleMatchable("dev",
                                                        new SimpleMatchable("hda"),
                                                        new SimpleMatchable("hda1"),
                                                        new SimpleMatchable("hdb"),
                                                        new SimpleMatchable("hdb1"),
                                                        new SimpleMatchable("hdb2")),
                                    new SimpleMatchable("home",
                                                        new SimpleMatchable("me",
                                                                            new SimpleMatchable("hdmi")),
                                                        new SimpleMatchable("you")),
                                    new SimpleMatchable("var")));

            var parser   = new GlobParser();
            var includes = new []
            {
                parser.Parse("**/h*")
            };
            var excludes = new []
            {
                parser.Parse("home")
            };

            var enumerator = new MultiGlobMatchEnumerator().Include(parser.Parse("**"));
            var filter     = new MultiGlobMatchFilter(hierarchy.CaseSensitive)
                             .Exclude(excludes)
                             .Include(includes);

            var matches = enumerator.EnumerateMatches(hierarchy)
                          .Where(filter.Filter)
                          .ToArray();

            Assert.That(matches.Select(m => m.Item.Name).ToArray(),
                        Is.EquivalentTo(new []
            {
                "hda",
                "hda1",
                "hdb",
                "hdb1",
                "hdb2",
                "hdmi"
            }));
        }
コード例 #25
0
        public void WildcardMultiSegmentCanMatchMultipleDepths()
        {
            var glob  = new GlobParser().Parse("a/**/b/**/c");
            var start = new GlobMatchFactory(true).Start(glob);

            Assert.That(ApplyToHierarchy(start, "a", "b", "c").IsMatch, Is.True);
            Assert.That(ApplyToHierarchy(start, "a", "b", "c").GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "b", "c" }));

            Assert.That(ApplyToHierarchy(start, "a", "b", "c", "d").IsMatch, Is.False);

            Assert.That(ApplyToHierarchy(start, "a", "b", "c", "d", "c").IsMatch, Is.True);
            Assert.That(ApplyToHierarchy(start, "a", "b", "c", "d", "c").GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "b", "c", "d", "c" }));

            Assert.That(ApplyToHierarchy(start, "a", "c").IsMatch, Is.False);

            Assert.That(ApplyToHierarchy(start, "a", "d", "b", "c").IsMatch, Is.True);
            Assert.That(ApplyToHierarchy(start, "a", "d", "b", "c").GetPathSegments().ToArray(), Is.EqualTo(new [] { "a", "d", "b", "c" }));
        }
コード例 #26
0
        public void WildcardSegmentMatchYieldsVariables()
        {
            var parser = new GlobParser
            {
                Variables =
                {
                    new GlobVariable("yyyy", @"\d{4}")
                }
            };
            var glob  = parser.Parse("directory{yyyy}");
            var start = new GlobMatchFactory(true).Start(glob);

            var child = ApplyToHierarchy(start, "directory2020");

            Assert.That(child.IsMatch, Is.True);
            Assert.That(child.CanContinue, Is.False);
            Assert.That(child.GetPathSegments().ToArray(), Is.EqualTo(new [] { "directory2020" }));
            Assert.That(child.GetVariables().ToArray(), Is.EqualTo(new [] { new MatchedVariable("yyyy", "2020") }));
        }
コード例 #27
0
        public void YieldsDetailsForEveryMatchingIncludeGlob()
        {
            var hierarchy = new TestHierarchy(
                new SimpleMatchable("root",
                                    new SimpleMatchable("dev",
                                                        new SimpleMatchable("hda"),
                                                        new SimpleMatchable("hda1"),
                                                        new SimpleMatchable("hdb"),
                                                        new SimpleMatchable("hdb1"),
                                                        new SimpleMatchable("hdb2")),
                                    new SimpleMatchable("home",
                                                        new SimpleMatchable("me",
                                                                            new SimpleMatchable("hdmi")),
                                                        new SimpleMatchable("you")),
                                    new SimpleMatchable("var")));

            var parser   = new GlobParser();
            var includes = new []
            {
                parser.Parse("**/hd[am]?"),
                parser.Parse("home/**")
            };

            var matches = new MultiGlobMatchEnumerator()
                          .Include(includes)
                          .EnumerateMatches(hierarchy).ToArray();

            Assume.That(matches.Select(m => m.Item.Name).ToArray(),
                        Is.EquivalentTo(new []
            {
                "hda1",
                "me",
                "hdmi",
                "you"
            }));

            var hdmiMatch = matches.Single(m => m.Item.Name == "hdmi");

            Assert.That(hdmiMatch.Details.Select(s => s.Glob).ToArray(), Is.EquivalentTo(includes));
        }
コード例 #28
0
        public void IncludesValidVariableCaptures()
        {
            var parser = new GlobParser
            {
                Variables =
                {
                    new GlobVariable("yyyy", @"\d{4}"),
                    new GlobVariable("MM",   @"\d{2}"),
                    new GlobVariable("dd",   @"\d{2}")
                }
            };

            var glob = parser.Parse("./logs-{yyyy}/{MM}{dd}/*.log");

            var expected = new Glob(null,
                                    new WildcardSegment("logs-{yyyy}", @"^logs-(?<yyyy>(\d{4}))$", "logs-"),
                                    new WildcardSegment("{MM}{dd}", @"^(?<MM>(\d{2}))(?<dd>(\d{2}))$", ""),
                                    new WildcardSegment("*.log", @"^.*\.log$", ""));

            Assert.That(glob.Root, Is.EqualTo(expected.Root).Using(rootSegmentComparer));
            Assert.That(glob.Segments, Is.EqualTo(expected.Segments).Using(segmentComparer));
            Assert.That(glob, Is.EqualTo(expected).Using(globComparer));
        }
コード例 #29
0
        public void ReturnsMatchedNamedGroupValues()
        {
            var parser = new GlobParser
            {
                Variables =
                {
                    new GlobVariable("yyyy", @"\d{4}"),
                    new GlobVariable("MM",   @"\d{2}"),
                    new GlobVariable("dd",   @"\d{2}"),
                }
            };
            var segment = parser.ParseSingleSegment <WildcardSegment>("{yyyy}-{MM}-{dd}");

            var match = segment.Match("2020-01-10", false);

            Assert.That(match.Success, Is.True);
            Assert.That(match.Variables,
                        Is.EqualTo(new [] {
                new MatchedVariable("yyyy", "2020"),
                new MatchedVariable("MM", "01"),
                new MatchedVariable("dd", "10"),
            }));
        }