Skip to content

Commit

Permalink
Public option tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Apr 9, 2022
1 parent a500144 commit 3ae98bb
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 161 deletions.
154 changes: 0 additions & 154 deletions src/EditorFeatures/Test2/Simplification/SimplifierAPITests.vb

This file was deleted.

16 changes: 9 additions & 7 deletions src/Workspaces/Core/Portable/Simplification/Simplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ public static async Task<Document> ReduceAsync(Document document, IEnumerable<Te
throw new ArgumentNullException(nameof(spans));
}

var options = (optionSet != null) ?
SimplifierOptions.Create(optionSet, document.Project.Solution.Workspace.Services, document.Project.Language) :
await SimplifierOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var options = await GetOptionsAsync(document, optionSet, cancellationToken).ConfigureAwait(false);

return await document.GetRequiredLanguageService<ISimplificationService>().ReduceAsync(
document, spans.ToImmutableArrayOrEmpty(), options, cancellationToken: cancellationToken).ConfigureAwait(false);
Expand All @@ -209,14 +207,18 @@ public static async Task<Document> ReduceAsync(Document document, IEnumerable<Te
internal static async Task<Document> ReduceAsync(
Document document, ImmutableArray<AbstractReducer> reducers, OptionSet? optionSet = null, CancellationToken cancellationToken = default)
{
var options = (optionSet != null) ?
SimplifierOptions.Create(optionSet, document.Project.Solution.Workspace.Services, document.Project.Language) :
await SimplifierOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

var options = await GetOptionsAsync(document, optionSet, cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return await document.GetRequiredLanguageService<ISimplificationService>()
.ReduceAsync(document, ImmutableArray.Create(root.FullSpan), options,
reducers, cancellationToken).ConfigureAwait(false);
}

internal static async Task<SimplifierOptions> GetOptionsAsync(Document document, OptionSet? optionSet, CancellationToken cancellationToken)
{
return (optionSet != null) ?
SimplifierOptions.Create(optionSet, document.Project.Solution.Workspace.Services, fallbackOptions: null, document.Project.Language) :
await SimplifierOptions.FromDocumentAsync(document, fallbackOptions: null, cancellationToken).ConfigureAwait(false);
}
}
}
157 changes: 157 additions & 0 deletions src/Workspaces/CoreTest/Simplifier/SimplifierTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Simplification;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VisualBasic.Simplification;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.UnitTests.Simplification;

[UseExportProvider]
[Trait(Traits.Feature, Traits.Features.Simplification)]
public class SimplifierTests
{
private static Document GetDocument()
{
var workspace = new AdhocWorkspace();
var project = workspace.AddProject("CSharpTest", LanguageNames.CSharp);
return workspace.AddDocument(project.Id, "CSharpFile.cs", SourceText.From("class C { }"));
}

[Fact]
public async Task ExpandAsync_BadArguments()
{
var node = SyntaxFactory.IdentifierName(SyntaxFactory.Identifier("Test"));
var semanticModel = await GetDocument().GetRequiredSemanticModelAsync(CancellationToken.None);

await Assert.ThrowsAsync<ArgumentNullException>("node", () => Simplifier.ExpandAsync<SyntaxNode>(node: null!, document: null!));
await Assert.ThrowsAsync<ArgumentNullException>("document", () => Simplifier.ExpandAsync(node: node, document: null!));
await Assert.ThrowsAsync<ArgumentNullException>("document", () => Simplifier.ExpandAsync(token: default, document: null!));
}

[Fact]
public async Task Expand_BadArguments()
{
var node = SyntaxFactory.IdentifierName(SyntaxFactory.Identifier("Test"));
var semanticModel = await GetDocument().GetRequiredSemanticModelAsync(CancellationToken.None);

Assert.Throws<ArgumentNullException>("node", () => Simplifier.Expand<SyntaxNode>(node: null!, semanticModel: null!, workspace: null!));
Assert.Throws<ArgumentNullException>("semanticModel", () => Simplifier.Expand(node, semanticModel: null!, workspace: null!));
Assert.Throws<ArgumentNullException>("workspace", () => Simplifier.Expand(node, semanticModel, workspace: null!));
Assert.Throws<ArgumentNullException>("semanticModel", () => Simplifier.Expand(token: default, semanticModel: null!, workspace: null!));
Assert.Throws<ArgumentNullException>("workspace", () => Simplifier.Expand(token: default, semanticModel, workspace: null!));
}

[Fact]
public async Task ReduceAsync_BadArguments()
{
var document = GetDocument();

await Assert.ThrowsAsync<ArgumentNullException>("document", () => Simplifier.ReduceAsync(document: null!));
await Assert.ThrowsAsync<ArgumentNullException>("document", () => Simplifier.ReduceAsync(document: null!, annotation: null!));
await Assert.ThrowsAsync<ArgumentNullException>("annotation", () => Simplifier.ReduceAsync(document, annotation: null!));
await Assert.ThrowsAsync<ArgumentNullException>("document", () => Simplifier.ReduceAsync(document: null!, span: default));
await Assert.ThrowsAsync<ArgumentNullException>("document", () => Simplifier.ReduceAsync(document: null!, spans: null!));
await Assert.ThrowsAsync<ArgumentNullException>("spans", () => Simplifier.ReduceAsync(document, spans: null!));
}

[Fact]
public async Task ReduceAsync_Options()
{
using var workspace = new AdhocWorkspace();
var csProject = workspace.AddProject("CS", LanguageNames.CSharp);
var vbProject = workspace.AddProject("VB", LanguageNames.VisualBasic);
var csDocument = workspace.AddDocument(csProject.Id, "File.cs", SourceText.From("class C { }"));
var vbDocument = workspace.AddDocument(vbProject.Id, "File.vb", SourceText.From("Class C : End Class"));

var updatedOptions = GetOptionSetWithChangedPublicOptions(workspace.CurrentSolution.Options);

// Validate that options are read from specified OptionSet:

ValidateCSharpOptions((CSharpSimplifierOptions)await Simplifier.GetOptionsAsync(csDocument, updatedOptions, CancellationToken.None));
ValidateVisualBasicOptions((VisualBasicSimplifierOptions)await Simplifier.GetOptionsAsync(vbDocument, updatedOptions, CancellationToken.None));

// Validate that options are read from solution snapshot as a fallback (we have no editorconfig file, so all options should fall back):

var solutionWithUpdatedOptions = workspace.CurrentSolution.WithOptions(updatedOptions);
var csDocumentWithUpdatedOptions = solutionWithUpdatedOptions.GetRequiredDocument(csDocument.Id);
var vbDocumentWithUpdatedOptions = solutionWithUpdatedOptions.GetRequiredDocument(vbDocument.Id);

ValidateCSharpOptions((CSharpSimplifierOptions)await Simplifier.GetOptionsAsync(csDocumentWithUpdatedOptions, optionSet: null, CancellationToken.None));
ValidateVisualBasicOptions((VisualBasicSimplifierOptions)await Simplifier.GetOptionsAsync(vbDocumentWithUpdatedOptions, optionSet: null, CancellationToken.None));

static OptionSet GetOptionSetWithChangedPublicOptions(OptionSet options)
{
// all public options and their non-default values:

var publicOptions = new (IOption, object)[]
{
(CodeStyleOptions.QualifyFieldAccess, false),
(CodeStyleOptions.QualifyPropertyAccess, false),
(CodeStyleOptions.QualifyMethodAccess, false),
(CodeStyleOptions.QualifyEventAccess, false),
(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInMemberAccess, false),
(CodeStyleOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, false),
(CSharpCodeStyleOptions.VarForBuiltInTypes, false),
(CSharpCodeStyleOptions.VarWhenTypeIsApparent, false),
(CSharpCodeStyleOptions.VarElsewhere, false),
(CSharpCodeStyleOptions.PreferSimpleDefaultExpression, false),
(CSharpCodeStyleOptions.PreferBraces, PreferBracesPreference.WhenMultiline),
};

var updatedOptions = options;
foreach (var (option, newValue) in publicOptions)
{
var languages = (option is IPerLanguageOption) ? new[] { LanguageNames.CSharp, LanguageNames.VisualBasic } : new string?[] { null };

foreach (var language in languages)
{
var key = new OptionKey(option, language);
var current = (ICodeStyleOption)options.GetOption(key)!;
updatedOptions = updatedOptions.WithChangedOption(key, current.WithValue(newValue));
}
}

return updatedOptions;
}

static void ValidateCommonOptions(SimplifierOptions simplifierOptions)
{
Assert.False(simplifierOptions.QualifyFieldAccess.Value);
Assert.False(simplifierOptions.QualifyPropertyAccess.Value);
Assert.False(simplifierOptions.QualifyMethodAccess.Value);
Assert.False(simplifierOptions.QualifyEventAccess.Value);
Assert.False(simplifierOptions.PreferPredefinedTypeKeywordInMemberAccess.Value);
Assert.False(simplifierOptions.PreferPredefinedTypeKeywordInDeclaration.Value);
}

static void ValidateCSharpOptions(CSharpSimplifierOptions simplifierOptions)
{
ValidateCommonOptions(simplifierOptions);

Assert.False(simplifierOptions.VarForBuiltInTypes.Value);
Assert.False(simplifierOptions.VarWhenTypeIsApparent.Value);
Assert.False(simplifierOptions.VarElsewhere.Value);
Assert.False(simplifierOptions.PreferSimpleDefaultExpression.Value);
Assert.Equal(PreferBracesPreference.WhenMultiline, simplifierOptions.PreferBraces.Value);
}

static void ValidateVisualBasicOptions(VisualBasicSimplifierOptions simplifierOptions)
{
ValidateCommonOptions(simplifierOptions);
}
}
}

0 comments on commit 3ae98bb

Please sign in to comment.