コード例 #1
0
        public void TestReadOnlyCloneable()
        {
            var cloneable = new GoodCloneableObject();

            Assert.AreNotSame(cloneable, cloneable.Clone());

            cloneable.IsReadOnly = true;
            Assert.AreSame(cloneable, cloneable.Clone());

            var mutableClone = (GoodCloneableObject)cloneable.MutableClone();
            Assert.AreNotSame(cloneable, mutableClone);
            Assert.DoesNotThrow(() =>
            {
                mutableClone.SomeProperty = 7;
            });

            cloneable = new GoodCloneableObject();
            var readonlyClone = (GoodCloneableObject)cloneable.ImmutableClone();
            Assert.AreNotSame(cloneable, readonlyClone);

            cloneable.IsReadOnly = true;
            readonlyClone = (GoodCloneableObject)cloneable.ImmutableClone();
            Assert.AreSame(cloneable, readonlyClone);

            Assert.Throws<InvalidOperationException>(() =>
            {
                readonlyClone.SomeProperty = 7;
            });
        }
コード例 #2
0
 public void TestGoodCloneable()
 {
     var goodguy = new GoodCloneableObject();
     var goodGuy2 = (GoodCloneableObject)goodguy.Clone();
     Assert.AreEqual(goodguy.SomeProperty, goodGuy2.SomeProperty);
 }
コード例 #3
0
        public void TestReadOnlyMutationCloneable()
        {
            var cloneable = new GoodCloneableObject {IsReadOnly = true};

            Assert.Throws<InvalidOperationException>(() =>
            {
                cloneable.IsReadOnly = false;
            }); // deja-vu...

            // This behavior is defined within the test, it's a simple reminder that this behavior
            // must be implemented on all Cloneables.
            Assert.Throws<InvalidOperationException>(() =>
            {
                cloneable.SomeProperty = 1;
            });
        }