Пример #1
0
 public void Copy([NotNull] Type type, [CanBeNull] object other)
 {
     ConstructorTestHelpers.Copy(
         type,
         other,
         o =>
     {
         var immediateType = new ImmediateType(type);
         return(immediateType.Copy(o));
     });
 }
Пример #2
0
        public void Copy_Throws()
        {
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            var immediateType = new ImmediateType(typeof(NoCopyConstructorClass));

            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new NoCopyConstructorClass()));

            immediateType = new ImmediateType(typeof(NotAccessibleCopyConstructor));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new NotAccessibleCopyConstructor()));

            immediateType = new ImmediateType(typeof(IList <int>));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new List <int>()));

            immediateType = new ImmediateType(typeof(IDictionary <int, string>));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new Dictionary <int, string>()));

            immediateType = new ImmediateType(typeof(AbstractCopyConstructor));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new CopyConstructorClass(12)));

            immediateType = new ImmediateType(typeof(StaticClass));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new CopyConstructorClass(12)));

            immediateType = new ImmediateType(typeof(TemplateStruct <>));
            Assert.Throws <ArgumentException>(() => immediateType.Copy(new CopyConstructorClass(12)));

            immediateType = new ImmediateType(typeof(TemplateCopyConstructor <>));
            Assert.Throws <ArgumentException>(() => immediateType.Copy(new CopyConstructorClass(12)));

            immediateType = new ImmediateType(typeof(NoCopyInheritedCopyConstructorClass));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new NoCopyInheritedCopyConstructorClass(1)));

            immediateType = new ImmediateType(typeof(NoCopyInheritedCopyConstructorClass));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new CopyConstructorClass(2)));

            immediateType = new ImmediateType(typeof(BaseCopyInheritedCopyConstructorClass));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new BaseCopyInheritedCopyConstructorClass(3)));

            immediateType = new ImmediateType(typeof(BaseCopyInheritedCopyConstructorClass));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new CopyConstructorClass(4))); // Constructor exists but is not considered as copy constructor

            immediateType = new ImmediateType(typeof(SpecializedCopyConstructorClass));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new SpecializedCopyConstructorClass(5)));

            immediateType = new ImmediateType(typeof(SpecializedCopyConstructorClass));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new InheritedSpecializedCopyConstructorClass(6))); // Constructor exists but is not considered as copy constructor

            immediateType = new ImmediateType(typeof(MultipleCopyConstructorClass));
            Assert.Throws <ArgumentException>(() => immediateType.Copy(new InheritedMultipleCopyConstructorClass(12))); // Constructor exists but is not considered as copy constructor

            // ReSharper disable once PossibleMistakenCallToGetType.2
            immediateType = new ImmediateType(typeof(CopyConstructorClass).GetType());
            Assert.Throws <ArgumentException>(() => immediateType.Copy(new CopyConstructorClass(12)));

            immediateType = new ImmediateType(typeof(CopyConstructorThrows));
            Assert.Throws(Is.InstanceOf <Exception>(), () => immediateType.Copy(new CopyConstructorThrows()));

            immediateType = new ImmediateType(typeof(int[]));
            Assert.Throws <MissingMethodException>(() => immediateType.Copy(new int[0]));

            // Wrong argument
            immediateType = new ImmediateType(typeof(CopyConstructorClass));
            Assert.Throws <ArgumentException>(() => immediateType.Copy(new NoCopyConstructorClass()));
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
        }