TestParameters
TestParameters class holds any named parameters supplied to the test run
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace NUnit.Framework
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class TestParameters
{
private static readonly IFormatProvider MODIFIED_INVARIANT_CULTURE = CreateModifiedInvariantCulture();
private readonly Dictionary<string, string> _parameters = new Dictionary<string, string>();
public int Count => _parameters.Count;
public ICollection<string> Names => _parameters.Keys;
[System.Runtime.CompilerServices.Nullable(2)]
public string this[string name] {
[return: System.Runtime.CompilerServices.Nullable(2)]
get {
return Get(name);
}
}
public bool Exists(string name)
{
return _parameters.ContainsKey(name);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public string Get(string name)
{
if (!Exists(name))
return null;
return _parameters[name];
}
[System.Runtime.CompilerServices.NullableContext(2)]
[return: NotNullIfNotNull("defaultValue")]
public string Get([System.Runtime.CompilerServices.Nullable(1)] string name, string defaultValue)
{
return Get(name) ?? defaultValue;
}
[return: NotNullIfNotNull("defaultValue")]
public T Get<[System.Runtime.CompilerServices.Nullable(2)] T>(string name, [MaybeNull] T defaultValue)
{
string text = Get(name);
if (text == null)
return defaultValue;
return (T)Convert.ChangeType(text, typeof(T), MODIFIED_INVARIANT_CULTURE);
}
internal void Add(string name, string value)
{
_parameters[name] = value;
}
private static IFormatProvider CreateModifiedInvariantCulture()
{
CultureInfo cultureInfo = (CultureInfo)CultureInfo.InvariantCulture.Clone();
cultureInfo.NumberFormat.CurrencyGroupSeparator = string.Empty;
cultureInfo.NumberFormat.NumberGroupSeparator = string.Empty;
cultureInfo.NumberFormat.PercentGroupSeparator = string.Empty;
return cultureInfo;
}
}
}