需要测试的类:
namespace SPM.Tools.DataFeed.Generator
{
static class GenCodeUtil
{
internal static void CleanUpFile(string directory, string fileName)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
return;
}
String[] files = Directory.GetFiles(directory, fileName);
foreach (String file in files)
{
File.Delete(file);
}
}
internal static void WriteCSharpSourceCode(CodeNamespace codeNameSpace, string file)
{
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BlankLinesBetweenMembers = true;
options.ElseOnClosing = true;
options.VerbatimOrder = true; WriteCSharpSourceCode(codeNameSpace, file, options);
} internal static void WriteCSharpSourceCode(CodeNamespace codeNameSpace, string file,
CodeGeneratorOptions options)
{
DirectoryInfo direcotry = new FileInfo(file).Directory;
if (!direcotry.Exists)
{
CreateDirectory(direcotry);
}
if (File.Exists(file))
{
File.Delete(file); // replace
}
Console.WriteLine(String.Format("Writing to file {0}...", file)); using (var writer = new StringWriter())
{
new CSharpCodeProvider().GenerateCodeFromNamespace(codeNameSpace, writer, options);
String code = writer.GetStringBuilder().ToString();
File.WriteAllText(file, code);
}
}
private static void CreateDirectory(DirectoryInfo direcotry)
{
if (!direcotry.Parent.Exists)
{
CreateDirectory(direcotry.Parent);
}
direcotry.Create();
}
internal static CodeNamespace GenClass(string spaceName, string className, string baseType, string[] imports, string comment)
{
CodeNamespace codeNameSpace = new CodeNamespace(spaceName);
codeNameSpace.Comments.Add(new CodeCommentStatement(comment));
if (imports != null)
{
foreach (string s in imports)
{
codeNameSpace.Imports.Add(new CodeNamespaceImport(s));
}
}
CodeTypeDeclaration clazzDecl = new CodeTypeDeclaration(className);
clazzDecl.IsClass = true;
codeNameSpace.Types.Add(clazzDecl); if (baseType != null)
{
clazzDecl.BaseTypes.Add(baseType);
}
clazzDecl.Attributes = MemberAttributes.Public;
return codeNameSpace;
}
internal static CodeSnippetExpression BuildCodeSnippetExpression(string text)
{
return new CodeSnippetExpression("\"" + text + "\"");
} internal static CodeMemberProperty CreateProperty(string name, String typeName, bool hasGet, bool hasSet, String fieldName)
{
CodeMemberProperty prop = new CodeMemberProperty();
prop.Name = name;
prop.Type = new CodeTypeReference(typeName);
prop.HasGet = hasGet;
prop.HasSet = hasSet;
prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; prop.GetStatements.Add(
new CodeMethodReturnStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), fieldName)));
prop.SetStatements.Add(
new CodeAssignStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), fieldName),
new CodeArgumentReferenceExpression("value")));
return prop;
}
internal static CodeMemberProperty CreateProperty(string name, String typeName, String fieldName)
{
return CreateProperty(name, typeName, true, true, fieldName);
}
internal static List<CodeMemberProperty> GetMemberProperty(this CodeTypeDeclaration type)
{
List<CodeMemberProperty> list = new List<CodeMemberProperty>();
foreach (CodeTypeMember member in type.Members)
{
if (member is CodeMemberProperty)
{
CodeMemberProperty prop = (CodeMemberProperty)member;
list.Add(prop);
}
}
return list;
}
internal static List<CodeMemberField> GetMemberField(this CodeTypeDeclaration type)
{
List<CodeMemberField> list = new List<CodeMemberField>(); foreach (CodeTypeMember member in type.Members)
{
if (member is CodeMemberField)
{
CodeMemberField fld = (CodeMemberField)member;
list.Add(fld);
}
}
return list;
}
internal static CodeAttributeDeclaration[] GetCustomAttributes(this CodeTypeMember member)
{
CodeAttributeDeclaration[] attrs = new CodeAttributeDeclaration[member.CustomAttributes.Count];
member.CustomAttributes.CopyTo(attrs, 0);
return attrs;
} internal static Boolean ContainsType(this CodeNamespace space, String type)
{
CodeTypeDeclaration[] array = new CodeTypeDeclaration[space.Types.Count];
space.Types.CopyTo(array, 0);
return array.Select(aType => aType.Name.Equals(type) || (space.Name + "." + aType.Name).Equals(type)).Contains(true);
} internal static Boolean ContainsCustomAttribute(this CodeTypeDeclaration type, Type attribute)
{
CodeAttributeDeclaration[] attributes = GetCustomAttributes(type); return attributes.AsQueryable().Select(attr => attr.Name.Equals(attribute.ToString())).Contains(true);
} internal static Boolean ContainsCustomAttribute(this CodeTypeMember member, Type attribute)
{
CodeAttributeDeclaration[] attributes = GetCustomAttributes(member); return attributes.AsQueryable().Select(attr => attr.Name.Equals(attribute.ToString())).Contains(true);
} internal static CodeAttributeArgument GetArgument(this CodeAttributeDeclaration codeAttrDecl, string argmentName)
{
foreach (CodeAttributeArgument arg in codeAttrDecl.Arguments)
{
if (arg.Name == argmentName)
{
return arg;
}
}
return null;
} internal static void SetPostion(CodeAttributeDeclaration attr, int p)
{
attr.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(p)));
} internal static int GetIndexOfTypeInClass(CodeMemberProperty property, CodeTypeDeclaration type)
{
List<CodeMemberProperty> propertyList = GenCodeUtil.GetMemberProperty(type);
int index = 0;
foreach (CodeMemberProperty p in propertyList)
{
if (!p.Type.BaseType.Equals(property.Type.BaseType))
{
continue;
}
if (p == property)
{
break;
} index++;
}
return index;
}
namespace SPM.Tools.DataFeed.Generator
{
static class GenCodeUtil
{
internal static void CleanUpFile(string directory, string fileName)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
return;
}
String[] files = Directory.GetFiles(directory, fileName);
foreach (String file in files)
{
File.Delete(file);
}
}
internal static void WriteCSharpSourceCode(CodeNamespace codeNameSpace, string file)
{
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BlankLinesBetweenMembers = true;
options.ElseOnClosing = true;
options.VerbatimOrder = true; WriteCSharpSourceCode(codeNameSpace, file, options);
} internal static void WriteCSharpSourceCode(CodeNamespace codeNameSpace, string file,
CodeGeneratorOptions options)
{
DirectoryInfo direcotry = new FileInfo(file).Directory;
if (!direcotry.Exists)
{
CreateDirectory(direcotry);
}
if (File.Exists(file))
{
File.Delete(file); // replace
}
Console.WriteLine(String.Format("Writing to file {0}...", file)); using (var writer = new StringWriter())
{
new CSharpCodeProvider().GenerateCodeFromNamespace(codeNameSpace, writer, options);
String code = writer.GetStringBuilder().ToString();
File.WriteAllText(file, code);
}
}
private static void CreateDirectory(DirectoryInfo direcotry)
{
if (!direcotry.Parent.Exists)
{
CreateDirectory(direcotry.Parent);
}
direcotry.Create();
}
internal static CodeNamespace GenClass(string spaceName, string className, string baseType, string[] imports, string comment)
{
CodeNamespace codeNameSpace = new CodeNamespace(spaceName);
codeNameSpace.Comments.Add(new CodeCommentStatement(comment));
if (imports != null)
{
foreach (string s in imports)
{
codeNameSpace.Imports.Add(new CodeNamespaceImport(s));
}
}
CodeTypeDeclaration clazzDecl = new CodeTypeDeclaration(className);
clazzDecl.IsClass = true;
codeNameSpace.Types.Add(clazzDecl); if (baseType != null)
{
clazzDecl.BaseTypes.Add(baseType);
}
clazzDecl.Attributes = MemberAttributes.Public;
return codeNameSpace;
}
internal static CodeSnippetExpression BuildCodeSnippetExpression(string text)
{
return new CodeSnippetExpression("\"" + text + "\"");
} internal static CodeMemberProperty CreateProperty(string name, String typeName, bool hasGet, bool hasSet, String fieldName)
{
CodeMemberProperty prop = new CodeMemberProperty();
prop.Name = name;
prop.Type = new CodeTypeReference(typeName);
prop.HasGet = hasGet;
prop.HasSet = hasSet;
prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; prop.GetStatements.Add(
new CodeMethodReturnStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), fieldName)));
prop.SetStatements.Add(
new CodeAssignStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), fieldName),
new CodeArgumentReferenceExpression("value")));
return prop;
}
internal static CodeMemberProperty CreateProperty(string name, String typeName, String fieldName)
{
return CreateProperty(name, typeName, true, true, fieldName);
}
internal static List<CodeMemberProperty> GetMemberProperty(this CodeTypeDeclaration type)
{
List<CodeMemberProperty> list = new List<CodeMemberProperty>();
foreach (CodeTypeMember member in type.Members)
{
if (member is CodeMemberProperty)
{
CodeMemberProperty prop = (CodeMemberProperty)member;
list.Add(prop);
}
}
return list;
}
internal static List<CodeMemberField> GetMemberField(this CodeTypeDeclaration type)
{
List<CodeMemberField> list = new List<CodeMemberField>(); foreach (CodeTypeMember member in type.Members)
{
if (member is CodeMemberField)
{
CodeMemberField fld = (CodeMemberField)member;
list.Add(fld);
}
}
return list;
}
internal static CodeAttributeDeclaration[] GetCustomAttributes(this CodeTypeMember member)
{
CodeAttributeDeclaration[] attrs = new CodeAttributeDeclaration[member.CustomAttributes.Count];
member.CustomAttributes.CopyTo(attrs, 0);
return attrs;
} internal static Boolean ContainsType(this CodeNamespace space, String type)
{
CodeTypeDeclaration[] array = new CodeTypeDeclaration[space.Types.Count];
space.Types.CopyTo(array, 0);
return array.Select(aType => aType.Name.Equals(type) || (space.Name + "." + aType.Name).Equals(type)).Contains(true);
} internal static Boolean ContainsCustomAttribute(this CodeTypeDeclaration type, Type attribute)
{
CodeAttributeDeclaration[] attributes = GetCustomAttributes(type); return attributes.AsQueryable().Select(attr => attr.Name.Equals(attribute.ToString())).Contains(true);
} internal static Boolean ContainsCustomAttribute(this CodeTypeMember member, Type attribute)
{
CodeAttributeDeclaration[] attributes = GetCustomAttributes(member); return attributes.AsQueryable().Select(attr => attr.Name.Equals(attribute.ToString())).Contains(true);
} internal static CodeAttributeArgument GetArgument(this CodeAttributeDeclaration codeAttrDecl, string argmentName)
{
foreach (CodeAttributeArgument arg in codeAttrDecl.Arguments)
{
if (arg.Name == argmentName)
{
return arg;
}
}
return null;
} internal static void SetPostion(CodeAttributeDeclaration attr, int p)
{
attr.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(p)));
} internal static int GetIndexOfTypeInClass(CodeMemberProperty property, CodeTypeDeclaration type)
{
List<CodeMemberProperty> propertyList = GenCodeUtil.GetMemberProperty(type);
int index = 0;
foreach (CodeMemberProperty p in propertyList)
{
if (!p.Type.BaseType.Equals(property.Type.BaseType))
{
continue;
}
if (p == property)
{
break;
} index++;
}
return index;
}
internal static CodeMemberField CreateField(string name, String typeName)
{
CodeMemberField field = new CodeMemberField();
field.Name = name;
field.Attributes = MemberAttributes.Private;
field.Type = new CodeTypeReference(typeName);
return field;
} internal static void CreateProperty(string propertyName, Type propertyType, CodeTypeDeclaration clazz)
{
String fieldName = NamingConventionConverter.Convert(propertyName, NamingConventionType.LowerCamelCaseWithUnderScorePrefix);
CodeMemberField field = GenCodeUtil.CreateField(fieldName, propertyType.FullName);
CodeMemberProperty prop = GenCodeUtil.CreateProperty(propertyName, propertyType.FullName, field.Name); clazz.Members.Add(field);
clazz.Members.Add(prop);
}
internal static CodeTypeDeclaration GenClassDeclaration(string typeName, String baseTypeName, MemberAttributes classAttributes)
{
return GenClassDeclaration(typeName, new List<String> { baseTypeName }, classAttributes);
} internal static CodeTypeDeclaration GenClassDeclaration(string typeName, List<String> baseTypeNameList, MemberAttributes classAttributes)
{
CodeTypeDeclaration clazz = new CodeTypeDeclaration(typeName);
clazz.IsClass = true; if (baseTypeNameList != null) {
baseTypeNameList.ForEach(v => clazz.BaseTypes.Add(v) );
}
clazz.Attributes = classAttributes;
return clazz;
} public static String QuoteName(String name)
{
return "`" + name + "`";
} internal static CodeNamespace CreateCodeNameSpace(string spaceName, String[] imports)
{
CodeNamespace codeNameSpace = new CodeNamespace(spaceName);
if (imports!= null) {
imports.ToList<String>().ForEach(v => codeNameSpace.Imports.Add(new CodeNamespaceImport(v)));
} codeNameSpace.Comments.Add(new CodeCommentStatement(BuildCodeNameSpaceComment()));
return codeNameSpace;
} static String BuildCodeNameSpaceComment()
{
return "This file is generated automatically on " + DateTime.Now + " by codegen tool.";
}
} public static class GenOrmIdKeyUtil
{
public static Boolean isOrmIdProperty(CodeMemberProperty property)
{
return property.Name.Equals(OrmIdPropertyName);
} public static Boolean isOrmIdField(CodeMemberField field)
{
return field.Name.Equals(OrmIdFieldName);
} const String OrmIdFieldName = "_ormId";
const String OrmIdPropertyName = "OrmId"; public static CodeMemberField GenOrmIdField()
{
CodeMemberField field = new CodeMemberField();
field.Name = GenOrmIdKeyUtil.OrmIdFieldName;
field.Attributes = MemberAttributes.Private;
field.Type = new CodeTypeReference(typeof(int)); return field;
} public static CodeMemberProperty GenOrmIdProperty()
{
CodeMemberProperty prop = new CodeMemberProperty();
prop.Name = GenOrmIdKeyUtil.OrmIdPropertyName;
prop.Type = new CodeTypeReference(typeof(int));
prop.HasGet = true;
prop.HasSet = true;
prop.Attributes = MemberAttributes.Public | MemberAttributes.Final; prop.GetStatements.Add(
new CodeMethodReturnStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), OrmIdFieldName)));
prop.SetStatements.Add(
new CodeAssignStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(), OrmIdFieldName),
new CodeArgumentReferenceExpression("value"))); return prop;
}
}
}
要求:就是对里面的每个方法进行测试(写法:如下面所示:)
[TestFixture]
public class TestGenCodeUtil
{
[Test]
public void TestCleanUpFile()
{
string fileName = "A.txt";
string directory="C:\\temp";
GenCodeUtil.CleanUpFile(directory,fileName);
Console.WriteLine("FileName:" + fileName + "Directory"+directory);
} [Test]
public void TestQuoteName()
{
string name = "OrmId";
GenCodeUtil.QuoteName(name);
Console.WriteLine("Name:"+name);
}
[Test]
public void TestGenOrmIdKeyUtil()
{
string OrmIdFieldName = "__ormId";
string OrmIdPropertyName = "OrmId";
CodeMemberProperty codeProperty = new CodeMemberProperty();
codeProperty.HasGet = true;
codeProperty.HasSet = true;
}
}请哪位高手帮忙写下!谢谢!