Skip to content

Commit

Permalink
Merge pull request #3 from StuffOfInterest/feature-while
Browse files Browse the repository at this point in the history
Feature while
  • Loading branch information
StuffOfInterest authored Apr 29, 2021
2 parents 23640ac + fe98618 commit 9cffd76
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 29 deletions.
5 changes: 5 additions & 0 deletions LogicTagHelpers.Demo/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public IActionResult TestForeach()
{
return View();
}

public IActionResult TestWhile()
{
return View();
}
}
}
1 change: 1 addition & 0 deletions LogicTagHelpers.Demo/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<li><a asp-action="TestIf">Test If</a></li>
<li><a asp-action="TestSwitch">Test Switch</a></li>
<li><a asp-action="TestForeach">Test Foreach</a></li>
<li><a asp-action="TestWhile">Test While</a></li>
</ul>
91 changes: 67 additions & 24 deletions LogicTagHelpers.Demo/Views/Home/TestSwitch.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,91 @@

<h2>Test 3</h2>

@try
{
<switch expression="2">
@{ string errorResult = null; }
<switch expression="4">
@try
{
<case value="1"><p>Should not display 1.</p></case>
<case value="2"><p>Should not display 2.</p></case>
<case value="2"><p>Should not display 3.</p></case>
<case value="2"><p>Should not display 4.</p></case>
</switch>
}
catch (LogicTagHelperException ex)
}
catch (LogicTagHelperException ex)
{
errorResult = $"Expected exception encountered: '{ex.Message}'.";
}
</switch>
@if (!string.IsNullOrWhiteSpace(errorResult))
{
<p>Expected exception encountered: '@ex.Message'.</p>
<p>@errorResult</p>
}


<h2>Test 4</h2>

@try
{
<switch expression="2">
@{ errorResult = null; }
<switch expression="3">
@try
{
<case value="1"><p>Should not display 1.</p></case>
<case value="2"><p>Should not display 2.</p></case>
<default><p>Should not display 3.</p></default>
<default><p>Should not display 4.</p></default>
</switch>
}
catch (LogicTagHelperException ex)
}
catch (LogicTagHelperException ex)
{
errorResult = $"Expected exception encountered: '{ex.Message}'.";
}
</switch>
@if (!string.IsNullOrWhiteSpace(errorResult))
{
<p>Expected exception encountered: '@ex.Message'.</p>
<p>@errorResult</p>
}

<h2>Test 5</h2>

@try
{
@{
var stringVal = "some string";
<switch expression="2">
var result = string.Empty;
}
<switch expression="2">
@try
{
<case value="1"><p>Should not display 1.</p></case>
<case value="@stringVal"><p>Should not display 2.</p></case>
</switch>
}
catch (LogicTagHelperException ex)
{
<p>Expected exception encountered: '@ex.Message'.</p>
}
}
catch (LogicTagHelperException ex)
{
result = $"Expected exception encountered: {ex.Message}.";
}
</switch>
<p>@result</p>

<h2>Test 6</h2>

@{ result = string.Empty; }
<switch>
@try
{
<case value="1"><p>Should not display.</p></case>
}
catch (LogicTagHelperException ex)
{
result = $"Expected exception encountered: {ex.Message}.";
}
</switch>
<p>@result</p>

<h2>Test 7</h2>

@{ result = string.Empty; }
<switch>
@try
{
<default><p>Should not display.</p></default>
}
catch (LogicTagHelperException ex)
{
result = $"Expected exception encountered: {ex.Message}.";
}
</switch>
<p>@result</p>
20 changes: 20 additions & 0 deletions LogicTagHelpers.Demo/Views/Home/TestWhile.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h2>Test 1</h2>

@{ var x = 0; }
<while condition="() => x < 10">
<p>Should display @x.</p>
@{ x++; }
</while>

<h2>Test 2</h2>

@try
{
<while>
<p>Should not display.</p>
</while>
}
catch (Exception ex)
{
<p>Expected exception occurred: '@ex.Message'.</p>
}
10 changes: 10 additions & 0 deletions LogicTagHelpers.Tests/CreateClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,15 @@ public void CanCreateForeachTagHelper()
// Assert
Assert.IsNotNull(result);
}

[TestMethod]
public void CanCreateWhileTagHelper()
{
// Act
var result = new WhileTagHelper();

// Assert
Assert.IsNotNull(result);
}
}
}
5 changes: 5 additions & 0 deletions LogicTagHelpers/CaseTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
{
var switchContext = (SwitchContext) context.Items[SwitchContext.ContextKey];

if (switchContext == null)
{
throw new LogicTagHelperException("Case statement inside of switch with no valid expression.");
}

if (Value.GetType() != switchContext.Expression.GetType())
{
throw new LogicTagHelperException(
Expand Down
6 changes: 5 additions & 1 deletion LogicTagHelpers/DefaultTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ public class DefaultTagHelper : TagHelper
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
var switchContext = (SwitchContext) context.Items[SwitchContext.ContextKey];


if (switchContext == null)
{
throw new LogicTagHelperException("Default statement inside of switch with no valid expression.");
}

if (switchContext.HasDefault)
{
Expand Down
4 changes: 2 additions & 2 deletions LogicTagHelpers/LogicTagHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.3.0</Version>
<Version>0.4.0</Version>
<Authors>Delbert Matlock</Authors>
<Company>Stuff Of Interest</Company>
<Product />
Expand All @@ -13,9 +13,9 @@ Done:
* if/then/else
* switch/case/default
* foreach
* while

Future:
* while
* for
* do</Description>
<PackageProjectUrl>https://github.com/StuffOfInterest/LogicTagHelpers</PackageProjectUrl>
Expand Down
2 changes: 1 addition & 1 deletion LogicTagHelpers/SwitchTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace LogicTagHelpers
/// <summary>
/// Include at most one section based on value of an expression.
/// </summary>
[HtmlTargetElement("switch")]
[HtmlTargetElement("switch", Attributes = "expression")]
[RestrictChildren("case", "default")]
public class SwitchTagHelper : TagHelper
{
Expand Down
35 changes: 35 additions & 0 deletions LogicTagHelpers/WhileTagHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace LogicTagHelpers
{
/// <summary>
/// Re-render block of code as long as condition evaluates to true.
/// </summary>
public class WhileTagHelper : TagHelper
{
/// <summary>
/// Function to evaluate exit condition.
/// Inner content will render as long as the condition returns true.
/// </summary>
public Func<bool> Condition { get; set; }

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (Condition == null)
{
throw new LogicTagHelperException("Condition may not be null.");
}

output.TagName = null;
output.Content.Clear();

while (Condition.Invoke())
{
var childContent = await output.GetChildContentAsync(false);
output.Content.AppendHtml(childContent);
}
}
}
}
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
* [switch](#switch)
* case
* [foreach](#foreach)
* [while](#while)

## Future Tags

* while
* for
* do

Expand Down Expand Up @@ -61,4 +61,14 @@ Following line must be added to the `_ViewImports.cshtml` file for the logic tag
<foreach iterator="context">
(content to display for each item in collection)
</foreach>
```

### while

```cshtml
@{ var x = 0; }
<while condition="() => x < 10">
(content to display while condition is true)
@{ x++; }
</while>
```

0 comments on commit 9cffd76

Please sign in to comment.