/// <summary>Attribute testing</summary> private static void AttributeTest() { Console.Write("AttributeTest:"); Options opts = new Options("testing", "someclass"); opts.AddAssembly("System.Runtime.Serialization.dll"); opts.AddPropertyAttributes("test", new List<string>() { "[DataMember]" }); opts.AddClassAttribute("[DataContract]"); opts.AddNamespace("System.Runtime.Serialization"); var result = ObjectWrapper.CreateFrom(new { test = "abc" }, opts).Instance; foreach (var attr in result.GetType().GetCustomAttributes(false)) { if (attr.ToString() != "System.Runtime.Serialization.DataContractAttribute") { Console.WriteLine(attr.ToString()); Console.WriteLine(Fail); return; } } foreach (var prop in result.GetType().GetProperties()) { if (prop.Name == "test") { foreach (var attr in prop.GetCustomAttributes(false)) { if (attr.ToString() != "System.Runtime.Serialization.DataMemberAttribute") { Console.WriteLine(attr.ToString()); Console.WriteLine(Fail); return; } } } } Console.WriteLine(Pass); }
/// <summary>Invalid class definition</summary> private static void InvalidClassTest() { Console.Write("InvalidClassTest:"); try { Options opts = new Options("test", "abc"); opts.AddNamespace("INVALID"); ObjectWrapper.CreateFrom(new { name = "test" }, opts); } catch (InvalidOperationException error) { if (!error.Message.StartsWith("Invalid syntax or usage")) { Console.WriteLine(error); Console.WriteLine(Fail); return; } } Console.WriteLine(Pass); }
/// <summary>Testing option input arguments</summary> private static void OptionTestArgs() { Console.Write("OptionTestArgs:"); Options opt = null; try { new Options(null, "test_class"); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("namespaceName")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt = new Options("test", null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("className")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } opt = new Options("test", "test_class"); try { opt.AddNamespace(null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("name")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddPropertyType(null, null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("property")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddPropertyType("abc", null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("typeName")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddConverter(null, null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("property")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddConverter("test", null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("function")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddClassAttribute(null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("attribute")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddAssembly(null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("assemblyName")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddPropertyAttributes(null, null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("propertyName")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } try { opt.AddPropertyAttributes("test", null); Console.WriteLine(Fail); return; } catch (ArgumentNullException ex) { if (!ex.Message.Contains("attributes")) { Console.WriteLine(ex); Console.WriteLine(Fail); return; } } Console.WriteLine(Pass); }
/// <summary>Adding a namespace</summary> private static void NamespaceTest() { Console.Write("NamespaceTest:"); Options opts = new Options("test", "testclass"); opts.AddNamespace("System.Text"); ObjectWrapper.CreateFrom(new { abc = new System.Text.StringBuilder() }, opts); Console.WriteLine(Pass); }
/// <summary>Option creation testing</summary> private static void OptionTest() { Console.Write("OptionTest:"); Options opt = new Options("test", "test_class"); opt.AddAssembly("System.Data.dll"); opt.AddClassAttribute("[DataContract]"); opt.AddPropertyAttributes("Something", new List<string>() { "[DataMember]" }); opt.AddPropertyType("abc", "System.String"); opt.AddConverter("abc", x => { return 1; }); opt.AddNamespace("system.namespace"); var props = opt.GetPropertyAttributes("someTHING"); if (props.Count == 1 && props[0] == "[DataMember]") { } else { Console.WriteLine("Invalid property attributes"); Console.WriteLine(Fail); return; } props = opt.GetPropertyAttributes("OTHER"); if (props.Count != 0) { Console.WriteLine("Invalid property attributes, should be empty"); Console.WriteLine(Fail); return; } var classAttributes = opt.GetClassAttributes(); if (classAttributes.Count == 1 && classAttributes[0] == "[DataContract]") { } else { Console.WriteLine("Invalid class attributes"); Console.WriteLine(Fail); return; } var assemblies = opt.GetAssemblies(); if (assemblies.Count == 1 && assemblies[0] == "System.Data.dll") { } else { Console.WriteLine("Invalid assembly set"); Console.WriteLine(Fail); return; } if (opt.GetPropertyType("abc") != "System.String") { Console.WriteLine("Incorrect type"); Console.WriteLine(Fail); return; } if (opt.GetPropertyType("xyz") != null) { Console.WriteLine("Type shouldn't be set"); Console.WriteLine(Fail); return; } if (opt.GetConverter("abc") == null) { Console.WriteLine("Incorrect converter"); Console.WriteLine(Fail); return; } if (opt.GetConverter("xyz") != null) { Console.WriteLine("Converter shouldn't be set"); Console.WriteLine(Fail); return; } var names = opt.GetNamespaces(); if (names.Length != 1 || names[0] != "system.namespace") { Console.WriteLine("Incorrect namespace set"); Console.WriteLine(Fail); return; } Console.WriteLine(Pass); }