public void ProcessMessage_ResolveTagHelperDescriptors_ResolvesDesignTimeTagHelperDescriptors() { // Arrange var expectedSourceLocation = new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3); var sourceLocationJson = JsonConvert.SerializeObject(expectedSourceLocation); var messageData = new JObject { { "AssemblyName", CustomTagHelperAssembly }, { "SourceLocation", JObject.Parse(sourceLocationJson) } }; var message = new JObject { { "MessageType", RazorPluginMessageTypes.ResolveTagHelperDescriptors }, { "Data", messageData }, }; ResolveTagHelperDescriptorsMessage responseMessage = null; var messageBroker = new TestPluginMessageBroker( data => responseMessage = (ResolveTagHelperDescriptorsMessage)data); var assembly = new TestAssembly(typeof(DesignTimeTagHelper)); var assemblyNameLookups = new Dictionary <string, Assembly> { { CustomTagHelperAssembly, assembly } }; var assemblyLoadContext = new TestAssemblyLoadContext(assemblyNameLookups); var plugin = new RazorPlugin(messageBroker); var expectedDescriptor = new TagHelperDescriptor { Prefix = DefaultPrefix, TagName = "design-time", TypeName = typeof(DesignTimeTagHelper).FullName, AssemblyName = typeof(DesignTimeTagHelper).Assembly.GetName().Name, AllowedChildren = new[] { "br" }, TagStructure = TagStructure.NormalOrSelfClosing, DesignTimeDescriptor = new TagHelperDesignTimeDescriptor { OutputElementHint = "strong" } }; // Act var handled = plugin.ProcessMessage(message, assemblyLoadContext); // Assert Assert.NotNull(responseMessage); Assert.Equal( RazorPluginMessageTypes.ResolveTagHelperDescriptors, responseMessage.MessageType, StringComparer.Ordinal); var responseData = responseMessage.Data; Assert.Equal(CustomTagHelperAssembly, responseData.AssemblyName, StringComparer.Ordinal); var actualDescriptor = Assert.Single(responseData.Descriptors); Assert.Equal(expectedDescriptor, actualDescriptor, CaseSensitiveTagHelperDescriptorComparer.Default); Assert.Empty(responseData.Errors); Assert.True(handled); }
public static void Register(CommandLineApplication app, IAssemblyLoadContext assemblyLoadContext) { app.Command("resolve-taghelpers", config => { config.Description = "Resolves TagHelperDescriptors in the specified assembly(s)."; config.HelpOption("-?|-h|--help"); var clientProtocol = config.Option( "-p|--protocol", "Provide client protocol version.", CommandOptionType.SingleValue); var assemblyNames = config.Argument( "[name]", "Assembly name to resolve TagHelperDescriptors in.", multipleValues: true); config.OnExecute(() => { var messageBroker = new CommandMessageBroker(); var plugin = new RazorPlugin(messageBroker); var resolvedProtocol = ResolveProtocolCommand.ResolveProtocol(clientProtocol, plugin.Protocol); plugin.Protocol = resolvedProtocol; var success = true; foreach (var assemblyName in assemblyNames.Values) { var messageData = new ResolveTagHelperDescriptorsRequestData { AssemblyName = assemblyName, SourceLocation = SourceLocation.Zero }; var message = new RazorPluginRequestMessage( RazorPluginMessageTypes.ResolveTagHelperDescriptors, JObject.FromObject(messageData)); success &= plugin.ProcessMessage(JObject.FromObject(message), assemblyLoadContext); } var resolvedDescriptors = messageBroker.Results.SelectMany(result => result.Data.Descriptors); var resolvedErrors = messageBroker.Results.SelectMany(result => result.Data.Errors); var resolvedResult = new ResolvedTagHelperDescriptorsResult { Descriptors = resolvedDescriptors, Errors = resolvedErrors }; var serializedResult = JsonConvert.SerializeObject(resolvedResult, Formatting.Indented); Console.WriteLine(serializedResult); return(success ? 0 : 1); }); }); }
public static void Register(CommandLineApplication app, IAssemblyLoadContext assemblyLoadContext) { app.Command("resolve-taghelpers", config => { config.Description = "Resolves TagHelperDescriptors in the specified assembly(s)."; config.HelpOption("-?|-h|--help"); var clientProtocol = config.Option( "-p|--protocol", "Provide client protocol version.", CommandOptionType.SingleValue); var assemblyNames = config.Argument( "[name]", "Assembly name to resolve TagHelperDescriptors in.", multipleValues: true); config.OnExecute(() => { var messageBroker = new CommandMessageBroker(); var plugin = new RazorPlugin(messageBroker); var resolvedProtocol = ResolveProtocolCommand.ResolveProtocol(clientProtocol, plugin.Protocol); plugin.Protocol = resolvedProtocol; var success = true; foreach (var assemblyName in assemblyNames.Values) { var messageData = new ResolveTagHelperDescriptorsRequestData { AssemblyName = assemblyName, SourceLocation = SourceLocation.Zero }; var message = new RazorPluginRequestMessage( RazorPluginMessageTypes.ResolveTagHelperDescriptors, JObject.FromObject(messageData)); success &= plugin.ProcessMessage(JObject.FromObject(message), assemblyLoadContext); } var resolvedDescriptors = messageBroker.Results.SelectMany(result => result.Data.Descriptors); var resolvedErrors = messageBroker.Results.SelectMany(result => result.Data.Errors); var resolvedResult = new ResolvedTagHelperDescriptorsResult { Descriptors = resolvedDescriptors, Errors = resolvedErrors }; var serializedResult = JsonConvert.SerializeObject(resolvedResult, Formatting.Indented); Console.WriteLine(serializedResult); return success ? 0 : 1; }); }); }
public void ProcessMessage_ThrowsWhenNoMessageType() { // Arrange var message = new JObject(); var messageBroker = new TestPluginMessageBroker(); var assemblyLoadContext = new TestAssemblyLoadContext(); var plugin = new RazorPlugin(messageBroker); var expectedMessage = "'MessageType' must be provided for a 'RazorPluginRequestMessage' message."; // Act & Assert var error = Assert.Throws <InvalidOperationException>( () => plugin.ProcessMessage(message, assemblyLoadContext)); Assert.Equal(expectedMessage, error.Message, StringComparer.Ordinal); }
public void ProcessMessage_ResolveTagHelperDescriptors_ResolvesTagHelperDescriptors() { // Arrange var expectedSourceLocation = new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3); var sourceLocationJson = JsonConvert.SerializeObject(expectedSourceLocation); var messageData = new JObject { { "AssemblyName", CustomTagHelperAssembly }, { "SourceLocation", JObject.Parse(sourceLocationJson) } }; var message = new JObject { { "MessageType", RazorPluginMessageTypes.ResolveTagHelperDescriptors }, { "Data", messageData }, }; ResolveTagHelperDescriptorsMessage responseMessage = null; var messageBroker = new TestPluginMessageBroker( data => responseMessage = (ResolveTagHelperDescriptorsMessage)data); var assembly = new TestAssembly(typeof(CustomTagHelper)); var assemblyNameLookups = new Dictionary <string, Assembly> { { CustomTagHelperAssembly, assembly } }; var assemblyLoadContext = new TestAssemblyLoadContext(assemblyNameLookups); var plugin = new RazorPlugin(messageBroker); // Act var handled = plugin.ProcessMessage(message, assemblyLoadContext); // Assert Assert.NotNull(responseMessage); Assert.Equal( RazorPluginMessageTypes.ResolveTagHelperDescriptors, responseMessage.MessageType, StringComparer.Ordinal); var responseData = responseMessage.Data; Assert.Equal(CustomTagHelperAssembly, responseData.AssemblyName, StringComparer.Ordinal); var actualDescriptor = Assert.Single(responseData.Descriptors); Assert.Equal( CustomTagHelperDescriptor, actualDescriptor, CaseSensitiveTagHelperDescriptorComparer.Default); Assert.Empty(responseData.Errors); Assert.True(handled); }
public void ProcessMessage_ResolveTagHelperDescriptors_ReturnsErrors() { // Arrange var expectedSourceLocation = new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3); var sourceLocationJson = JsonConvert.SerializeObject(expectedSourceLocation); var messageData = new JObject { { "AssemblyName", "invalid" }, { "SourceLocation", JObject.Parse(sourceLocationJson) } }; var message = new JObject { { "MessageType", RazorPluginMessageTypes.ResolveTagHelperDescriptors }, { "Data", messageData }, }; ResolveTagHelperDescriptorsMessage responseMessage = null; var messageBroker = new TestPluginMessageBroker( data => responseMessage = (ResolveTagHelperDescriptorsMessage)data); var assemblyLoadContext = new ThrowingAssemblyLoadContext("Invalid assembly"); var plugin = new RazorPlugin(messageBroker); // Act var handled = plugin.ProcessMessage(message, assemblyLoadContext); // Assert Assert.NotNull(responseMessage); Assert.Equal( RazorPluginMessageTypes.ResolveTagHelperDescriptors, responseMessage.MessageType, StringComparer.Ordinal); var responseData = responseMessage.Data; Assert.Equal("invalid", responseData.AssemblyName, StringComparer.Ordinal); Assert.Empty(responseData.Descriptors); var error = Assert.Single(responseData.Errors); Assert.Equal( "Cannot resolve TagHelper containing assembly 'invalid'. Error: Invalid assembly: invalid", error.Message, StringComparer.Ordinal); Assert.Equal(expectedSourceLocation, error.Location); Assert.Equal(7, error.Length); Assert.True(handled); }
public void ProcessMessage_ThrowsWhenNoData() { // Arrange var message = new JObject { { "MessageType", RazorPluginMessageTypes.ResolveTagHelperDescriptors } }; var messageBroker = new TestPluginMessageBroker(); var assemblyLoadContext = new TestAssemblyLoadContext(); var plugin = new RazorPlugin(messageBroker); var expectedMessage = "'Data' must be provided for a '" + RazorPluginMessageTypes.ResolveTagHelperDescriptors + "' message."; // Act & Assert var error = Assert.Throws <InvalidOperationException>( () => plugin.ProcessMessage(message, assemblyLoadContext)); Assert.Equal(expectedMessage, error.Message, StringComparer.Ordinal); }
public static void Register(CommandLineApplication app) { app.Command("resolve-protocol", config => { config.Description = "Resolves protocol used to resolve TagHeleprDescriptors."; config.HelpOption("-?|-h|--help"); var clientProtocol = config.Argument( "[clientProtocol]", "Client protocol used to consume returned TagHelperDescriptors."); config.OnExecute(() => { var pluginProtocol = new RazorPlugin(messageBroker: null).Protocol; var resolvedProtocol = ResolveProtocol(clientProtocol.Value, pluginProtocol); Console.WriteLine(resolvedProtocol); return 0; }); }); }
public void ProcessMessage_NoOpsForUnknownMessageType() { // Arrange var messageData = new JObject(); var message = new JObject { { "MessageType", "SomethingUnknown" }, { "Data", messageData }, }; var called = false; var messageBroker = new TestPluginMessageBroker((_) => called = true); var assemblyLoadContext = new TestAssemblyLoadContext(); var plugin = new RazorPlugin(messageBroker); // Act var handled = plugin.ProcessMessage(message, assemblyLoadContext); // Assert Assert.False(called); Assert.False(handled); }
public static void Register(CommandLineApplication app) { app.Command("resolve-protocol", config => { config.Description = "Resolves protocol used to resolve TagHeleprDescriptors."; config.HelpOption("-?|-h|--help"); var clientProtocol = config.Argument( "[clientProtocol]", "Client protocol used to consume returned TagHelperDescriptors."); config.OnExecute(() => { var pluginProtocol = new RazorPlugin(messageBroker: null).Protocol; var resolvedProtocol = ResolveProtocol(clientProtocol.Value, pluginProtocol); Console.WriteLine(resolvedProtocol); return(0); }); }); }
public void ProcessMessage_ResolveTagHelperDescriptors_ResolvesDesignTimeTagHelperDescriptors() { // Arrange var expectedSourceLocation = new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3); var sourceLocationJson = JsonConvert.SerializeObject(expectedSourceLocation); var messageData = new JObject { { "AssemblyName", CustomTagHelperAssembly }, { "SourceLocation", JObject.Parse(sourceLocationJson) } }; var message = new JObject { { "MessageType", RazorPluginMessageTypes.ResolveTagHelperDescriptors }, { "Data", messageData }, }; ResolveTagHelperDescriptorsMessage responseMessage = null; var messageBroker = new TestPluginMessageBroker( data => responseMessage = (ResolveTagHelperDescriptorsMessage)data); var assembly = new TestAssembly(typeof(DesignTimeTagHelper)); var assemblyNameLookups = new Dictionary<string, Assembly> { { CustomTagHelperAssembly, assembly } }; var assemblyLoadContext = new TestAssemblyLoadContext(assemblyNameLookups); var plugin = new RazorPlugin(messageBroker); var expectedDescriptor = new TagHelperDescriptor { Prefix = DefaultPrefix, TagName = "design-time", TypeName = typeof(DesignTimeTagHelper).FullName, AssemblyName = typeof(DesignTimeTagHelper).Assembly.GetName().Name, AllowedChildren = new[] { "br" }, TagStructure = TagStructure.NormalOrSelfClosing, DesignTimeDescriptor = new TagHelperDesignTimeDescriptor { OutputElementHint = "strong" } }; // Act var handled = plugin.ProcessMessage(message, assemblyLoadContext); // Assert Assert.NotNull(responseMessage); Assert.Equal( RazorPluginMessageTypes.ResolveTagHelperDescriptors, responseMessage.MessageType, StringComparer.Ordinal); var responseData = responseMessage.Data; Assert.Equal(CustomTagHelperAssembly, responseData.AssemblyName, StringComparer.Ordinal); var actualDescriptor = Assert.Single(responseData.Descriptors); Assert.Equal(expectedDescriptor, actualDescriptor, CaseSensitiveTagHelperDescriptorComparer.Default); Assert.Empty(responseData.Errors); Assert.True(handled); }
public void ProcessMessage_ThrowsWhenNoMessageType() { // Arrange var message = new JObject(); var messageBroker = new TestPluginMessageBroker(); var assemblyLoadContext = new TestAssemblyLoadContext(); var plugin = new RazorPlugin(messageBroker); var expectedMessage = "'MessageType' must be provided for a 'RazorPluginRequestMessage' message."; // Act & Assert var error = Assert.Throws<InvalidOperationException>( () => plugin.ProcessMessage(message, assemblyLoadContext)); Assert.Equal(expectedMessage, error.Message, StringComparer.Ordinal); }
public void ProcessMessage_ThrowsWhenNoData() { // Arrange var message = new JObject { { "MessageType", RazorPluginMessageTypes.ResolveTagHelperDescriptors } }; var messageBroker = new TestPluginMessageBroker(); var assemblyLoadContext = new TestAssemblyLoadContext(); var plugin = new RazorPlugin(messageBroker); var expectedMessage = "'Data' must be provided for a '" + RazorPluginMessageTypes.ResolveTagHelperDescriptors + "' message."; // Act & Assert var error = Assert.Throws<InvalidOperationException>( () => plugin.ProcessMessage(message, assemblyLoadContext)); Assert.Equal(expectedMessage, error.Message, StringComparer.Ordinal); }
public void ProcessMessage_ResolveTagHelperDescriptors_ResolvesTagHelperDescriptors() { // Arrange var expectedSourceLocation = new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3); var sourceLocationJson = JsonConvert.SerializeObject(expectedSourceLocation); var messageData = new JObject { { "AssemblyName", CustomTagHelperAssembly }, { "SourceLocation", JObject.Parse(sourceLocationJson) } }; var message = new JObject { { "MessageType", RazorPluginMessageTypes.ResolveTagHelperDescriptors }, { "Data", messageData }, }; ResolveTagHelperDescriptorsMessage responseMessage = null; var messageBroker = new TestPluginMessageBroker( data => responseMessage = (ResolveTagHelperDescriptorsMessage)data); var assembly = new TestAssembly(typeof(CustomTagHelper)); var assemblyNameLookups = new Dictionary<string, Assembly> { { CustomTagHelperAssembly, assembly } }; var assemblyLoadContext = new TestAssemblyLoadContext(assemblyNameLookups); var plugin = new RazorPlugin(messageBroker); // Act var handled = plugin.ProcessMessage(message, assemblyLoadContext); // Assert Assert.NotNull(responseMessage); Assert.Equal( RazorPluginMessageTypes.ResolveTagHelperDescriptors, responseMessage.MessageType, StringComparer.Ordinal); var responseData = responseMessage.Data; Assert.Equal(CustomTagHelperAssembly, responseData.AssemblyName, StringComparer.Ordinal); var actualDescriptor = Assert.Single(responseData.Descriptors); Assert.Equal( CustomTagHelperDescriptor, actualDescriptor, CaseSensitiveTagHelperDescriptorComparer.Default); Assert.Empty(responseData.Errors); Assert.True(handled); }