-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix project info reading #10623
Merged
Merged
Fix project info reading #10623
Changes from all commits
Commits
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 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 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
122 changes: 122 additions & 0 deletions
122
src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/StreamExtensionTests.cs
This file contains 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,122 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.ProjectSystem; | ||
using Microsoft.AspNetCore.Razor.Serialization; | ||
using Microsoft.AspNetCore.Razor.Utilities; | ||
using Microsoft.CodeAnalysis; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Razor.ProjectEngineHost.Test; | ||
|
||
public class StreamExtensionTests | ||
{ | ||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(int.MaxValue)] | ||
[InlineData(int.MinValue)] | ||
[InlineData(-500)] | ||
[InlineData(500)] | ||
public void SizeFunctions(int size) | ||
{ | ||
using var stream = new MemoryStream(); | ||
|
||
stream.WriteSize(size); | ||
stream.Position = 0; | ||
|
||
Assert.Equal(size, stream.ReadSize()); | ||
} | ||
|
||
public static TheoryData<string, Encoding?> StringFunctionData = new TheoryData<string, Encoding?> | ||
{ | ||
{ "", null }, | ||
{ "hello", null }, | ||
{ "", Encoding.UTF8 }, | ||
{ "hello", Encoding.UTF8 }, | ||
{ "", Encoding.ASCII }, | ||
{ "hello", Encoding.ASCII }, | ||
{ "", Encoding.UTF32 }, | ||
{ "hello", Encoding.UTF32 }, | ||
{ "", Encoding.Unicode }, | ||
{ "hello", Encoding.Unicode }, | ||
{ "", Encoding.BigEndianUnicode }, | ||
{ "hello", Encoding.BigEndianUnicode }, | ||
}; | ||
|
||
[Theory] | ||
[MemberData(nameof(StringFunctionData))] | ||
public async Task StringFunctions(string expected, Encoding? encoding) | ||
{ | ||
using var stream = new MemoryStream(); | ||
|
||
await stream.WriteStringAsync(expected, encoding, default); | ||
stream.Position = 0; | ||
|
||
var actual = await stream.ReadStringAsync(encoding, default); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Fact] | ||
public async Task SerializeProjectInfo() | ||
{ | ||
using var stream = new MemoryStream(); | ||
|
||
var configuration = new RazorConfiguration( | ||
RazorLanguageVersion.Latest, | ||
"TestConfiguration", | ||
ImmutableArray<RazorExtension>.Empty); | ||
|
||
var tagHelper = TagHelperDescriptorBuilder.Create("TypeName", "AssemblyName") | ||
.TagMatchingRuleDescriptor(rule => rule.RequireTagName("tag-name")) | ||
.Build(); | ||
|
||
var projectWorkspaceState = ProjectWorkspaceState.Create([tagHelper], CodeAnalysis.CSharp.LanguageVersion.Latest); | ||
|
||
var projectInfo = new RazorProjectInfo( | ||
new ProjectKey("TestProject"), | ||
@"C:\test\test.csproj", | ||
configuration, | ||
"TestNamespace", | ||
"Test", | ||
projectWorkspaceState, | ||
[new DocumentSnapshotHandle(@"C:\test\document.razor", @"document.razor", FileKinds.Component)]); | ||
|
||
var bytesToSerialize = projectInfo.Serialize(); | ||
|
||
await stream.WriteProjectInfoAsync(projectInfo, default); | ||
|
||
// WriteProjectInfoAsync prepends the size before writing which is 4 bytes long | ||
Assert.Equal(bytesToSerialize.Length + 4, stream.Position); | ||
|
||
var streamContents = stream.ToArray(); | ||
var expectedSize = BitConverter.GetBytes(bytesToSerialize.Length); | ||
Assert.Equal(expectedSize, streamContents.Take(4).ToArray()); | ||
|
||
Assert.Equal(bytesToSerialize, streamContents.Skip(4).ToArray()); | ||
|
||
stream.Position = 0; | ||
var deserialized = await stream.ReadProjectInfoAsync(default); | ||
|
||
Assert.Equal(projectInfo, deserialized); | ||
} | ||
|
||
[Theory] | ||
[CombinatorialData] | ||
internal void ProjectInfoActionFunctions(ProjectInfoAction infoAction) | ||
{ | ||
using var stream = new MemoryStream(); | ||
stream.WriteProjectInfoAction(infoAction); | ||
|
||
stream.Position = 0; | ||
Assert.Equal(infoAction, stream.ReadProjectInfoAction()); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do a follow up cleanup to move these to net only instead of having a bunch of ifdefs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, none of these methods need to actually run on netstandard2.0 or NetFx. You could #ifdef the whole file for NET or change the file name to
StreamExtensions.NetCore.cs
so that they only compile for NET. Then, you can get rid of the non-NET branches. That will open up lots of Span-based APIs on Stream and other types.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea that's the plan, move the whole file over.