public void Can_Get_Property_Attribute() { var sample = new Sample1(); var sampelTypes = typeof(Sample1).GetProperties().SelectMany( x => x.GetCustomAttributes(typeof(LookupAttribute), false)); Assert.Equal(1, sampelTypes.Count()); var ctype = sampelTypes.First(); var lookuptype = ((LookupAttribute)ctype).LookupType; var result = typeof(LookupAttribute).Assembly.GetTypes() .Where(t => t.BaseType != null && t.BaseType.IsGenericType && t.BaseType.GetGenericTypeDefinition() == typeof(Repository <,>) && t.BaseType.GetGenericArguments()[0].FullName == lookuptype.FullName).FirstOrDefault(); dynamic repository = Activator.CreateInstance(result); List <Car1Model> cc = repository.GetList(); Assert.Equal(cc.Count, 2); //dynamic repository = Activator.CreateInstance(lookuptype); //List<Car1Model> cc = repository.GetList(); //List < Car1Model > cc= repository.GetList(); //Assert.Equal(cc.Count, 2); //.Where(x => x.GetCustomAttributes(typeof(LookupAttribute), false).Length > 0); // var attr = (LookupAttribute[])pi.GetCustomAttributes(typeof(LookupAttribute), false); //var list=(LookupAttribute)sampelType.First() }
public void TestBasicTypesEqualityInner() { Sample1 obj1 = new Sample1(); Sample1 obj2 = new Sample1(); EqualityHelper helper = new EqualityHelper(obj1); helper.Add((Sample1 x) => x.IntProp); helper.Add((Sample1 x) => x.StringProp); helper.ObjectEquals(obj2).Should().Be.True(); obj1.IntProp = obj2.IntProp = 42; helper.ObjectEquals(obj2).Should().Be.True(); helper.ObjectGetHashCode(obj2).Should().Be(helper.ObjectGetHashCode()); obj1.StringProp = "A"; helper.ObjectEquals(obj2).Should().Be.False(); obj2.StringProp = "B"; helper.ObjectEquals(obj2).Should().Be.False(); obj1.StringProp = "B"; helper.ObjectEquals(obj2).Should().Be.True(); helper.ObjectGetHashCode(obj2).Should().Be(helper.ObjectGetHashCode()); }
public void ShouldServeAsNonGenericIEqualityComparer() { Sample1 obj1 = new Sample1(); EqualityHelper <Sample1> helper = new EqualityHelper <Sample1>(); helper.Add(x => x.IntProp); helper.Add(x => x.StringProp); var hash = new Hashtable(helper); hash.Add(new Sample1() { IntProp = 2, StringProp = "asd" }, "doesn't matter"); hash.ContainsKey(new Sample1() { IntProp = 2, StringProp = "asd" }).Should().Be.True(); hash.ContainsKey(new Sample1() { IntProp = 3, StringProp = "asd" }).Should().Be.False(); }
private bool getSampleRatios() { bool check = true; if (StrataField == "") { int numberSamples1 = Sample1.RowCount(null); int numberSamples2 = Sample2.RowCount(null); cntDic.Add("1", new int[] { numberSamples1, numberSamples2 }); } else { Dictionary <string, int> s1DicCnt = getUniqueValueCounts(Sample1); Dictionary <string, int> s2DicCnt = getUniqueValueCounts(Sample2); foreach (string s in s1DicCnt.Keys) { if (!s2DicCnt.Keys.Contains(s)) { check = false; return(check); } else { int s1Cnt = s1DicCnt[s]; int s2Cnt = s2DicCnt[s]; cntDic.Add(s, new int[] { s1Cnt, s2Cnt }); } } } return(check); }
public void TestInheritanceOuter() { Sample1 obj1 = new Sample1(); Sample2 obj2 = new Sample2(); EqualityHelper helper = new EqualityHelper(typeof(Sample1)); helper.AddAllProperties(); helper.ObjectEquals(obj1, obj2).Should().Be.True(); obj1.IntProp = obj2.IntProp = 42; helper.ObjectEquals(obj1, obj2).Should().Be.True(); helper.ObjectGetHashCode(obj2).Should().Be(helper.ObjectGetHashCode(obj1)); obj1.StringProp = "A"; helper.ObjectEquals(obj1, obj2).Should().Be.False(); obj2.StringProp = "B"; helper.ObjectEquals(obj1, obj2).Should().Be.False(); obj1.StringProp = "B"; helper.ObjectEquals(obj1, obj2).Should().Be.True(); helper.ObjectGetHashCode(obj2).Should().Be(helper.ObjectGetHashCode(obj1)); }
static void Main(string[] args) { Sample1.DateTimeSample(); //Sample2.SampleTester(); //Sample3.TimerCallbackSample(); }
static void Main(string[] args) { Sample1 app = new Sample1(); while (true) { } }
static void Main() { Sample1 S1 = new Sample1(); Sample2 S2 = new Sample2(); S1.Method(); S2.Method(); }
public static void Main() { Sample1 del1 = new Sample1(SampleMethod1); del1 += SampleMethod2; //invokes both samplemethod1 and samplemethod2 del1(); }
private void Button_Click_3(object sender, RoutedEventArgs e) { Sample1.InitAxisX(); Sample1.InitAxisY(); Sample2.InitAxisX(); Sample2.InitAxisY(); }
public void InvokeTest() { var sample1 = new Sample1(); Assert.AreEqual("A1000", sample1.Invoke("Method1", "A", 1000)); AssertEx.AreThrow(new InvalidDataException(), () => sample1.Invoke("method2", "B", 2000)); AssertEx.AreThrow(new InvalidDataException(), () => sample1.Invoke("method1", 2000)); }
public void CanCompareEqualInstances() { var obj1 = new Sample1() { IntProp = 1, StringProp = "2", IgnoreProp = DateTime.Now }; var obj2 = new Sample1() { IntProp = 1, StringProp = "2", IgnoreProp = DateTime.Now.AddDays(1) }; obj2.Should().Be(obj1); obj2.GetHashCode().Should().Be(obj1.GetHashCode()); }
public void Deserialize1() { JObject obj = new JObject(); Sample1 value = obj.ToObject <Sample1>(); Assert.IsNull(value.Query); }
public ActionResult DeleteConfirmed(int id) { Sample1 sample1 = db.Sample1.Find(id); db.Sample1.Remove(sample1); db.SaveChanges(); return(RedirectToAction("Index")); }
public void GetParametersTypeTest() { var sample1 = new Sample1(); Assert.AreEqual(typeof(string), sample1.GetParametersType("Method1", 0)); Assert.AreEqual(typeof(int), sample1.GetParametersType("Method1", 1)); AssertEx.AreThrow(new InvalidDataException(), () => sample1.GetParametersType("Method2", 1)); AssertEx.AreThrow(new IndexOutOfRangeException(), () => sample1.GetParametersType("Method1", 3)); }
public FileStreamResult GetSample1() { var pdf = Sample1.GetSample(null); pdf.Buffer.BaseStream.Seek(0, SeekOrigin.Begin); var result = new FileStreamResult(pdf.Buffer.BaseStream, "application/pdf"); return(result); }
public void TestDifferentTypes() { Sample1 obj1 = new Sample1(); int obj2 = 42; EqualityHelper helper = new EqualityHelper(typeof(Sample1)); helper.ObjectEquals(obj1, obj2).Should().Be.False(); }
public void TestSerializeToString() { var value = new Sample1() { Prop1 = 2, Prop2 = "333" }; var data = GetSerializer <Sample1>().Serialize(value); var data2 = GetStringSerializer <Sample1>().SerializeToString(value); }
public MainWindow() { InitializeComponent(); this.DataContext = this; Sample1.Init(); Sample2.Init(plot, x, y, series); }
public void TestToStringSingleKey() { var obj1 = new Sample1(); var helper = new EqualityHelper <Sample1>(x => x.IntProp); obj1.IntProp = 123; helper.ObjectToString(obj1).Should().Be("(IntProp=123)"); }
public FileStreamResult GetSample1() { var buffer = Sample1.GetSample(null); buffer.Seek(0, SeekOrigin.Begin); var result = new FileStreamResult(buffer, "application/pdf"); return(result); }
public FileStreamResult GetSample1() { var pdf = Sample1.GetSample(); string s = pdf.Output("a.pdf", OutputDevice.ReturnAsString); MemoryStream m = new MemoryStream(FPdf.PrivateEncoding.GetBytes(s)); var result = new FileStreamResult(m, "application/pdf"); return(result); }
public void Serialize1() { Sample1 sample = new Sample1(); JObject obj = JObject.FromObject(sample); JObject query = obj.GetObject("query"); Assert.IsNull(query); }
public void CreateSimpleObjectFromExistingClass() { var now = DateTime.Now; var obj = new Sample1() { AAA = now, BBB = "ASD" }; var value = DictionaryHelper.FromAnonymous(obj); value.Count.Should().Be(2); value["AAA"].Should().Be(now); value["BBB"].Should().Be("ASD"); }
public void TestToStringMultipleKeyWithNullKey() { var obj1 = new Sample1(); var helper = new EqualityHelper <Sample1>(x => x.IntProp, x => x.StringProp); obj1.IntProp = 123; obj1.StringProp = null; helper.ObjectToString(obj1).Should().Be("(IntProp=123 | StringProp=<null>)"); }
public static void Main() { Sample1 del1 = new Sample1(SampleMethod1); del1 += SampleMethod2; int x = del1(); // the value returned by the last delegate function //that is called gets assigned to x i.e 2 in the given case Console.WriteLine(x); }
public void DynamicMethodInvoke() { var sample = new Sample1() { Id = 1 }; sample.AsDynamic().Apply(5); sample.Id.Should().Be(6); }
public ActionResult Edit([Bind(Include = "Id,Name")] Sample1 sample1) { if (ModelState.IsValid) { db.Entry(sample1).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(sample1)); }
public IActionResult WebSample1() { using (var package = new ExcelPackage()) { Sample1.InitWorkbook(package); var excelData = package.GetAsByteArray(); var fileName = "MyWorkbook.xlsx"; return(File(excelData, ContentType, fileName)); } }
static void Main(string[] args) { ISample sample1 = new Sample1(); sample1.SayHello("小明"); ISample sample2 = new Sample2(); sample2.SayHello("Mike"); ISample sample3 = new Sample3(); sample3.SayHello("山本桑"); Console.WriteLine("==== new 关键字 测试 ==="); AbstractSample sample3a = new Sample3(); sample3a.SayHello("山本桑"); sample3a.SayEnd(); Sample3 sample3b = new Sample3(); sample3b.SayHello("山本桑"); sample3b.SayEnd(); Console.WriteLine("==== override 与 new 的区别 ==="); Console.WriteLine("卡=普通卡!"); Card card1 = new NormalCard(); card1.LineUp(); card1.TakeMoney(); Console.WriteLine("卡=金卡!"); Card card2 = new GoldCard(); card2.LineUp(); card2.TakeMoney(); Console.WriteLine("金卡=金卡!"); GoldCard card3 = new GoldCard(); card3.LineUp(); card3.TakeMoney(); Console.ReadLine(); }
public void Deserialize2() { JObject obj = new JObject { { "query", new JObject() } }; Sample1 value = obj.ToObject <Sample1>(); Assert.IsNotNull(value.Query); Assert.IsNull(value.Query["hi"]); }
public void PersistenceSingle() { try { PersistenceClient <Guid, Sample1> repo; var p1 = new MicroservicePipeline("Local") .AddChannelIncoming("request") .AttachPersistenceManagerHandlerMemory( keyMaker: (Sample1 e) => e.Id , keyDeserializer: (s) => new Guid(s) , versionPolicy: ((e) => e.VersionId.ToString("N").ToUpperInvariant(), (e) => e.VersionId = Guid.NewGuid(), true) ) .AttachPersistenceClient(out repo) .Revert() ; p1.Start(); var sample = new Sample1() { Message = "Hello mom" }; var id = sample.Id; //Run a set of simple version entity tests. //Create Assert.IsTrue(repo.Create(sample).Result.IsSuccess); //Read var result = repo.Read(id).Result; Assert.IsTrue(result.IsSuccess); Assert.IsTrue(result.Entity.Message == "Hello mom"); //Update success var rs = repo.Update(sample).Result; Assert.IsTrue(rs.IsSuccess); //We have enabled version policy and optimistic locking so the next command should fail. //Update fail as old version Assert.IsFalse(repo.Update(sample).Result.IsSuccess); //But this one should pass. //Update pass as new entity. Assert.IsTrue(repo.Update(rs.Entity).Result.IsSuccess); //Read Assert.IsTrue(repo.Read(sample.Id).Result.IsSuccess); //Delete Assert.IsTrue(repo.Delete(sample.Id).Result.IsSuccess); //Read fail. Assert.IsFalse(repo.Read(sample.Id).Result.IsSuccess); p1.Stop(); } catch (Exception ex) { throw; } }
public void TestProtobufSimple() { var v1 = new Sample1 { X = 150, Y = "test" }; var ps = new ProtobufSerializer(); var result = ps.ToBytes(v1); CollectionAssert.AreEqual(new byte[] { 0x08, 0x96, 0x01, 0x12, 0x04, (byte)'t', (byte)'e', (byte)'s', (byte)'t' }, result); CollectionAssert.AreEqual(result, ProtoBufNetToBytes(v1)); var v2 = new Sample1(); (new ProtobufDeserializer()).FromBytes(v2, result); Assert.AreEqual(v1.X, v2.X); Assert.AreEqual(v1.Y, v2.Y); }
public void TestDefault() { var js = new JsonSerializer(); js.JsonOptions.Indent = ""; js.JsonOptions.FieldSeparator = " "; var v1 = new Sample1 { X = 6, Y = "ttt" }; var result1 = js.ToString(v1); Assert.AreEqual("{ \"X\":6 }", result1); var w1 = (Sample1)Sample1_JsonDeserializer.Instance.FromString(result1); Assert.AreEqual(6, w1.X); Assert.AreEqual("zzz", w1.Y); var v2 = new Sample2 { X = 5, Y = "5" }; var result2 = js.ToString(v2); Assert.AreEqual("{ \"X\":5 }", result2); var v3 = new SampleDefault(); Assert.AreEqual("{ }", js.ToString(new SampleDefault())); v3.B = "z"; var result3 = js.ToString(v3); Assert.AreEqual("{ \"B\":\"z\" }", result3); var w3 = new SampleDefault(); (new JsonDeserializer()).FromString(w3, result3); Assert.AreEqual(3, w3.A); Assert.AreEqual("z", w3.B); Assert.AreEqual(new SamplePoint { X = 7, Y = 2 }, w3.P); }
public void TestDefault() { var bs = new BinarySerializer(); var bd = new BinaryDeserializer(); var bdg = new BinaryDeserializerGen(); var v1 = new Sample1 { X = 6, Y = "ttt" }; var result1 = bs.ToBytes(v1); Assert.AreEqual( "20 01 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 06 00 00 00 00 00", XS(result1)); var w1 = (Sample1)bd.FromBytes(result1); Assert.AreEqual(6, w1.X); Assert.AreEqual("zzz", w1.Y); var w1g = (Sample1)bdg.FromBytes(result1); Assert.AreEqual(6, w1g.X); Assert.AreEqual("zzz", w1g.Y); var v2 = new Sample2 { X = 5, Y = "5" }; var result2 = bs.ToBytes(v2); Assert.AreEqual( "20 02 00 " + XS(typeof(Sample2)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 05 00 00 00 00 00", XS(result2)); Assert.IsInstanceOfType(bd.FromBytes(result2), typeof(Sample2)); var v3 = new SampleDefault(); var result3 = bs.ToBytes(new SampleDefault()); Assert.AreEqual( "20 03 00 " + XS(typeof(SampleDefault)) + " 03 00 " + XS("A", RoughType.Int, "B", RoughType.String, "P", RoughType.Record) + " 00 00", XS(result3)); Assert.IsInstanceOfType(bd.FromBytes(result3), typeof(SampleDefault)); v3.B = "z"; var result3m = bs.ToBytes(v3); Assert.AreEqual("20 03 00 02 00 " + XS("z") + " 00 00", XS(result3m)); var w3 = new SampleDefault(); bd.FromBytes(w3, result3m); Assert.AreEqual(3, w3.A); Assert.AreEqual("z", w3.B); Assert.AreEqual(new SamplePoint { X = 7, Y = 2 }, w3.P); var result4 = SX( "20 01 00 " + XS(typeof(SampleDefault)) + " 02 00 " + XS("A", RoughType.Int, "P", RoughType.Record) + " 01 00 05 00 00 00 " + "02 00 02 00 " + XS(typeof(SamplePoint)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.Int) + " 04 00 00 00 06 00 00 00 " + "00 00" ); bdg.ClearClassIds(); var w4 = bdg.FromBytes<SampleDefault>(result4); Assert.AreEqual(5, w4.A); Assert.AreEqual("default", w4.B); Assert.AreEqual(new SamplePoint { X = 4, Y = 6 }, w4.P); }
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"); }
public void TestSimple() { var js = new JsonSerializer(); js.Options.AllowEmptyTypes = true; Assert.AreEqual("{\n}", js.ToString(new Empty())); var v1 = new Sample1 { X = 345, Y = "test" }; js.JsonOptions.Indent = ""; var result = js.ToString(v1); Assert.AreEqual("{\n\"X\":345,\n\"Y\":\"test\"\n}", result); Sample1 v2 = new Sample1(); var jd = new JsonDeserializer(); jd.FromString(v2, result); Assert.AreEqual(v1.X, v2.X); Assert.AreEqual(v1.Y, v2.Y); jd.FromString(v2, "{\"X\":999}"); Assert.AreEqual(999, v2.X); Assert.AreEqual(v1.Y, v2.Y); v1.X = int.MaxValue; jd.FromString(v2, js.ToString(v1)); Assert.AreEqual(v1.X, v2.X); v1.X = int.MinValue; jd.FromString(v2, js.ToString(v1)); Assert.AreEqual(v1.X, v2.X); }
public void TestCodeAssignSimple() { var v1 = new Sample1 { X = 150, Y = "test" }; var cs = new CodeAssignSerializer(); var result1 = cs.ToString(v1); Assert.AreEqual("void Init(Sample1 obj) {\n\tobj.X = 150;\n\tobj.Y = \"test\";\n}\n", result1); var v2 = new Sample2 { X = 150, Y = "test" }; var result2 = cs.ToString(v2); Assert.AreEqual("void Init(Sample2 obj) {\n\tobj.X = 150;\n\tobj.Y = \"test\";\n}\n", result2); }
public void TestGenerated() { var str = "20 01 00 " + XS(typeof(Sample3)) + " 03 00 " + XS("S1", RoughType.Record, "F", RoughType.Int, "S2", RoughType.Record) + " 01 00 02 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 59 01 00 00 02 00 " + XS("test") + " 00 00 " + "02 00 DE 00 00 00 " + "03 00 03 00 " + XS(typeof(Sample2)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 A6 FE FF FF 02 00 " + XS("test1") + " 00 00 00 00"; var bd = new BinaryDeserializerGen(); bd.Options.TagMode = TagMode.Names; var w = (Sample3)bd.FromBytes(SX(str)); Assert.AreEqual(345, w.S1.X); Assert.AreEqual("test", w.S1.Y); Assert.AreEqual(222, w.F); Assert.AreEqual(-346, w.S2.X); Assert.AreEqual("test1", w.S2.Y); var w1 = new Sample1(); bd.FromBytes(w1, SX("20 02 00 01 00 58 00 00 00 00 00")); Assert.IsInstanceOfType(w1, typeof(Sample1)); Assert.AreEqual(88, w1.X); var w2 = bd.FromBytes(SX("20 02 00 01 00 63 00 00 00 00 00")); Assert.IsInstanceOfType(w2, typeof(Sample1)); Assert.AreEqual(99, ((Sample1)w2).X); var w3 = new SampleMemberI(); bd.FromBytes(w3, SX( "20 04 00 " + XS(typeof(SampleMemberI)) + " 01 00 " + XS("X", RoughType.Int) + " 00 00")); Assert.AreEqual(71, ((SampleMemberI)w3).X); }
public void TestSimple() { var bs = new BinarySerializer(); bs.Options.AllowEmptyTypes = true; Assert.AreEqual( XS(RoughType.Record) + " 01 00 " + XS(typeof(Empty)) + " 00 00 00 00", XS(bs.ToBytes(new Empty()))); var v1 = new Sample1 { X = 345, Y = "test" }; bs.ClearClassIds(); var result = bs.ToBytes(v1); Assert.AreEqual( XS(RoughType.Record) + " 01 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 59 01 00 00 02 00 " + XS("test") + " 00 00", XS(result)); Sample1 v2 = new Sample1(); var bd = new BinaryDeserializer(); bd.FromBytes(v2, result); Assert.AreEqual(v1.X, v2.X); Assert.AreEqual(v1.Y, v2.Y); bd.FromBytes(v2, new byte[] { 0x20, 01, 00, 01, 00, 0xE7, 03, 00, 00, 00, 00 }); Assert.AreEqual(999, v2.X); Assert.AreEqual(v1.Y, v2.Y); v1.X = int.MaxValue; bd.FromBytes(v2, bs.ToBytes(v1)); Assert.AreEqual(v1.X, v2.X); v1.X = int.MaxValue; bd.FromBytes(v2, bs.ToBytes(v1)); Assert.AreEqual(v1.X, v2.X); }
public void GetSample1() { var sample = new Sample1(); sample.Add(10,100); }
public void TestGenerated() { const string str = "{\n\"S1\":" + "{\n\"X\":345,\n\"Y\":\"test\"\n},\n" + "\"F\":222,\n" + "\"S2\":" + "{\n\"X\":-346,\n\"Y\":\"test1\"\n}\n" + "}"; var jd = new Sample3_JsonDeserializer(); var w = (Sample3)jd.FromString(str); Assert.AreEqual(345, w.S1.X); Assert.AreEqual("test", w.S1.Y); Assert.AreEqual(222, w.F); Assert.AreEqual(-346, w.S2.X); Assert.AreEqual("test1", w.S2.Y); var jdg = new JsonDeserializerGenerator(); jdg.Assembly = GetType().Assembly; var w1 = new Sample1(); jdg.FromString(w1, "{\"X\":88}"); Assert.IsInstanceOfType(w1, typeof(Sample1)); Assert.AreEqual(88, w1.X); var w2 = jdg.FromString("{\"class\":\"YuzuTest.Sample1, YuzuTest\",\"X\":99}"); Assert.IsInstanceOfType(w2, typeof(Sample1)); Assert.AreEqual(99, ((Sample1)w2).X); var w3 = new SampleMemberI(); jdg.FromString(w3, "{\"class\":\"YuzuTest.SampleMemberI, YuzuTest\"}"); Assert.AreEqual(71, ((SampleMemberI)w3).X); }
public void TestEscape() { var js = new JsonSerializer(); js.JsonOptions.Indent = ""; js.JsonOptions.FieldSeparator = ""; var s = "\"/{\u0001}\n\t\"\""; var v = new Sample1 { Y = s }; var result = js.ToString(v); Assert.AreEqual("{\"X\":0,\"Y\":\"\\\"/{\\u0001}\\n\\t\\\"\\\"\"}", result); var w = new Sample1(); var jd = new JsonDeserializer(); jd.FromString(w, result); Assert.AreEqual(s, w.Y); v.Y = result; var result1 = js.ToString(v); jd.FromString(w, result1); Assert.AreEqual(result, w.Y); v.Y = "привет"; var result2 = js.ToString(v); Assert.AreEqual("{\"X\":0,\"Y\":\"привет\"}", result2); jd.FromString(w, result2); Assert.AreEqual(v.Y, w.Y); jd.FromString(w, "{\"X\":0,\"Y\":\"\u043F\u0440\u0438\u0432\u0435\u0442\"}"); Assert.AreEqual(v.Y, w.Y); }
public void TestErrors() { var js = new JsonSerializer(); XAssert.Throws<YuzuException>(() => js.ToString(new Empty()), "Empty"); XAssert.Throws<YuzuException>(() => js.ToString(new SampleCollectionWithField<int>()), "collection"); XAssert.Throws<YuzuException>(() => js.ToString(new SampleInterfacedFieldDup()), "X"); XAssert.Throws<YuzuException>(() => js.ToString(new BadMerge1()), "merge"); XAssert.Throws<YuzuException>(() => js.ToString(new BadMerge2()), "merge"); var jd = new JsonDeserializer(); var w = new Sample1(); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{}"), "X"); XAssert.Throws<YuzuException>(() => jd.FromString("{null:1}"), "'n'"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\" }"), ":"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "nn"), "'u'"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\":1, \"Y\": \"\\z\" }"), "z"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"X\":1, \"Y\": \"\\uQ\" }"), "Q"); XAssert.Throws<YuzuException>(() => jd.FromString(new SampleBool(), "{ \"B\": 1 }"), "1"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ ,}"), ","); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"Y\": \"q\" }"), "'X'"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "[]"), "'Sample1'"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"class\": \"Q\" }"), "'Q'"); XAssert.Throws<YuzuException>(() => jd.FromString(w, "{ \"class\": \"YuzuTest.Sample2, YuzuTest\" }"), ".Sample2"); XAssert.Throws<YuzuException>(() => jd.FromString( new Sample2Allow(), "{ \"class\": \"YuzuTest.Sample1, YuzuTest\" }"), ".Sample1"); XAssert.Throws<YuzuException>(() => jd.FromString(new SamplePoint(), "[ \"QQ\" ]"), ""); XAssert.Throws<YuzuException>(() => jd.FromString(new object(), "{}"), "object"); XAssert.Throws<EndOfStreamException>(() => jd.FromString(""), ""); XAssert.Throws<EndOfStreamException>(() => jd.FromString(w, "{ \"X\": 1")); XAssert.Throws<YuzuException>(() => jd.FromString( "{\"class\":\"YuzuTest.SampleInterfaceField, YuzuTest\",\"I\":{}}"), "class"); XAssert.Throws<YuzuException>(() => jd.FromString( "{\"class\":\"YuzuTest.SampleInterfaceField, YuzuTest\",\"I\":{\"class\":\"YuzuTest.Sample1, YuzuTest\"}}"), "ISample"); jd.Options.ReportErrorPosition = true; XAssert.Throws<YuzuException>(() => jd.FromString(w, " z"), "7"); jd.Options.ReportErrorPosition = false; try { jd.FromString(w, " z"); } catch (YuzuException e) { Assert.IsFalse(e.Message.Contains("7")); } }
public void TestEscape() { var bs = new BinarySerializer(); var s = "\"/{\u0001}\n\t\"\""; var v = new Sample1 { Y = s }; var result = bs.ToBytes(v); Assert.AreEqual( "20 01 00 " + XS(typeof(Sample1)) + " 02 00 " + XS("X", RoughType.Int, "Y", RoughType.String) + " 01 00 00 00 00 00 02 00 " + XS(s) + " 00 00", XS(result)); var w = new Sample1(); var bd = new BinaryDeserializer(); bd.FromBytes(w, result); Assert.AreEqual(s, w.Y); v.Y = "привет"; var result2 = bs.ToBytes(v); Assert.AreEqual( "20 01 00 01 00 00 00 00 00 02 00 0C " + XS(Encoding.UTF8.GetBytes("привет")) + " 00 00", XS(result2)); bd.FromBytes(w, result2); Assert.AreEqual(v.Y, w.Y); var ms = new MemoryStream(result2.Length); ms.Write(result2, 0, result2.Length); ms.Position = 0; bd.FromReader(w, new UnsafeBinaryReader(ms)); Assert.AreEqual(v.Y, w.Y); }