public void TestBasicFunctionality () { var param = new Parameter ("name", "value"); Assert.AreEqual (Encoding.UTF8.HeaderName, param.Encoding.HeaderName); Assert.AreEqual (ParameterEncodingMethod.Default, param.EncodingMethod); Assert.AreEqual ("name", param.Name); Assert.AreEqual ("value", param.Value); Assert.AreEqual ("name=\"value\"", param.ToString ()); }
public void TestArgumentExceptions () { const string invalid = "X-测试文本"; var list = new ParameterList (); Parameter param; string value; // Add Assert.Throws<ArgumentNullException> (() => list.Add ((Encoding) null, "name", "value")); Assert.Throws<ArgumentNullException> (() => list.Add (Encoding.UTF8, null, "value")); Assert.Throws<ArgumentException> (() => list.Add (Encoding.UTF8, string.Empty, "value")); Assert.Throws<ArgumentException> (() => list.Add (Encoding.UTF8, invalid, "value")); Assert.Throws<ArgumentNullException> (() => list.Add (Encoding.UTF8, "name", null)); Assert.Throws<ArgumentNullException> (() => list.Add ((string) null, "name", "value")); Assert.Throws<ArgumentNullException> (() => list.Add ("utf-8", null, "value")); Assert.Throws<ArgumentException> (() => list.Add ("utf-8", string.Empty, "value")); Assert.Throws<ArgumentException> (() => list.Add ("utf-8", invalid, "value")); Assert.Throws<ArgumentNullException> (() => list.Add ("utf-8", "name", null)); Assert.Throws<ArgumentNullException> (() => list.Add (null, "value")); Assert.Throws<ArgumentException> (() => list.Add (string.Empty, "value")); Assert.Throws<ArgumentException> (() => list.Add (invalid, "value")); Assert.Throws<ArgumentNullException> (() => list.Add ("name", null)); Assert.Throws<ArgumentNullException> (() => list.Add (null)); // Contains Assert.Throws<ArgumentNullException> (() => list.Contains ((Parameter) null)); Assert.Throws<ArgumentNullException> (() => list.Contains ((string) null)); // CopyTo Assert.Throws<ArgumentOutOfRangeException> (() => list.CopyTo (new Parameter[0], -1)); Assert.Throws<ArgumentNullException> (() => list.CopyTo (null, 0)); // IndexOf Assert.Throws<ArgumentNullException> (() => list.IndexOf ((Parameter) null)); Assert.Throws<ArgumentNullException> (() => list.IndexOf ((string) null)); // Insert list.Add ("name", "value"); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, new Parameter ("name", "value"))); Assert.Throws<ArgumentOutOfRangeException> (() => list.Insert (-1, "field", "value")); Assert.Throws<ArgumentNullException> (() => list.Insert (0, null, "value")); Assert.Throws<ArgumentException> (() => list.Insert (0, string.Empty, "value")); Assert.Throws<ArgumentException> (() => list.Insert (0, invalid, "value")); Assert.Throws<ArgumentNullException> (() => list.Insert (0, "name", null)); Assert.Throws<ArgumentNullException> (() => list.Insert (0, null)); // Remove Assert.Throws<ArgumentNullException> (() => list.Remove ((Parameter) null)); Assert.Throws<ArgumentNullException> (() => list.Remove ((string) null)); // RemoveAt Assert.Throws<ArgumentOutOfRangeException> (() => list.RemoveAt (-1)); // TryGetValue Assert.Throws<ArgumentNullException> (() => list.TryGetValue (null, out param)); Assert.Throws<ArgumentNullException> (() => list.TryGetValue (null, out value)); // Indexers Assert.Throws<ArgumentOutOfRangeException> (() => list[-1] = new Parameter ("name", "value")); Assert.Throws<ArgumentOutOfRangeException> (() => param = list[-1]); Assert.Throws<ArgumentNullException> (() => list[0] = null); Assert.Throws<ArgumentNullException> (() => list[null] = "value"); Assert.Throws<ArgumentNullException> (() => value = list[null]); Assert.Throws<ArgumentNullException> (() => list["name"] = null); }
public void TestBasicFunctionality () { var list = new ParameterList (); Parameter parameter; string value; int index; Assert.IsFalse (list.IsReadOnly, "IsReadOnly"); Assert.AreEqual (0, list.Count); list.Add (new Parameter ("abc", "0")); list.Add (Encoding.UTF8, "def", "1"); list.Add ("ghi", "2"); Assert.AreEqual (3, list.Count, "Count"); Assert.IsTrue (list.Contains (list[0])); Assert.IsTrue (list.Contains ("aBc")); Assert.IsTrue (list.Contains ("DEf")); Assert.IsTrue (list.Contains ("gHI")); Assert.AreEqual (0, list.IndexOf ("aBc")); Assert.AreEqual (1, list.IndexOf ("dEF")); Assert.AreEqual (2, list.IndexOf ("Ghi")); Assert.AreEqual ("abc", list[0].Name); Assert.AreEqual ("def", list[1].Name); Assert.AreEqual ("ghi", list[2].Name); Assert.AreEqual ("0", list["AbC"]); Assert.AreEqual ("1", list["dEf"]); Assert.AreEqual ("2", list["GHi"]); Assert.IsTrue (list.TryGetValue ("Abc", out parameter)); Assert.AreEqual ("abc", parameter.Name); Assert.IsTrue (list.TryGetValue ("Abc", out value)); Assert.AreEqual ("0", value); Assert.IsFalse (list.Remove ("xyz"), "Remove"); list.Insert (0, new Parameter ("xyz", "3")); Assert.IsTrue (list.Remove ("xyz"), "Remove"); var array = new Parameter[list.Count]; list.CopyTo (array, 0); Assert.AreEqual ("abc", array[0].Name); Assert.AreEqual ("def", array[1].Name); Assert.AreEqual ("ghi", array[2].Name); index = 0; foreach (var param in list) { Assert.AreEqual (array[index], param); index++; } list.Clear (); Assert.AreEqual (0, list.Count, "Clear"); list.Add ("xyz", "3"); list.Insert (0, array[2]); list.Insert (0, array[1].Name, array[1].Value); list.Insert (0, array[0]); Assert.AreEqual (4, list.Count); Assert.AreEqual ("abc", list[0].Name); Assert.AreEqual ("def", list[1].Name); Assert.AreEqual ("ghi", list[2].Name); Assert.AreEqual ("xyz", list[3].Name); Assert.AreEqual ("0", list["AbC"]); Assert.AreEqual ("1", list["dEf"]); Assert.AreEqual ("2", list["GHi"]); Assert.AreEqual ("3", list["XYZ"]); list.RemoveAt (3); Assert.AreEqual (3, list.Count); Assert.AreEqual ("; abc=\"0\"; def=\"1\"; ghi=\"2\"", list.ToString ()); }