public void Deserialize_should_correctly_deserialize_decimal() { var jobInfo = new JobInfo { JobType = typeof(SimpleBinarySerializer), ArgsType = typeof(decimal), Args = 123.5m }; var serializer = new SimpleBinarySerializer(); var typeProvider = new DefaultTypeProvider(); var serialized = serializer.Serialize(jobInfo, typeProvider); var deserialized = serializer.Deserialize(serialized, typeProvider.TypeToTag(jobInfo.ArgsType), typeProvider); Assert.IsNotNull(deserialized.JobType); Assert.AreEqual(typeof(SimpleBinarySerializer), deserialized.JobType); Assert.IsNotNull(deserialized.Args); Assert.AreEqual(jobInfo.ArgsType, deserialized.Args.GetType()); var args = (decimal)deserialized.Args; Assert.AreEqual(123.5m, args); }
public void Deserialize_should_correctly_deserialize_string() { var jobInfo = new JobInfo { JobType = typeof(SimpleBinarySerializer), ArgsType = typeof(string), Args = "Hello, World!" }; var serializer = new SimpleBinarySerializer(); var typeProvider = new DefaultTypeProvider(); var serialized = serializer.Serialize(jobInfo, typeProvider); var deserialized = serializer.Deserialize(serialized, typeProvider.TypeToTag(jobInfo.ArgsType), typeProvider); Assert.IsNotNull(deserialized.JobType); Assert.AreEqual(typeof(SimpleBinarySerializer), deserialized.JobType); Assert.IsNotNull(deserialized.Args); Assert.AreEqual(jobInfo.ArgsType, deserialized.Args.GetType()); var args = (string)deserialized.Args; Assert.AreEqual("Hello, World!", args); }
public void Deserialize_should_correctly_deserialize_nullable_properties_A() { var jobInfo = new JobInfo { Args = new MyArg2 { ByteProp = 123, //ShortProp = -31234, UShortProp = 65432, //IntProp = int.MinValue, UIntProp = uint.MaxValue, //LongProp = long.MinValue + 1, ULongProp = ulong.MaxValue - 1, //FloatProp = 1.23f, DoubleProp = 2.34, //DecimalProp = decimal.MaxValue - 2, DateTimeProp = new DateTime(1976, 10, 23, 0, 0, 0, DateTimeKind.Utc), //DateTimeOffsetProp = new DateTimeOffset(1976, 10, 23, 0, 0, 0, TimeSpan.FromHours(3)), StringProp = "Hello world" }, ArgsType = typeof(MyArg2), JobType = typeof(SimpleBinarySerializer) }; var serializer = new SimpleBinarySerializer(); var typeProvider = new DefaultTypeProvider(); var serialized = serializer.Serialize(jobInfo, typeProvider); var deserialized = serializer.Deserialize(serialized, typeProvider.TypeToTag(typeof(MyArg2)), typeProvider); Assert.IsNotNull(deserialized.JobType); Assert.AreEqual(typeof(SimpleBinarySerializer), deserialized.JobType); Assert.IsNotNull(deserialized.Args); Assert.AreEqual(typeof(MyArg2), deserialized.Args.GetType()); var args = (MyArg2)deserialized.Args; Assert.AreEqual((byte?)123, args.ByteProp); Assert.IsNull(args.ShortProp); Assert.AreEqual((ushort?)65432, args.UShortProp); Assert.IsNull(args.IntProp); Assert.AreEqual(uint.MaxValue, args.UIntProp); Assert.IsNull(args.LongProp); Assert.AreEqual(ulong.MaxValue - 1, args.ULongProp); Assert.IsNull(args.FloatProp); Assert.AreEqual(2.34, args.DoubleProp); Assert.IsNull(args.DecimalProp); Assert.AreEqual(new DateTime(1976, 10, 23, 0, 0, 0, DateTimeKind.Utc), args.DateTimeProp); Assert.AreEqual(DateTimeKind.Utc, args.DateTimeProp?.Kind); Assert.IsNull(args.DateTimeOffsetProp); Assert.AreEqual("Hello world", args.StringProp); }
protected BaseReport() { Workbook = new XLWorkbook(); var typeProvider = new DefaultTypeProvider(new[] { Assembly.GetExecutingAssembly(), Assembly.GetAssembly(typeof(DateTime)), }, GetType()); var instanceProvider = new DefaultInstanceProvider(this); TemplateProcessor = new DefaultTemplateProcessor(new DefaultPropertyValueProvider(typeProvider, instanceProvider), new SystemVariableProvider(), new DefaultMethodCallValueProvider(typeProvider, instanceProvider), new DefaultDataItemValueProvider(new DataItemValueProviderFactory()) { DataItemSelfTemplate = "di" }); }
public void TestGetType() { //// These tests do not pass because the entry assembly exists after migration to .NET Core //ExceptionAssert.Throws<InvalidOperationException>(() => new DefaultTypeProvider(), "Assemblies are not provided but entry assembly is null. Provide assemblies and try again."); //ExceptionAssert.Throws<InvalidOperationException>(() => new DefaultTypeProvider(new Assembly[0]), "Assemblies are not provided but entry assembly is null. Provide assemblies and try again."); var typeProvider = new DefaultTypeProvider(new[] { typeof(ExcelHelper).Assembly }); Assert.AreSame(typeof(ExcelHelper), typeProvider.GetType("ExcelHelper")); ExceptionAssert.Throws <TypeNotFoundException>(() => typeProvider.GetType("TestType_1"), "Cannot find type by template \"TestType_1\""); ExceptionAssert.Throws <TypeNotFoundException>(() => typeProvider.GetType("DateTime"), "Cannot find type by template \"DateTime\""); ExceptionAssert.Throws <InvalidOperationException>(() => typeProvider.GetType(null), "Template is not specified but defaultType is null"); ExceptionAssert.Throws <InvalidOperationException>(() => typeProvider.GetType(string.Empty), "Template is not specified but defaultType is null"); ExceptionAssert.Throws <InvalidOperationException>(() => typeProvider.GetType(" "), "Template is not specified but defaultType is null"); typeProvider = new DefaultTypeProvider(new[] { Assembly.GetExecutingAssembly() }, typeof(TestType_1)); Assert.AreSame(typeof(TestType_1), typeProvider.GetType("TestType_1")); Assert.AreSame(typeof(TestType_1), typeProvider.GetType(null)); Assert.AreSame(typeProvider.GetType("TestType_1"), typeProvider.GetType(null)); Assert.AreSame(typeof(TestType_1), typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers:TestType_1")); Assert.AreSame(typeof(TestType_1.TestType_2), typeProvider.GetType("TestType_2")); Assert.AreSame(typeof(TestType_1.TestType_2), typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers:TestType_2")); Assert.AreSame(typeof(TestType_1.TestType_2), typeProvider.GetType(" ExcelReportGenerator.Tests.Rendering.Providers : TestType_2 ")); ExceptionAssert.Throws <TypeNotFoundException>(() => typeProvider.GetType("ExcelHelper"), "Cannot find type by template \"ExcelHelper\""); Assert.AreSame(typeof(TestType_3), typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers:TestType_3")); Assert.AreSame(typeof(InnerNamespace.TestType_3), typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers.InnerNamespace:TestType_3")); ExceptionAssert.Throws <InvalidTemplateException>(() => typeProvider.GetType("TestType_3"), "More than one type found by template \"TestType_3\""); ExceptionAssert.Throws <TypeNotFoundException>(() => typeProvider.GetType("DateTime"), "Cannot find type by template \"DateTime\""); typeProvider = new DefaultTypeProvider(new[] { Assembly.GetExecutingAssembly(), Assembly.GetAssembly(typeof(DateTime)) }); Assert.AreSame(typeof(InnerNamespace.TestType_5), typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers.InnerNamespace:TestType_5")); Assert.AreSame(typeof(TestType_5), typeProvider.GetType(":TestType_5")); Assert.AreSame(typeof(TestType_5), typeProvider.GetType(":TestType_5")); Assert.AreSame(typeof(DateTime), typeProvider.GetType("DateTime")); Assert.AreSame(typeof(DateTime), typeProvider.GetType("System:DateTime")); ExceptionAssert.Throws <InvalidTemplateException>(() => typeProvider.GetType("TestType_5"), "More than one type found by template \"TestType_5\""); Assert.AreSame(typeof(InnerNamespace.TestType_4), typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers.InnerNamespace:TestType_4")); Assert.AreSame(typeof(InnerNamespace.TestType_4), typeProvider.GetType("TestType_4")); ExceptionAssert.Throws <TypeNotFoundException>(() => typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers:TestType_4"), "Cannot find type by template \"ExcelReportGenerator.Tests.Rendering.Providers:TestType_4\""); ExceptionAssert.Throws <InvalidTemplateException>(() => typeProvider.GetType("ExcelReportGenerator.Tests.Rendering.Providers:InnerNamespace:TestType_4"), "Type name template \"ExcelReportGenerator.Tests.Rendering.Providers:InnerNamespace:TestType_4\" is invalid"); }
public void Deserialize_should_correctly_deserialize_nullable_decimal_with_value() { var jobInfo = new JobInfo { JobType = typeof(SimpleBinarySerializer), ArgsType = typeof(decimal?), Args = null }; var serializer = new SimpleBinarySerializer(); var typeProvider = new DefaultTypeProvider(); var serialized = serializer.Serialize(jobInfo, typeProvider); var deserialized = serializer.Deserialize(serialized, typeProvider.TypeToTag(jobInfo.ArgsType), typeProvider); Assert.IsNotNull(deserialized.JobType); Assert.AreEqual(typeof(SimpleBinarySerializer), deserialized.JobType); Assert.IsNull(deserialized.Args); }
public void TestGetValue() { ExceptionAssert.Throws <ArgumentNullException>(() => new DefaultPropertyValueProvider(null, Substitute.For <IInstanceProvider>())); ExceptionAssert.Throws <ArgumentNullException>(() => new DefaultPropertyValueProvider(Substitute.For <ITypeProvider>(), null)); var typeProvider = new DefaultTypeProvider(new[] { Assembly.GetExecutingAssembly(), Assembly.GetAssembly(typeof(DateTime)), }, typeof(PropValProviderTestClass)); var testInstance = new PropValProviderTestClass(); var instanceProvider = new DefaultInstanceProvider(testInstance); var reflectionHelper = new ReflectionHelper(); IPropertyValueProvider propertyValueProvider = new DefaultPropertyValueProvider(typeProvider, instanceProvider, reflectionHelper); Assert.AreEqual(testInstance.StrProp, propertyValueProvider.GetValue("StrProp")); Assert.AreEqual(testInstance.StrProp, propertyValueProvider.GetValue("PropValProviderTestClass: StrProp ")); Assert.AreEqual(testInstance.IntField, propertyValueProvider.GetValue("IntField")); Assert.AreEqual(PropValProviderTestClass.StaticStrProp, propertyValueProvider.GetValue("StaticStrProp")); Assert.AreEqual(PropValProviderTestClass.StaticIntField, propertyValueProvider.GetValue("StaticIntField")); Assert.AreEqual(testInstance.ParentProp, propertyValueProvider.GetValue("ParentProp")); Assert.AreEqual(testInstance.ParentField, propertyValueProvider.GetValue("ExcelReportGenerator.Tests.Rendering.Providers:PropValProviderTestClass:ParentField")); Assert.AreEqual(PropValProviderTestClass.StaticParentProp, propertyValueProvider.GetValue("StaticParentProp")); Assert.AreEqual(Parent.StaticParentField, propertyValueProvider.GetValue("StaticParentField")); Assert.AreEqual(testInstance.DynamicObj.GuidProp, propertyValueProvider.GetValue("DynamicObj.GuidProp")); Assert.AreEqual(testInstance.ExpandoObj.StrProp, propertyValueProvider.GetValue("ExpandoObj.StrProp")); Assert.AreEqual(testInstance.ExpandoObj.DecimalProp, propertyValueProvider.GetValue("ExpandoObj.DecimalProp")); Assert.AreEqual(testInstance.ExpandoObj.ComplexProp.GuidProp, propertyValueProvider.GetValue("ExpandoObj.ComplexProp.GuidProp")); Assert.AreEqual(PropValProviderTestClass.ExpandoField.FloatProp, propertyValueProvider.GetValue("ExpandoField.FloatProp")); Assert.AreEqual(testInstance.ObjField.ExpandoField.IntProp, propertyValueProvider.GetValue("ObjField.ExpandoField.IntProp")); Assert.AreEqual(PropValProviderTestClass.StaticObjField.ExpandoField.IntProp, propertyValueProvider.GetValue("StaticObjField.ExpandoField.IntProp")); Assert.AreEqual("TestClass2:StrProp", propertyValueProvider.GetValue("ObjProp.StrProp")); Assert.AreEqual("TestClass2:StrProp", propertyValueProvider.GetValue("ExcelReportGenerator.Tests.Rendering.Providers:PropValProviderTestClass:ObjField.StrProp")); Assert.AreEqual("TestClass2:StrField", propertyValueProvider.GetValue("PropValProviderTestClass:ObjProp.StrField")); Assert.AreEqual("TestClass2:StrField", propertyValueProvider.GetValue("ObjField.StrField")); Assert.AreEqual("TestClass2:StrProp", propertyValueProvider.GetValue("PropValProviderTestClass:StaticObjProp.StrProp")); Assert.AreEqual("TestClass2:StrProp", propertyValueProvider.GetValue("StaticObjField.StrProp")); Assert.AreEqual(Guid.Parse("5be1d032-6d93-466e-bce0-31dfcefdda22"), propertyValueProvider.GetValue("ObjProp.ObjField.GuidProp")); Assert.AreEqual(Guid.Parse("5be1d032-6d93-466e-bce0-31dfcefdda22"), propertyValueProvider.GetValue("ObjField.ObjField.GuidProp")); Assert.AreEqual(Guid.Parse("5be1d032-6d93-466e-bce0-31dfcefdda22"), propertyValueProvider.GetValue("StaticObjProp.ObjField.GuidProp")); Assert.AreEqual(Guid.Parse("5be1d032-6d93-466e-bce0-31dfcefdda22"), propertyValueProvider.GetValue("ExcelReportGenerator.Tests.Rendering.Providers:PropValProviderTestClass:StaticObjField.ObjField.GuidProp")); testInstance.StrProp = null; Assert.AreEqual("DefaultStrProp", propertyValueProvider.GetValue("StrProp")); testInstance.ObjProp.StrField = null; Assert.AreEqual("DefaultStrField", propertyValueProvider.GetValue("ObjProp.StrField")); testInstance.ObjProp.ObjField.GuidProp = null; Assert.AreEqual(0, propertyValueProvider.GetValue("ObjProp.ObjField.GuidProp")); testInstance.ObjProp = null; Assert.IsNull(propertyValueProvider.GetValue("ObjProp")); ExceptionAssert.Throws <ArgumentException>(() => propertyValueProvider.GetValue(null)); ExceptionAssert.Throws <ArgumentException>(() => propertyValueProvider.GetValue(string.Empty)); ExceptionAssert.Throws <ArgumentException>(() => propertyValueProvider.GetValue(" ")); ExceptionAssert.Throws <InvalidTemplateException>(() => propertyValueProvider.GetValue("PropValProviderTestClass:")); ExceptionAssert.Throws <InvalidTemplateException>(() => propertyValueProvider.GetValue("ExcelReportGenerator.Tests.Rendering.Providers:PropValProviderTestClass: ")); ExceptionAssert.Throws <MemberNotFoundException>(() => propertyValueProvider.GetValue("BadField"), "Cannot find property or field \"BadField\" in class \"PropValProviderTestClass\" and all its parents. BindingFlags = Instance, Static, Public, FlattenHierarchy"); ExceptionAssert.Throws <InvalidOperationException>(() => propertyValueProvider.GetValue("PrivateGetterProp"), "Property \"PrivateGetterProp\" of type \"PropValProviderTestClass\" has no public getter"); // Static system type props Assert.AreEqual(DateTime.Now.Date, (DateTime)propertyValueProvider.GetValue("DateTime:Now.Date")); Assert.AreEqual(Guid.Empty, (Guid)propertyValueProvider.GetValue("Guid:Empty")); // Non static system type prop Assert.AreEqual(0, propertyValueProvider.GetValue("Version:Minor")); }