PageRenderTime 32ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/src/NUnit/core/CultureDetector.cs

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