protected string GetTestAdapterPath(UnitTestFramework testFramework = UnitTestFramework.MSTest) { string adapterRelativePath = string.Empty; if (testFramework == UnitTestFramework.MSTest) { adapterRelativePath = string.Format(TestAdapterRelativePath, this.testEnvironment.DependencyVersions["MSTestAdapterVersion"]); } else if (testFramework == UnitTestFramework.NUnit) { adapterRelativePath = string.Format(NUnitTestAdapterRelativePath, this.testEnvironment.DependencyVersions["NUnitAdapterVersion"]); } else if (testFramework == UnitTestFramework.XUnit) { adapterRelativePath = string.Format(XUnitTestAdapterRelativePath, this.testEnvironment.DependencyVersions["XUnitAdapterVersion"]); } else if (testFramework == UnitTestFramework.Chutzpah) { adapterRelativePath = string.Format(ChutzpahTestAdapterRelativePath, this.testEnvironment.DependencyVersions["ChutzpahAdapterVersion"]); } return(this.testEnvironment.GetNugetPackage(adapterRelativePath)); }
public void TestProbeResolver_UsesLatestHandler() { //arrange const int childCount = 5; Type childType = typeof(ReplyChildActor1); Guid message = Guid.NewGuid(); int replyCount = 0; //act UnitTestFramework <ParentActor> sut = UnitTestFrameworkSettings .Empty .RegisterHandler <ReplyChildActor1, Guid>(guid => (default(Guid), default(int))) .RegisterHandler <ReplyChildActor1, Guid>(guid => (guid, ++replyCount)) .CreateFramework <ParentActor>(this, Props.Create(() => new ParentActor(childType, childCount)), childCount); //assert sut.Sut.Tell(new TellAllChildren(message)); ExpectMsgAllOf(Enumerable .Range(1, childCount) .Select(i => (message, i)) .ToArray() ); }
public async Task UnitTestFramework_DelayAsync_AwaitsDelayerAsync() { //arrange List <string> callOrder = new List <string>(); DelayerMock .Setup(delayer => delayer.DelayAsync(this, DelayDuration)) .Returns(() => Task.Run(() => { callOrder.Add("ReturnedTask1"); Thread.Sleep(TimeSpan.FromMilliseconds(200)); callOrder.Add("ReturnedTask2"); })); UnitTestFramework <DummyActor> sut = CreateUnitTestFramework(); //act Task result = sut.DelayAsync(DelayDuration); //assert await result; callOrder.Add("AfterTask"); callOrder.Should().Equal("ReturnedTask1", "ReturnedTask2", "AfterTask"); }
/// <summary> /// Called to test a C# DiagnosticAnalyzer when applied on the single inputted string as a source. /// Note: input a DiagnosticResult for each Diagnostic expected. /// </summary> /// <param name="source"> A class in the form of a string to run the analyzer on.</param> /// <param name="expected"> DiagnosticResults that should appear after the analyzer is run on the source.</param> /// <param name="cancellationToken"> The cancelation token.</param> /// <param name="framework">The targeted unit test framework.</param> protected Task VerifyCSharpDiagnosticAsync(string source, DiagnosticResult[] expected, CancellationToken cancellationToken, UnitTestFramework framework) { return(this.VerifyDiagnosticsAsync(new[] { source }, LanguageNames.CSharp, this.GetCSharpDiagnosticAnalyzer(), expected, cancellationToken, framework)); }
/// <summary> /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run, /// then verifies each of them. /// </summary> /// <param name="sources">An array of strings to create source documents from to run the analyzers on</param> /// <param name="language">The language of the classes represented by the source strings</param> /// <param name="analyzer">The analyzer to be run on the source code</param> /// <param name="expected">DiagnosticResults that should appear after the analyzer is run on the sources</param> /// <param name="cancellationToken">The cancelation token.</param> /// <param name="framework">The targeted unit test framework.</param> private async Task VerifyDiagnosticsAsync(string[] sources, string language, DiagnosticAnalyzer analyzer, DiagnosticResult[] expected, CancellationToken cancellationToken, UnitTestFramework framework) { var diagnostics = await this.GetSortedDiagnosticsAsync(sources, language, analyzer, cancellationToken, framework).ConfigureAwait(false); VerifyDiagnosticResults(diagnostics, analyzer, expected); }
public TestBase() : base(AkkaConfig.Config) { // Create values passed into sut TestKitPassedIntoSut = this; HandlersPassedIntoSut = ImmutableDictionary <(Type, Type), Func <object, object> > .Empty .Add((typeof(DummyActor1), typeof(Message1)), message1 => new Reply1()) .Add((typeof(DummyActor1), typeof(Message2)), message1 => new Reply2()) .Add((typeof(DummyActor2), typeof(Message1)), message1 => new Reply1()); PropsPassedIntoSut = Props.Create <DummyActor1>(); NumberOfChildrenIntoSut = TestUtils.Create <int>(); // Create shims _shimContext = ShimsContext.Create(); // Set up shims ShimSutCreator.Constructor = @this => { SutCreatorConstructorCount++; ConstructedSutCreator = @this; }; ShimTellChildWaiter.Constructor = @this => { TellChildWaiterConstructorCount++; ConstructedTellChildWaiter = @this; }; ShimChildWaiter.Constructor = @this => { ChildWaiterConstructorCount++; ConstructedChildWaiter = @this; }; ShimDependencyResolverAdder.Constructor = @this => { DependencyResolverAdderConstructorCount++; ConstructedDependencyResolverAdder = @this; }; ShimTestProbeDependencyResolverAdder.Constructor = @this => { TestProbeDependencyResolverAdderConstructorCount++; ConstructedTestProbeDependencyResolverAdder = @this; }; ShimTestProbeCreator.Constructor = @this => { TestProbeCreatorConstructorCount++; ConstructedTestProbeCreator = @this; }; ShimResolvedTestProbeStore.Constructor = @this => { ResolvedTestProbeStoreConstructorCount++; ConstructedResolvedTestProbeStore = @this; }; ShimTestProbeActorCreator.Constructor = @this => { TestProbeActorCreatorConstructorCount++; ConstructedTestProbeActorCreator = @this; }; ShimTestProbeHandlersMapper.Constructor = @this => { TestProbeHandlersMapperConstructorCount++; ConstructedTestProbeHandlersMapper = @this; }; ShimSutSupervisorStrategyGetter.Constructor = @this => { SutSupervisorStrategyGetterConstructorCount++; ConstructedSutSupervisorStrategyGetter = @this; }; ShimUnitTestFramework <DummyActor1> .ConstructorISutCreatorITellChildWaiterIChildWaiterIDependencyResolverAdderITestProbeDependencyResolverAdderITestProbeCreatorIResolvedTestProbeStoreITestProbeActorCreatorITestProbeHandlersMapperISutSupervisorStrategyGetterImmutableDictionaryOfValueTupleOfTy = (@this, sutCreator, tellChildWaiter, childWaiter, dependencyResolverAdder, testProbeDependencyResolverAdder, testProbeCreator, resolvedTestProbeStore, testProbeActorCreator, testProbeHandlersMapper, sutSupervisorStrategyGetter, handlers, testKit, props, numberOfChildren) => { UnitTestFrameworkConstructorCount++; ConstructedUnitTestFramework = @this; SutCreatorPassedIntoShim = sutCreator; TellChildWaiterPassedIntoShim = tellChildWaiter; ChildWaiterPassedIntoShim = childWaiter; DependencyResolverAdderPassedIntoShim = dependencyResolverAdder; TestProbeDependencyResolverAdderPassedIntoShim = testProbeDependencyResolverAdder; TestProbeCreatorPassedIntoShim = testProbeCreator; ResolvedTestProbeStorePassedIntoShim = resolvedTestProbeStore; TestProbeActorCreatorPassedIntoShim = testProbeActorCreator; TestProbeHandlersMapperPassedIntoShim = testProbeHandlersMapper; SutSupervisorStrategyGetterIntoShim = sutSupervisorStrategyGetter; HandlersPassedIntoShim = handlers; TestKitPassedIntoShim = testKit; PropsPassedIntoShim = props; NumberOfChildrenIntoShim = numberOfChildren; }; }
/// <summary> /// Given classes in the form of strings, their language, and an <see cref="DiagnosticAnalyzer"/> to apply to /// it, return the <see cref="Diagnostic"/>s found in the string after converting it to a /// <see cref="Document"/>. /// </summary> /// <param name="sources">Classes in the form of strings.</param> /// <param name="language">The language the source classes are in. Values may be taken from the /// <see cref="LanguageNames"/> class.</param> /// <param name="analyzer">The analyzer to be run on the sources.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param> /// <param name="framework">The targeted unit test framework.</param> /// <returns>A collection of <see cref="Diagnostic"/>s that surfaced in the source code, sorted by /// <see cref="Diagnostic.Location"/>.</returns> private Task <ImmutableArray <Diagnostic> > GetSortedDiagnosticsAsync(string[] sources, string language, DiagnosticAnalyzer analyzer, CancellationToken cancellationToken, UnitTestFramework framework) { return(GetSortedDiagnosticsFromDocumentsAsync(analyzer, this.GetDocuments(sources, language, framework), cancellationToken)); }
/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="language">The language the source code is in</param> /// <param name="framework">The targed unit test framework.</param> /// <returns>A Project created out of the Documents created from the source strings</returns> private Project CreateProject(string[] sources, string language, UnitTestFramework framework) { string fileNamePrefix = DefaultFilePathPrefix; string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true); var projectId = ProjectId.CreateNewId(debugName: TestProjectName); MetadataReference corlibReference; MetadataReference unitTestFrameworkReference; switch (framework) { case UnitTestFramework.MSTest: unitTestFrameworkReference = MsTestReference; corlibReference = CorlibReference; break; case UnitTestFramework.Xunit: unitTestFrameworkReference = XunitReference; corlibReference = PortableCorlibReference; break; case UnitTestFramework.NUnit: unitTestFrameworkReference = NUnitReference; corlibReference = CorlibReference; break; default: throw new ArgumentException("The provided value is not supported", nameof(framework)); } var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, language) .WithProjectCompilationOptions(projectId, compilationOptions) .AddMetadataReference(projectId, corlibReference) .AddMetadataReference(projectId, SystemCoreReference) .AddMetadataReference(projectId, CSharpSymbolsReference) .AddMetadataReference(projectId, CodeAnalysisReference) .AddMetadataReference(projectId, ThisProjectReference) .AddMetadataReference(projectId, unitTestFrameworkReference); var settings = this.GetSettings(); if (!string.IsNullOrEmpty(settings)) { var documentId = DocumentId.CreateNewId(projectId); solution = solution.AddAdditionalDocument(documentId, SettingsFileName, settings); } ParseOptions parseOptions = solution.GetProject(projectId).ParseOptions; #pragma warning disable RS1014 // Do not ignore values returned by methods on immutable objects. solution.WithProjectParseOptions(projectId, parseOptions.WithDocumentationMode(DocumentationMode.Diagnose)); #pragma warning restore RS1014 // Do not ignore values returned by methods on immutable objects. int count = 0; foreach (var source in sources) { var newFileName = fileNamePrefix + count + "." + fileExt; var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); count++; } return(solution.GetProject(projectId)); }