/* Copyright 2011 Google Inc Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using Google.Apis.Requests.Parameters; using Google.Apis.Util; using System; using System.Collections.Generic; using Xunit; namespace Google.Apis.Tests.Apis.Requests.Parameters { /// Tests for the ParameterCollection class. public class ParameterCollectionTest { /// Tests the constructors. [Fact] public void ConstructTest() { new ParameterCollection(); // Assert does not throw } /// Confirm that different count of items can be added to the collection. [Fact] public void MultipleAddTest() { var col = new ParameterCollection(); // 1. Empty case. Assert.Empty(col); Assert.Equal(new string[0], col.GetAllMatches("test")); // 2. One element. col.Add("black", "hole"); Assert.Single(col); Assert.Equal(new[] { "hole" }, col.GetAllMatches("black")); // 3. Another element. col.Add("white", "dwarf"); Assert.Equal(2, col.Count); Assert.Equal(new[] { "hole" }, col.GetAllMatches("black")); Assert.Equal(new[] { "dwarf" }, col.GetAllMatches("white")); } /// Checks that duplicates are supported. [Fact] public void DuplicateTest() { var col = new ParameterCollection(); col.Add("chocolate", "cookie"); col.Add("chocolate", "bar"); col.Add("chocolate", "pudding"); Assert.Equal(3, col.Count); Assert.Equal(new[] { "cookie", "bar", "pudding" }, col.GetAllMatches("chocolate")); } /// Tests the FromQueryString method and confirms that duplicates are possible. [Fact] public void FromQueryStringTest() { const string query = "q=foo1&q=bar&q=foo2&chocolate=bar"; var collection = ParameterCollection.FromQueryString(query); Assert.Equal(4, collection.Count); Assert.Equal(new[] { "foo1", "bar", "foo2" }, collection.GetAllMatches("q")); Assert.Equal(new[] { "bar" }, collection.GetAllMatches("chocolate")); } /// Tests the FromQueryString method throws exception on invalid input. [Fact] public void FromQueryStringTest_Invalid() { const string query = "q=foo1&q=bar&q=foo2=invalid&chocolate=bar"; Assert.Throws(() => ParameterCollection.FromQueryString(query)); } /// Tests the FromQueryString method. [Fact] public void FromDictionaryTest() { Dictionary dictionary = new Dictionary(); dictionary.Add("q", new string[] { "foo", "bar" }); dictionary.Add("q2", new object[] { "foo", "bar" }); dictionary.Add("q3", (Repeatable)(new[] { "foo", "bar" })); dictionary.Add("chocolate", "bar"); dictionary.Add("test", null); // Test the created dictionary. var collection = ParameterCollection.FromDictionary(dictionary); Assert.Equal(new[] { "foo", "bar" }, collection.GetAllMatches("q")); Assert.Equal(new[] { "foo", "bar" }, collection.GetAllMatches("q2")); Assert.Equal(new[] { "foo", "bar" }, collection.GetAllMatches("q3")); Assert.Equal(new[] { "bar" }, collection.GetAllMatches("chocolate")); Assert.Equal(new string[] { null }, collection.GetAllMatches("test")); Assert.Equal(8, collection.Count); } } }