예제 #1
0
        public bool Execute()
        {
            var paths   = new Paths();
            var success = true;

            try
            {
                Directory.CreateDirectory(paths.TestDirPath);

                var currentCode = string.Empty;
                if (File.Exists(paths.ComponentTestsFilePath))
                {
                    currentCode = File.ReadAllText(paths.ComponentTestsFilePath);
                }

                var cb = new CodeBuilder();

                cb.AddHeader();
                cb.AddLine("using MudBlazor.Docs.Examples;");
                cb.AddLine("using MudBlazor.Docs.Wireframes;");
                cb.AddLine("using NUnit.Framework;");
                cb.AddLine();

                cb.AddLine("namespace MudBlazor.UnitTests.Components");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("// These tests just check if all the examples from the doc page render without errors");
                cb.AddLine("public partial class ExampleDocsTests");
                cb.AddLine("{");
                cb.IndentLevel++;

                foreach (var entry in Directory.EnumerateFiles(paths.DocsDirPath, "*.razor", SearchOption.AllDirectories)
                         .OrderBy(e => e.Replace("\\", "/"), StringComparer.Ordinal))
                {
                    if (entry.EndsWith("Code.razor"))
                    {
                        continue;
                    }
                    var filename      = Path.GetFileName(entry);
                    var componentName = Path.GetFileNameWithoutExtension(filename);
                    if (!filename.Contains(Paths.ExampleDiscriminator))
                    {
                        continue;
                    }
                    // skip over table/data grid virutalization since it takes too long.
                    if (filename == "TableVirtualizationExample.razor" || filename == "DataGridVirtualizationExample.razor")
                    {
                        continue;
                    }
                    cb.AddLine("[Test]");
                    cb.AddLine($"public void {componentName}_Test()");
                    cb.AddLine("{");
                    cb.IndentLevel++;
                    cb.AddLine($"ctx.RenderComponent<{componentName}>();");
                    cb.IndentLevel--;
                    cb.AddLine("}");
                }

                cb.IndentLevel--;
                cb.AddLine("}");
                cb.IndentLevel--;
                cb.AddLine("}");

                if (currentCode != cb.ToString())
                {
                    File.WriteAllText(paths.ComponentTestsFilePath, cb.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error generating {paths.ComponentTestsFilePath} : {e.Message}");
                success = false;
            }

            return(success);
        }
예제 #2
0
        public bool Execute()
        {
            var paths   = new Paths();
            var success = true;

            try
            {
                Directory.CreateDirectory(paths.TestDirPath);

                var currentCode = string.Empty;
                if (File.Exists(paths.ApiPageTestsFilePath))
                {
                    currentCode = File.ReadAllText(paths.ApiPageTestsFilePath);
                }

                var cb = new CodeBuilder();

                cb.AddHeader();
                cb.AddLine("using MudBlazor.Charts;");
                cb.AddLine("using MudBlazor.Docs.Components;");
                cb.AddLine("using MudBlazor.Internal;");
                cb.AddLine("using NUnit.Framework;");
                cb.AddLine("using ComponentParameter = Bunit.ComponentParameter;");
                cb.AddLine();

                cb.AddLine("namespace MudBlazor.UnitTests.Components");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("// These tests just check all the API pages to see if they throw any exceptions");
                cb.AddLine("public partial class ApiDocsTests");
                cb.AddLine("{");
                cb.IndentLevel++;
                var mudBlazorComponents = typeof(MudAlert).Assembly.GetTypes().OrderBy(t => t.FullName).Where(t => t.IsSubclassOf(typeof(ComponentBase)));
                foreach (var type in mudBlazorComponents)
                {
                    if (type.IsAbstract)
                    {
                        continue;
                    }
                    if (type.Name.Contains("Base"))
                    {
                        continue;
                    }
                    if (type.Namespace.Contains("InternalComponents"))
                    {
                        continue;
                    }
                    cb.AddLine("[Test]");
                    cb.AddLine($"public void {SafeTypeName(type, removeT: true)}_API_Test()");
                    cb.AddLine("{");
                    cb.IndentLevel++;
                    cb.AddLine(@$ "ctx.RenderComponent<DocsApi>(ComponentParameter.CreateParameter(" "Type" ", typeof({SafeTypeName(type)})));");
                    cb.IndentLevel--;
                    cb.AddLine("}");
                }

                cb.IndentLevel--;
                cb.AddLine("}");
                cb.IndentLevel--;
                cb.AddLine("}");

                if (currentCode != cb.ToString())
                {
                    File.WriteAllText(paths.ApiPageTestsFilePath, cb.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error generating {paths.ApiPageTestsFilePath} : {e.Message}");
                success = false;
            }

            return(success);
        }
예제 #3
0
        public bool Execute()
        {
            var  paths   = new Paths();
            bool success = true;

            try
            {
                Directory.CreateDirectory(paths.TestDirPath);

                string currentCode = string.Empty;
                if (File.Exists(paths.ApiPageTestsFilePath))
                {
                    currentCode = File.ReadAllText(paths.ApiPageTestsFilePath);
                }

                var cb = new CodeBuilder();

                cb.AddHeader();
                cb.AddLine("using Bunit.TestDoubles.JSInterop;");
                cb.AddLine("using Microsoft.AspNetCore.Components;");
                cb.AddLine("using Microsoft.Extensions.DependencyInjection;");
                cb.AddLine("using NUnit.Framework;");
                cb.AddLine("using MudBlazor.UnitTests.Mocks;");
                cb.AddLine("using MudBlazor.Docs.Examples;");
                cb.AddLine("using MudBlazor.Services;");
                cb.AddLine("using MudBlazor.Docs.Components;");
                cb.AddLine("using Bunit.Rendering;");
                cb.AddLine("using System;");
                cb.AddLine("using Toolbelt.Blazor.HeadElement;");
                cb.AddLine("using MudBlazor.UnitTests;");
                cb.AddLine("using MudBlazor.Charts;");
                cb.AddLine("using Bunit;");
                cb.AddLine();
                cb.AddLine("#if NET5_0");
                cb.AddLine("using ComponentParameter = Bunit.ComponentParameter;");
                cb.AddLine("#endif");
                cb.AddLine();
                cb.AddLine("namespace MudBlazor.UnitTests.Components");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("// These tests just check all the API pages to see if they throw any exceptions");
                cb.AddLine("[TestFixture]");
                cb.AddLine("public class _AllApiPages");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("private Bunit.TestContext ctx;");
                cb.AddLine();
                cb.AddLine("[SetUp]");
                cb.AddLine("public void Setup()");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("ctx = new Bunit.TestContext();");
                cb.AddLine("ctx.Services.AddMockJSRuntime();");
                cb.AddLine("ctx.Services.AddSingleton<NavigationManager>(new MockNavigationManager());");
                cb.AddLine("ctx.Services.AddSingleton<IDialogService>(new DialogService());");
                cb.AddLine("ctx.Services.AddSingleton<ISnackbar>(new MockSnackbar());");
                cb.AddLine("ctx.Services.AddSingleton<IResizeListenerService>(new MockResizeListenerService());");
                cb.AddLine("ctx.Services.AddSingleton<IHeadElementHelper>(new MockHeadElementHelper());");
                cb.IndentLevel--;
                cb.AddLine("}");
                cb.AddLine();
                cb.AddLine("[TearDown]");
                cb.AddLine("public void TearDown() => ctx.Dispose();");
                cb.AddLine();
                var mudBlazorComponents = typeof(MudAlert).Assembly.GetTypes().OrderBy(t => t.FullName).Where(t => t.IsSubclassOf(typeof(ComponentBase)));
                foreach (var type in mudBlazorComponents)
                {
                    if (type.IsAbstract)
                    {
                        continue;
                    }
                    if (type.Name.Contains("Base"))
                    {
                        continue;
                    }
                    if (type.Namespace.Contains("InternalComponents"))
                    {
                        continue;
                    }
                    cb.AddLine("[Test]");
                    cb.AddLine($"public void {SafeTypeName(type, removeT: true)}_API_Test()");
                    cb.AddLine("{");
                    cb.IndentLevel++;
                    cb.AddLine(@$ "ctx.RenderComponent<DocsApi>(ComponentParameter.CreateParameter(" "Type" ", typeof({SafeTypeName(type)})));");
                    cb.IndentLevel--;
                    cb.AddLine("}");
                }

                cb.IndentLevel--;
                cb.AddLine("}");
                cb.IndentLevel--;
                cb.AddLine("}");

                if (currentCode != cb.ToString())
                {
                    File.WriteAllText(paths.ApiPageTestsFilePath, cb.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error generating {paths.ApiPageTestsFilePath} : {e.Message}");
                success = false;
            }

            return(success);
        }
예제 #4
0
        public bool Execute()
        {
            var  paths   = new Paths();
            bool success = true;

            try
            {
                Directory.CreateDirectory(paths.TestDirPath);

                string currentCode = string.Empty;
                if (File.Exists(paths.ComponentTestsFilePath))
                {
                    currentCode = File.ReadAllText(paths.ComponentTestsFilePath);
                }

                var cb = new CodeBuilder();

                cb.AddHeader();

                cb.AddLine("using Bunit.TestDoubles.JSInterop;");
                cb.AddLine("using Microsoft.AspNetCore.Components;");
                cb.AddLine("using Microsoft.Extensions.DependencyInjection;");
                cb.AddLine("using NUnit.Framework;");
                cb.AddLine("using MudBlazor.UnitTests.Mocks;");
                cb.AddLine("using MudBlazor.Docs.Examples;");
                cb.AddLine("using MudBlazor.Docs.Wireframes;");
                cb.AddLine("using MudBlazor.Services;");
                cb.AddLine();
                cb.AddLine("namespace MudBlazor.UnitTests.Components");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("// These tests just check if all the examples from the doc page render without errors");
                cb.AddLine("[TestFixture]");
                cb.AddLine("public class _AllComponents");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("private Bunit.TestContext ctx;");
                cb.AddLine();
                cb.AddLine("[SetUp]");
                cb.AddLine("public void Setup()");
                cb.AddLine("{");
                cb.IndentLevel++;
                cb.AddLine("ctx = new Bunit.TestContext();");
                cb.AddLine("ctx.Services.AddMockJSRuntime();");
                cb.AddLine("ctx.Services.AddSingleton<NavigationManager>(new MockNavigationManager());");
                cb.AddLine("ctx.Services.AddSingleton<IDialogService>(new DialogService());");
                cb.AddLine("ctx.Services.AddSingleton<ISnackbar>(new MockSnackbar());");
                cb.AddLine("ctx.Services.AddSingleton<IResizeListenerService>(new MockResizeListenerService());");
                cb.IndentLevel--;
                cb.AddLine("}");
                cb.AddLine();
                cb.AddLine("[TearDown]");
                cb.AddLine("public void TearDown() => ctx.Dispose();");
                cb.AddLine();

                foreach (var entry in Directory.EnumerateFiles(paths.DocsDirPath, "*.razor", SearchOption.AllDirectories)
                         .OrderBy(e => e.Replace("\\", "/"), StringComparer.Ordinal))
                {
                    if (entry.EndsWith("Code.razor"))
                    {
                        continue;
                    }
                    var filename      = Path.GetFileName(entry);
                    var componentName = Path.GetFileNameWithoutExtension(filename);
                    if (!filename.Contains(Paths.ExampleDiscriminator))
                    {
                        continue;
                    }
                    cb.AddLine("[Test]");
                    cb.AddLine($"public void {componentName}_Test()");
                    cb.AddLine("{");
                    cb.IndentLevel++;
                    cb.AddLine($"ctx.RenderComponent<{componentName}>();");
                    cb.IndentLevel--;
                    cb.AddLine("}");
                }

                cb.IndentLevel--;
                cb.AddLine("}");
                cb.IndentLevel--;
                cb.AddLine("}");

                if (currentCode != cb.ToString())
                {
                    File.WriteAllText(paths.ComponentTestsFilePath, cb.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error generating {paths.ComponentTestsFilePath} : {e.Message}");
                success = false;
            }

            return(success);
        }
예제 #5
0
        public bool Execute()
        {
            var paths            = new Paths();
            var newFiles         = new StringBuilder();
            var success          = true;
            var noOfFilesUpdated = 0;
            var noOfFilesCreated = 0;

            try
            {
                var formatter       = new HtmlClassFormatter();
                var lastCheckedTime = new DateTime();
                if (File.Exists(paths.NewFilesToBuildPath))
                {
                    var lastNewFilesToBuild = new FileInfo(paths.NewFilesToBuildPath);
                    lastCheckedTime = lastNewFilesToBuild.LastWriteTime;
                }

                var directoryInfo = new DirectoryInfo(paths.DocsDirPath);

                foreach (var entry in directoryInfo.GetFiles("*.razor", SearchOption.AllDirectories))
                {
                    if (entry.Name.EndsWith("Code.razor"))
                    {
                        continue;
                    }
                    if (!entry.Name.Contains(Paths.ExampleDiscriminator))
                    {
                        continue;
                    }
                    var markupPath = entry.FullName.Replace("Examples", "Code").Replace(".razor", "Code.html");
                    if (entry.LastWriteTime < lastCheckedTime && File.Exists(markupPath))
                    {
                        continue;
                    }

                    var markupDir = Path.GetDirectoryName(markupPath);
                    if (!Directory.Exists(markupDir))
                    {
                        Directory.CreateDirectory(markupDir);
                    }

                    var src     = StripComponentSource(entry.FullName);
                    var blocks  = src.Split("@code");
                    var blocks0 = Regex.Replace(blocks[0], @"</?DocsFrame>", string.Empty)
                                  .Replace("@", "PlaceholdeR")
                                  .Trim();

                    // Note: the @ creates problems and thus we replace it with an unlikely placeholder and in the markup replace back.
                    var html = formatter.GetHtmlString(blocks0, Languages.Html).Replace("PlaceholdeR", "@");
                    html = AttributePostprocessing(html).Replace("@", "<span class=\"atSign\">&#64;</span>");

                    var currentCode = string.Empty;
                    if (File.Exists(markupPath))
                    {
                        currentCode = File.ReadAllText(markupPath);
                    }

                    var cb = new CodeBuilder();
                    // cb.AddLine("@* Auto-generated markup. Any changes will be overwritten *@");
                    // cb.AddLine("@namespace MudBlazor.Docs.Examples.Markup");
                    cb.AddLine("<div class=\"mud-codeblock\">");
                    cb.AddLine(html.ToLfLineEndings());
                    if (blocks.Length == 2)
                    {
                        cb.AddLine(
                            formatter.GetHtmlString("@code" + blocks[1], Languages.CSharp)
                            .Replace("@", "<span class=\"atSign\">&#64;</span>")
                            .ToLfLineEndings());
                    }

                    cb.AddLine("</div>");

                    if (currentCode != cb.ToString())
                    {
                        File.WriteAllText(markupPath, cb.ToString());
                        if (currentCode == string.Empty)
                        {
                            newFiles.AppendLine(markupPath);
                            noOfFilesCreated++;
                        }
                        else
                        {
                            noOfFilesUpdated++;
                        }
                    }
                }

                File.WriteAllText(paths.NewFilesToBuildPath, newFiles.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error generating examples markup : {e.Message}");
                success = false;
            }

            Console.WriteLine($"Docs.Compiler updated {noOfFilesUpdated} generated files");
            Console.WriteLine($"Docs.Compiler generated {noOfFilesCreated} new files");
            return(success);
        }