Пример #1
0
 public void TestMerge()
 {
     TestGen(cl => {
         var src = new SampleMerge();
         src.DI.Add(3, 4);
         src.LI.Add(33);
         src.M = new Sample1 {
             X = 768, Y = "ttt"
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         CollectionAssert.AreEqual(src.DI, dst.DI);
         CollectionAssert.AreEqual(src.LI, dst.LI);
         Assert.AreNotEqual(src.M, dst.M);
         Assert.IsNull(dst.M);
     }, useBinary: false);
     TestGen(cl => {
         cl.Options.Meta = new MetaOptions().AddOverride(typeof(SampleMerge), o =>
                                                         o.AddItem(nameof(SampleMerge.Make), i => i.AddAttr(new YuzuFactory()))
                                                         );
         var src = new SampleMerge();
         src.DI.Add(3, 4);
         src.LI.Add(33);
         src.M = new Sample1 {
             X = 768, Y = "ttt"
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         CollectionAssert.AreEqual(src.DI, dst.DI);
         CollectionAssert.AreEqual(src.LI, dst.LI);
         Assert.AreNotEqual(src.M, dst.M);
         // Generated cloner is not affected by meta override.
         if (cl is ClonerGenBase)
         {
             Assert.IsNull(dst.M);
         }
         else
         {
             Assert.AreEqual(src.M.X, dst.M.X);
         }
     });
     TestGen(cl => {
         var src = new SampleMergeNonPrimitive();
         src.DI.Add(3, new Sample1 {
             X = 13
         });
         src.LI.Add(new Sample1 {
             X = 14
         });
         src.M = new Sample1 {
             X = 15
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         Assert.AreEqual(src.DI.Count, dst.DI.Count);
         Assert.AreEqual(src.DI[3].X, dst.DI[3].X);
         Assert.AreEqual(src.LI.Count, dst.LI.Count);
         Assert.AreEqual(src.LI[0].X, dst.LI[0].X);
         Assert.AreNotEqual(src.M, dst.M);
         Assert.AreEqual(src.M.X, dst.M.X);
     });
     TestGen(cl => {
         var src = new SampleCollection <int>();
         src.Add(3);
         src.Add(4);
         src.Filter = 1;
         var dst    = new SampleCollection <int>();
         dst.Add(5);
         cl.Merge(dst, src);
         Assert.AreNotEqual(src, dst);
         CollectionAssert.AreEqual(new List <int> {
             5, 3
         }, dst.ToList());
     });
 }
Пример #2
0
        public void TestSerializeItemIf()
        {
            TestGen(cl => {
                var src = new SampleCollection <int> {
                    5, 2, 4, 1
                };

                var dst1 = cl.Deep(src);
                Assert.AreNotEqual(src, dst1);
                Assert.AreEqual(src.Count, dst1.Count);
                CollectionAssert.AreEqual(src.ToList(), dst1.ToList());

                src.Filter = 1;
                var dst2   = cl.Deep(src);
                CollectionAssert.AreEqual(new List <int> {
                    5, 4
                }, dst2.ToList());

                src.Filter = 2;
                var dst3   = cl.Deep(src);
                CollectionAssert.AreEqual(new List <int> {
                    2, 4
                }, dst3.ToList());

                src.Filter = 3;
                var dst4   = cl.Deep(src);
                Assert.AreEqual(0, dst4.Count);
            });
            TestGen(cl => {
                var src = new SampleCollection <Sample1>();
                foreach (var i in new List <int> {
                    5, 2, 4, 1
                })
                {
                    src.Add(new Sample1 {
                        X = i
                    });
                }

                var dst1 = cl.Deep(src);
                Assert.AreNotEqual(src, dst1);
                Assert.AreEqual(src.Count, dst1.Count);
                foreach (var t in src.Zip(dst1, Tuple.Create))
                {
                    Assert.AreEqual(t.Item1.X, t.Item2.X);
                }

                src.Filter = 1;
                var dst2   = cl.Deep(src);
                Assert.AreEqual(2, dst2.Count);
                Assert.AreEqual(5, dst2.First().X);
                Assert.AreEqual(4, dst2.ElementAt(1).X);

                src.Filter = 3;
                var dst4   = cl.Deep(src);
                Assert.AreEqual(0, dst4.Count);
            });
            TestGen(cl => {
                var src = new SampleWithCollectionMerge();
                foreach (var i in new List <int> {
                    5, 2, 4, 1
                })
                {
                    src.A.Add(i);
                }

                var dst1 = cl.Deep(src);
                Assert.AreNotEqual(src, dst1);
                Assert.AreEqual(src.A.Count, dst1.A.Count);
                CollectionAssert.AreEqual(src.A.ToList(), dst1.A.ToList());

                src.A.Filter = 1;
                var dst2     = cl.Deep(src);
                CollectionAssert.AreEqual(new List <int> {
                    5, 4
                }, dst2.A.ToList());

                src.A.Filter = 2;
                var dst3     = cl.Deep(src);
                CollectionAssert.AreEqual(new List <int> {
                    2, 4
                }, dst3.A.ToList());

                src.A.Filter = 3;
                var dst4     = cl.Deep(src);
                Assert.AreEqual(0, dst4.A.Count);
            });
        }
Пример #3
0
 public void TestCollection()
 {
     TestGen(cl => {
         var src = new List <string> {
             "s1", "s2"
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         CollectionAssert.AreEqual(src, dst);
     });
     TestGen(cl => {
         var src = new List <Sample1> {
             new Sample1 {
                 X = 34
             }
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         Assert.AreEqual(src.Count, dst.Count);
         Assert.AreNotEqual(src[0], dst[0]);
         Assert.AreEqual(src[0].X, dst[0].X);
     });
     TestGen(cl => {
         var src = new SampleList {
             E = new List <string> {
                 "sq"
             }
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         Assert.AreEqual(src.E.Count, dst.E.Count);
         Assert.AreNotEqual(src.E, dst.E);
         Assert.AreEqual(src.E[0], dst.E[0]);
     });
     TestGen(cl => {
         var src = new SampleMatrix {
             M = new List <List <int> > {
                 new List <int> {
                     1, 2, 3
                 },
                 new List <int> {
                     4
                 },
             }
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         Assert.AreNotEqual(src.M, dst.M);
         Assert.AreNotEqual(src.M[0], dst.M[0]);
         CollectionAssert.AreEqual(src.M[0], dst.M[0]);
         CollectionAssert.AreEqual(src.M[1], dst.M[1]);
     });
     TestGen(cl => {
         var src = new SampleCollection <int> {
             1, 5
         };
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         int[] srcA = new int[2], dstA = new int[2];
         src.CopyTo(srcA, 0);
         dst.CopyTo(dstA, 0);
         CollectionAssert.AreEqual(srcA, dstA);
     });
     TestGen(cl => {
         var src = new SampleExplicitCollection <int>();
         ((ICollection <int>)src).Add(1);
         ((ICollection <int>)src).Add(5);
         var dst = cl.Deep(src);
         Assert.AreNotEqual(src, dst);
         CollectionAssert.AreEqual(src.ToList(), dst.ToList());
     });
 }