public void Test() { InstrumentedAssembly instrumentedAssembly; TypeDefinition typeDefinition; string il; if (_methodToInstrument != null) { var methodDefinition = _methodToInstrument.ToDefinition(); typeDefinition = methodDefinition?.DeclaringType; instrumentedAssembly = methodDefinition.Instrument(); il = new ILFormatter(false).FormatMethodBody(methodDefinition); } else { typeDefinition = _typeToInstrument.ToDefinition(); instrumentedAssembly = typeDefinition.Instrument(); il = new ILFormatter(false).FormatType(typeDefinition); } if (ExpectedIL != null) { il.ToOSLineEnding().Should().Be(ExpectedIL.ToOSLineEnding()); } var instrumentedInstructions = instrumentedAssembly.SourceFiles .SelectMany(file => file.Sequences) .ToArray(); if (ExpectedInstructions != null) { instrumentedInstructions.Should().BeEquivalentTo(ExpectedInstructions, config => config .Using <string>(strCtx => strCtx.Subject?.ToOSLineEnding().Should().Be(strCtx.Expectation?.ToOSLineEnding())) .WhenTypeIs <string>()); } var instrumentedType = typeDefinition.Load(); var instrumentedTestType = instrumentedType.Assembly.GetType(GetType().FullName); var functionalTestMethod = instrumentedTestType.GetMethod(nameof(FunctionalTest)); HitContext.Current = new HitContext("Assembly", "Class", "Method"); HitContext.Current.EnterMethod(); var instrumentedTest = Activator.CreateInstance(instrumentedTestType); functionalTestMethod.Invoke(instrumentedTest, new object[0]); if (ExpectedHits != null) { HitContext.Current.Hits.Should().BeEquivalentTo(ExpectedHits); } if (ExpectedHitCount != null) { HitContext.Current.Hits.Sum(h => h.Value).Should().Be(ExpectedHitCount); } }
public void Test() { InstrumentedAssembly instrumentedAssembly; TypeDefinition typeDefinition; if (_methodToInstrument != null) { var methodDefinition = _methodToInstrument.ToDefinition(); typeDefinition = methodDefinition?.DeclaringType; instrumentedAssembly = methodDefinition.Instrument(); var il = new ILFormatter(false).FormatMethodBody(methodDefinition); il.NormalizeLineEndings().Should().Be(ExpectedIL.NormalizeLineEndings()); } else { typeDefinition = _typeToInstrument.ToDefinition(); instrumentedAssembly = typeDefinition.Instrument(); var il = new ILFormatter(false).FormatType(typeDefinition); il.NormalizeLineEndings().Should().Be(ExpectedIL.NormalizeLineEndings()); } var instrumentedInstructions = instrumentedAssembly.SourceFiles .SelectMany(kv => kv.Value.Instructions) .ToArray(); if (ExpectedInstructions != null) { instrumentedInstructions.Should().BeEquivalentTo(ExpectedInstructions); } var instrumentedType = typeDefinition.Load(); var instrumentedTestType = instrumentedType.Assembly.GetType(GetType().FullName); var functionalTestMethod = instrumentedTestType.GetMethod(nameof(FunctionalTest)); HitContext.Current = new HitContext("Assembly", "Class", "Method"); var instrumentedTest = Activator.CreateInstance(instrumentedTestType); functionalTestMethod.Invoke(instrumentedTest, new object[0]); HitContext.Current.Hits.Should().BeEquivalentTo(ExpectedHits); }