OLD | NEW |
1 /* | 1 /* |
2 Copyright 2011 Google Inc | 2 Copyright 2011 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
11 distributed under the License is distributed on an "AS IS" BASIS, | 11 distributed under the License is distributed on an "AS IS" BASIS, |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 See the License for the specific language governing permissions and | 13 See the License for the specific language governing permissions and |
14 limitations under the License. | 14 limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 using System; | 17 using System; |
18 using System.Collections.Generic; | 18 using System.Collections.Generic; |
19 | 19 |
20 using NUnit.Framework; | 20 using NUnit.Framework; |
21 | 21 |
22 using Google.Apis.Requests; | 22 using Google.Apis.Requests.Parameters; |
23 using Google.Apis.Util; | 23 using Google.Apis.Util; |
24 | 24 |
25 namespace Google.Apis.Tests.Apis.Requests | 25 namespace Google.Apis.Tests.Apis.Requests |
26 { | 26 { |
27 /// <summary> | 27 /// <summary>Tests for the ParameterCollection class.</summary> |
28 /// Tests for the ParameterCollection class. | |
29 /// </summary> | |
30 [TestFixture] | 28 [TestFixture] |
31 public class ParameterCollectionTest | 29 public class ParameterCollectionTest |
32 { | 30 { |
33 /// <summary> | 31 /// <summary>Tests the constructors.</summary> |
34 /// Tests the constructors. | |
35 /// </summary> | |
36 [Test] | 32 [Test] |
37 public void ConstructTest() | 33 public void ConstructTest() |
38 { | 34 { |
39 Assert.DoesNotThrow(() => new ParameterCollection()); | 35 Assert.DoesNotThrow(() => new ParameterCollection()); |
40 } | 36 } |
41 | 37 |
42 /// <summary> | 38 /// <summary>Confirm that different counts of items can be added to the
collection.</summary> |
43 /// Confirm that different counts of items can be added to the collectio
n. | |
44 /// </summary> | |
45 [Test] | 39 [Test] |
46 public void MultipleAddTest() | 40 public void MultipleAddTest() |
47 { | 41 { |
48 var col = new ParameterCollection(); | 42 var col = new ParameterCollection(); |
49 | 43 |
50 // 1. Empty case. | 44 // 1. Empty case. |
51 Assert.That(col.Count, Is.EqualTo(0)); | 45 Assert.That(col.Count, Is.EqualTo(0)); |
52 CollectionAssert.AreEqual(new string[0], col.GetAllMatches("test")); | 46 CollectionAssert.AreEqual(new string[0], col.GetAllMatches("test")); |
53 | 47 |
54 // 2. One element. | 48 // 2. One element. |
55 col.Add("black", "hole"); | 49 col.Add("black", "hole"); |
56 Assert.That(col.Count, Is.EqualTo(1)); | 50 Assert.That(col.Count, Is.EqualTo(1)); |
57 CollectionAssert.AreEqual(new[] { "hole" }, col.GetAllMatches("black
")); | 51 CollectionAssert.AreEqual(new[] { "hole" }, col.GetAllMatches("black
")); |
58 | 52 |
59 // 3. Another element. | 53 // 3. Another element. |
60 col.Add("white", "dwarf"); | 54 col.Add("white", "dwarf"); |
61 Assert.That(col.Count, Is.EqualTo(2)); | 55 Assert.That(col.Count, Is.EqualTo(2)); |
62 CollectionAssert.AreEqual(new[] { "hole" }, col.GetAllMatches("black
")); | 56 CollectionAssert.AreEqual(new[] { "hole" }, col.GetAllMatches("black
")); |
63 CollectionAssert.AreEqual(new[] { "dwarf" }, col.GetAllMatches("whit
e")); | 57 CollectionAssert.AreEqual(new[] { "dwarf" }, col.GetAllMatches("whit
e")); |
64 } | 58 } |
65 | 59 |
66 /// <summary> | 60 /// <summary>Checks that duplicates are supported.</summary> |
67 /// Checks that duplicates are supported. | |
68 /// </summary> | |
69 [Test] | 61 [Test] |
70 public void DuplicateTest() | 62 public void DuplicateTest() |
71 { | 63 { |
72 var col = new ParameterCollection(); | 64 var col = new ParameterCollection(); |
73 col.Add("chocolate", "cookie"); | 65 col.Add("chocolate", "cookie"); |
74 col.Add("chocolate", "bar"); | 66 col.Add("chocolate", "bar"); |
75 col.Add("chocolate", "pudding"); | 67 col.Add("chocolate", "pudding"); |
76 Assert.That(col.Count, Is.EqualTo(3)); | 68 Assert.That(col.Count, Is.EqualTo(3)); |
77 CollectionAssert.AreEqual(new[] { "cookie", "bar", "pudding" }, col.
GetAllMatches("chocolate")); | 69 CollectionAssert.AreEqual(new[] { "cookie", "bar", "pudding" }, col.
GetAllMatches("chocolate")); |
78 } | 70 } |
79 | 71 |
80 /// <summary> Tests the FromQueryString method and confirms that duplica
tes are possible. </summary> | 72 /// <summary>Tests the FromQueryString method and confirms that duplicat
es are possible.</summary> |
81 [Test] | 73 [Test] |
82 public void FromQueryStringTest() | 74 public void FromQueryStringTest() |
83 { | 75 { |
84 const string query = "q=foo1&q=bar&q=foo2&chocolate=bar"; | 76 const string query = "q=foo1&q=bar&q=foo2&chocolate=bar"; |
85 var collection = ParameterCollection.FromQueryString(query); | 77 var collection = ParameterCollection.FromQueryString(query); |
86 Assert.That(collection.Count, Is.EqualTo(4)); | 78 Assert.That(collection.Count, Is.EqualTo(4)); |
87 CollectionAssert.AreEqual(new[] { "foo1", "bar", "foo2" }, collectio
n.GetAllMatches("q")); | 79 CollectionAssert.AreEqual(new[] { "foo1", "bar", "foo2" }, collectio
n.GetAllMatches("q")); |
88 CollectionAssert.AreEqual(new[] { "bar" }, collection.GetAllMatches(
"chocolate")); | 80 CollectionAssert.AreEqual(new[] { "bar" }, collection.GetAllMatches(
"chocolate")); |
89 } | 81 } |
90 | 82 |
91 /// <summary> Tests the FromQueryString method throws exception on inval
id input. </summary> | 83 /// <summary>Tests the FromQueryString method throws exception on invali
d input.</summary> |
92 [Test] | 84 [Test] |
93 public void FromQueryStringTest_Invalid() | 85 public void FromQueryStringTest_Invalid() |
94 { | 86 { |
95 const string query = "q=foo1&q=bar&q=foo2=invalid&chocolate=bar"; | 87 const string query = "q=foo1&q=bar&q=foo2=invalid&chocolate=bar"; |
96 Assert.Throws<ArgumentException>(() => ParameterCollection.FromQuery
String(query)); | 88 Assert.Throws<ArgumentException>(() => ParameterCollection.FromQuery
String(query)); |
97 } | 89 } |
98 | 90 |
99 /// <summary> | 91 /// <summary>Tests the FromQueryString method.</summary> |
100 /// Tests the FromQueryString method. | |
101 /// </summary> | |
102 [Test] | 92 [Test] |
103 public void FromDictionaryTest() | 93 public void FromDictionaryTest() |
104 { | 94 { |
105 Dictionary<string, object> dictionary = new Dictionary<string, objec
t>(); | 95 Dictionary<string, object> dictionary = new Dictionary<string, objec
t>(); |
106 dictionary.Add("q", new string[] { "foo", "bar" }); | 96 dictionary.Add("q", new string[] { "foo", "bar" }); |
107 dictionary.Add("q2", new object[] { "foo", "bar" }); | 97 dictionary.Add("q2", new object[] { "foo", "bar" }); |
108 dictionary.Add("q3", (Repeatable<string>)(new[] { "foo", "bar" })); | 98 dictionary.Add("q3", (Repeatable<string>)(new[] { "foo", "bar" })); |
109 dictionary.Add("chocolate", "bar"); | 99 dictionary.Add("chocolate", "bar"); |
110 dictionary.Add("test", null); | 100 dictionary.Add("test", null); |
111 | 101 |
112 // Test the created dictionary. | 102 // Test the created dictionary. |
113 var collection = ParameterCollection.FromDictionary(dictionary); | 103 var collection = ParameterCollection.FromDictionary(dictionary); |
114 CollectionAssert.AreEqual(new[] { "foo", "bar" }, collection.GetAllM
atches("q")); | 104 CollectionAssert.AreEqual(new[] { "foo", "bar" }, collection.GetAllM
atches("q")); |
115 CollectionAssert.AreEqual(new[] { "foo", "bar" }, collection.GetAllM
atches("q2")); | 105 CollectionAssert.AreEqual(new[] { "foo", "bar" }, collection.GetAllM
atches("q2")); |
116 CollectionAssert.AreEqual(new[] { "foo", "bar" }, collection.GetAllM
atches("q3")); | 106 CollectionAssert.AreEqual(new[] { "foo", "bar" }, collection.GetAllM
atches("q3")); |
117 CollectionAssert.AreEqual(new[] { "bar" }, collection.GetAllMatches(
"chocolate")); | 107 CollectionAssert.AreEqual(new[] { "bar" }, collection.GetAllMatches(
"chocolate")); |
118 CollectionAssert.AreEqual(new string[] { null }, collection.GetAllMa
tches("test")); | 108 CollectionAssert.AreEqual(new string[] { null }, collection.GetAllMa
tches("test")); |
119 Assert.That(collection.Count, Is.EqualTo(8)); | 109 Assert.That(collection.Count, Is.EqualTo(8)); |
120 } | 110 } |
121 } | 111 } |
122 } | 112 } |
OLD | NEW |