[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Attributes / CultureAttribute.cs
1 // ***********************************************************************
2 // Copyright (c) 2007 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 using System;
30 using System.Globalization;
31 using NUnit.Framework.Interfaces;
32 using NUnit.Framework.Internal;
33
34 namespace NUnit.Framework
35 {
36     /// <summary>
37     /// CultureAttribute is used to mark a test fixture or an
38     /// individual method as applying to a particular Culture only.
39     /// </summary>
40     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = false, Inherited=false)]
41     public class CultureAttribute : IncludeExcludeAttribute, IApplyToTest
42     {
43         private CultureDetector cultureDetector = new CultureDetector();
44         private CultureInfo currentCulture = CultureInfo.CurrentCulture;
45
46         /// <summary>
47         /// Constructor with no cultures specified, for use
48         /// with named property syntax.
49         /// </summary>
50         public CultureAttribute() { }
51
52         /// <summary>
53         /// Constructor taking one or more cultures
54         /// </summary>
55         /// <param name="cultures">Comma-deliminted list of cultures</param>
56         public CultureAttribute(string cultures) : base(cultures) { }
57
58         #region IApplyToTest members
59
60         /// <summary>
61         /// Causes a test to be skipped if this CultureAttribute is not satisfied.
62         /// </summary>
63         /// <param name="test">The test to modify</param>
64         public void ApplyToTest(Test test)
65         {
66             if (test.RunState != RunState.NotRunnable && !IsCultureSupported())
67             {
68                 test.RunState = RunState.Skipped;
69                 test.Properties.Set(PropertyNames.SkipReason, Reason);
70             }
71         }
72
73         #endregion
74
75         /// <summary>
76         /// Tests to determine if the current culture is supported
77         /// based on the properties of this attribute.
78         /// </summary>
79         /// <returns>True, if the current culture is supported</returns>
80         private bool IsCultureSupported()
81         {
82             if (Include != null && !cultureDetector.IsCultureSupported(Include))
83             {
84                 Reason = string.Format("Only supported under culture {0}", Include);
85                 return false;
86             }
87
88             if (Exclude != null && cultureDetector.IsCultureSupported(Exclude))
89             {
90                 Reason = string.Format("Not supported under culture {0}", Exclude);
91                 return false;
92             }
93
94             return true;
95         }
96
97         /// <summary>
98         /// Test to determine if the a particular culture or comma-
99         /// delimited set of cultures is in use.
100         /// </summary>
101         /// <param name="culture">Name of the culture or comma-separated list of culture ids</param>
102         /// <returns>True if the culture is in use on the system</returns>
103         public bool IsCultureSupported(string culture)
104         {
105             culture = culture.Trim();
106
107             if (culture.IndexOf(',') >= 0)
108             {
109                 if (IsCultureSupported(culture.Split(new char[] { ',' })))
110                     return true;
111             }
112             else
113             {
114                 if (currentCulture.Name == culture || currentCulture.TwoLetterISOLanguageName == culture)
115                     return true;
116             }
117
118             return false;
119         }
120
121         /// <summary>
122         /// Test to determine if one of a collection of cultures
123         /// is being used currently.
124         /// </summary>
125         /// <param name="cultures"></param>
126         /// <returns></returns>
127         public bool IsCultureSupported(string[] cultures)
128         {
129             foreach (string culture in cultures)
130                 if (IsCultureSupported(culture))
131                     return true;
132
133             return false;
134         }
135     }
136 }