Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- /// <summary>
- /// Test data
- /// </summary>
- struct TestData
- {
- public String TestName { get; set; }
- public List<KeyValuePair<String, String>> AValues { get; set; }
- public List<KeyValuePair<String, String>> BValues { get; set; }
- public bool IsTrue { get; set; }
- }
- static void Main(string[] args)
- {
- var types = typeof(II).Assembly.GetTypes();
- var anyTypes = Array.FindAll(types, o => !o.IsAbstract &&
- o.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length == 0 &&
- typeof(ANY).IsAssignableFrom(o) &&
- o.GetMethod("Equals", new Type[] { o }) != null);
- foreach (Type t in anyTypes)
- GenerateTestFile(t);
- }
- /// <summary>
- /// Generate test file
- /// </summary>
- private static void GenerateTestFile(Type t)
- {
- if (t.IsGenericTypeDefinition)
- {
- // Create a generic type of INT
- List<Type> genericArguments = new List<Type>();
- foreach (Type genParm in t.GetGenericArguments())
- genericArguments.Add(typeof(INT));
- t = t.MakeGenericType(genericArguments.ToArray());
- }
- TextWriter tw = null;
- try
- {
- // Code Here
- string testName = t.Name;
- if (testName.Contains("`"))
- testName = testName.Substring(0, testName.IndexOf("`"));
- tw = File.CreateText(String.Format("{0}EqualityTest.cs", testName));
- List<TestData> data = GenerateTestData(t);
- tw.WriteLine(Templates.TestFile.Replace("$name$", testName));
- foreach (var td in data)
- {
- tw.WriteLine("/// <summary>");
- tw.WriteLine("/// Determine if one <see cref=\"T:{0}\"/> is {1} equal to another", CreateClassRef(t), td.IsTrue ? "" : "not");
- tw.WriteLine("/// <list type=\"table\">");
- tw.WriteLine("/// <listheader><term>Property</term><description>Comments</description></listheader>");
- for (int i = 0; i < td.AValues.Count - 1; i++)
- tw.WriteLine("/// <item><term>{0}</term><description>A={1},B={2}</description></item>", td.AValues[i].Key, td.AValues[i].Value.Replace("<", "<"), td.BValues[i].Value.Replace("<", "<"));
- tw.WriteLine("/// </list>");
- tw.WriteLine("[TestMethod]");
- tw.WriteLine("public void {0}{1}Test() {{", testName, td.TestName);
- tw.WriteLine("{0} aValue = new {0}(), bValue = new {0}();", CreateClassRef(t));
- foreach (var av in td.AValues)
- {
- if (!String.IsNullOrEmpty(av.Value) && !av.Key.Contains("("))
- tw.WriteLine("aValue.{0} = {1};", av.Key, av.Value);
- else if (av.Key.Contains("("))
- tw.WriteLine("aValue.{0}{1});", av.Key, av.Value);
- }
- foreach (var av in td.BValues)
- {
- if (!String.IsNullOrEmpty(av.Value) && !av.Key.Contains("("))
- tw.WriteLine("bValue.{0} = {1};", av.Key, av.Value);
- else if (av.Key.Contains("("))
- tw.WriteLine("bValue.{0}{1});", av.Key, av.Value);
- }
- if (td.IsTrue)
- tw.WriteLine("Assert.AreEqual(aValue, bValue);");
- else
- tw.WriteLine("Assert.AreNotEqual(aValue, bValue);");
- tw.WriteLine("}");
- }
- tw.WriteLine("}}");
- }
- finally
- {
- if (tw != null)
- tw.Close();
- }
- }
- private static string CreateClassRef(Type t)
- {
- String className = t.FullName;
- if (className.Contains("`"))
- className = className.Substring(0, className.IndexOf("`"));
- if (t.IsGenericType)
- {
- className += "<";
- foreach (var genParm in t.GetGenericArguments())
- className += String.Format("{0},", CreateClassRef(genParm));
- className = className.Remove(className.Length - 1);
- className += ">";
- }
- return className;
- }
- /// <summary>
- /// Generate test data
- /// </summary>
- private static List<TestData> GenerateTestData(Type t)
- {
- var anyTypes = Array.FindAll<Type>(typeof(II).Assembly.GetTypes(), o => !o.IsAbstract && typeof(ANY).IsAssignableFrom(o) && o.GetMethod("Equals", new Type[] { o }) != null);
- PropertyInfo[] pi = Array.FindAll<PropertyInfo>(t.GetProperties(BindingFlags.Public | BindingFlags.Instance), o => o.CanWrite && o.GetIndexParameters().Length == 0 && o.GetSetMethod() != null);
- // First type, equals each other
- TestData equals = new TestData() { AValues = new List<KeyValuePair<string, string>>(), BValues = new List<KeyValuePair<string, string>>(), TestName = "Equals", IsTrue = true };
- var notEquals = new List<TestData>(pi.Length);
- foreach (var info in pi)
- {
- if (String.IsNullOrEmpty(GetInitializer(info.PropertyType, 0)) ||
- info.GetCustomAttributes(typeof(BrowsableAttribute), false).Length > 0)
- continue;
- equals.AValues.Add(CreatePropertyValue(info, 0));
- equals.BValues.Add(CreatePropertyValue(info, 0));
- var currentNotEquals = new TestData() { TestName = String.Format("NotEquals{0}", info.Name), IsTrue = false, AValues = new List<KeyValuePair<string, string>>(), BValues = new List<KeyValuePair<string, string>>() };
- foreach (var info2 in pi)
- {
- if (String.IsNullOrEmpty(GetInitializer(info2.PropertyType, 0)) ||
- info2.GetCustomAttributes(typeof(BrowsableAttribute), false).Length > 0)
- continue;
- if (info2 == info)
- {
- currentNotEquals.AValues.Add(CreatePropertyValue(info, 0));
- currentNotEquals.BValues.Add(CreatePropertyValue(info2, 1));
- }
- else
- {
- currentNotEquals.AValues.Add(CreatePropertyValue(info2, 0));
- currentNotEquals.BValues.Add(CreatePropertyValue(info2, 0));
- }
- }
- notEquals.Add(currentNotEquals);
- }
- notEquals.Add(equals);
- return notEquals;
- }
- /// <summary>
- /// Create property value setter
- /// </summary>
- private static KeyValuePair<string, string> CreatePropertyValue(PropertyInfo info, int p)
- {
- string initializer = GetInitializer(info.PropertyType, p);
- return new KeyValuePair<string, string>(info.Name, initializer);
- }
- private static string GetInitializer(Type type, int p)
- {
- string initializer = "";
- if (type == typeof(byte) || type == typeof(byte?))
- initializer = p.ToString();
- else if (type == typeof(int) || type == typeof(int?))
- initializer = p.ToString();
- else if (type == typeof(double) || type == typeof(double?))
- initializer = p.ToString("0.0f");
- else if (type == typeof(decimal) || type == typeof(decimal?))
- initializer = String.Format("(decimal){0}", p);
- else if (type == typeof(String))
- initializer = String.Format("\"{0}\"", p);
- else if (type == typeof(bool) || type == typeof(bool?))
- initializer = Convert.ToBoolean(p).ToString().ToLower();
- else if (type == typeof(DateTime) || type == typeof(DateTime?))
- initializer = String.Format("DateTime.Parse(\"2011-{0}-10\")", p + 1);
- else if (type.IsArray)
- initializer = String.Format("new {0} {{ {1} }}", CreateClassRef(type), GetInitializer(type.GetMethod("Get").ReturnParameter.ParameterType, p));
- else if (type.IsEnum) // CS?
- {
- initializer = String.Format("{0}.{1}", type.FullName, type.GetFields()[p == 0 ? 1 : type.GetFields().Count() - 1].Name);
- }
- else if (!type.IsAbstract)
- {
- var ctor = Array.Find<ConstructorInfo>(type.GetConstructors(), o => o.GetParameters().Length > 0 && !Array.Exists<ParameterInfo>(o.GetParameters(), pa => String.IsNullOrEmpty(GetInitializer(pa.ParameterType, p))));
- if (ctor == null)
- return string.Empty;
- else
- {
- StringBuilder initBuilder = new StringBuilder(String.Format("new {0}(", CreateClassRef(type)));
- foreach (ParameterInfo pi in ctor.GetParameters())
- initBuilder.AppendFormat("{0},", GetInitializer(pi.ParameterType, p));
- initBuilder.Remove(initBuilder.Length - 1, 1);
- initBuilder.Append(")");
- initializer = initBuilder.ToString();
- }
- }
- if (type.GetMethod("Add") != null && type.IsGenericType)
- {
- initializer += String.Format(" {{ {0} }}", GetInitializer(type.GetGenericArguments()[0], p));
- }
- return initializer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment