PathUtilTest.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » Core » Util » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » UnitTests » Core » Util » PathUtilTest.cs
using System;
using NUnit.Framework;
using ThoughtWorks.CruiseControl.Core.Util;

namespace ThoughtWorks.CruiseControl.UnitTests.Core.Util{
  [TestFixture]
  public class PathUtilTest
  {
    private static readonly string[] names = {
                                               "theName.dat", "TheName.dat",
                                               "/theFolder/theName.dat", "theFile.bin",
                                               "/theFolder/theFile.bin", "/TheFolder/theName.dat"
                                               , "/theFolder/theSubFolder/theName.dat",
                                               "/theFolder/theSubFolder/theSubSubFolder/theName.dat"
                                               ,
                                               "/theFolder/theSubFolder/theSubFolder/theName.dat"
                                               ,
                                               "/theFolder/theSubFolder/ThesubFolder/theName.dat"
                                               , "theName", "/theFolder/theSubFolder/theName",
                                               "\\theFolder\\theSubFolder/theName.dav",
                                               "\\theFolder/theName.dav"
                                             };

    private static readonly int[] namesTokenLen = {1, 1, 2, 1, 2, 2, 3, 4, 4, 4, 1, 3, 3, 2};

    [Test]
    public void TheFolderCaseInsensitive()
    {
      new TestCase("/thefolder/**/*", false, names,
                   new bool[]
                     {
                       false, false, true, false, true, true, true, true, true, true, false,
                       true, true, true
                     }).RunTest();
    }

    [Test]
    public void AnySubFolder()
    {
      new TestCase("**/theSubFolder/**/*", true, names,
                   new bool[]
                     {
                       false, false, false, false, false, false, true, true, true, true,
                       false, true, true, false
                     }).RunTest();
    }

    [Test]
    public void SingleCharacterInName()
    {
      new TestCase("**/the???e.da?", true, names,
                   new bool[]
                     {
                       true, false, true, false, false, true, true, true, true, true, false,
                       false, true, true
                     }).RunTest();
    }

    [Test]
    public void CaseInsensitiveMatch()
    {
      Assert.IsTrue(PathUtils.Match("thename", "TheName", false));
    }

    [Test]
    public void CaseInsensitiveMisMatch()
    {
      Assert.IsFalse(PathUtils.Match("thefile", "TheName", false));
    }

    [Test]
    public void CaseInsensitiveStarMismatch()
    {
      Assert.IsFalse(PathUtils.Match("the*", "ratBone", false));
    }

    [Test]
    public void MatchWithStarAtEnd()
    {
      Assert.IsTrue(PathUtils.Match("thename*", "thename", false));
    }

    [Test]
    public void MisMatchWithMatchBeforeStar()
    {
      Assert.IsFalse(PathUtils.Match("thename*x", "thename", false));
    }

    [Test]
    public void MisMatchWithStar()
    {
      Assert.IsFalse(PathUtils.Match("the*name", "thexfile", false));
    }

    [Test]
    public void MatchWithExhaustingStrings()
    {
      Assert.IsTrue(PathUtils.Match("the*name*back", "thexnameisBack", false));
    }

    [Test]
    public void MatchMiddleStar()
    {
      Assert.IsFalse(PathUtils.Match("t*a*x", "tx", false));
    }

    [Test]
    public void MatchTwoStars()
    {
      Assert.IsTrue(PathUtils.Match("t**a*x", "txax", false));
    }

    [Test]
    public void MisMatchWithTwoStarsExhaustedStrings()
    {
      Assert.IsFalse(PathUtils.Match("t*ay*x", "txax", false));
    }

    [Test]
    public void MatchStarStarStarStar()
    {
      Assert.IsTrue(PathUtils.MatchPath("**/**/*", "/f/dat", false));
    }

    [Test]
    public void MismatchExhaustedStrings()
    {
      Assert.IsFalse(PathUtils.Match("the*name*back", "thexnamisBack", false));
    }

    [Test]
    public void MismatchStringExhausted()
    {
      Assert.IsFalse(PathUtils.MatchPath("/f/dat/**/x", "/f/dat", false));
    }

    [Test]
    public void MismatchExhaustedStringNoStarAtEnd()
    {
      Assert.IsFalse(PathUtils.Match("the*name*backxx", "thexnameisBack", false));
    }

    [Test]
    public void TokenizeTest()
    {
      for (int i = 0; i < names.Length; i++)
      {
        Assert.AreEqual(namesTokenLen[i], PathUtils.SplitPath(names[i]).Length);
      }
    }

    [Test]
    public void TokenizeComplexTest()
    {
      string[] r = PathUtils.SplitPath(names[7]);
      Assert.AreEqual(4, r.Length);
      Assert.AreEqual("theFolder", r[0]);
      Assert.AreEqual("theSubFolder", r[1]);
      Assert.AreEqual("theSubSubFolder", r[2]);
      Assert.AreEqual("theName.dat", r[3]);
    }

    [Test]
    public void TokenizeFunnySlants()
    {
      string[] r = PathUtils.SplitPath("\\theFolder/theName.dav");
      Assert.AreEqual(2, r.Length);
      Assert.AreEqual("theFolder", r[0]);
      Assert.AreEqual("theName.dav", r[1]);
    }

    [Test]
    public void FileNameMatch()
    {
      new TestCase("theName.dat", true, names,
                   new bool[]
                     {
                       true, false, false, false, false, false, false, false, false, false,
                       false, false, false, false
                     }).RunTest();
    }

    [Test]
    public void AnyFileNameMatch()
    {
      new TestCase("*", true, names,
                   new bool[]
                     {
                       true, true, false, true, false, false, false, false, false, false,
                       true, false, false, false
                     }).RunTest();
    }

    [Test]
    public void ExactFolderAnyFile()
    {
      new TestCase("/theFolder/*.*", true, names,
                   new bool[]
                     {
                       false, false, true, false, true, false, false, false, false, false,
                       false, false, false, true
                     }).RunTest();
    }

    [Test]
    public void AnyFolderExactFolder()
    {
      new TestCase("**/theName.dat", true, names,
                   new bool[]
                     {
                       true, false, true, false, false, true, true, true, true, true, false,
                       false, false, false
                     }).RunTest();
    }

    [Test]
    public void ExactSubFolderAnyFile()
    {
      new TestCase("**/theSubFolder/*.*", true, names,
                   new bool[]
                     {
                       false, false, false, false, false, false, true, false, true, false,
                       false, true, true, false
                     }).RunTest();
    }

    [Test]
    public void AcceptAll()
    {
      new TestCase("**/*.*", true, names,
                   new bool[]
                     {
                       true, true, true, true, true, true, true, true, true, true, true, true
                       , true, true
                     }).RunTest();
    }

    [Test]
    public void PartialFileNames()
    {
      new TestCase("**/the*.dat", true, names,
                   new bool[]
                     {
                       true, false, true, false, false, true, true, true, true, true, false,
                       false, false, false
                     }).RunTest();
    }

    [Test]
    public void SpecificExtensionInAnyFolder()
    {
      new TestCase("**/*.bin", true, names,
                   new bool[]
                     {
                       false, false, false, true, true, false, false, false, false, false,
                       false, false, false, false
                     }).RunTest();
    }

    [Test]
    public void PartialFolderAnyFile()
    {
      new TestCase("**/the*Folder/*.*", true, names,
                   new bool[]
                     {
                       false, false, true, false, true, false, true, true, true, false, false
                       , true, true, true
                     }).RunTest();
    }

    [Test]
    public void AnyFolderPartialExtension()
    {
      new TestCase("**/*.da*", true, names,
                   new bool[]
                     {
                       true, true, true, false, false, true, true, true, true, true, false,
                       false, true, true
                     }).RunTest();
    }

    [Test]
    public void PathPrefixAnyFolder()
    {
      new TestCase("/theFolder/**/*.*", true, names,
                   new bool[]
                     {
                       false, false, true, false, true, false, true, true, true, true, false,
                       true, true, true
                     }).RunTest();
    }

    [Test]
    public void SingledOutFunnySlants()
    {
      Assert.IsTrue(PathUtils.MatchPath("/theFolder/*", "\\theFolder/theName.dav", true));
    }

    [Test]
    public void SingledOutStarStarNoExtension()
    {
      Assert.IsTrue(PathUtils.Match("*.*", "theName", true));
      Assert.IsTrue(PathUtils.Match("*.*", "theName.dat", true));
      Assert.IsTrue(PathUtils.Match("*", "theName", true));
      Assert.IsTrue(PathUtils.Match("*", "theName.dat", true));
    }

        [Test]
        public void NullOrEmptyTargetShouldNotMatchPattern()
        {
            Assert.IsFalse(PathUtils.MatchPath("/theFolder/*", null, true));
            Assert.IsFalse(PathUtils.MatchPath("/theFolder/*", null, false));
            Assert.IsFalse(PathUtils.MatchPath("/theFolder/*", "", true));
            Assert.IsFalse(PathUtils.MatchPath("/theFolder/*", "", false));
        }
  }

  internal class TestCase
  {
    private string pattern;
    private string[] strs;
    private bool caseSensitive;
    private bool[] expected;

    public TestCase(string pattern, bool caseSensitive, string[] strs, bool[] expected)
    {
      this.pattern = pattern;
      this.strs = strs;
      this.caseSensitive = caseSensitive;
      this.expected = expected;
    }

    public void RunTest()
    {
      Assert.AreEqual(strs.Length, expected.Length);
      for (int i = 0; i < strs.Length; i++)
      {
        if (PathUtils.MatchPath(pattern, strs[i], caseSensitive) != expected[i])
        {
          Assert.Fail(
            String.Format("[{4}] pattern={0} str={1} caseSensitive={2} expected={3}",
                          pattern, strs[i], caseSensitive, expected[i], i));
        }
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.