Skip to content

Commit

Permalink
Added dump of extension methods to the test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasherceg committed Feb 25, 2024
1 parent b5a5aa7 commit 9dd12b5
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Samples/Common/DotvvmStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using DotVVM.Samples.Common.ViewModels.FeatureSamples.BindingVariables;
using DotVVM.Samples.Common.Views.ControlSamples.TemplateHost;
using DotVVM.Framework.ResourceManagement;
using DotVVM.Samples.Common.Presenters;
using DotVVM.Samples.Common.ViewModels.FeatureSamples.CustomPrimitiveTypes;

namespace DotVVM.Samples.BasicSamples
Expand Down Expand Up @@ -245,6 +246,8 @@ private static void AddRoutes(DotvvmConfiguration config)
config.RouteTable.Add("FeatureSamples_PostBack_PostBackHandlers_Localized", "FeatureSamples/PostBack/PostBackHandlers_Localized", "Views/FeatureSamples/PostBack/ConfirmPostBackHandler.dothtml", presenterFactory: LocalizablePresenter.BasedOnQuery("lang"));

config.RouteTable.Add("Errors_UndefinedRouteLinkParameters-PageDetail", "Erros/UndefinedRouteLinkParameters/{Id}", "Views/Errors/UndefinedRouteLinkParameters.dothtml", new { Id = 0 });

config.RouteTable.Add("DumpExtensionsMethods", "dump-extension-methods", _ => new DumpExtensionMethodsPresenter());
}

private static void AddControls(DotvvmConfiguration config)
Expand Down
45 changes: 45 additions & 0 deletions src/Samples/Common/Presenters/DumpExtensionMethodsPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using DotVVM.Framework.Compilation;
using DotVVM.Framework.Hosting;
using DotVVM.Framework.Utils;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;

namespace DotVVM.Samples.Common.Presenters
{
public class DumpExtensionMethodsPresenter : IDotvvmPresenter
{
public async Task ProcessRequest(IDotvvmRequestContext context)
{
var cache = context.Configuration.ServiceProvider.GetService<ExtensionMethodsCache>();

var contents = typeof(ExtensionMethodsCache)
.GetField("methodsCache", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(cache) as ConcurrentDictionary<string, ImmutableArray<MethodInfo>>;

var dump = contents.SelectMany(p => p.Value.Select(m => new {
Namespace = p.Key,
m.Name,
m.DeclaringType!.FullName,
Params = m.GetParameters().Select(p => new {
p.Name,
Type = p.ParameterType!.FullName
}),
m.IsGenericMethodDefinition,
GenericParameters = m.IsGenericMethodDefinition ? m.GetGenericArguments().Select(a => new {
a.Name
}) : null
}))
.OrderBy(m => m.Namespace).ThenBy(m => m.Name);

await context.HttpContext.Response.WriteAsync("ExtensionMethodsCache dump: " + JsonConvert.SerializeObject(dump));
}
}
}
25 changes: 23 additions & 2 deletions src/Samples/Tests/Tests/Feature/StaticCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using DotVVM.Samples.Tests.Base;
using DotVVM.Testing.Abstractions;
using OpenQA.Selenium;
Expand Down Expand Up @@ -816,16 +819,34 @@ public void Feature_List_Translation_Remove_Range()
}

[Fact]
public void Feature_List_Translation_Remove_Reverse()
public async Task Feature_List_Translation_Remove_Reverse()
{
var wasError = false;

RunInAllBrowsers(browser => {
browser.NavigateToUrl(SamplesRouteUrls.FeatureSamples_JavascriptTranslation_ListMethodTranslations);
browser.Wait(1000);
if (browser.FindElements(".exceptionMessage").Count > 0)
{
wasError = true;
return;
}
var rows = GetSortedRow(browser, "Reverse");
var column = GetColumnContent(rows, 0);
browser.WaitFor(() => Assert.Equal(10, column.Count), 500);
Assert.Equal(new List<string> { "10", "9", "8", "7", "6", "5", "4", "3", "2", "1" }, column);
});

if (wasError)
{
// error page - extension methods not found
var client = new HttpClient();
var json = await client.GetStringAsync(TestSuiteRunner.Configuration.BaseUrls[0].TrimEnd('/') + "/dump-extension-methods");
TestOutput.WriteLine(json);
throw new Exception("Test failed");
}
}

protected IElementWrapperCollection<IElementWrapper, IBrowserWrapper> GetSortedRow(IBrowserWrapper browser, string btn)
Expand Down

0 comments on commit 9dd12b5

Please sign in to comment.