Add basic support for ranges in for loops

Closes kylef/Stencil#128
This commit is contained in:
Brad Lindsay
2017-08-08 17:38:36 -05:00
parent 5821e4849e
commit 64571464d9
2 changed files with 20 additions and 0 deletions

View File

@@ -90,6 +90,10 @@ class ForNode : NodeType {
values = dictionary.map { ($0.key, $0.value) } values = dictionary.map { ($0.key, $0.value) }
} else if let array = resolved as? [Any] { } else if let array = resolved as? [Any] {
values = array values = array
} else if let range = resolved as? CountableClosedRange<Int> {
values = Array(range)
} else if let range = resolved as? CountableRange<Int> {
values = Array(range)
} else { } else {
values = [] values = []
} }

View File

@@ -37,6 +37,22 @@ func testForNode() {
try expect(try node.render(any_context)) == "123" try expect(try node.render(any_context)) == "123"
} }
$0.it("renders a context variable of type CountableClosedRange<Int>") {
let context = Context(dictionary: ["range": 1...3])
let nodes: [NodeType] = [VariableNode(variable: "item")]
let node = ForNode(resolvable: Variable("range"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "123"
}
$0.it("renders a context variable of type CountableRange<Int>") {
let context = Context(dictionary: ["range": 1..<4])
let nodes: [NodeType] = [VariableNode(variable: "item")]
let node = ForNode(resolvable: Variable("range"), loopVariables: ["item"], nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "123"
}
#if os(OSX) #if os(OSX)
$0.it("renders a context variable of type NSArray") { $0.it("renders a context variable of type NSArray") {
let nsarray_context = Context(dictionary: [ let nsarray_context = Context(dictionary: [