1 // ***********************************************************************
2 // Copyright (c) 2015 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.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework.Interfaces;
34 namespace NUnit.Framework.Internal
37 /// A PropertyBag represents a collection of name value pairs
38 /// that allows duplicate entries with the same key. Methods
39 /// are provided for adding a new pair as well as for setting
40 /// a key to a single value. All keys are strings but _values
41 /// may be of any type. Null _values are not permitted, since
42 /// a null entry represents the absence of the key.
44 public class PropertyBag : IPropertyBag
46 private Dictionary<string, IList> inner = new Dictionary<string, IList>();
48 #region IPropertyBagMembers
51 /// Adds a key/value pair to the property set
53 /// <param name="key">The key</param>
54 /// <param name="value">The value</param>
55 public void Add(string key, object value)
58 if (!inner.TryGetValue(key, out list))
60 list = new List<object>();
67 /// Sets the value for a key, removing any other
68 /// _values that are already in the property set.
70 /// <param name="key"></param>
71 /// <param name="value"></param>
72 public void Set(string key, object value)
74 // Guard against mystery exceptions later!
75 Guard.ArgumentNotNull(key, "key");
76 Guard.ArgumentNotNull(value, "value");
78 IList list = new List<object>();
84 /// Gets a single value for a key, using the first
85 /// one if multiple _values are present and returning
86 /// null if the value is not found.
88 /// <param name="key"></param>
89 /// <returns></returns>
90 public object Get(string key)
93 return inner.TryGetValue(key, out list) && list.Count > 0
99 /// Gets a flag indicating whether the specified key has
100 /// any entries in the property set.
102 /// <param name="key">The key to be checked</param>
104 /// True if their are _values present, otherwise false
106 public bool ContainsKey(string key)
108 return inner.ContainsKey(key);
112 /// Gets a collection containing all the keys in the property set
115 public ICollection<string> Keys
117 get { return inner.Keys; }
121 /// Gets or sets the list of _values for a particular key
123 public IList this[string key]
128 if (!inner.TryGetValue(key, out list))
130 list = new List<object>();
131 inner.Add(key, list);
143 #region IXmlNodeBuilder Members
146 /// Returns an XmlNode representating the current PropertyBag.
148 /// <param name="recursive">Not used</param>
149 /// <returns>An XmlNode representing the PropertyBag</returns>
150 public TNode ToXml(bool recursive)
152 return AddToXml(new TNode("dummy"), recursive);
156 /// Returns an XmlNode representing the PropertyBag after
157 /// adding it as a child of the supplied parent node.
159 /// <param name="parentNode">The parent node.</param>
160 /// <param name="recursive">Not used</param>
161 /// <returns></returns>
162 public TNode AddToXml(TNode parentNode, bool recursive)
164 TNode properties = parentNode.AddElement("properties");
166 foreach (string key in Keys)
168 foreach (object value in this[key])
170 TNode prop = properties.AddElement("property");
172 // TODO: Format as string
173 prop.AddAttribute("name", key.ToString());
174 prop.AddAttribute("value", value.ToString());