|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Linq; |
| 5 | +using Microsoft.AspNetCore.Razor.Language; |
| 6 | +using Microsoft.AspNetCore.Razor.Serialization.Json; |
| 7 | +using Microsoft.AspNetCore.Razor.Test.Common; |
| 8 | +using Microsoft.CodeAnalysis.Razor.ProjectSystem; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +namespace Microsoft.CodeAnalysis.Razor.Serialization; |
| 14 | + |
| 15 | +public class JsonSerializationTest(ITestOutputHelper testOutput) : ToolingTestBase(testOutput) |
| 16 | +{ |
| 17 | + private readonly RazorConfiguration _configuration = new(RazorLanguageVersion.Experimental, ConfigurationName: "Custom", [new("TestExtension")]); |
| 18 | + |
| 19 | + private readonly ProjectWorkspaceState _projectWorkspaceState = ProjectWorkspaceState.Create( |
| 20 | + tagHelpers: [TagHelperDescriptorBuilder.Create("Test", "TestAssembly").Build()]); |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void RazorProjectInfo_InvalidVersionThrows() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + var projectInfo = new RazorProjectInfo( |
| 27 | + new ProjectKey("/path/to/obj/"), |
| 28 | + "/path/to/project.csproj", |
| 29 | + _configuration, |
| 30 | + rootNamespace: "TestProject", |
| 31 | + displayName: "project", |
| 32 | + _projectWorkspaceState, |
| 33 | + documents: []); |
| 34 | + |
| 35 | + var jsonText = JsonDataConvert.Serialize(projectInfo); |
| 36 | + Assert.NotNull(jsonText); |
| 37 | + |
| 38 | + var serializedJObject = JObject.Parse(jsonText); |
| 39 | + serializedJObject[WellKnownPropertyNames.Version] = -1; |
| 40 | + |
| 41 | + var updatedJsonText = serializedJObject.ToString(); |
| 42 | + Assert.NotNull(updatedJsonText); |
| 43 | + |
| 44 | + // Act |
| 45 | + RazorProjectInfo? deserializedProjectInfo = null; |
| 46 | + Assert.Throws<RazorProjectInfoSerializationException>(() => |
| 47 | + { |
| 48 | + deserializedProjectInfo = JsonDataConvert.DeserializeProjectInfo(updatedJsonText); |
| 49 | + }); |
| 50 | + |
| 51 | + // Assert |
| 52 | + Assert.Null(deserializedProjectInfo); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void RazorProjectInfo_MissingVersionThrows() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + var projectInfo = new RazorProjectInfo( |
| 60 | + new ProjectKey("/path/to/obj/"), |
| 61 | + "/path/to/project.csproj", |
| 62 | + _configuration, |
| 63 | + rootNamespace: "TestProject", |
| 64 | + displayName: "project", |
| 65 | + _projectWorkspaceState, |
| 66 | + documents: []); |
| 67 | + |
| 68 | + var jsonText = JsonDataConvert.Serialize(projectInfo); |
| 69 | + Assert.NotNull(jsonText); |
| 70 | + |
| 71 | + var serializedJObject = JObject.Parse(jsonText); |
| 72 | + serializedJObject.Remove(WellKnownPropertyNames.Version); |
| 73 | + |
| 74 | + var updatedJsonText = serializedJObject.ToString(); |
| 75 | + Assert.NotNull(updatedJsonText); |
| 76 | + |
| 77 | + // Act |
| 78 | + RazorProjectInfo? deserializedProjectInfo = null; |
| 79 | + Assert.Throws<RazorProjectInfoSerializationException>(() => |
| 80 | + { |
| 81 | + deserializedProjectInfo = JsonDataConvert.DeserializeProjectInfo(updatedJsonText); |
| 82 | + }); |
| 83 | + |
| 84 | + // Assert |
| 85 | + Assert.Null(deserializedProjectInfo); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void RazorProjectInfo_CanRoundTrip() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + var legacyDocument = new DocumentSnapshotHandle("/path/to/file.cshtml", "file.cshtml", FileKinds.Legacy); |
| 93 | + var componentDocument = new DocumentSnapshotHandle("/path/to/otherfile.razor", "otherfile.razor", FileKinds.Component); |
| 94 | + var projectInfo = new RazorProjectInfo( |
| 95 | + new ProjectKey("/path/to/obj/"), |
| 96 | + "/path/to/project.csproj", |
| 97 | + _configuration, |
| 98 | + rootNamespace: "TestProject", |
| 99 | + displayName: "project", |
| 100 | + _projectWorkspaceState, |
| 101 | + documents: [legacyDocument, componentDocument]); |
| 102 | + |
| 103 | + var jsonText = JsonDataConvert.Serialize(projectInfo); |
| 104 | + Assert.NotNull(jsonText); |
| 105 | + |
| 106 | + // Act |
| 107 | + var deserializedProjectInfo = JsonDataConvert.DeserializeProjectInfo(jsonText); |
| 108 | + Assert.NotNull(deserializedProjectInfo); |
| 109 | + |
| 110 | + // Assert |
| 111 | + Assert.Equal(projectInfo.FilePath, deserializedProjectInfo.FilePath); |
| 112 | + Assert.Equal(projectInfo.Configuration, deserializedProjectInfo.Configuration); |
| 113 | + Assert.Equal(projectInfo.RootNamespace, deserializedProjectInfo.RootNamespace); |
| 114 | + Assert.Equal(projectInfo.ProjectWorkspaceState, deserializedProjectInfo.ProjectWorkspaceState); |
| 115 | + Assert.Collection(projectInfo.Documents.OrderBy(doc => doc.FilePath), |
| 116 | + document => |
| 117 | + { |
| 118 | + Assert.Equal(legacyDocument.FilePath, document.FilePath); |
| 119 | + Assert.Equal(legacyDocument.TargetPath, document.TargetPath); |
| 120 | + Assert.Equal(legacyDocument.FileKind, document.FileKind); |
| 121 | + }, |
| 122 | + document => |
| 123 | + { |
| 124 | + Assert.Equal(componentDocument.FilePath, document.FilePath); |
| 125 | + Assert.Equal(componentDocument.TargetPath, document.TargetPath); |
| 126 | + Assert.Equal(componentDocument.FileKind, document.FileKind); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void RazorConfiguration_CanRoundTrip() |
| 132 | + { |
| 133 | + // Arrange |
| 134 | + var jsonText = JsonDataConvert.Serialize(_configuration); |
| 135 | + Assert.NotNull(jsonText); |
| 136 | + |
| 137 | + // Act |
| 138 | + var deserializedConfiguration = JsonDataConvert.DeserializeConfiguration(jsonText); |
| 139 | + |
| 140 | + // Assert |
| 141 | + Assert.Equal(_configuration, deserializedConfiguration); |
| 142 | + } |
| 143 | +} |
0 commit comments