@@ -98,13 +98,43 @@ public class VariableNode : Node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class NowNode : Node {
|
public class NowNode : Node {
|
||||||
|
public let format:Variable
|
||||||
|
|
||||||
public class func parse(parser:TokenParser, token:Token) -> (node:Node?, error:Error?) {
|
public class func parse(parser:TokenParser, token:Token) -> (node:Node?, error:Error?) {
|
||||||
return (NowNode(), nil)
|
var format:Variable?
|
||||||
|
|
||||||
|
let components = token.components()
|
||||||
|
if components.count == 2 {
|
||||||
|
format = Variable(components[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
return (NowNode(format:format), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(format:Variable?) {
|
||||||
|
if let format = format {
|
||||||
|
self.format = format
|
||||||
|
} else {
|
||||||
|
self.format = Variable("\"yyyy-MM-dd 'at' HH:mm\"")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func render(context: Context) -> (String?, Error?) {
|
public func render(context: Context) -> (String?, Error?) {
|
||||||
let date = NSDate()
|
let date = NSDate()
|
||||||
return ("\(date)", nil)
|
let format: AnyObject? = self.format.resolve(context)
|
||||||
|
var formatter:NSDateFormatter?
|
||||||
|
|
||||||
|
if let format = format as? NSDateFormatter {
|
||||||
|
formatter = format
|
||||||
|
} else if let format = format as? String {
|
||||||
|
formatter = NSDateFormatter()
|
||||||
|
formatter!.dateFormat = format
|
||||||
|
} else {
|
||||||
|
// TODO Error
|
||||||
|
return (nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ("\(formatter!.stringFromDate(date))", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -180,3 +180,47 @@ class IfNodeTests: NodeTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class NowNodeTests: NodeTests {
|
||||||
|
|
||||||
|
// MARK: Parsing
|
||||||
|
|
||||||
|
func testParseDefaultNow() {
|
||||||
|
let tokens = [ Token.Block(value: "now") ]
|
||||||
|
let parser = TokenParser(tokens: tokens)
|
||||||
|
let (nodes, error) = parser.parse()
|
||||||
|
|
||||||
|
let node = nodes!.first! as NowNode
|
||||||
|
|
||||||
|
XCTAssertTrue(error == nil)
|
||||||
|
XCTAssertEqual(nodes!.count, 1)
|
||||||
|
XCTAssertEqual(node.format.variable, "\"yyyy-MM-dd 'at' HH:mm\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testParseNowWithFormat() {
|
||||||
|
let tokens = [ Token.Block(value: "now \"HH:mm\"") ]
|
||||||
|
let parser = TokenParser(tokens: tokens)
|
||||||
|
let (nodes, error) = parser.parse()
|
||||||
|
|
||||||
|
let node = nodes!.first! as NowNode
|
||||||
|
|
||||||
|
XCTAssertTrue(error == nil)
|
||||||
|
XCTAssertEqual(nodes!.count, 1)
|
||||||
|
XCTAssertEqual(node.format.variable, "\"HH:mm\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Rendering
|
||||||
|
|
||||||
|
func testRenderNowNode() {
|
||||||
|
let node = NowNode(format: Variable("\"yyyy-MM-dd\""))
|
||||||
|
let result = node.render(context)
|
||||||
|
|
||||||
|
let formatter = NSDateFormatter()
|
||||||
|
formatter.dateFormat = "yyyy-MM-dd"
|
||||||
|
let date = formatter.stringFromDate(NSDate())
|
||||||
|
|
||||||
|
XCTAssertEqual(result.0!, date)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user