[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / PathConstraint.cs
1 // ***********************************************************************
2 // Copyright (c) 2008 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 #if !PORTABLE
30 using System;
31 using System.IO;
32 using NUnit.Framework.Internal;
33
34 namespace NUnit.Framework.Constraints
35 {
36 #region PathConstraint
37     /// <summary>
38     /// PathConstraint serves as the abstract base of constraints
39     /// that operate on paths and provides several helper methods.
40     /// </summary>
41     public abstract class PathConstraint : StringConstraint
42     {
43         private const char WindowsDirectorySeparatorChar = '\\';
44         private const char NonWindowsDirectorySeparatorChar = '/';
45         private static readonly char[] DirectorySeparatorChars = new char[] { WindowsDirectorySeparatorChar, NonWindowsDirectorySeparatorChar };
46
47         /// <summary>
48         /// Construct a PathConstraint for a give expected path
49         /// </summary>
50         /// <param name="expected">The expected path</param>
51         protected PathConstraint(string expected)
52             : base(expected)
53         {
54             this.expected = expected;
55             this.caseInsensitive = Path.DirectorySeparatorChar == WindowsDirectorySeparatorChar;
56         }
57
58         /// <summary>
59         /// Modifies the current instance to be case-sensitive
60         /// and returns it.
61         /// </summary>
62         public PathConstraint RespectCase
63         {
64             get { caseInsensitive = false; return this; }
65         }
66
67         /// <summary>
68         /// Returns the string representation of this constraint
69         /// </summary>
70         protected override string GetStringRepresentation()
71         {
72             return string.Format("<{0} \"{1}\" {2}>", DisplayName.ToLower(), expected, caseInsensitive ? "ignorecase" : "respectcase");
73         }
74
75 #region Helper Methods
76         /// <summary>
77         /// Canonicalize the provided path
78         /// </summary>
79         /// <param name="path"></param>
80         /// <returns>The path in standardized form</returns>
81         protected string Canonicalize(string path)
82         {
83             if (Path.DirectorySeparatorChar != Path.AltDirectorySeparatorChar)
84                 path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
85
86             string leadingSeparators = "";
87
88             foreach (char c in path)
89             {
90                 if (c == WindowsDirectorySeparatorChar || c == NonWindowsDirectorySeparatorChar)
91                 {
92                     leadingSeparators += Path.DirectorySeparatorChar;
93                 }
94                 else break;
95             }
96
97 #if !NETCF
98             string[] parts = path.Split(DirectorySeparatorChars, StringSplitOptions.RemoveEmptyEntries);
99 #else
100             string[] parts = path.Split(DirectorySeparatorChars);
101 #endif
102
103             int count = 0;
104             bool shifting = false;
105             foreach (string part in parts)
106             {
107                 switch (part)
108                 {
109                     case ".":
110                     case "":
111                         shifting = true;
112                         break;
113
114                     case "..":
115                         shifting = true;
116                         if (count > 0)
117                             --count;
118                         break;
119                     default:
120                         if (shifting)
121                             parts[count] = part;
122                         ++count;
123                         break;
124                 }
125             }
126
127             return leadingSeparators + String.Join(Path.DirectorySeparatorChar.ToString(), parts, 0, count);
128         }
129
130         /// <summary>
131         /// Test whether one path in canonical form is a subpath of another path
132         /// </summary>
133         /// <param name="path1">The first path - supposed to be the parent path</param>
134         /// <param name="path2">The second path - supposed to be the child path</param>
135         /// <returns></returns>
136         protected bool IsSubPath(string path1, string path2)
137         {
138             int length1 = path1.Length;
139             int length2 = path2.Length;
140
141             // if path1 is longer or equal, then path2 can't be a subpath
142             if (length1 >= length2)
143                 return false;
144
145             // path 2 is longer than path 1: see if initial parts match
146             if (!StringUtil.StringsEqual(path1, path2.Substring(0, length1), caseInsensitive))
147                 return false;
148
149             // must match through or up to a directory separator boundary
150             return path2[length1 - 1] == Path.DirectorySeparatorChar ||
151                 path2[length1] == Path.DirectorySeparatorChar;
152         }
153
154 #endregion
155     }
156 #endregion
157 }
158 #endif