ExportProvider !

    [TestClass]
    public class ExportProviderTests
    {

        static ExportProvider Provider
        {
            get;
            set;
        }

        [ClassInitialize()]
        public static void Initialize(TestContext testContext)
        {
            var mock = new Mock<ITypeEnumerator>();
            mock
                .Setup(i => i.GetTypes())
                .Returns(() =>
                    {
                        return new List<Type>()
                        {
                            typeof(Export),
                            typeof(Export2),  
                            typeof(Export3),

                            typeof(OrderedExport),
                            typeof(OrderedExport2),

                            typeof(MutiExport),

                            typeof(MutiOrderedExport),

                            typeof(MixExport),

                            typeof(DuplicateExport),
                            typeof(DuplicateExport2),

                            typeof(OrderedExportIsPriorToExport)
                        };
                    });


            Provider = new ExportProvider(mock.Object);
        }


        #region Mock

        #region Export
        interface IExport
        {
        }

        [Export(typeof(IExport))]
        class Export : IExport
        {
        }

        [Export(typeof(IExport))]
        class Export2 : IExport
        {
        }

        [Export("Name",typeof(IExport))]
        class Export3 : IExport
        {
        }
        #endregion

        #region OrderExport
        interface IOrderedExport
        {
        }


        [OrderedExport(typeof(IOrderedExport), 100)]
        class OrderedExport : IOrderedExport
        {
        }

        [OrderedExport("OrderedExport2",typeof(IOrderedExport),200)]
        class OrderedExport2 : IOrderedExport
        {
        }
        #endregion

        #region MutiExport
        interface IMutiExport
        {
        }

        interface IMutiExport2
        {
        }

        [Export(typeof(IMutiExport2))]
        [Export(typeof(IMutiExport))]
        class MutiExport : IMutiExport, IMutiExport2
        {
        } 
        #endregion

        #region MutiOrderedExport
        interface IMutiOrderedExport
        {
        }

        interface IMutiOrderedExport2
        {
        }


        [OrderedExport(typeof(IMutiOrderedExport), 1)]
        [OrderedExport(typeof(IMutiOrderedExport2), 2)]
        class MutiOrderedExport : IMutiOrderedExport, IMutiOrderedExport2
        {
        } 
        #endregion

        #region MixExport
        interface IMixExport
        {

        }

        interface IMixOrderedExport
        {
        }

        [OrderedExport(typeof(IMixOrderedExport),1)]
        [Export(typeof(IMixExport))]
        class MixExport : IMixExport, IMixOrderedExport
        {
        }
        #endregion

        #region DuplicateExport
        interface IDuplicateExport
        {
        }

        [Export(typeof(IDuplicateExport))]
        [Export(typeof(IDuplicateExport))]
        class DuplicateExport : IDuplicateExport
        {
        }

        [OrderedExport(typeof(IDuplicateExport), 3)]
        [OrderedExport(typeof(IDuplicateExport), 4)]
        class DuplicateExport2 : IDuplicateExport
        {
        }
        
        #endregion


        #region OrderedExportIsPriorToExport
        interface IOrderedExportIsPriorToExport
        { 
        }

        [Export(typeof(IOrderedExportIsPriorToExport))]
        [OrderedExport(typeof(IOrderedExportIsPriorToExport),10)]
        class OrderedExportIsPriorToExport : IOrderedExportIsPriorToExport
        {
        }
        #endregion

      
        #endregion

        [TestMethod]
        public void should_support_to_exports()
        {
            var actual = Provider.FindExports(typeof(IExport));

            Assert.AreEqual(3, actual.Count());
            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(Export)));
            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(Export2)));
            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(Export3) && i.Name == "Name"));

        }

        [TestMethod]
        public void should_support_to_ordered_exports()
        {
            var actual = Provider.FindExports(typeof(IOrderedExport));

            Assert.AreEqual(2, actual.Count());
            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(OrderedExport) && i.Index == 100));
            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(OrderedExport2) && i.Index == 200 && i.Name == "OrderedExport2"));
        }

        [TestMethod]
        public void should_support_to_export_contracts_more_than_once()
        {
            var mutiExport = Provider.FindExports(typeof(IMutiExport));

            Assert.AreEqual(1, mutiExport.Count());
            Assert.IsTrue(mutiExport.Any(i => i.ExportType == typeof(MutiExport)));

            var mutiExport2 = Provider.FindExports(typeof(IMutiExport2));

            Assert.AreEqual(1, mutiExport2.Count());
            Assert.IsTrue(mutiExport2.Any(i => i.ExportType == typeof(MutiExport)));
        }

        [TestMethod]
        public void should_support_to_ordered_export_contracts_more_than_once()
        {
            var mutiExport = Provider.FindExports(typeof(IMutiOrderedExport));

            Assert.AreEqual(1, mutiExport.Count());
            Assert.IsTrue(mutiExport.Any(i => i.ExportType == typeof(MutiOrderedExport)));

            var mutiExport2 = Provider.FindExports(typeof(IMutiOrderedExport2));

            Assert.AreEqual(1, mutiExport2.Count());
            Assert.IsTrue(mutiExport2.Any(i => i.ExportType == typeof(MutiOrderedExport)));
        }

        [TestMethod]
        public void should_support_to_mix_export_with_export_and_ordered_export()
        {
            var export = Provider.FindExports(typeof(IMixExport));
            Assert.AreEqual(1, export.Count());
            Assert.IsTrue(export.Any(i=>i.ExportType == typeof(MixExport)));

            var orderedExport = Provider.FindExports(typeof(IMixOrderedExport));
            Assert.AreEqual(1, orderedExport.Count());
            Assert.IsTrue(orderedExport.Any(i=>i.ExportType == typeof(MixExport)));
        }

        [TestMethod]
        public void should_only_get_first_export_on_exporting_the_contract_more_then_once()
        {
            var actual = Provider.FindExports(typeof(IDuplicateExport));

            Assert.AreEqual(2, actual.Count());

            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(DuplicateExport)));
            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(DuplicateExport2) && i.Index == 3));
        }

        [TestMethod]
        public void should_keep_ordered_export_and_skip_export_because_ordered_export_is_prior_to_export()
        {
            var actual = Provider.FindExports(typeof(IOrderedExportIsPriorToExport));

            Assert.AreEqual(1, actual.Count());
            Assert.IsTrue(actual.Any(i => i.ExportType == typeof(OrderedExportIsPriorToExport) && i.Index == 10));
        }
    }

Last edited Dec 31, 2010 at 3:02 PM by cnlangzi, version 1

Comments

No comments yet.