PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/src/NUnit/core/CultureDetector.cs

#
C# | 127 lines | 74 code | 16 blank | 37 comment | 13 complexity | b28d8d554b01fb14c7b1717bbaeeaa0f MD5 | raw file
  1// ****************************************************************
  2// This is free software licensed under the NUnit license. You
  3// may obtain a copy of the license as well as information regarding
  4// copyright ownership at http://nunit.org.
  5// ****************************************************************
  6
  7using System;
  8using System.Reflection;
  9using System.Globalization;
 10
 11namespace NUnit.Core
 12{
 13	public class CultureDetector
 14	{
 15		private CultureInfo currentCulture;
 16
 17		// Set whenever we fail to support a list of platforms
 18		private string reason = string.Empty;
 19
 20		/// <summary>
 21		/// Default constructor uses the current culutre.
 22		/// </summary>
 23		public CultureDetector()
 24		{
 25			this.currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
 26		}
 27
 28		/// <summary>
 29		/// Contruct a CultureHelper for a particular culture for testing.
 30		/// </summary>
 31		/// <param name="culture">The culture to be used</param>
 32		public CultureDetector( string culture )
 33		{
 34			this.currentCulture = new CultureInfo( culture );
 35		}
 36
 37		/// <summary>
 38		/// Test to determine if one of a collection of culturess
 39		/// is being used currently.
 40		/// </summary>
 41		/// <param name="cultures"></param>
 42		/// <returns></returns>
 43		public bool IsCultureSupported( string[] cultures )
 44		{
 45			foreach( string culture in cultures )
 46				if ( IsCultureSupported( culture ) )
 47					return true;
 48
 49			return false;
 50		}
 51
 52		/// <summary>
 53		/// Tests to determine if the current culture is supported
 54		/// based on a culture attribute.
 55		/// </summary>
 56		/// <param name="platformAttribute">The attribute to examine</param>
 57		/// <returns></returns>
 58		public bool IsCultureSupported( Attribute cultureAttribute )
 59		{
 60			//Use reflection to avoid dependency on a particular framework version
 61			string include = (string)Reflect.GetPropertyValue( 
 62				cultureAttribute, "Include", 
 63				BindingFlags.Public | BindingFlags.Instance );
 64
 65			string exclude = (string)Reflect.GetPropertyValue(
 66				cultureAttribute, "Exclude", 
 67				BindingFlags.Public | BindingFlags.Instance );
 68
 69			try
 70			{
 71				if (include != null && !IsCultureSupported(include))
 72				{
 73					reason = string.Format("Only supported under culture {0}", include);
 74					return false;
 75				}
 76
 77				if (exclude != null && IsCultureSupported(exclude))
 78				{
 79					reason = string.Format("Not supported under culture {0}", exclude);
 80					return false;
 81				}
 82			}
 83			catch( ArgumentException ex )
 84			{
 85				reason = string.Format( "Invalid culture: {0}", ex.ParamName );
 86				return false; 
 87			}
 88
 89			return true;
 90		}
 91
 92		/// <summary>
 93		/// Test to determine if the a particular culture or comma-
 94		/// delimited set of cultures is in use.
 95		/// </summary>
 96		/// <param name="platform">Name of the culture or comma-separated list of culture names</param>
 97		/// <returns>True if the culture is in use on the system</returns>
 98		public bool IsCultureSupported( string culture )
 99		{
100			culture = culture.Trim();
101
102			if ( culture.IndexOf( ',' ) >= 0 )
103			{
104				if ( IsCultureSupported( culture.Split( new char[] { ',' } ) ) )
105					return true;
106			}
107			else
108			{
109				if( this.currentCulture.Name == culture || this.currentCulture.TwoLetterISOLanguageName == culture)
110					return true;
111			}
112
113			this.reason = "Only supported under culture " + culture;
114			return false;
115		}
116
117		/// <summary>
118		/// Return the last failure reason. Results are not
119		/// defined if called before IsSupported( Attribute )
120		/// is called.
121		/// </summary>
122		public string Reason
123		{
124			get { return reason; }
125		}
126	}
127}