-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathPackageSettings.cs
More file actions
154 lines (130 loc) · 5.95 KB
/
PackageSettings.cs
File metadata and controls
154 lines (130 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using NUnit.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
namespace NUnit.Engine
{
/// <summary>
/// PackageSettings contains all the settings applied to a TestPackage.
/// It supports enumeration of the settings and various operations on
/// individual settings in the list.
/// </summary>
public class PackageSettings : IEnumerable<PackageSetting>
{
private readonly Dictionary<string, PackageSetting> _settings = new();
/// <summary>
/// Gets the number of settings in the list
/// </summary>
public int Count => _settings.Count;
/// <summary>
/// Returns true if a setting with the specified name is present
/// </summary>
public bool HasSetting(string name) => _settings.ContainsKey(name);
/// <summary>
/// Returns true if a setting with the specified definition is present
/// </summary>
public bool HasSetting(SettingDefinition setting) => _settings.ContainsKey(setting.Name);
/// <summary>
/// Return the value of a setting if present, otherwise null.
/// </summary>
/// <param name="name">The name of the setting</param>
public object GetSetting(string name)
{
return _settings[name].Value;
}
/// <summary>
/// Return the value of a setting or its defined default value.
/// </summary>
/// <param name="definition">The name and type of the setting</param>
public T GetValueOrDefault<T>(SettingDefinition<T> definition)
where T : notnull
{
if (_settings.TryGetValue(definition.Name, out PackageSetting? unTypedSetting))
if (unTypedSetting is PackageSetting<T> typedSetting && typedSetting is not null)
return typedSetting.Value;
return definition.DefaultValue;
}
/// <inheritdoc />
public IEnumerator<PackageSetting> GetEnumerator() => _settings.Values.GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => _settings.Values.GetEnumerator();
/// <summary>
/// Adds a setting to the list directly.
/// </summary>
/// <param name="setting">A PackageSetting instance</param>
public void Add(PackageSetting setting) => _settings.Add(setting.Name, setting);
/// <summary>
/// Creates and adds a setting to the list, specified by the name and a string value.
/// The string value is converted to a typed PackageSetting if the name specifies a known SettingDefinition.
/// </summary>
/// <param name="name">The name of the setting.</param>
/// <param name="value">The corresponding value to set.</param>
public void Add(string name, string value)
{
var definition = SettingDefinitions.Lookup(name);
// If this is a known setting but not a string throw an exception
if (definition is not null && !definition.ValueType.IsAssignableFrom(typeof(string)))
throw (new ArgumentException($"The {name} setting requires a value of type {definition.ValueType.Name}"));
// Otherwise add it
Add(new PackageSetting<string>(name, value));
}
/// <summary>
/// Creates and adds a custom boolean setting to the list, specifying the name and value.
/// </summary>
/// <param name="name">The name of the setting.</param>
/// <param name="value">The corresponding value to set.</param>
public void Add(string name, bool value)
{
var definition = SettingDefinitions.Lookup(name);
// If this is a known setting but not a boolean throw an exception
if (definition is not null && !definition.ValueType.IsAssignableFrom(typeof(bool)))
throw (new ArgumentException($"The {name} setting requires a value of type {definition.ValueType.Name}"));
Add(new PackageSetting<bool>(name, value));
}
/// <summary>
/// Creates and adds a custom int setting to the list, specifying the name and value.
/// </summary>
/// <param name="name">The name of the setting.</param>
/// <param name="value">The corresponding value to set.</param>
public void Add(string name, int value)
{
var definition = SettingDefinitions.Lookup(name);
// If this is a known setting but not an int throw an exception
if (definition is not null && !definition.ValueType.IsAssignableFrom(typeof(int)))
throw (new ArgumentException($"The {name} setting requires a value of type {definition.ValueType.Name}"));
Add(new PackageSetting<int>(name, value));
}
/// <summary>
/// Adds or replaces a setting to the list.
/// </summary>
public void Set(PackageSetting setting) => _settings[setting.Name] = setting;
/// <summary>
/// Adds or replaces a setting to the list.
/// </summary>
/// <param name="name">The name of the setting.</param>
/// <param name="value">The corresponding value to set.</param>
public void Set<T>(string name, T value)
where T : notnull
{
Set(new PackageSetting<T>(name, value));
}
/// <summary>
/// Remove a setting
/// </summary>
/// <param name="setting">The setting to remove</param>
public void Remove(SettingDefinition setting)
{
_settings.Remove(setting.Name);
}
/// <summary>
/// Remove a setting
/// </summary>
/// <param name="settingName">The name of the setting to remove</param>
public void Remove(string settingName)
{
_settings.Remove(settingName);
}
}
}