-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from StuffOfInterest/feature-foreach
Feature foreach
- Loading branch information
Showing
13 changed files
with
232 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[*.cs] | ||
|
||
# CS0162: Unreachable code detected | ||
dotnet_diagnostic.CS0162.severity = none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,5 +18,10 @@ public IActionResult TestSwitch() | |
{ | ||
return View(); | ||
} | ||
|
||
public IActionResult TestForeach() | ||
{ | ||
return View(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
@{ | ||
var stringList = new[] { "one", "two", "three" }; | ||
var numberList = new[] { 1, 22, 333 }; | ||
var emptyList = new List<double>(); | ||
} | ||
|
||
<h2>Test 1</h2> | ||
|
||
@{ var foreachContext = new ForeachContext<string>(stringList); } | ||
<foreach iterator="foreachContext"> | ||
<p>Should display @(foreachContext.Item).</p> | ||
</foreach> | ||
|
||
<h2>Test 2</h2> | ||
|
||
@{ var foreachContext2 = new ForeachContext<int>(numberList); } | ||
<foreach iterator="foreachContext2"> | ||
<p>Should display @(foreachContext2.Item).</p> | ||
</foreach> | ||
|
||
<h2>Test 3</h2> | ||
|
||
<p>Should not display any additional content.</p> | ||
|
||
@{ var foreachContext3 = new ForeachContext<double>(emptyList); } | ||
<foreach iterator="foreachContext3"> | ||
<p>Should not display @(foreachContext3.Item).</p> | ||
</foreach> | ||
|
||
<h2>Test 4</h2> | ||
|
||
@try | ||
{ | ||
<foreach> | ||
<p>Should not display.</p> | ||
</foreach> | ||
} | ||
catch (Exception ex) | ||
{ | ||
<p>Expected exception occurred: '@ex.Message'.</p> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace LogicTagHelpers.Tests | ||
{ | ||
[TestClass] | ||
public class ForeachContextTests | ||
{ | ||
[TestMethod] | ||
public void CanGetFirstRecord() | ||
{ | ||
// Arrange | ||
var list = new[] {"one", "two", "three"}; | ||
var context = new ForeachContext<string>(list); | ||
|
||
// Act | ||
var result = context.LoadNext(); | ||
|
||
// Assert | ||
Assert.IsTrue(result); | ||
Assert.AreEqual("one", context.Item); | ||
} | ||
|
||
[TestMethod] | ||
public void CanGetSecondRecord() | ||
{ | ||
// Arrange | ||
var list = new[] { "one", "two", "three" }; | ||
var context = new ForeachContext<string>(list); | ||
context.LoadNext(); | ||
|
||
// Act | ||
var result = context.LoadNext(); | ||
|
||
// Assert | ||
Assert.IsTrue(result); | ||
Assert.AreEqual("two", context.Item); | ||
} | ||
|
||
[TestMethod] | ||
public void AfterLastRecordReturnsFalse() | ||
{ | ||
// Arrange | ||
var list = new[] { "one", "two" }; | ||
var context = new ForeachContext<string>(list); | ||
context.LoadNext(); | ||
context.LoadNext(); | ||
|
||
// Act | ||
var result = context.LoadNext(); | ||
|
||
// Assert | ||
Assert.IsFalse(result); | ||
Assert.AreEqual("two", context.Item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace LogicTagHelpers | ||
{ | ||
/// <summary> | ||
/// Generic implementation of a foreach context which can take any iterable object as a generic type. | ||
/// </summary> | ||
/// <typeparam name="T">Type of object to iterate over.</typeparam> | ||
public class ForeachContext<T> : IForeachContext | ||
{ | ||
public ForeachContext() | ||
{ | ||
} | ||
|
||
public ForeachContext(IEnumerable<T> items) | ||
{ | ||
Collection = items; | ||
} | ||
|
||
private IEnumerator<T> _enumerator; | ||
|
||
public IEnumerable<T> Collection { get; set; } | ||
public T Item { get; set; } | ||
|
||
public bool LoadNext() | ||
{ | ||
var enumerator = _enumerator ??= Collection.GetEnumerator(); | ||
var result = enumerator.MoveNext(); | ||
if (result) | ||
{ | ||
Item = enumerator.Current; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
|
||
namespace LogicTagHelpers | ||
{ | ||
/// <summary> | ||
/// Perform 'foreach' iteration over a set of data. | ||
/// Inner content is rendered for each item in the iterator. | ||
/// </summary> | ||
[HtmlTargetElement("foreach")] | ||
public class ForeachTagHelper : TagHelper | ||
{ | ||
/// <summary> | ||
/// Context which holds the collection to iterate over and a property for presenting the current item to the inner content. | ||
/// </summary> | ||
public IForeachContext Iterator { get; set; } | ||
|
||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) | ||
{ | ||
if (Iterator == null) | ||
{ | ||
throw new LogicTagHelperException("Iterator may not be null."); | ||
} | ||
|
||
output.TagName = null; | ||
output.Content.Clear(); | ||
|
||
while (Iterator.LoadNext()) | ||
{ | ||
var childContent = await output.GetChildContentAsync(false); | ||
output.Content.AppendHtml(childContent); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace LogicTagHelpers | ||
{ | ||
/// <summary> | ||
/// Context for 'foreach' tag helper which holds the collection to iterator over | ||
/// and presents each item to the tag helper content. | ||
/// </summary> | ||
public interface IForeachContext | ||
{ | ||
bool LoadNext(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters