1 // ***********************************************************************
2 // Copyright (c) 2007 Charlie Poole
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Globalization;
31 using NUnit.Framework.Interfaces;
32 using NUnit.Framework.Internal;
34 namespace NUnit.Framework
37 /// CultureAttribute is used to mark a test fixture or an
38 /// individual method as applying to a particular Culture only.
40 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Assembly, AllowMultiple = false, Inherited=false)]
41 public class CultureAttribute : IncludeExcludeAttribute, IApplyToTest
43 private CultureDetector cultureDetector = new CultureDetector();
44 private CultureInfo currentCulture = CultureInfo.CurrentCulture;
47 /// Constructor with no cultures specified, for use
48 /// with named property syntax.
50 public CultureAttribute() { }
53 /// Constructor taking one or more cultures
55 /// <param name="cultures">Comma-deliminted list of cultures</param>
56 public CultureAttribute(string cultures) : base(cultures) { }
58 #region IApplyToTest members
61 /// Causes a test to be skipped if this CultureAttribute is not satisfied.
63 /// <param name="test">The test to modify</param>
64 public void ApplyToTest(Test test)
66 if (test.RunState != RunState.NotRunnable && !IsCultureSupported())
68 test.RunState = RunState.Skipped;
69 test.Properties.Set(PropertyNames.SkipReason, Reason);
76 /// Tests to determine if the current culture is supported
77 /// based on the properties of this attribute.
79 /// <returns>True, if the current culture is supported</returns>
80 private bool IsCultureSupported()
82 if (Include != null && !cultureDetector.IsCultureSupported(Include))
84 Reason = string.Format("Only supported under culture {0}", Include);
88 if (Exclude != null && cultureDetector.IsCultureSupported(Exclude))
90 Reason = string.Format("Not supported under culture {0}", Exclude);
98 /// Test to determine if the a particular culture or comma-
99 /// delimited set of cultures is in use.
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)
105 culture = culture.Trim();
107 if (culture.IndexOf(',') >= 0)
109 if (IsCultureSupported(culture.Split(new char[] { ',' })))
114 if (currentCulture.Name == culture || currentCulture.TwoLetterISOLanguageName == culture)
122 /// Test to determine if one of a collection of cultures
123 /// is being used currently.
125 /// <param name="cultures"></param>
126 /// <returns></returns>
127 public bool IsCultureSupported(string[] cultures)
129 foreach (string culture in cultures)
130 if (IsCultureSupported(culture))