Esempio n. 1
0
        public void TestAllowReadingFromAncestor()
        {
            var bs = new BinarySerializer();
            var v1 = new Sample2 { X = 83, Y = "83" };
            var result1 = bs.ToBytes(v1);
            Assert.AreEqual(
                "20 01 00 " + XS(typeof(Sample2)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) +
                " 01 00 53 00 00 00 00 00", XS(result1));

            var w1 = new Sample2Allow();
            var bd = new BinaryDeserializer();
            bd.FromBytes(w1, result1);
            Assert.AreEqual(v1.X, w1.X);
        }
Esempio n. 2
0
        public void TestAllowReadingFromAncestor()
        {
            var js = new JsonSerializer();
            js.JsonOptions.Indent = "";
            js.JsonOptions.SaveRootClass = true;
            var v1 = new Sample2 { X = 83, Y = "83" };
            var result1 = js.ToString(v1);
            Assert.AreEqual("{\n\"class\":\"YuzuTest.Sample2, YuzuTest\",\n\"X\":83\n}", result1);

            var w1 = new Sample2Allow();
            var jd = new JsonDeserializer();
            jd.FromString(w1, result1);
            Assert.AreEqual(v1.X, w1.X);
        }
Esempio n. 3
0
        public void TestErrors()
        {
            var bd = new BinaryDeserializer();
            bd.Options.AllowEmptyTypes = true;
            var bdg = new BinaryDeserializerGenerator();

            XAssert.Throws<YuzuException>(() => bdg.Generate<ISample>(), "ISample");
            XAssert.Throws<YuzuException>(() => bdg.Generate<SampleAbstract>(), "SampleAbstract");
            XAssert.Throws<YuzuException>(() => bd.FromBytes<int>(new byte[] { 0xFF }), "255");

            XAssert.Throws<YuzuException>(() => bd.FromBytes(new byte[] { 0xFF }), "255");
            XAssert.Throws<YuzuException>(() => bd.FromBytes<int>(new byte[] { 0xFF }), "255");
            XAssert.Throws<YuzuException>(() => bd.FromBytes<int>(new byte[] { 07 }), "Int32");

            XAssert.Throws<YuzuException>(() => bd.FromBytes(new byte[] { (byte)RoughType.Any }), "pure");

            XAssert.Throws<YuzuException>(() => bd.FromBytes<Sample1>(SX(
                "20 01 00 " + XS("notype") + " 00 00 00 00"
            )), "YuzuUnknown");

            var w = new Sample1();
            XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX(
                "20 02 00 " + XS(typeof(Empty)) + " 00 00 00 00"
            )), "Sample1");

            var w2 = new Sample2Allow();
            XAssert.Throws<YuzuException>(() => bd.FromBytes(w2, SX(
                "20 02 00 00 00"
            )), "Sample2Allow");

            XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX("20 05 00")), "5");

            XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX(
                "20 03 00 " + XS(typeof(Sample1)) + " 00 00 00 00"
            )), " X ");

            XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX(
                "20 03 00 " + XS(typeof(Empty)) + " 00 01 " + XS("New", RoughType.Int) + " 00 00"
            )), "New");

            XAssert.Throws<YuzuException>(() => bd.FromBytes(w, SX(
                "20 03 00 " + XS(typeof(Sample1)) + " 00 01 " + XS("X", RoughType.String) + " 00 00"
            )), "Int32");
        }