public void ConsoleOutAndErrorAreReplacedDuringTestExecution() { TextWriter originalOut = Console.Out; TextWriter originalError = Console.Error; try { TextWriter newOut = new StringWriter(); TextWriter newError = new StringWriter(); Console.SetOut(newOut); Console.SetError(newError); StubCommand cmd = new StubCommand(); var outputCmd = new ExceptionAndOutputCaptureCommand(cmd, null); outputCmd.Execute(null); Assert.Empty(newOut.ToString()); Assert.Empty(newError.ToString()); } finally { Console.SetOut(originalOut); Console.SetError(originalError); } }
public void RunOnce() { StubCommand command = new StubCommand(); commandExecutor.Execute(command); Assert.AreEqual(1, command.executeCount); }
public ContextItem(string text, Action <object> action, object parameter = null) { Text = text; Command = new StubCommand { CanExecute = action == null ? sender => false : (Func <object, bool>)CanAlwaysExecute, Execute = action }; Parameter = parameter; }
public DisabledContextItem(string text, object parameter = null) { Text = text; Command = new StubCommand { CanExecute = arg => false, Execute = arg => { } }; Parameter = parameter; }
public void CreatesNewInstanceWhenPassedNull() { StubCommand innerCommand = new StubCommand(); MethodInfo method = typeof(StubCommand).GetMethod("Execute"); LifetimeCommand command = new LifetimeCommand(innerCommand, Reflector.Wrap(method)); command.Execute(null); Assert.NotNull(innerCommand.TestClass); }
public void CreatesNewInstanceWhenPassedNull() { StubCommand innerCommand = new StubCommand(); MethodInfo method = typeof(StubCommand).GetMethod("Execute"); LifetimeCommand command = new LifetimeCommand(innerCommand, Reflector.Wrap(method)); command.Execute(null); Assert.NotNull(innerCommand.TestClass); }
protected override bool TryCopy(StreamElementViewModel other) { if (!(other is TrainerPokemonTeamElementViewModel that)) { return(false); } setDefaultMoves = that.setDefaultMoves; setDefaultItems = that.setDefaultItems; Run = that.Run; return(base.TryCopy(other)); }
private StubTabContent CreateClosableTab() { var tab = new StubTabContent(); var close = new StubCommand { CanExecute = arg => true }; close.Execute = arg => tab.Closed.Invoke(tab, EventArgs.Empty); tab.Close = close; return(tab); }
public void DoesNotCreateNewInstanceWhenPassedExistingInstance() { StubCommand innerCommand = new StubCommand(); MethodInfo method = typeof(StubCommand).GetMethod("Execute"); LifetimeCommand command = new LifetimeCommand(innerCommand, Reflector.Wrap(method)); object instance = new object(); command.Execute(instance); Assert.Same(instance, innerCommand.TestClass); }
public void DoesNotCreateNewInstanceWhenPassedExistingInstance() { StubCommand innerCommand = new StubCommand(); MethodInfo method = typeof(StubCommand).GetMethod("Execute"); LifetimeCommand command = new LifetimeCommand(innerCommand, Reflector.Wrap(method)); object instance = new object(); command.Execute(instance); Assert.Same(instance, innerCommand.TestClass); }
protected StubCommand StubCommand(ref StubCommand field, Action execute, Func <bool> canExecute = null) { if (field != null) { return(field); } field = new StubCommand { Execute = arg => execute(), CanExecute = arg => canExecute?.Invoke() ?? true, }; return(field); }
public ChangeHistory(Func <T, T> revertChange) { revert = revertChange; undo = new StubCommand { Execute = arg => UndoExecuted(), CanExecute = arg => undoStack.Count > 0 || (currentChange != null && currentChange.HasAnyChange), }; redo = new StubCommand { Execute = arg => RedoExecuted(), CanExecute = arg => redoStack.Count > 0, }; }
protected StubCommand StubCommand <T>(ref StubCommand field, Action <T> execute, Func <T, bool> canExecute = null) { if (field != null) { return(field); } field = new StubCommand { Execute = arg => execute(arg is T t ? t : default(T)), CanExecute = arg => canExecute?.Invoke((T)arg) ?? true, }; return(field); }
public void ConsoleOutAndErrorAndTraceIsCapturedAndPlacedInMethodResult() { string expected = "Standard Output" + Environment.NewLine + "Standard Error" + Environment.NewLine + "Trace" + Environment.NewLine; StubCommand cmd = new StubCommand(); var outputCmd = new ExceptionAndOutputCaptureCommand(cmd, null); MethodResult result = outputCmd.Execute(null); Assert.Equal(expected, result.Output); }
public void ConsoleOutAndErrorAndTraceIsCapturedAndPlacedInMethodResult() { const string expected = @"Standard Output Standard Error Trace "; StubCommand cmd = new StubCommand(); var outputCmd = new ExceptionAndOutputCaptureCommand(cmd, null); MethodResult result = outputCmd.Execute(null); Assert.Equal(expected, result.Output); }
public void SaveAllChangesWhenCurrentFileChanges() { int count = 0; var save = new StubCommand(); var tab = new StubTabContent { Save = save }; editor.Add(tab); editor.SaveAll.CanExecuteChanged += (sender, e) => count++; save.CanExecute = arg => true; save.CanExecuteChanged.Invoke(save, EventArgs.Empty); Assert.Equal(1, count); }
public void EditorNotifiesWhenUndoCanExecuteChange() { int count = 0; var undo = new StubCommand(); var tab = new StubTabContent { Undo = undo }; editor.Add(tab); editor.Undo.CanExecuteChanged += (sender, e) => count++; undo.CanExecute = arg => true; undo.CanExecuteChanged.Invoke(undo, EventArgs.Empty); Assert.Equal(1, count); }
public async Task Should_Not_Wrap_Exception_When_CommandHandler_Throws_Exception() { var command = new StubCommand(); var handler = Substitute.For <ICommandHandler <StubCommand> >(); handler .HandleAsync(Arg.Any <StubCommand>(), Arg.Any <CancellationToken>()) .Throws(new ArgumentException()); using (var container = new Container()) { container.Register(() => handler); container.Verify(); IProcessor processor = new SimpleInjectorProcessor(container); await Assert.ThrowsAsync <ArgumentException>(async() => await processor.ProcessAsync(command)); } }
public async Task Should_Process_Command() { var command = new StubCommand(); var handler = Substitute.For <ICommandHandler <StubCommand> >(); handler .HandleAsync(Arg.Any <StubCommand>(), Arg.Any <CancellationToken>()) .Returns(Task.CompletedTask); using (var container = new Container()) { container.Register(() => handler); container.Verify(); IProcessor processor = new SimpleInjectorProcessor(container); await processor.ProcessAsync(command); await handler .Received() .HandleAsync(Arg.Any <StubCommand>(), Arg.Any <CancellationToken>()); } }
public void ExecuteCommand() { // init const int currentVersion = 0; const int expectedVerstion = currentVersion + 1; var model = new StubModel { AggregateId = new AggregateId<StubModel>(), BoolVal = false, LatestVersion = currentVersion }; var expected = new StubNoChangeEvent(model.AggregateId) { Version = expectedVerstion }; var command = new StubCommand(); // run var actual = command.ExecuteOn(model); Assert.AreEqual(expected.Id.Value, actual.Id.Value); Assert.AreEqual(expected.Version, actual.Version); Assert.AreEqual(1, actual.Version); Assert.AreEqual(false, model.BoolVal); }
public void SaveCommandsPassInFileSystemAsParameter() { int count = 0; void checkIfArgIsFileSystem(object arg) { if (arg is IFileSystem) { count++; } } bool canExecuteWrapper(object arg) { checkIfArgIsFileSystem(arg); return(true); } var save = new StubCommand { CanExecute = canExecuteWrapper, Execute = checkIfArgIsFileSystem }; editor.Add(new StubTabContent { Save = save, SaveAs = save }); editor.Add(new StubTabContent { Save = save, SaveAs = save }); editor.Add(new StubTabContent { Save = save, SaveAs = save }); editor.Save.Execute(); // once editor.SaveAs.Execute(); // once editor.SaveAll.Execute(); // 6 times, since SaveAll should also check CanExecute Assert.Equal(8, count); }
public void SaveAllSavesAllDocumentsThatNeedSaving() { int executeCount = 0; var save = new StubCommand { CanExecute = arg => true, Execute = arg => executeCount++ }; var noSave = new StubCommand { CanExecute = arg => false, Execute = arg => executeCount++ }; editor.Add(new StubTabContent { Save = save }); editor.Add(new StubTabContent { Save = noSave }); editor.Add(new StubTabContent { Save = save }); editor.SaveAll.Execute(); Assert.Equal(2, executeCount); }
public void ConsoleOutAndErrorAreReplacedDuringTestExecution() { TextWriter originalOut = Console.Out; TextWriter originalError = Console.Error; try { TextWriter newOut = new StringWriter(); TextWriter newError = new StringWriter(); Console.SetOut(newOut); Console.SetError(newError); StubCommand cmd = new StubCommand(); var outputCmd = new ExceptionAndOutputCaptureCommand(cmd, null); outputCmd.Execute(null); Assert.Empty(newOut.ToString()); Assert.Empty(newError.ToString()); } finally { Console.SetOut(originalOut); Console.SetError(originalError); } }
public void ConsoleOutAndErrorAndTraceIsCapturedAndPlacedInMethodResult() { string expected = "Standard Output" + Environment.NewLine + "Standard Error" + Environment.NewLine + "Trace" + Environment.NewLine; StubCommand cmd = new StubCommand(); var outputCmd = new ExceptionAndOutputCaptureCommand(cmd, null); MethodResult result = outputCmd.Execute(null); Assert.Equal(expected, result.Output); }
public void ConsoleOutAndErrorAndTraceIsCapturedAndPlacedInMethodResult() { const string expected = @"Standard Output Standard Error Trace "; StubCommand cmd = new StubCommand(); var outputCmd = new ExceptionAndOutputCaptureCommand(cmd, null); MethodResult result = outputCmd.Execute(null); Assert.Equal(expected, result.Output); }
public static void RaiseCanExecuteChanged(this StubCommand self) => self?.CanExecuteChanged.Invoke(self, EventArgs.Empty);