public IEnumerable<MSpecTestCase> EnumerateSpecs(string assemblyFilePath) { assemblyFilePath = Path.GetFullPath(assemblyFilePath); if (!File.Exists(assemblyFilePath)) { throw new ArgumentException("Could not find file: " + assemblyFilePath); } this.AssemblyFilename = assemblyFilePath; List<MSpecTestCase> list = new List<MSpecTestCase>(); // statically inspect the types in the assembly using mono.cecil foreach (TypeDefinition type in AssemblyDefinition.ReadAssembly(this.AssemblyFilename, new ReaderParameters() { ReadSymbols = true }).MainModule.GetTypes()) { // if a type is an It delegate generate some test case info for it foreach (FieldDefinition fieldDefinition in type.Fields.Where(x => x.FieldType.FullName == "Machine.Specifications.It" && !x.Name.Contains("__Cached"))) { string typeName = NormalizeCecilTypeName(type.Name); string typeFullName = NormalizeCecilTypeName(type.FullName); MSpecTestCase testCase = new MSpecTestCase() { ContextType = typeName, ContextFullType = typeFullName, SpecificationName = fieldDefinition.Name }; // get the source code location for the It delegate from the PDB file using mono.cecil.pdb this.UpdateTestCaseWithLocation(type, testCase); list.Add(testCase); } } return list.Select(x => x); }
public IEnumerable <MSpecTestCase> EnumerateSpecs(string assemblyFilePath) { assemblyFilePath = Path.GetFullPath(assemblyFilePath); if (!File.Exists(assemblyFilePath)) { throw new ArgumentException("Could not find file: " + assemblyFilePath); } this.AssemblyFilename = assemblyFilePath; List <MSpecTestCase> list = new List <MSpecTestCase>(); // statically inspect the types in the assembly using mono.cecil foreach (TypeDefinition type in AssemblyDefinition.ReadAssembly(this.AssemblyFilename, new ReaderParameters() { ReadSymbols = true }).MainModule.Types) { // if a type is an It delegate generate some test case info for it foreach (FieldDefinition fieldDefinition in type.Fields.Where(x => x.FieldType.FullName == "Machine.Specifications.It" && !x.Name.Contains("__Cached"))) { MSpecTestCase testCase = new MSpecTestCase() { ContextType = type.Name, ContextFullType = type.FullName, SpecificationName = fieldDefinition.Name }; // get the source code location for the It delegate from the PDB file using mono.cecil.pdb this.UpdateTestCaseWithLocation(type, testCase); list.Add(testCase); } } return(list.Select(x => x)); }
public IEnumerable <MSpecTestCase> EnumerateSpecs(string assemblyFilePath) { assemblyFilePath = Path.GetFullPath(assemblyFilePath); if (!File.Exists(assemblyFilePath)) { throw new ArgumentException("Could not find file: " + assemblyFilePath); } this.AssemblyFilename = assemblyFilePath; // make sure that cecil looks in the assembly path for mspec (+ related assemblies) first this.AssemblyResolver = new ScopedAssemblyResolver(Path.GetDirectoryName(assemblyFilePath)); this.ReaderParameters = new ReaderParameters() { ReadSymbols = true, AssemblyResolver = AssemblyResolver }; List <MSpecTestCase> list = new List <MSpecTestCase>(); List <IDelegateFieldScanner> fieldScanners = new List <IDelegateFieldScanner>(); fieldScanners.Add(new ItDelegateFieldScanner()); fieldScanners.Add(new CustomDelegateFieldScanner()); // statically inspect the types in the assembly using mono.cecil var assembly = AssemblyDefinition.ReadAssembly(this.AssemblyFilename, this.ReaderParameters); foreach (TypeDefinition type in GetNestedTypes(assembly.MainModule.Types)) { // if a type is an It delegate generate some test case info for it foreach (FieldDefinition fieldDefinition in type.Fields.Where(x => !x.Name.Contains("__Cached"))) { foreach (IDelegateFieldScanner scanner in fieldScanners) { if (scanner.ProcessFieldDefinition(fieldDefinition)) { string typeName = NormalizeCecilTypeName(type.Name); string typeFullName = NormalizeCecilTypeName(type.FullName); MSpecTestCase testCase = new MSpecTestCase() { ContextType = typeName, ContextFullType = typeFullName, SpecificationName = fieldDefinition.Name }; // get the source code location for the It delegate from the PDB file using mono.cecil.pdb this.UpdateTestCaseWithLocation(type, testCase); list.Add(testCase); break; } } } } return(list.Select(x => x)); }
public IEnumerable<MSpecTestCase> EnumerateSpecs(string assemblyFilePath) { assemblyFilePath = Path.GetFullPath(assemblyFilePath); if (!File.Exists(assemblyFilePath)) { throw new ArgumentException("Could not find file: " + assemblyFilePath); } this.AssemblyFilename = assemblyFilePath; // make sure that cecil looks in the assembly path for mspec (+ related assemblies) first this.AssemblyResolver = new ScopedAssemblyResolver(Path.GetDirectoryName(assemblyFilePath)); this.ReaderParameters = new ReaderParameters() { ReadSymbols = true, AssemblyResolver = AssemblyResolver }; List<MSpecTestCase> list = new List<MSpecTestCase>(); List<IDelegateFieldScanner> fieldScanners = new List<IDelegateFieldScanner>(); fieldScanners.Add(new ItDelegateFieldScanner()); fieldScanners.Add(new CustomDelegateFieldScanner()); // statically inspect the types in the assembly using mono.cecil var assembly = AssemblyDefinition.ReadAssembly(this.AssemblyFilename, this.ReaderParameters); foreach (TypeDefinition type in GetNestedTypes(assembly.MainModule.Types)) { // if a type is an It delegate generate some test case info for it foreach (FieldDefinition fieldDefinition in type.Fields.Where(x => !x.Name.Contains("__Cached"))) { foreach (IDelegateFieldScanner scanner in fieldScanners) { if (scanner.ProcessFieldDefinition(fieldDefinition)) { string typeName = NormalizeCecilTypeName(type.Name); string typeFullName = NormalizeCecilTypeName(type.FullName); MSpecTestCase testCase = new MSpecTestCase() { ContextType = typeName, ContextFullType = typeFullName, SpecificationName = fieldDefinition.Name }; // get the source code location for the It delegate from the PDB file using mono.cecil.pdb this.UpdateTestCaseWithLocation(type, testCase); list.Add(testCase); break; } } } } return list.Select(x => x); }
private void UpdateTestCaseWithLocation(TypeDefinition type, MSpecTestCase testCase) { if (!type.HasMethods) { return; } string fieldFullName = testCase.SpecificationName.Replace(" ", "_"); MethodDefinition methodDefinition = type.Methods.Where(x => x.IsConstructor && x.Parameters.Count == 0 && x.Name.EndsWith(".ctor")).SingleOrDefault(); if (methodDefinition.HasBody) { // check if there is a subject attribute if (type.HasCustomAttributes) { List <CustomAttribute> list = type.CustomAttributes.Where(x => x.AttributeType.FullName == "Machine.Specifications.SubjectAttribute").ToList(); if (list.Count > 0 && list[0].ConstructorArguments.Count > 0) { testCase.SubjectName = Enumerable.First <CustomAttributeArgument>((IEnumerable <CustomAttributeArgument>)list[0].ConstructorArguments).Value.ToString(); } List <CustomAttribute> tagsList = type.CustomAttributes.Where(x => x.AttributeType.FullName == "Machine.Specifications.TagsAttribute").ToList(); if (tagsList.Count > 0 && tagsList[0].ConstructorArguments.Count > 0) { List <string> tags = new List <string>(); tags.Add(tagsList[0].ConstructorArguments[0].Value.ToString()); if (tagsList[0].ConstructorArguments.Count == 2) { foreach (CustomAttributeArgument additionalTag in ((IEnumerable <CustomAttributeArgument>)tagsList[0].ConstructorArguments[1].Value)) { tags.Add(additionalTag.Value.ToString()); } } testCase.Tags = tags.ToArray(); } } // now find the source code location Instruction instruction = methodDefinition.Body.Instructions.Where(x => x.Operand != null && x.Operand.GetType().IsAssignableFrom(typeof(FieldDefinition)) && ((MemberReference)x.Operand).Name == fieldFullName).SingleOrDefault(); while (instruction != null) { if (instruction.SequencePoint != null && instruction.SequencePoint.StartLine != PdbHiddenLine) { testCase.CodeFilePath = instruction.SequencePoint.Document.Url; testCase.LineNumber = instruction.SequencePoint.StartLine; break; } instruction = instruction.Previous; } } }
private void UpdateTestCaseWithLocation(TypeDefinition type, MSpecTestCase testCase) { if (!type.HasMethods) { return; } string fieldFullName = testCase.SpecificationName.Replace(" ", "_"); string constructorMethodFullName = string.Format("System.Void {0}::{1}", (object)testCase.ContextFullType, (object)".ctor()"); MethodDefinition methodDefinition = type.Methods.Where(x => x.FullName == constructorMethodFullName).SingleOrDefault(); if (methodDefinition.HasBody) { // check if there is a subject attribute if (type.HasCustomAttributes) { List <CustomAttribute> list = type.CustomAttributes.Where(x => x.AttributeType.FullName == "Machine.Specifications.SubjectAttribute").ToList(); if (list.Count > 0 && list[0].ConstructorArguments.Count > 0) { testCase.SubjectName = Enumerable.First <CustomAttributeArgument>((IEnumerable <CustomAttributeArgument>)list[0].ConstructorArguments).Value.ToString(); } } // now find the source code location Instruction instruction = methodDefinition.Body.Instructions.Where(x => x.Operand != null && x.Operand.GetType().IsAssignableFrom(typeof(FieldDefinition)) && ((MemberReference)x.Operand).Name == fieldFullName).SingleOrDefault(); while (instruction != null) { if (instruction.SequencePoint != null && instruction.SequencePoint.StartLine != PdbHiddenLine) { testCase.CodeFilePath = instruction.SequencePoint.Document.Url; testCase.LineNumber = instruction.SequencePoint.StartLine; break; } instruction = instruction.Previous; } } }
private void UpdateTestCaseWithLocation(TypeDefinition type, MSpecTestCase testCase) { if (!type.HasMethods) { return; } string fieldFullName = testCase.SpecificationName.Replace(" ", "_"); MethodDefinition methodDefinition = type.Methods.Where(x => x.IsConstructor && x.Parameters.Count == 0).SingleOrDefault(); if (methodDefinition.HasBody) { // check if there is a subject attribute if (type.HasCustomAttributes) { List<CustomAttribute> list = type.CustomAttributes.Where(x => x.AttributeType.FullName == "Machine.Specifications.SubjectAttribute").ToList(); if (list.Count > 0 && list[0].ConstructorArguments.Count > 0) { testCase.SubjectName = Enumerable.First<CustomAttributeArgument>((IEnumerable<CustomAttributeArgument>)list[0].ConstructorArguments).Value.ToString(); } } // now find the source code location Instruction instruction = methodDefinition.Body.Instructions.Where(x => x.Operand != null && x.Operand.GetType().IsAssignableFrom(typeof(FieldDefinition)) && ((MemberReference)x.Operand).Name == fieldFullName).SingleOrDefault(); while (instruction != null) { if (instruction.SequencePoint != null && instruction.SequencePoint.StartLine != PdbHiddenLine) { testCase.CodeFilePath = instruction.SequencePoint.Document.Url; testCase.LineNumber = instruction.SequencePoint.StartLine; break; } instruction = instruction.Previous; } } }
public static TestCase GetVSTestCaseFromMSpecTestCase(string source, MSpecTestCase mspecTestCase, Uri uri, Func<string, string, dynamic> traitCreator) { string specificationName = mspecTestCase.SpecificationName; string fullyQualifiedName = string.Format("{0}::{1}", (object)mspecTestCase.ContextFullType, (object)specificationName); TestCase testCase = new TestCase(fullyQualifiedName, uri, source) { DisplayName = mspecTestCase.SpecificationName.Replace("_", " "), CodeFilePath = mspecTestCase.CodeFilePath, LineNumber = mspecTestCase.LineNumber }; if (MSpecTestAdapter.UseTraits) { dynamic dynTestCase = testCase; dynamic classTrait = traitCreator(Strings.TRAIT_CLASS, mspecTestCase.ContextType); dynamic subjectTrait = traitCreator(Strings.TRAIT_SUBJECT, string.IsNullOrEmpty(mspecTestCase.SubjectName) ? Strings.TRAIT_SUBJECT_NOSUBJECT : mspecTestCase.SubjectName); dynTestCase.Traits.Add(classTrait); dynTestCase.Traits.Add(subjectTrait); } Debug.WriteLine(string.Format("TestCase {0}", (object)testCase.FullyQualifiedName)); return testCase; }
public static TestCase GetVSTestCaseFromMSpecTestCase(string source, MSpecTestCase mspecTestCase, Uri uri, Func <string, string, dynamic> traitCreator) { string specificationName = mspecTestCase.SpecificationName; string fullyQualifiedName = string.Format("{0}::{1}", (object)mspecTestCase.ContextFullType, (object)specificationName); TestCase testCase = new TestCase(fullyQualifiedName, uri, source) { DisplayName = mspecTestCase.SpecificationName.Replace("_", " "), CodeFilePath = mspecTestCase.CodeFilePath, LineNumber = mspecTestCase.LineNumber }; if (MSpecTestAdapter.UseTraits) { dynamic dynTestCase = testCase; dynamic classTrait = traitCreator(Strings.TRAIT_CLASS, mspecTestCase.ContextType); dynamic subjectTrait = traitCreator(Strings.TRAIT_SUBJECT, string.IsNullOrEmpty(mspecTestCase.SubjectName) ? Strings.TRAIT_SUBJECT_NOSUBJECT : mspecTestCase.SubjectName); dynTestCase.Traits.Add(classTrait); dynTestCase.Traits.Add(subjectTrait); if (mspecTestCase.Tags != null) { foreach (var tag in mspecTestCase.Tags) { if (!string.IsNullOrEmpty(tag)) { dynamic tagTrait = traitCreator(Strings.TRAIT_TAG, tag); dynTestCase.Traits.Add(tagTrait); } } } } Debug.WriteLine(string.Format("TestCase {0}", (object)testCase.FullyQualifiedName)); return(testCase); }