Click here to Skip to main content
15,893,814 members
Articles / Desktop Programming / WPF

Calcium: A Modular Application Toolset Leveraging PRISM – Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (48 votes)
23 Nov 2009BSD12 min read 117.7K   3   90  
Calcium provides much of what one needs to rapidly build a multifaceted and sophisticated modular application. Includes a host of modules and services, and an infrastructure that is ready to use in your next application.
//===================================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation and Silverlight
//===================================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===================================================================================
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Practices.Composite.Tests
{
    [TestClass]
    public class ListDictionaryFixture
    {
        static ListDictionary<string, object> list;

        [TestInitialize]
        public void SetUp()
        {
            list = new ListDictionary<string, object>();
        }

        [ExpectedException(typeof(ArgumentNullException))]
        [TestMethod]
        public void AddThrowsIfKeyNull()
        {
            list.Add(null, new object());
        }

        [ExpectedException(typeof(ArgumentNullException))]
        [TestMethod]
        public void AddThrowsIfValueNull()
        {
            list.Add("", null);
        }

        [TestMethod]
        public void CanAddValue()
        {
            object value1 = new object();
            object value2 = new object();

            list.Add("foo", value1);
            list.Add("foo", value2);

            Assert.AreEqual(2, list["foo"].Count);
            Assert.AreSame(value1, list["foo"][0]);
            Assert.AreSame(value2, list["foo"][1]);
        }

        [TestMethod]
        public void CanIndexValuesByKey()
        {
            list.Add("foo", new object());
            list.Add("foo", new object());

            Assert.AreEqual(2, list["foo"].Count);
        }

        [ExpectedException(typeof(ArgumentNullException))]
        [TestMethod]
        public void ThrowsIfRemoveKeyNull()
        {
            list.Remove(null, new object());
        }

        [TestMethod]
        public void CanRemoveValue()
        {
            object value = new object();

            list.Add("foo", value);
            list.Remove("foo", value);

            Assert.AreEqual(0, list["foo"].Count);
        }

        [TestMethod]
        public void CanRemoveValueFromAllLists()
        {
            object value = new object();
            list.Add("foo", value);
            list.Add("bar", value);

            list.Remove(value);

            Assert.AreEqual(0, list.Values.Count);
        }

        [TestMethod]
        public void RemoveNonExistingValueNoOp()
        {
            list.Add("foo", new object());

            list.Remove("foo", new object());
        }

        [TestMethod]
        public void RemoveNonExistingKeyNoOp()
        {
            list.Remove("foo", new object());
        }

        [ExpectedException(typeof(ArgumentNullException))]
        [TestMethod]
        public void ThrowsIfRemoveListKeyNull()
        {
            list.Remove(null);
        }

        [TestMethod]
        public void CanRemoveList()
        {
            list.Add("foo", new object());
            list.Add("foo", new object());

            bool removed = list.Remove("foo");

            Assert.IsTrue(removed);
            Assert.AreEqual(0, list.Keys.Count);
        }

        [TestMethod]
        public void CanSetList()
        {
            List<object> values = new List<object>();
            values.Add(new object());
            list.Add("foo", new object());
            list.Add("foo", new object());

            list["foo"] = values;

            Assert.AreEqual(1, list["foo"].Count);
        }

        [TestMethod]
        public void CanEnumerateKeyValueList()
        {
            int count = 0;
            list.Add("foo", new object());
            list.Add("foo", new object());

            foreach (KeyValuePair<string, IList<object>> pair in list)
            {
                foreach (object value in pair.Value)
                {
                    count++;
                }
                Assert.AreEqual("foo", pair.Key);
            }

            Assert.AreEqual(2, count);
        }

        [TestMethod]
        public void CanGetFlatListOfValues()
        {
            list.Add("foo", new object());
            list.Add("foo", new object());
            list.Add("bar", new object());

            IList<object> values = list.Values;

            Assert.AreEqual(3, values.Count);
        }

        [TestMethod]
        public void IndexerAccessAlwaysSucceeds()
        {
            IList<object> values = list["foo"];

            Assert.IsNotNull(values);
        }


        [ExpectedException(typeof(ArgumentNullException))]
        [TestMethod]
        public void ThrowsIfContainsKeyNull()
        {
            list.ContainsKey(null);
        }

        [TestMethod]
        public void CanAskContainsKey()
        {
            Assert.IsFalse(list.ContainsKey("foo"));
        }

        [TestMethod]
        public void CanAskContainsValueInAnyList()
        {
            object obj = new object();
            list.Add("foo", new object());
            list.Add("bar", new object());
            list.Add("baz", obj);

            bool contains = list.ContainsValue(obj);

            Assert.IsTrue(contains);
        }

        [TestMethod]
        public void CanClearDictionary()
        {
            list.Add("foo", new object());
            list.Add("bar", new object());
            list.Add("baz", new object());

            list.Clear();

            Assert.AreEqual(0, list.Count);
        }

        [TestMethod]
        public void CanGetFilteredValuesByKeys()
        {
            list.Add("foo", new object());
            list.Add("bar", new object());
            list.Add("baz", new object());

            IEnumerable<object> filtered = list.FindAllValuesByKey(delegate(string key)
                                                                       {
                                                                           return key.StartsWith("b");
                                                                       });

            int count = 0;
            foreach (object obj in filtered)
            {
                count++;
            }

            Assert.AreEqual(2, count);
        }

        [TestMethod]
        public void CanGetFilteredValues()
        {
            list.Add("foo", DateTime.Now);
            list.Add("bar", new object());
            list.Add("baz", DateTime.Today);

            IEnumerable<object> filtered = list.FindAllValues(delegate(object value)
                                                                  {
                                                                      return value is DateTime;
                                                                  });
            int count = 0;
            foreach (object obj in filtered)
            {
                count++;
            }

            Assert.AreEqual(2, count);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions