public void TestTagHelper() { //Arrange var context = new TagHelperContext( new TagHelperAttributeList(), new Dictionary <object, object>(), "myuniqueid"); var output = new TagHelperOutput("button", new TagHelperAttributeList(), (cahce, encoder) => Task.FromResult <TagHelperContent> (new DefaultTagHelperContent())); //Act var tagHelper = new ButtonTagHelper { BsButtonColor = "testValue" }; tagHelper.Process(context, output); //Assert Assert.Equal($"btn btn-{tagHelper.BsButtonColor}", output.Attributes["class"].Value); }
public override void Process(TagHelperContext context, TagHelperOutput output) { if (ViewContext.ViewData.Model is ListView listView) { if ((listView.ItemsCollection?.Count() ?? 0) > 0) { foreach (var currentObject in listView.ItemsCollection) { listView.SetCurrentObject(currentObject); var rowOutput = CreateTagHelperOutput("tr"); rowOutput.Content.AppendHtml("<th scope=\"row\" class=\"col-0 pl-0 pr-0\"> </th>"); //Buttons if ((listView.Buttons?.Length ?? 0) > 0 && listView.Buttons.Any(x => x.IsApplicable("Inline"))) { rowOutput.Content.AppendHtml("<th scope=\"row\" class=\"col-fit text-center\">"); var buttonTagHelper = new ButtonTagHelper(UrlHelperFactory, ApplicationService, ExpressionsService) { For = listView.Buttons, Target = "Inline", _HideText = "true", CurrentObject = currentObject, ViewContext = ViewContext }; var buttonOutput = CreateTagHelperOutput("div"); buttonTagHelper.Process(context, buttonOutput); rowOutput.Content.AppendHtml(buttonOutput.Content.GetContent()); rowOutput.Content.AppendHtml("</th>"); } //Columns var columnTagHelper = new TableListViewColumnTagHelper(UrlHelperFactory, ApplicationService, ExpressionsService) { For = listView.Columns, IsSubListView = IsSubListView, ViewContext = ViewContext }; columnTagHelper.Process(context, rowOutput); var backColor = listView.GetApplicableBackColor(ExpressionsService, currentObject); output.Content.AppendHtml($"<tr class=\"{backColor}\">{rowOutput.Content.GetContent() }</tr>"); } listView.ClearCurrentObject(); } else if (!string.IsNullOrWhiteSpace(listView.MessageOnEmpty)) { output.Content.AppendHtml( $"<tr>" + $"<td class=\"col text-center\" colspan=\"{1 + ((listView?.Buttons?.Length ?? 0) > 0 && listView.Buttons.Any(x => x.IsApplicable("Inline")) ? 1 : 0) + (listView?.Columns?.Length ?? 0)}\">{listView.MessageOnEmpty}</td>" + "</tr>"); } } }
/// <summary> /// 获取结果 /// </summary> private string GetResult(ButtonTagHelper button, TagHelperAttributeList contextAttributes = null, TagHelperAttributeList outputAttributes = null, TagHelperContent content = null) { var context = new TagHelperContext("", contextAttributes ?? new TagHelperAttributeList(), new Dictionary <object, object>(), Id.Guid()); var output = new TagHelperOutput("", outputAttributes ?? new TagHelperAttributeList(), (useCachedResult, encoder) => Task.FromResult(content ?? new DefaultTagHelperContent())); button.Process(context, output); var writer = new StringWriter(); output.WriteTo(writer, HtmlEncoder.Default); var result = writer.ToString(); _output.WriteLine(result); return(result); }
public void Process_ShouldSetBsButtonClass() { var context = new TagHelperContext(new TagHelperAttributeList(), new Dictionary <object, object>(), "myuniqueid"); var output = new TagHelperOutput("button", new TagHelperAttributeList(), (cache, encoder) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent())); var testee = new ButtonTagHelper { BsButtonColor = "danger" }; testee.Process(context, output); output.Attributes["class"].Value.Should().Be($"btn btn-{testee.BsButtonColor}"); }
public void TestButtonTagHelper() { //arrange var context = new TagHelperContext(new TagHelperAttributeList(), new Dictionary <object, object>(), "uniqueid"); var output = new TagHelperOutput("button", new TagHelperAttributeList(), (cache, encoder) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent())); //act var helper = new ButtonTagHelper { BsButtonColor = "valueToBeTested" }; helper.Process(context, output); //assert Assert.AreEqual($"btn btn-{helper.BsButtonColor}", output.Attributes["class"].Value); }