コード例 #1
0
        static void Main(string[] args)
        {
            //////////////// SPEED TEST //////////////////

            var watches0 = Stopwatch.StartNew();

            var list = new List <ModerateClass>();

            for (int i = 0; i < 10000; i++)
            {
                list.Add(ModerateClass.CreateForTests(i));
            }

            Console.WriteLine("List of " + list.Count + " objects generated in: " + watches0.ElapsedMilliseconds);

            var watches1 = Stopwatch.StartNew();

            list.ForEach(a => DeepCopyBySerialization.DeepClone(a));

            Console.WriteLine("By Serialization: " + watches1.ElapsedMilliseconds);

            var watches2 = Stopwatch.StartNew();

            list.ForEach(a => DeepCopyByReflection.Copy(a));

            Console.WriteLine("By Reflection: " + watches2.ElapsedMilliseconds);

            var watches3 = Stopwatch.StartNew();

            list.ForEach(a => DeepCopyByExpressionTrees.DeepCopyByExpressionTree(a));

            Console.WriteLine("By Expression Trees: " + watches3.ElapsedMilliseconds);

            Console.ReadLine();
        }
コード例 #2
0
        public void Test1()
        {
            var m     = ModerateClass.CreateForTests(1);
            var mCopy = (ModerateClass)CopyFunctionSelection.CopyMethod(m);

            // test that the copy is a different instance but with equal content
            Assert_AreEqualButNotSame(m, mCopy);

            // test of copy if we insert it as interface
            var mAsCopiedAsInterface = (ModerateClass)CopyFunctionSelection.CopyMethod((ISimpleClass)m);

            Assert_AreEqualButNotSame(m, mAsCopiedAsInterface);
        }
コード例 #3
0
        public void GivenRatingIsLowerThanHalfOfRatingCeiling_ShouldContainLOW()
        {
            int rating       = 1;
            int ratingCeling = 4;

            externalRatingApprovalService.Setup(mock => mock.IsApproved(rating)).Returns(true);
            moderateClass = new ModerateClass(notificationService.Object, externalRatingApprovalService.Object);
            using (ShimsContext.Create())
            {
                String result = moderateClass.CreateRatingString(rating, ratingCeling);
                Assert.AreEqual("LOW-1", result);
                externalRatingApprovalService.Verify(mock => mock.IsApproved(rating), Times.Once());
                notificationService.Verify(mock => mock.Notify(rating), Times.Once());
            }
        }
コード例 #4
0
        public void GivenRatingGreaterThanRatingCeiling_ShouldFail()
        {
            //ARRANGE AND ACT
            int rating       = 2;
            int ratingCeling = 1;

            using (ShimsContext.Create())
            {
                moderateClass = new ModerateClass(notificationService.Object, externalRatingApprovalService.Object);
                //ASSERT
                Should.Throw <ArgumentException>(() => {
                    moderateClass.CreateRatingString(rating, ratingCeling);
                }).Message.ShouldBe("Cannot be over the hard ceiling");
            }
        }
コード例 #5
0
        public void GivenRatingIsEqualToRatingCeilingAndExternalRatingIsApprovedFalse_ShouldContainNOTAPP()
        {
            //ARRANGE AND ACT
            int rating       = 2;
            int ratingCeling = 2;

            using (ShimsContext.Create())
            {
                moderateClass = new ModerateClass(notificationService.Object, externalRatingApprovalService.Object);
                externalRatingApprovalService.Setup(mock => mock.IsApproved(rating)).Returns(false);
                string result = moderateClass.CreateRatingString(rating, ratingCeling);
                //ASSERT
                Assert.AreEqual("NOT-APP", result);
                externalRatingApprovalService.Verify(mock => mock.IsApproved(rating), Times.Once());
            }
        }
コード例 #6
0
        public static void Assert_AreEqualButNotSame(ModerateClass m, ModerateClass mCopy)
        {
            if (m == null && mCopy == null)
            {
                return;
            }

            // original and copy are different instances
            Assert.AreNotSame(m, mCopy);

            // the same values in fields
            Assert.AreEqual(m.FieldPublic2, mCopy.FieldPublic2);
            Assert.AreEqual(m.PropertyPublic2, mCopy.PropertyPublic2);
            Assert.AreEqual(m.FieldPublic, mCopy.FieldPublic);
            Assert.AreEqual(m.GetPrivateField2(), mCopy.GetPrivateField2());
            Assert.AreEqual(m.GetPrivateProperty2(), mCopy.GetPrivateProperty2());
            Assert.AreEqual(m.GetPrivateField(), mCopy.GetPrivateField());
            Assert.AreEqual(m.GetPrivateProperty(), mCopy.GetPrivateProperty());
            Assert.AreEqual((string)m.ObjectTextProperty, (string)mCopy.ObjectTextProperty);

            // check that structs copied well (but with different instances of subclasses)
            Assert_StructsAreEqual(m.StructField, mCopy.StructField);

            // chech that classes in structs in structs are copied well
            Assert_DeeperStructsAreEqual(m.DeeperStructField, mCopy.DeeperStructField);

            // generic classes are well copied
            Assert_GenericClassesAreEqual(m.GenericClassField, mCopy.GenericClassField);

            // subclass in property copied well
            SimpleClassTests.Assert_AreEqualButNotSame(m.SimpleClassProperty, mCopy.SimpleClassProperty);

            // subclass in readonly field copied well
            SimpleClassTests.Assert_AreEqualButNotSame(m.ReadonlySimpleClassField, mCopy.ReadonlySimpleClassField);

            // array of subclasses copied well
            if (m.SimpleClassArray != null)
            {
                Assert.AreEqual(m.SimpleClassArray.Length, mCopy.SimpleClassArray.Length);

                for (int i = 0; i < m.SimpleClassArray.Length; i++)
                {
                    SimpleClassTests.Assert_AreEqualButNotSame(m.SimpleClassArray[i], mCopy.SimpleClassArray[i]);
                }
            }
        }
コード例 #7
0
        public void ShouldFail_WhenExternalRatingApprovalServiceIsNotApprovedAndRatingIsLowerThanRatingCeiling()
        {
            int rating       = 1;
            int ratingCeling = 2;

            //ARRANGE
            externalRatingApprovalService.Setup(mock => mock.IsApproved(rating)).Returns(false);
            moderateClass = new ModerateClass(notificationService.Object, externalRatingApprovalService.Object);
            //ACT
            using (ShimsContext.Create())
            {
                string result = moderateClass.CreateRatingString(rating, ratingCeling);
                //ASSERT
                Assert.AreEqual("NOT-APP", result);
                externalRatingApprovalService.Verify(mock => mock.IsApproved(rating), Times.Once());
            }
        }
コード例 #8
0
        public void GivenRatingIsLowerThanHalfOfRatingCeilingAndLastRatingIsEqualToRating_ShouldContainLOWAndCACHED()
        {
            int rating       = 1;
            int ratingCeling = 4;

            externalRatingApprovalService.Setup(mock => mock.IsApproved(rating)).Returns(true);
            moderateClass = new ModerateClass(notificationService.Object, externalRatingApprovalService.Object);
            using (ShimsContext.Create())
            {
                PrivateObject privSub = new PrivateObject(moderateClass, new PrivateType(typeof(ModerateClass)));
                privSub.SetField("lastRating", 1);
                String result = moderateClass.CreateRatingString(rating, ratingCeling);
                Assert.AreEqual("LOW-1-CACHED", result);
                externalRatingApprovalService.Verify(mock => mock.IsApproved(rating), Times.Once());
                notificationService.Verify(mock => mock.Notify(rating), Times.Once());
            }
        }