LazyMemberInfoTests.cs :  » 2.6.4-mono-.net-core » System.ComponentModel » System » ComponentModel » Composition » ReflectionModel » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » 2.6.4 mono .net core » System.ComponentModel 
System.ComponentModel » System » ComponentModel » Composition » ReflectionModel » LazyMemberInfoTests.cs
// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.ComponentModel.Composition.Factories;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Reflection;
using Microsoft.Internal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using System.UnitTesting;
using System.Threading;

namespace System.ComponentModel.Composition.ReflectionModel{
    [TestClass]
    public class LazyMemberInfoTests
    {
        [TestMethod]
        public void Constructor_PassMember()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                LazyMemberInfo lazy = new LazyMemberInfo(member);
                Assert.AreEqual(memberType, lazy.MemberType);
                Assert.AreEqual(accessors.Length, lazy.GetAccessors().Length);
                Assert.IsTrue(accessors.SequenceEqual(lazy.GetAccessors()));
            }
        }

        [TestMethod]
        public void Constructor_PassNullMember()
        {
            ExceptionAssert.ThrowsArgument<ArgumentNullException>("member", () =>
                {
                    LazyMemberInfo lazy = new LazyMemberInfo((MemberInfo)null);
                });
        }

        [TestMethod]
        public void Constructor_PassAccessors()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                LazyMemberInfo lazy = new LazyMemberInfo(memberType, accessors);
                Assert.AreEqual(memberType, lazy.MemberType);
                Assert.AreEqual(accessors.Length, lazy.GetAccessors().Length);
                Assert.IsTrue(accessors.SequenceEqual(lazy.GetAccessors()));
            }
        }

        [TestMethod]
        public void Constructor_PassInvalidAccessors()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                foreach (MemberTypes wrongMemberType in GetValidMemberTypes())
                {
                    if (wrongMemberType == memberType)
                    {
                        continue;
                    }
                    ExceptionAssert.ThrowsArgument<ArgumentException>("accessors", () =>
                    {
                        LazyMemberInfo lazy = new LazyMemberInfo(wrongMemberType, accessors);
                    });
                }
            }
        }

        [TestMethod]
        public void Constructor_PassAccessorsInvalidMemberType()
        {
            MemberTypes[] validMemberTypes = GetValidMemberTypes().ToArray();
            foreach (MemberTypes memberType in GetMemberTypeValues())
            {
                if (!validMemberTypes.Contains(memberType))
                {
                    ExceptionAssert.ThrowsArgument<ArgumentException>("memberType", () =>
                    {
                        LazyMemberInfo lazy = new LazyMemberInfo(memberType, typeof(LazyMemberTestClass));
                    });
                }
            }
        }

        [TestMethod]
        public void Constructor_PassNullAccessors()
        {
            ExceptionAssert.ThrowsArgument<ArgumentNullException>("accessors", () =>
            {
                LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (MemberInfo[])null);
            });
        }

        [TestMethod]
        public void Constructor_PassAccessorsWithNulls()
        {
            ExceptionAssert.ThrowsArgument<ArgumentException>("accessors", () =>
            {
                LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, new MemberInfo[] { null, null });
            });
        }

        [TestMethod]
        public void Constructor_PassAccessorCreators()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                LazyMemberInfo lazy = new LazyMemberInfo(memberType, () => accessors);
                Assert.AreEqual(memberType, lazy.MemberType);
                Assert.AreEqual(accessors.Length, lazy.GetAccessors().Length);
                Assert.IsTrue(accessors.SequenceEqual(lazy.GetAccessors()));
            }
        }

        [TestMethod]
        public void Constructor_PassInvalidAccessorCreators()
        {
            foreach (var memberAndAccessorsInfo in GetMembersAndAccessors(typeof(LazyMemberTestClass)))
            {
                MemberInfo member = memberAndAccessorsInfo.Item1;
                MemberTypes memberType = memberAndAccessorsInfo.Item2.Item1;
                MemberInfo[] accessors = memberAndAccessorsInfo.Item2.Item2;

                foreach (MemberTypes wrongMemberType in GetValidMemberTypes())
                {
                    if (wrongMemberType == memberType)
                    {
                        continue;
                    }
                    LazyMemberInfo lazy = new LazyMemberInfo(wrongMemberType, () => accessors);
                    ExceptionAssert.Throws<InvalidOperationException>(() =>
                    {
                        lazy.GetAccessors();
                    });
                }
            }
        }

        [TestMethod]
        public void Constructor_PassAccessorCreatorsWithInvalidMemberType()
        {
            MemberTypes[] validMemberTypes = GetValidMemberTypes().ToArray();
            foreach (MemberTypes memberType in GetMemberTypeValues())
            {
                if (!validMemberTypes.Contains(memberType))
                {
                    ExceptionAssert.ThrowsArgument<ArgumentException>("memberType", () =>
                    {
                        LazyMemberInfo lazy = new LazyMemberInfo(memberType, () => new MemberInfo[] { typeof(LazyMemberTestClass) });
                    });
                }
            }
        }

        [TestMethod]
        public void Constructor_PassNullAccessorCreators()
        {
            ExceptionAssert.ThrowsArgument<ArgumentNullException>("accessorsCreator", () =>
            {
                LazyMemberInfo lazy = new LazyMemberInfo(MemberTypes.Field, (Func<MemberInfo[]>)null);
            });
        }


        private static IEnumerable<Tuple<MemberInfo, Tuple<MemberTypes, MemberInfo[]>>> GetMembersAndAccessors(Type type)
        {
            yield return new Tuple<MemberInfo, Tuple<MemberTypes, MemberInfo[]>>(
                type, new Tuple<MemberTypes, MemberInfo[]>(type.MemberType, new MemberInfo[] { type }));

            foreach (MemberInfo member in type.GetMembers())
            {
                MemberInfo[] accessors = null;
                if (member.MemberType == MemberTypes.Property)
                {
                    PropertyInfo property = (PropertyInfo)member;
                    accessors = new MemberInfo[] { property.GetGetMethod(true), property.GetSetMethod(true) };
                }
                else if (member.MemberType == MemberTypes.Event)
                {
                    EventInfo event_ = (EventInfo)member;
                    accessors = new MemberInfo[] { event_.GetRaiseMethod(true), event_.GetAddMethod(true), event_.GetRemoveMethod(true) };
                }
                else
                {
                    accessors = new MemberInfo[] { member };
                }

                yield return new Tuple<MemberInfo, Tuple<MemberTypes, MemberInfo[]>>(
                    member, new Tuple<MemberTypes, MemberInfo[]>(member.MemberType, accessors));
            }
        }

        private static IEnumerable<MemberTypes> GetMemberTypeValues()
        {
            yield return MemberTypes.All;
            yield return MemberTypes.Constructor;
            yield return MemberTypes.Custom;
            yield return MemberTypes.Event;
            yield return MemberTypes.Field;
            yield return MemberTypes.Method;
            yield return MemberTypes.NestedType;
            yield return MemberTypes.Property;
            yield return MemberTypes.TypeInfo;
        }

        private static IEnumerable<MemberTypes> GetValidMemberTypes()
        {
            yield return MemberTypes.TypeInfo;
            yield return MemberTypes.NestedType;
            yield return MemberTypes.Constructor;
            yield return MemberTypes.Field;
            yield return  MemberTypes.Method;
            yield return  MemberTypes.Property;
            yield return MemberTypes.Event;
        }

        public class LazyMemberTestClass
        {
            public LazyMemberTestClass() { }
            public string Property { get; set;  }
            public string SetProperty { set { } }
            public string GetProperty { get { return null; } }
            public string Field;
            public void Method() { this.Event(this, new EventArgs()); }
            public event EventHandler Event;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.