using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using CTA.WebForms.FileConverters; using CTA.WebForms.FileInformationModel; using CTA.WebForms.Helpers.TagConversion; using CTA.WebForms.Metrics; using CTA.WebForms.Services; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using NUnit.Framework; namespace CTA.WebForms.Tests.FileConverters { [TestFixture] public class ViewFileConverterTests : WebFormsTestBase { private SemanticModel _testSemanticModel; private ClassDeclarationSyntax _testClassDeclaration; [OneTimeSetUp] public void OneTimeSetup() { var testTree = CSharpSyntaxTree.ParseText("namespace Test { public class TestClass { } }"); var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); var testCompilation = CSharpCompilation.Create( "Test", syntaxTrees: new[] { testTree }, references: new[] { mscorlib }); _testSemanticModel = testCompilation.GetSemanticModel(testTree); _testClassDeclaration = testTree.GetRoot().DescendantNodes().OfType().FirstOrDefault(); } [Test] public async Task HyperLinkControlConverter_Returns_Href_Node() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestHyperLinkControlFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestHyperLinkControlFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "HyperLinkControlOnly.razor"); string relativePath = Path.Combine("Pages", Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath)); string expectedContents = @"
"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(relativePath, fi.RelativePath); } [Test] public async Task ButtonControlConverter_Returns_Button_Node() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestButtonControlFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestButtonControlFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "ButtonControlOnly.razor"); string relativePath = Path.Combine("Pages", Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath)); string expectedContents = @"
[ Cancel ]
"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(relativePath, fi.RelativePath); } [Test] public async Task LabelControlConverter_Returns_DynamicText() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestLabelControlFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestLabelControlFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "LabelControlOnly.razor"); string relativePath = Path.Combine("Pages", Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath)); string expectedContents = @"
Name
Description
Brand
Type
Price
Picture name
Stock
Restock
Max stock
"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(relativePath, fi.RelativePath); } [Test] public async Task ListViewControlConverter_Returns_ListView_Node() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestListViewControlFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestListViewControlFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "ListViewControlOnly.razor"); string relativePath = Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath); relativePath = Path.Combine("Pages", relativePath); string expectedContents = @"
No data was returned.
@itemPlaceHolder
Name

@(Item.Name)

"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(relativePath, fi.RelativePath); } [Test] public async Task TestViewFileConverter_Returns_GridView_Node() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestGridViewControlFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestGridViewControlFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "GridViewControlOnly.razor"); string relativePath = Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath); relativePath = Path.Combine("Pages", relativePath); string expectedContents = @"
"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(fi.RelativePath, relativePath); } [Test] public async Task TestViewFileConverter_Returns_ContentPlaceHolderNode_As_Body_Directive() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestContentPlaceHolderControlFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestContentPlaceHolderControlFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "ContentPlaceHolderControlOnly.razor"); string relativePath = Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath); relativePath = Path.Combine("Pages", relativePath); string expectedContents = @"
@Body
"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(relativePath, fi.RelativePath); } [Test] public async Task TestViewFileConverter_Returns_ContentNode_As_Div() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestContentControlFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestContentControlFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "ContentControlOnly.razor"); string relativePath = Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath); relativePath = Path.Combine("Pages", relativePath); string expectedContents = @"

Some random stuff

"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(relativePath, fi.RelativePath); } [Test] public async Task TestViewFileConverter_Converts_Directives() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestDirectiveFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestDirectiveFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); string newPath = Path.Combine(FileConverterSetupFixture.TestFilesDirectoryPath, "DirectiveOnly.razor"); string relativePath = Path.GetRelativePath(FileConverterSetupFixture.TestProjectPath, newPath); relativePath = Path.Combine("Pages", relativePath); string expectedContents = @"@using CTA.WebForms.Tests.CustomControls @using eShopOnBlazor @page ""/TestingArea/TestFiles/DirectiveOnly"" @layout Site @inherits eShopLegacyWebForms._Default @namespace Replace_this_with_code_behind_namespace @inherits LayoutComponentBase
@* The following tag is not supported: *@@* *@ @* The following tag is not supported: *@@* *@
"; Assert.AreEqual(expectedContents, fileContents); Assert.AreEqual(relativePath, fi.RelativePath); } // This is a full view layer migration test; any features added later may cause this test to fail // and thus expectedContents might need to be updated in order for the test to be accurate [Test] public async Task TestViewFileConverter_DefaultAspx() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestViewFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestViewFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); var expectedContents = @"@page ""/TestingArea/TestFiles/SampleViewFile"" @layout Site @inherits eShopLegacyWebForms._Default @using CTA.WebForms.Tests @using CTA.WebForms.Tests

Create New

No data was returned.
@itemPlaceHolder
Name

@(Item.Name)

Edit
@* The following tag is not supported: *@@* *@ @* The following tag is not supported: *@@* *@
"; Assert.AreEqual(expectedContents, fileContents); } [Test] public async Task TestViewFileConverter_SiteMaster() { var cbLinkerService = new CodeBehindReferenceLinkerService(); FileConverter fc = new ViewFileConverter( FileConverterSetupFixture.TestProjectPath, FileConverterSetupFixture.TestSiteMasterFilePath, new ViewImportService(), cbLinkerService, new TaskManagerService(), new TagConfigParser(Rules.Config.Constants.TagConfigsExtractedPath), new WebFormMetricContext()); cbLinkerService.RegisterClassDeclaration( FileConverterSetupFixture.TestSiteMasterFilePath, _testSemanticModel, _testClassDeclaration); IEnumerable fileList = await fc.MigrateFileAsync(); FileInformation fi = fileList.Single(); byte[] bytes = fi.FileBytes; var fileContents = Encoding.UTF8.GetString(bytes); } [Test] public void ConvertEmbeddedCode_Converts_DataBinding() { string htmlString = @"

<%#Item.MaxStockThreshold%>

Edit
"; string contents = ViewFileConverter.ConvertEmbeddedCode(htmlString); string expectedContents = @"

@(Item.MaxStockThreshold)

Edit
"; Assert.AreEqual(expectedContents, contents); } [Test] public void ConvertEmbeddedCode_Converts_SingExpr() { string htmlString = @"
"; string contents = ViewFileConverter.ConvertEmbeddedCode(htmlString); string expectedContents = @"
"; Assert.AreEqual(expectedContents, contents); } } }