Added sorted methods

This commit is contained in:
Adam Fowler
2021-03-15 10:53:33 +00:00
parent 465ad49f1f
commit 0f2c3bcea9
3 changed files with 91 additions and 12 deletions

View File

@@ -26,9 +26,9 @@ final class MethodTests: XCTestCase {
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>first: resque</b>
<b>hub</b>
<b>last: rip</b>
<b>first: resque</b>
<b>hub</b>
<b>last: rip</b>
""")
}
@@ -36,7 +36,7 @@ final class MethodTests: XCTestCase {
func testIndex() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{#index()}}{{plus1(.)}}) {{/}}{{ name }}</b>
<b>{{#index()}}{{plusone(.)}}{{/}}) {{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
@@ -48,6 +48,21 @@ final class MethodTests: XCTestCase {
""")
}
func testEvenOdd() throws {
let template = try HBMustacheTemplate(string: """
{{#repo}}
<b>{{index()}}) {{#even()}}even {{/}}{{#odd()}}odd {{/}}{{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
XCTAssertEqual(template.render(object), """
<b>0) even resque</b>
<b>1) odd hub</b>
<b>2) even rip</b>
""")
}
func testReversed() throws {
let template = try HBMustacheTemplate(string: """
{{#reversed(repo)}}
@@ -63,10 +78,10 @@ final class MethodTests: XCTestCase {
""")
}
func testArrayEnumerated() throws {
func testArrayIndex() throws {
let template = try HBMustacheTemplate(string: """
{{#enumerated(repo)}}
<b>{{ offset }}) {{ element.name }}</b>
{{#repo}}
<b>{{ index() }}) {{ name }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": [["name": "resque"], ["name": "hub"], ["name": "rip"]]]
@@ -78,13 +93,37 @@ final class MethodTests: XCTestCase {
""")
}
func testArraySorted() throws {
let template = try HBMustacheTemplate(string: """
{{#sorted(repo)}}
<b>{{ index() }}) {{ . }}</b>
{{/repo}}
""")
let object: [String: Any] = ["repo": ["resque", "hub", "rip"]]
XCTAssertEqual(template.render(object), """
<b>0) hub</b>
<b>1) resque</b>
<b>2) rip</b>
""")
}
func testDictionaryEnumerated() throws {
let template = try HBMustacheTemplate(string: """
{{#enumerated(.)}}<b>{{ element.key }} = {{ element.value }}</b>{{/.}}
{{#enumerated(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
""")
let object: [String: Any] = ["one": 1, "two": 2]
let result = template.render(object)
XCTAssertTrue(result == "<b>one = 1</b><b>two = 2</b>" || result == "<b>two = 2</b><b>one = 1</b>")
}
func testDictionarySortedByKey() throws {
let template = try HBMustacheTemplate(string: """
{{#sorted(.)}}<b>{{ key }} = {{ value }}</b>{{/.}}
""")
let object: [String: Any] = ["one": 1, "two": 2, "three": 3]
let result = template.render(object)
XCTAssertEqual(result, "<b>one = 1</b><b>three = 3</b><b>two = 2</b>")
}
}