Guest User

Program.cs

a guest
Aug 21st, 2011
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.21 KB | None | 0 0
  1. class Program
  2. {
  3.  
  4.     /// <summary>
  5.     /// Test data
  6.     /// </summary>
  7.     struct TestData
  8.     {
  9.         public String TestName { get; set; }
  10.         public List<KeyValuePair<String, String>> AValues { get; set; }
  11.         public List<KeyValuePair<String, String>> BValues { get; set; }
  12.         public bool IsTrue { get; set; }
  13.     }
  14.  
  15.     static void Main(string[] args)
  16.     {
  17.         var types = typeof(II).Assembly.GetTypes();
  18.         var anyTypes = Array.FindAll(types, o => !o.IsAbstract &&
  19.             o.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length == 0 &&
  20.             typeof(ANY).IsAssignableFrom(o) &&
  21.             o.GetMethod("Equals", new Type[] { o }) != null);
  22.  
  23.         foreach (Type t in anyTypes)
  24.             GenerateTestFile(t);
  25.     }
  26.  
  27.     /// <summary>
  28.     /// Generate test file
  29.     /// </summary>
  30.     private static void GenerateTestFile(Type t)
  31.     {
  32.  
  33.         if (t.IsGenericTypeDefinition)
  34.         {
  35.             // Create a generic type of INT
  36.             List<Type> genericArguments = new List<Type>();
  37.             foreach (Type genParm in t.GetGenericArguments())
  38.                 genericArguments.Add(typeof(INT));
  39.             t = t.MakeGenericType(genericArguments.ToArray());
  40.         }
  41.  
  42.         TextWriter tw = null;
  43.         try
  44.         {
  45.             // Code Here
  46.             string testName = t.Name;
  47.             if (testName.Contains("`"))
  48.                 testName = testName.Substring(0, testName.IndexOf("`"));
  49.             tw = File.CreateText(String.Format("{0}EqualityTest.cs", testName));
  50.  
  51.             List<TestData> data = GenerateTestData(t);
  52.  
  53.             tw.WriteLine(Templates.TestFile.Replace("$name$", testName));
  54.  
  55.             foreach (var td in data)
  56.             {
  57.                 tw.WriteLine("/// <summary>");
  58.                 tw.WriteLine("/// Determine if one <see cref=\"T:{0}\"/> is {1} equal to another", CreateClassRef(t), td.IsTrue ? "" : "not");
  59.                 tw.WriteLine("/// <list type=\"table\">");
  60.                 tw.WriteLine("/// <listheader><term>Property</term><description>Comments</description></listheader>");
  61.                 for (int i = 0; i < td.AValues.Count - 1; i++)
  62.                     tw.WriteLine("/// <item><term>{0}</term><description>A={1},B={2}</description></item>", td.AValues[i].Key, td.AValues[i].Value.Replace("<", "&lt;"), td.BValues[i].Value.Replace("<", "&lt;"));
  63.                 tw.WriteLine("/// </list>");
  64.  
  65.  
  66.                 tw.WriteLine("[TestMethod]");
  67.                 tw.WriteLine("public void {0}{1}Test() {{", testName, td.TestName);
  68.                 tw.WriteLine("{0} aValue = new {0}(), bValue = new {0}();", CreateClassRef(t));
  69.                 foreach (var av in td.AValues)
  70.                 {
  71.                     if (!String.IsNullOrEmpty(av.Value) && !av.Key.Contains("("))
  72.                         tw.WriteLine("aValue.{0} = {1};", av.Key, av.Value);
  73.                     else if (av.Key.Contains("("))
  74.                         tw.WriteLine("aValue.{0}{1});", av.Key, av.Value);
  75.                 }
  76.                 foreach (var av in td.BValues)
  77.                 {
  78.                     if (!String.IsNullOrEmpty(av.Value) && !av.Key.Contains("("))
  79.                         tw.WriteLine("bValue.{0} = {1};", av.Key, av.Value);
  80.                     else if (av.Key.Contains("("))
  81.                         tw.WriteLine("bValue.{0}{1});", av.Key, av.Value);
  82.                 }
  83.  
  84.                 if (td.IsTrue)
  85.                     tw.WriteLine("Assert.AreEqual(aValue, bValue);");
  86.                 else
  87.                     tw.WriteLine("Assert.AreNotEqual(aValue, bValue);");
  88.  
  89.                 tw.WriteLine("}");
  90.             }
  91.  
  92.             tw.WriteLine("}}");
  93.  
  94.         }
  95.         finally
  96.         {
  97.             if (tw != null)
  98.                 tw.Close();
  99.         }
  100.     }
  101.  
  102.  
  103.     private static string CreateClassRef(Type t)
  104.     {
  105.         String className = t.FullName;
  106.         if (className.Contains("`"))
  107.             className = className.Substring(0, className.IndexOf("`"));
  108.         if (t.IsGenericType)
  109.         {
  110.             className += "<";
  111.             foreach (var genParm in t.GetGenericArguments())
  112.                 className += String.Format("{0},", CreateClassRef(genParm));
  113.             className = className.Remove(className.Length - 1);
  114.             className += ">";
  115.         }
  116.         return className;
  117.     }
  118.  
  119.     /// <summary>
  120.     /// Generate test data
  121.     /// </summary>
  122.     private static List<TestData> GenerateTestData(Type t)
  123.     {
  124.         var anyTypes = Array.FindAll<Type>(typeof(II).Assembly.GetTypes(), o => !o.IsAbstract && typeof(ANY).IsAssignableFrom(o) && o.GetMethod("Equals", new Type[] { o }) != null);
  125.  
  126.         PropertyInfo[] pi = Array.FindAll<PropertyInfo>(t.GetProperties(BindingFlags.Public | BindingFlags.Instance), o => o.CanWrite && o.GetIndexParameters().Length == 0 && o.GetSetMethod() != null);
  127.  
  128.         // First type, equals each other
  129.         TestData equals = new TestData() { AValues = new List<KeyValuePair<string, string>>(), BValues = new List<KeyValuePair<string, string>>(), TestName = "Equals", IsTrue = true };
  130.         var notEquals = new List<TestData>(pi.Length);
  131.  
  132.         foreach (var info in pi)
  133.         {
  134.  
  135.             if (String.IsNullOrEmpty(GetInitializer(info.PropertyType, 0)) ||
  136.                 info.GetCustomAttributes(typeof(BrowsableAttribute), false).Length > 0)
  137.                 continue;
  138.  
  139.             equals.AValues.Add(CreatePropertyValue(info, 0));
  140.             equals.BValues.Add(CreatePropertyValue(info, 0));
  141.             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>>() };
  142.             foreach (var info2 in pi)
  143.             {
  144.                 if (String.IsNullOrEmpty(GetInitializer(info2.PropertyType, 0)) ||
  145.                     info2.GetCustomAttributes(typeof(BrowsableAttribute), false).Length > 0)
  146.                     continue;
  147.  
  148.                 if (info2 == info)
  149.                 {
  150.                     currentNotEquals.AValues.Add(CreatePropertyValue(info, 0));
  151.                     currentNotEquals.BValues.Add(CreatePropertyValue(info2, 1));
  152.                 }
  153.                 else
  154.                 {
  155.                     currentNotEquals.AValues.Add(CreatePropertyValue(info2, 0));
  156.                     currentNotEquals.BValues.Add(CreatePropertyValue(info2, 0));
  157.                 }
  158.             }
  159.             notEquals.Add(currentNotEquals);
  160.         }
  161.  
  162.         notEquals.Add(equals);
  163.         return notEquals;
  164.     }
  165.  
  166.     /// <summary>
  167.     /// Create property value setter
  168.     /// </summary>
  169.     private static KeyValuePair<string, string> CreatePropertyValue(PropertyInfo info, int p)
  170.     {
  171.         string initializer = GetInitializer(info.PropertyType, p);
  172.  
  173.         return new KeyValuePair<string, string>(info.Name, initializer);
  174.     }
  175.  
  176.     private static string GetInitializer(Type type, int p)
  177.     {
  178.         string initializer = "";
  179.         if (type == typeof(byte) || type == typeof(byte?))
  180.             initializer = p.ToString();
  181.         else if (type == typeof(int) || type == typeof(int?))
  182.             initializer = p.ToString();
  183.         else if (type == typeof(double) || type == typeof(double?))
  184.             initializer = p.ToString("0.0f");
  185.         else if (type == typeof(decimal) || type == typeof(decimal?))
  186.             initializer = String.Format("(decimal){0}", p);
  187.         else if (type == typeof(String))
  188.             initializer = String.Format("\"{0}\"", p);
  189.         else if (type == typeof(bool) || type == typeof(bool?))
  190.             initializer = Convert.ToBoolean(p).ToString().ToLower();
  191.         else if (type == typeof(DateTime) || type == typeof(DateTime?))
  192.             initializer = String.Format("DateTime.Parse(\"2011-{0}-10\")", p + 1);
  193.         else if (type.IsArray)
  194.             initializer = String.Format("new {0} {{ {1} }}", CreateClassRef(type), GetInitializer(type.GetMethod("Get").ReturnParameter.ParameterType, p));
  195.         else if (type.IsEnum) // CS?
  196.         {
  197.             initializer = String.Format("{0}.{1}", type.FullName, type.GetFields()[p == 0 ? 1 : type.GetFields().Count() - 1].Name);
  198.         }
  199.  
  200.         else if (!type.IsAbstract)
  201.         {
  202.             var ctor = Array.Find<ConstructorInfo>(type.GetConstructors(), o => o.GetParameters().Length > 0 && !Array.Exists<ParameterInfo>(o.GetParameters(), pa => String.IsNullOrEmpty(GetInitializer(pa.ParameterType, p))));
  203.             if (ctor == null)
  204.                 return string.Empty;
  205.             else
  206.             {
  207.                 StringBuilder initBuilder = new StringBuilder(String.Format("new {0}(", CreateClassRef(type)));
  208.                 foreach (ParameterInfo pi in ctor.GetParameters())
  209.                     initBuilder.AppendFormat("{0},", GetInitializer(pi.ParameterType, p));
  210.                 initBuilder.Remove(initBuilder.Length - 1, 1);
  211.                 initBuilder.Append(")");
  212.                 initializer = initBuilder.ToString();
  213.             }
  214.  
  215.         }
  216.  
  217.         if (type.GetMethod("Add") != null && type.IsGenericType)
  218.         {
  219.             initializer += String.Format(" {{ {0} }}", GetInitializer(type.GetGenericArguments()[0], p));
  220.         }
  221.         return initializer;
  222.  
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment