提交 04534a7a 编写于 作者: S Sam Harwell

Avoid showing dialogs in tests

Fixes #43709
上级 7fd07d52
......@@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Notification;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
......@@ -70,6 +71,26 @@ public TestWorkspace(ExportProvider exportProvider, string? workspaceKind = null
this.CanApplyChangeDocument = true;
this.IgnoreUnchangeableDocumentsWhenApplyingChanges = ignoreUnchangeableDocumentsWhenApplyingChanges;
if (Services.GetService<INotificationService>() is INotificationServiceCallback callback)
{
// Avoid showing dialogs in tests by default
callback.NotificationCallback = (message, title, severity) =>
{
var severityText = severity switch
{
NotificationSeverity.Information => "💡",
NotificationSeverity.Warning => "⚠",
_ => "❌"
};
var fullMessage = string.IsNullOrEmpty(title)
? message
: $"{title}:{Environment.NewLine}{Environment.NewLine}{message}";
throw new InvalidOperationException($"{severityText} {fullMessage}");
};
}
_backgroundCompiler = new BackgroundCompiler(this);
_backgroundParser = new BackgroundParser(this);
_backgroundParser.Start();
......
......@@ -4,6 +4,7 @@
#nullable enable
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Windows;
using Microsoft.CodeAnalysis;
......@@ -121,35 +122,47 @@ public string VerbatimTypeName
}
}
internal bool TrySubmit()
internal bool CanSubmit([NotNullWhen(false)] out string? message)
{
if (string.IsNullOrEmpty(VerbatimTypeName) || string.IsNullOrEmpty(ParameterName))
{
SendFailureNotification(ServicesVSResources.A_type_and_name_must_be_provided);
message = ServicesVSResources.A_type_and_name_must_be_provided;
return false;
}
if (!IsParameterTypeSyntacticallyValid(VerbatimTypeName))
{
SendFailureNotification(ServicesVSResources.Parameter_type_contains_invalid_characters);
message = ServicesVSResources.Parameter_type_contains_invalid_characters;
return false;
}
if (!IsParameterNameValid(ParameterName))
{
SendFailureNotification(ServicesVSResources.Parameter_name_contains_invalid_characters);
message = ServicesVSResources.Parameter_name_contains_invalid_characters;
return false;
}
if (IsCallsiteRegularValue && CallSiteValue.IsNullOrWhiteSpace())
{
SendFailureNotification(ServicesVSResources.Enter_a_call_site_value_or_choose_a_different_value_injection_kind);
message = ServicesVSResources.Enter_a_call_site_value_or_choose_a_different_value_injection_kind;
return false;
}
if (IsOptional && DefaultValue.IsNullOrWhiteSpace())
{
SendFailureNotification(ServicesVSResources.Optional_parameters_must_provide_a_default_value);
message = ServicesVSResources.Optional_parameters_must_provide_a_default_value;
return false;
}
message = null;
return true;
}
internal bool TrySubmit()
{
if (!CanSubmit(out var message))
{
SendFailureNotification(message);
return false;
}
......
......@@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
......@@ -493,7 +494,7 @@ private void Move(List<ParameterViewModel> list, int index, int delta)
NotifyPropertyChanged(nameof(SignaturePreviewAutomationText));
}
internal bool TrySubmit()
internal bool CanSubmit([NotNullWhen(false)] out string? message)
{
var canSubmit = AllParameters.Any(p => p.IsRemoved) ||
AllParameters.Any(p => p is AddedParameterViewModel) ||
......@@ -502,7 +503,19 @@ internal bool TrySubmit()
if (!canSubmit)
{
_notificationService.SendNotification(ServicesVSResources.You_must_change_the_signature, severity: NotificationSeverity.Information);
message = ServicesVSResources.You_must_change_the_signature;
return false;
}
message = null;
return true;
}
internal bool TrySubmit()
{
if (!CanSubmit(out var message))
{
_notificationService.SendNotification(message, severity: NotificationSeverity.Information);
return false;
}
......
......@@ -31,14 +31,18 @@ class MyClass
VerifyOpeningState(viewModel)
viewModel.VerbatimTypeName = "int"
Assert.False(viewModel.TrySubmit())
Dim message As String = Nothing
Assert.False(viewModel.CanSubmit(message))
Assert.Equal(ServicesVSResources.A_type_and_name_must_be_provided, message)
viewModel.VerbatimTypeName = ""
viewModel.ParameterName = "x"
Assert.False(viewModel.TrySubmit())
Assert.False(viewModel.CanSubmit(message))
Assert.Equal(ServicesVSResources.A_type_and_name_must_be_provided, message)
viewModel.VerbatimTypeName = "int"
Assert.False(viewModel.TrySubmit())
Assert.False(viewModel.CanSubmit(message))
Assert.Equal(ServicesVSResources.Enter_a_call_site_value_or_choose_a_different_value_injection_kind, message)
viewModel.CallSiteValue = "7"
Assert.True(viewModel.TrySubmit())
......@@ -279,7 +283,9 @@ class MyClass
Assert.False(viewModel.IsCallsiteTodo)
Assert.False(viewModel.IsCallsiteOmitted)
Assert.False(viewModel.TrySubmit)
Dim message As String = Nothing
Assert.False(viewModel.CanSubmit(message))
Assert.Equal(ServicesVSResources.A_type_and_name_must_be_provided, message)
End Sub
Private Function GetViewModelTestStateAsync(
......
......@@ -69,7 +69,9 @@ class MyClass
Assert.True(viewModel.TrySubmit)
viewModel.MoveUp()
Assert.False(viewModel.TrySubmit)
Dim message As String = Nothing
Assert.False(viewModel.CanSubmit(message))
Assert.Equal(ServicesVSResources.You_must_change_the_signature, message)
VerifyAlteredState(
viewModelTestState,
......@@ -357,7 +359,12 @@ class Goo
End If
If canCommit IsNot Nothing Then
Assert.Equal(canCommit, viewModel.TrySubmit())
Dim message As String = Nothing
Assert.Equal(canCommit, viewModel.CanSubmit(message))
If canCommit.Value Then
Assert.True(viewModel.TrySubmit())
End If
End If
If canMoveUp IsNot Nothing Then
......@@ -396,7 +403,9 @@ class Goo
Private Sub VerifyOpeningState(viewModel As ChangeSignatureDialogViewModel, openingSignatureDisplay As String)
Assert.Equal(openingSignatureDisplay, viewModel.TEST_GetSignatureDisplayText())
Assert.False(viewModel.TrySubmit)
Dim message As String = Nothing
Assert.False(viewModel.CanSubmit(message))
Assert.Equal(ServicesVSResources.You_must_change_the_signature, message)
Assert.False(viewModel.CanMoveUp)
End Sub
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册