/// <summary> /// Validates the current changes in the TextBox. Does nothing is there are no changes. /// </summary> public void Validate() { if (IsReadOnly || !HasChangesToValidate || validating) { return; } var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); OnValidating(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } if (!IsTextCompatibleWithValueBinding(Text)) { var textBindingFailedArgs = new RoutedEventArgs(TextToSourceValueConversionFailedEvent); RaiseEvent(textBindingFailedArgs); // We allow this to continue through since it'll revert itself through later code. } validating = true; var coercedText = CoerceTextForValidation(Text); SetCurrentValue(TextProperty, coercedText); BindingExpression expression = GetBindingExpression(TextProperty); try { expression?.UpdateSource(); } catch (TargetInvocationException ex) when(ex.InnerException is InvalidCastException) { var textBindingFailedArgs = new RoutedEventArgs(TextToSourceValueConversionFailedEvent); RaiseEvent(textBindingFailedArgs); } ClearUndoStack(); var validatedArgs = new ValidationRoutedEventArgs <string>(ValidatedEvent, coercedText); OnValidated(); RaiseEvent(validatedArgs); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) { ValidateCommand.Execute(ValidateCommandParameter); } validating = false; HasChangesToValidate = false; }
public void TestProcessCommand_OnBootstrapperError() { // Preconditions Debug.Assert(managerMock != null); Debug.Assert(outputMock != null); Debug.Assert(bootstrapperMock != null); /* GIVEN */ bootstrapperMock .Setup(bootstrapper => bootstrapper.ComposeImports(It.IsAny <object>()))? .Throws(new InvalidOperationException()); var command = new ValidateCommand(managerMock.Object, outputMock.Object, bootstrapperMock.Object); var options = new ValidateOptions { IsVerbose = false, ConfigPath = Assembly.GetExecutingAssembly().Location }; /* WHEN */ var returnCode = command.Execute(options); /* THEN */ // We test if the command failed and returned code -1. Assert.AreEqual(failCode, returnCode); bootstrapperMock.Verify(bootstrapper => bootstrapper.ComposeImports(managerMock.Object), Times.Once); managerMock.Verify(manager => manager.LoadConfiguration(It.IsAny <FilePath>()), Times.Never); outputMock.Verify(output => output.PrintError(It.IsAny <InvalidOperationException>()), Times.Once); }
private void Validate() { var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); AssociatedObject.RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } BindingExpression expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty); if (expression != null) { expression.UpdateSource(); } ClearUndoStack(); AssociatedObject.RaiseEvent(new RoutedEventArgs(ValidatedEvent)); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) { ValidateCommand.Execute(ValidateCommandParameter); } }
public void TestProcessCommand_Successful() { // Preconditions Debug.Assert(managerMock != null); Debug.Assert(outputMock != null); Debug.Assert(bootstrapperMock != null); /* GIVEN */ var command = new ValidateCommand(managerMock.Object, outputMock.Object, bootstrapperMock.Object); var options = new ValidateOptions { IsVerbose = false, ConfigPath = Assembly.GetExecutingAssembly().Location }; /* WHEN */ var returnCode = command.Execute(options); /* THEN */ // We test if the command was successful and returned code 0. Assert.AreEqual(successCode, returnCode); bootstrapperMock.Verify(bootstrapper => bootstrapper.ComposeImports(managerMock.Object), Times.Once); managerMock.Verify(manager => manager.LoadConfiguration(It.IsAny <FilePath>()), Times.Once); }
/// <summary> /// Validates the current changes in the TextBox. /// </summary> public void Validate() { var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); OnValidating(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) return; RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) return; validating = true; var coercedText = CoerceTextForValidation(Text); SetCurrentValue(TextProperty, coercedText); BindingExpression expression = GetBindingExpression(TextProperty); if (expression != null) expression.UpdateSource(); ClearUndoStack(); var validatedArgs = new ValidationRoutedEventArgs<string>(ValidatedEvent, coercedText); OnValidated(); RaiseEvent(validatedArgs); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) ValidateCommand.Execute(ValidateCommandParameter); validating = false; }
public void Execute_WithEmptyConfig_WillPrintEmptyConfig() { // Arrange ValidateCommand command = new ValidateCommand(false, "testpath"); testConfigHandler.Setup(m => m.LoadConfig("testpath")).Returns(testConfig.Object); // Act command.Execute(testConsole, testConfigHandler.Object, testFileSystem, testPathFormatter); // Assert Assert.IsTrue(testConsole.GetHistory().Contains("empty", System.StringComparison.OrdinalIgnoreCase)); }
public void Execute_WithInvalidDirectoryLink_WillPrintInvalidConfig() { // Arrange ValidateCommand command = new ValidateCommand(false, "testpath"); testLinks.Add(testLinkElements[2]); testConfigHandler.Setup(m => m.LoadConfig("testpath")).Returns(testConfig.Object); // Act command.Execute(testConsole, testConfigHandler.Object, testFileSystem, testPathFormatter); // Assert Assert.IsTrue(testConsole.GetHistory().Contains(testLinkElements[2].ToString(), System.StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Validates the current changes in the TextBox. Does nothing is there are no changes. /// </summary> public void Validate() { if (IsReadOnly || !HasChangesToValidate) { return; } var cancelRoutedEventArgs = new CancelRoutedEventArgs(ValidatingEvent); OnValidating(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } RaiseEvent(cancelRoutedEventArgs); if (cancelRoutedEventArgs.Cancel) { return; } validating = true; var coercedText = CoerceTextForValidation(Text); SetCurrentValue(TextProperty, coercedText); BindingExpression expression = GetBindingExpression(TextProperty); expression?.UpdateSource(); ClearUndoStack(); var validatedArgs = new ValidationRoutedEventArgs <string>(ValidatedEvent, coercedText); OnValidated(); RaiseEvent(validatedArgs); if (ValidateCommand != null && ValidateCommand.CanExecute(ValidateCommandParameter)) { ValidateCommand.Execute(ValidateCommandParameter); } validating = false; HasChangesToValidate = false; }
public void TestProcessCommand_NullOptions() { // Preconditions Debug.Assert(managerMock != null); Debug.Assert(outputMock != null); Debug.Assert(bootstrapperMock != null); /* GIVEN */ var command = new ValidateCommand(managerMock.Object, outputMock.Object, bootstrapperMock.Object); /* WHEN */ var returnCode = command.Execute(null); /* THEN */ // We test if the command was successful and returned code 0. Assert.AreEqual(-1, returnCode); managerMock.Verify(manager => manager.LoadConfiguration(It.IsAny <FilePath>()), Times.Never); }
public void TestProcessCommand_IsVerbosePropagation() { // Preconditions Debug.Assert(managerMock != null); Debug.Assert(outputMock != null); Debug.Assert(bootstrapperMock != null); /* GIVEN */ outputMock.SetupSet(output => output.IsVerbose = true); var command = new ValidateCommand(managerMock.Object, outputMock.Object, bootstrapperMock.Object); var options = new ValidateOptions { IsVerbose = true, ConfigPath = "" }; /* WHEN */ command.Execute(options); /* THEN */ outputMock.VerifySet(output => output.IsVerbose = true); }