This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
Removed 'goto' from 'LineFormatter' and other code refactorings #168
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
58c9371
- Removed 'goto' from 'LineFormatter', straighten case breaks
AlexanderSher 6a2b5a7
Merge PR #162
AlexanderSher 3ebb288
Fix assertion failure on strings that contain their own formatting ch…
AlexanderSher bde584f
- Fix `Position.ToString()` and `Range.ToString()` implementation
AlexanderSher 484443d
Merge remote-tracking branch 'Microsoft/master'
AlexanderSher 2aed314
- Changed `LineFormatter`:
AlexanderSher 466bb66
Remove unused braces
AlexanderSher ec60ab2
Update function description.
AlexanderSher e55339b
Fix file formatting.
AlexanderSher 6e9f719
Builder is empty, can simply add space.
AlexanderSher 0833135
Merge remote-tracking branch 'Microsoft/master'
AlexanderSher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/Analysis/Engine/Test/FluentAssertions/TextEditCollectionAssertions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Python Tools for Visual Studio | ||
| // Copyright(c) Microsoft Corporation | ||
| // All rights reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the License); you may not use | ||
| // this file except in compliance with the License. You may obtain a copy of the | ||
| // License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
| // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY | ||
| // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
| // MERCHANTABLITY OR NON-INFRINGEMENT. | ||
| // | ||
| // See the Apache Version 2.0 License for specific language governing | ||
| // permissions and limitations under the License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using FluentAssertions; | ||
| using FluentAssertions.Collections; | ||
| using FluentAssertions.Execution; | ||
| using Microsoft.Python.LanguageServer; | ||
| using static Microsoft.PythonTools.Analysis.FluentAssertions.AssertionsUtilities; | ||
|
|
||
| namespace Microsoft.PythonTools.Analysis.FluentAssertions { | ||
| [ExcludeFromCodeCoverage] | ||
| internal sealed class TextEditCollectionAssertions : SelfReferencingCollectionAssertions<TextEdit, TextEditCollectionAssertions> { | ||
| public TextEditCollectionAssertions(IEnumerable<TextEdit> references) : base(references) { } | ||
|
|
||
| protected override string Identifier => nameof(TextEdit) + "Collection"; | ||
|
|
||
|
|
||
| [CustomAssertion] | ||
| public AndConstraint<TextEditCollectionAssertions> OnlyHaveTextEdit(string expectedText, (int startLine, int startCharacter, int endLine, int endCharacter) expectedRange, string because = "", params object[] reasonArgs) | ||
| => OnlyHaveTextEdits(new[] {(expectedText, expectedRange)}, because, reasonArgs); | ||
|
|
||
| [CustomAssertion] | ||
| public AndConstraint<TextEditCollectionAssertions> OnlyHaveTextEdits(params (string expectedText, (int startLine, int startCharacter, int endLine, int endCharacter) expectedRange)[] textEdits) | ||
| => OnlyHaveTextEdits(textEdits, string.Empty); | ||
|
|
||
| [CustomAssertion] | ||
| public AndConstraint<TextEditCollectionAssertions> OnlyHaveTextEdits(IEnumerable<(string expectedText, (int startLine, int startCharacter, int endLine, int endCharacter) expectedRange)> textEdits, string because = "", params object[] reasonArgs) { | ||
| var expected = textEdits.ToArray(); | ||
| foreach (var (expectedText, (startLine, startCharacter, endLine, endCharacter)) in expected) { | ||
| HaveTextEditAt(expectedText, (startLine, startCharacter, endLine, endCharacter), because, reasonArgs); | ||
| } | ||
|
|
||
| var excess = Subject.Select(r => (r.newText, (r.range.start.line, r.range.start.character, r.range.end.line, r.range.end.character))) | ||
| .Except(expected) | ||
| .ToArray(); | ||
|
|
||
| if (excess.Length > 0) { | ||
| var excessString = string.Join(", ", excess.Select(((string text, (int, int, int, int) range) te) => $"({te.text}, {te.range.ToString()})")); | ||
| var errorMessage = expected.Length > 1 | ||
| ? $"Expected {GetSubjectName()} to have only {expected.Length} textEdits{{reason}}, but it also has textEdits: {excessString}." | ||
| : expected.Length > 0 | ||
| ? $"Expected {GetSubjectName()} to have only one reference{{reason}}, but it also has textEdits: {excessString}." | ||
| : $"Expected {GetSubjectName()} to have no textEdits{{reason}}, but it has textEdits: {excessString}."; | ||
|
|
||
| Execute.Assertion.BecauseOf(because, reasonArgs).FailWith(errorMessage); | ||
| } | ||
|
|
||
| return new AndConstraint<TextEditCollectionAssertions>(this); | ||
| } | ||
|
|
||
| [CustomAssertion] | ||
| public AndConstraint<TextEditCollectionAssertions> HaveTextEditAt(string expectedText, (int startLine, int startCharacter, int endLine, int endCharacter) expectedRange, string because = "", params object[] reasonArgs) { | ||
| var range = new Range { | ||
| start = new Position { line = expectedRange.startLine, character = expectedRange.startCharacter }, | ||
| end = new Position { line = expectedRange.endLine, character = expectedRange.endCharacter } | ||
| }; | ||
|
|
||
| var errorMessage = GetHaveTextEditErrorMessage(expectedText, range); | ||
| if (errorMessage != string.Empty) { | ||
| var assertion = Execute.Assertion.BecauseOf(because, reasonArgs); | ||
| assertion.AddNonReportable("expectedText", expectedText); | ||
| assertion.AddNonReportable("expectedRange", range); | ||
| assertion.AddNonReportable("currentTexts", GetQuotedNames(Subject.Select(te => te.newText))); | ||
| assertion.FailWith(errorMessage); | ||
| } | ||
|
|
||
| return new AndConstraint<TextEditCollectionAssertions>(this); | ||
| } | ||
|
|
||
| [CustomAssertion] | ||
| private string GetHaveTextEditErrorMessage(string expectedText, Range expectedRange) { | ||
| var candidates = Subject.Where(av => string.Equals(av.newText, expectedText, StringComparison.Ordinal)).ToArray(); | ||
| if (candidates.Length == 0) { | ||
| return "Expected {context:subject} to have text edit with newText \'{expectedText}\'{reason}, but " | ||
| + (Subject.Any() ? "it has {currentTexts}" : "it is empty"); | ||
| } | ||
|
|
||
| var candidatesWithRange = candidates.Where(c => RangeEquals(c.range, expectedRange)).ToArray(); | ||
| if (candidatesWithRange.Length > 1) { | ||
| return $"Expected {{context:subject}} to have only one text edit with newText '{{expectedText}}' and range {{expectedRange}}{{reason}}, but there are {candidatesWithRange.Length}"; | ||
| } | ||
|
|
||
| if (candidatesWithRange.Length == 0) { | ||
| return "Expected {context:subject} to have text edit with newText \'{expectedText}\' in range {expectedRange}{reason}, but " | ||
| + (candidates.Length == 1 | ||
| ? $"it has range {candidates[0].range.ToString()}" | ||
| : $"they are in ranges {string.Join(", ", candidates.Select(te => te.range.ToString()))}"); | ||
| } | ||
|
|
||
| return string.Empty; | ||
| } | ||
|
|
||
| [CustomAssertion] | ||
| private static string GetSubjectName() => CallerIdentifier.DetermineCallerIdentity() ?? "collection"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.