17 Commits

Author SHA1 Message Date
Kyle Fuller
4ffc888ba4 Release 0.6.0-beta.1 2016-04-04 22:39:19 +02:00
Kyle Fuller
3c21975b97 Merge pull request #61 from GregKaleka/patch-1
Minor grammatical fixes to README.md
2016-03-16 13:53:25 +00:00
Greg Kaleka
df9065f5a8 Minor grammatical fixes to README.md 2016-03-13 21:30:48 -07:00
Kyle Fuller
05b71736aa Merge pull request #59 from shnhrrsn/namespace-fix
Added namespace to Context
2016-03-09 23:41:43 +00:00
shnhrrsn
aa1399be55 Fixed tests for namespace changes 2016-03-06 00:39:10 -05:00
shnhrrsn
bdc14ab1e1 Added namespace to Context 2016-03-05 23:57:15 -05:00
Kyle Fuller
67d4c52535 [Context] Ensure pop happens when an error is thrown 2016-03-05 00:37:12 +00:00
Kyle Fuller
48026cde2c Split tags into separate files 2016-03-05 00:15:18 +00:00
Kyle Fuller
dc4b965aaa Merge pull request #55 from dtrenz/dtrenz-readme-usage
Added `try` to throwing expressions in example.
2016-02-27 15:46:05 -05:00
Dan Trenz
2190afee0d Added try to throwing expressions in example.
Both `Template(named: "template.stencil")` and `Template(named: "template.stencil")` throw but were not preceded by `try`. This usage example, in it's current form, triggers compiler errors.
2016-02-27 15:20:55 -05:00
Kyle Fuller
9b7e6ba7ed Release 0.5.3 2016-02-26 16:37:08 -05:00
Kyle Fuller
bf0989d329 Merge pull request #54 from kylef/kylef/linux
Make tests pass and run on Linux
2016-02-26 16:34:11 -05:00
Kyle Fuller
affd56ec99 [travis] Test on Linux and OS X 2016-02-26 16:18:00 -05:00
Kyle Fuller
070a82cb2d Change how we normalize values to be linux compatible
Closes #51
2016-02-26 16:16:36 -05:00
Kyle Fuller
3ec009381d Disable the now tag on Linux
NSDate is unavailable
2016-02-11 19:34:46 -05:00
Kyle Fuller
6deb93ac19 Disable NSObject based tests on Linux 2016-02-11 19:25:26 -05:00
Kyle Fuller
b4ba12bbde Use spectre-build for tests 2016-02-08 13:47:52 +00:00
33 changed files with 895 additions and 875 deletions

View File

@@ -1 +1 @@
2.2-dev DEVELOPMENT-SNAPSHOT-2016-01-25-a

11
.travis.yml Normal file
View File

@@ -0,0 +1,11 @@
os:
- osx
- linux
language: generic
sudo: required
dist: trusty
osx_image: xcode7.2
install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)"
script:
- make test

7
Makefile Normal file
View File

@@ -0,0 +1,7 @@
stencil:
@echo "Building Stencil"
@swift build
test: stencil
@echo "Running Tests"
@.build/debug/spectre-build

View File

@@ -1,8 +1,11 @@
import PackageDescription import PackageDescription
let package = Package( let package = Package(
name: "Stencil", name: "Stencil",
dependencies: [ dependencies: [
.Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 6), .Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 6),
] ],
testDependencies: [
.Package(url: "https://github.com/kylef/spectre-build", majorVersion: 0),
]
) )

View File

@@ -1,6 +1,6 @@
# Stencil # Stencil
[![Build Status](http://img.shields.io/circleci/project/kylef/Stencil/master.svg)](https://circleci.com/gh/kylef/Stencil) [![Build Status](https://travis-ci.org/kylef/Stencil.svg?branch=master)](https://travis-ci.org/kylef/Stencil)
Stencil is a simple and powerful template language for Swift. It provides a Stencil is a simple and powerful template language for Swift. It provides a
syntax similar to Django and Mustache. If you're familiar with these, you will syntax similar to Django and Mustache. If you're familiar with these, you will
@@ -25,8 +25,8 @@ let context = Context(dictionary: [
]) ])
do { do {
let template = Template(named: "template.stencil") let template = try Template(named: "template.stencil")
let rendered = template.render(context) let rendered = try template.render(context)
print(rendered) print(rendered)
} catch { } catch {
print("Failed to render template \(error)") print("Failed to render template \(error)")
@@ -73,8 +73,8 @@ following lookup:
For example, if `people` was an array: For example, if `people` was an array:
```html+django ```html+django
There are {{ people.count }} people, {{ people.first }} is first person. There are {{ people.count }} people. {{ people.first }} is the first person,
Followed by {{ people.1 }}. followed by {{ people.1 }}.
``` ```
#### Filters #### Filters
@@ -142,7 +142,7 @@ A for loop allows you to iterate over an array found by variable lookup.
{% for item in items %} {% for item in items %}
{{ item }} {{ item }}
{% empty %} {% empty %}
There we're no items. There were no items.
{% endfor %} {% endfor %}
``` ```

View File

@@ -1,15 +1,17 @@
/// A container for template variables. /// A container for template variables.
public class Context { public class Context {
var dictionaries:[[String: Any]] var dictionaries: [[String: Any]]
let namespace: Namespace
/// Initialise a Context with a dictionary /// Initialise a Context with an optional dictionary and optional namespace
public init(dictionary:[String: Any]) { public init(dictionary: [String: Any]? = nil, namespace: Namespace = Namespace()) {
dictionaries = [dictionary] if let dictionary = dictionary {
} dictionaries = [dictionary]
} else {
dictionaries = []
}
/// Initialise an empty Context self.namespace = namespace
public init() {
dictionaries = []
} }
public subscript(key: String) -> Any? { public subscript(key: String) -> Any? {
@@ -35,20 +37,19 @@ public class Context {
} }
/// Push a new level into the Context /// Push a new level into the Context
public func push(dictionary: [String: Any]? = nil) { private func push(dictionary: [String: Any]? = nil) {
dictionaries.append(dictionary ?? [:]) dictionaries.append(dictionary ?? [:])
} }
/// Pop the last level off of the Context /// Pop the last level off of the Context
public func pop() -> [String: Any]? { private func pop() -> [String: Any]? {
return dictionaries.popLast() return dictionaries.popLast()
} }
/// Push a new level onto the context for the duration of the execution of the given closure /// Push a new level onto the context for the duration of the execution of the given closure
public func push<Result>(dictionary: [String: Any]? = nil, @noescape closure: (() throws -> Result)) rethrows -> Result { public func push<Result>(dictionary: [String: Any]? = nil, @noescape closure: (() throws -> Result)) rethrows -> Result {
push(dictionary) push(dictionary)
let result = try closure() defer { pop() }
pop() return try closure()
return result
} }
} }

62
Sources/ForTag.swift Normal file
View File

@@ -0,0 +1,62 @@
public class ForNode : NodeType {
let variable:Variable
let loopVariable:String
let nodes:[NodeType]
let emptyNodes: [NodeType]
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components()
guard components.count == 4 && components[2] == "in" else {
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
}
let loopVariable = components[1]
let variable = components[3]
var emptyNodes = [NodeType]()
let forNodes = try parser.parse(until(["endfor", "empty"]))
guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endfor` was not found.")
}
if token.contents == "empty" {
emptyNodes = try parser.parse(until(["endfor"]))
parser.nextToken()
}
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
}
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
self.variable = Variable(variable)
self.loopVariable = loopVariable
self.nodes = nodes
self.emptyNodes = emptyNodes
}
public func render(context: Context) throws -> String {
let values = try variable.resolve(context)
if let values = values as? [Any] where values.count > 0 {
let count = values.count
return try values.enumerate().map { index, item in
let forContext: [String: Any] = [
"first": index == 0,
"last": index == (count - 1),
"counter": index + 1,
]
return try context.push([loopVariable: item, "forloop": forContext]) {
try renderNodes(nodes, context)
}
}.joinWithSeparator("")
}
return try context.push {
try renderNodes(emptyNodes, context)
}
}
}

78
Sources/IfTag.swift Normal file
View File

@@ -0,0 +1,78 @@
public class IfNode : NodeType {
public let variable:Variable
public let trueNodes:[NodeType]
public let falseNodes:[NodeType]
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components()
guard components.count == 2 else {
throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")
}
let variable = components[1]
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()
trueNodes = try parser.parse(until(["endif", "else"]))
guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endif` was not found.")
}
if token.contents == "else" {
falseNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
}
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components()
guard components.count == 2 else {
throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")
}
let variable = components[1]
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()
falseNodes = try parser.parse(until(["endif", "else"]))
guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endif` was not found.")
}
if token.contents == "else" {
trueNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
}
public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {
self.variable = Variable(variable)
self.trueNodes = trueNodes
self.falseNodes = falseNodes
}
public func render(context: Context) throws -> String {
let result = try variable.resolve(context)
var truthy = false
if let result = result as? [Any] {
truthy = !result.isEmpty
} else if let result = result as? [String:Any] {
truthy = !result.isEmpty
} else if result != nil {
truthy = true
}
return try context.push {
if truthy {
return try renderNodes(trueNodes, context)
} else {
return try renderNodes(falseNodes, context)
}
}
}
}

View File

@@ -1,28 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2014 Cocode. All rights reserved.</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>

View File

@@ -74,10 +74,9 @@ class ExtendsNode : NodeType {
} }
let blockContext = BlockContext(blocks: blocks) let blockContext = BlockContext(blocks: blocks)
context.push([BlockContext.contextKey: blockContext]) return try context.push([BlockContext.contextKey: blockContext]) {
let result = try template.render(context) return try template.render(context)
context.pop() }
return result
} }
} }

View File

@@ -13,7 +13,9 @@ public class Namespace {
registerTag("for", parser: ForNode.parse) registerTag("for", parser: ForNode.parse)
registerTag("if", parser: IfNode.parse) registerTag("if", parser: IfNode.parse)
registerTag("ifnot", parser: IfNode.parse_ifnot) registerTag("ifnot", parser: IfNode.parse_ifnot)
#if !os(Linux)
registerTag("now", parser: NowNode.parse) registerTag("now", parser: NowNode.parse)
#endif
registerTag("include", parser: IncludeNode.parse) registerTag("include", parser: IncludeNode.parse)
registerTag("extends", parser: ExtendsNode.parse) registerTag("extends", parser: ExtendsNode.parse)
registerTag("block", parser: BlockNode.parse) registerTag("block", parser: BlockNode.parse)

View File

@@ -1,5 +1,6 @@
import Foundation import Foundation
public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertible { public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertible {
public let description:String public let description:String
@@ -8,15 +9,18 @@ public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertibl
} }
} }
public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool { public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
return lhs.description == rhs.description return lhs.description == rhs.description
} }
public protocol NodeType { public protocol NodeType {
/// Render the node in the given context /// Render the node in the given context
func render(context:Context) throws -> String func render(context:Context) throws -> String
} }
/// Render the collection of nodes in the given context /// Render the collection of nodes in the given context
public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String { public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String {
return try nodes.map { try $0.render(context) }.joinWithSeparator("") return try nodes.map { try $0.render(context) }.joinWithSeparator("")
@@ -34,6 +38,7 @@ public class SimpleNode : NodeType {
} }
} }
public class TextNode : NodeType { public class TextNode : NodeType {
public let text:String public let text:String
@@ -46,10 +51,12 @@ public class TextNode : NodeType {
} }
} }
public protocol Resolvable { public protocol Resolvable {
func resolve(context: Context) throws -> Any? func resolve(context: Context) throws -> Any?
} }
public class VariableNode : NodeType { public class VariableNode : NodeType {
public let variable: Resolvable public let variable: Resolvable
@@ -75,187 +82,3 @@ public class VariableNode : NodeType {
return "" return ""
} }
} }
public class NowNode : NodeType {
public let format:Variable
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
var format:Variable?
let components = token.components()
guard components.count <= 2 else {
throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")
}
if components.count == 2 {
format = Variable(components[1])
}
return NowNode(format:format)
}
public init(format:Variable?) {
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
}
public func render(context: Context) throws -> String {
let date = NSDate()
let format = try 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 {
return ""
}
return formatter!.stringFromDate(date)
}
}
public class ForNode : NodeType {
let variable:Variable
let loopVariable:String
let nodes:[NodeType]
let emptyNodes: [NodeType]
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components()
guard components.count == 4 && components[2] == "in" else {
throw TemplateSyntaxError("'for' statements should use the following 'for x in y' `\(token.contents)`.")
}
let loopVariable = components[1]
let variable = components[3]
var emptyNodes = [NodeType]()
let forNodes = try parser.parse(until(["endfor", "empty"]))
guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endfor` was not found.")
}
if token.contents == "empty" {
emptyNodes = try parser.parse(until(["endfor"]))
parser.nextToken()
}
return ForNode(variable: variable, loopVariable: loopVariable, nodes: forNodes, emptyNodes:emptyNodes)
}
public init(variable:String, loopVariable:String, nodes:[NodeType], emptyNodes:[NodeType]) {
self.variable = Variable(variable)
self.loopVariable = loopVariable
self.nodes = nodes
self.emptyNodes = emptyNodes
}
public func render(context: Context) throws -> String {
let values = try variable.resolve(context)
if let values = values as? [Any] where values.count > 0 {
let count = values.count
return try values.enumerate().map { index, item in
let forContext: [String: Any] = [
"first": index == 0,
"last": index == (count - 1),
"counter": index + 1,
]
return try context.push([loopVariable: item, "forloop": forContext]) {
try renderNodes(nodes, context)
}
}.joinWithSeparator("")
}
return try context.push {
try renderNodes(emptyNodes, context)
}
}
}
public class IfNode : NodeType {
public let variable:Variable
public let trueNodes:[NodeType]
public let falseNodes:[NodeType]
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components()
guard components.count == 2 else {
throw TemplateSyntaxError("'if' statements should use the following 'if condition' `\(token.contents)`.")
}
let variable = components[1]
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()
trueNodes = try parser.parse(until(["endif", "else"]))
guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endif` was not found.")
}
if token.contents == "else" {
falseNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
}
public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType {
let components = token.components()
guard components.count == 2 else {
throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.")
}
let variable = components[1]
var trueNodes = [NodeType]()
var falseNodes = [NodeType]()
falseNodes = try parser.parse(until(["endif", "else"]))
guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endif` was not found.")
}
if token.contents == "else" {
trueNodes = try parser.parse(until(["endif"]))
parser.nextToken()
}
return IfNode(variable: variable, trueNodes: trueNodes, falseNodes: falseNodes)
}
public init(variable:String, trueNodes:[NodeType], falseNodes:[NodeType]) {
self.variable = Variable(variable)
self.trueNodes = trueNodes
self.falseNodes = falseNodes
}
public func render(context: Context) throws -> String {
let result = try variable.resolve(context)
var truthy = false
if let result = result as? [Any] {
truthy = !result.isEmpty
} else if let result = result as? [String:Any] {
truthy = !result.isEmpty
} else if result != nil {
truthy = true
}
context.push()
let output:String
if truthy {
output = try renderNodes(trueNodes, context)
} else {
output = try renderNodes(falseNodes, context)
}
context.pop()
return output
}
}

43
Sources/NowTag.swift Normal file
View File

@@ -0,0 +1,43 @@
#if !os(Linux)
import Foundation
public class NowNode : NodeType {
public let format:Variable
public class func parse(parser:TokenParser, token:Token) throws -> NodeType {
var format:Variable?
let components = token.components()
guard components.count <= 2 else {
throw TemplateSyntaxError("'now' tags may only have one argument: the format string `\(token.contents)`.")
}
if components.count == 2 {
format = Variable(components[1])
}
return NowNode(format:format)
}
public init(format:Variable?) {
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
}
public func render(context: Context) throws -> String {
let date = NSDate()
let format = try 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 {
return ""
}
return formatter!.stringFromDate(date)
}
}
#endif

View File

@@ -36,9 +36,10 @@ public class Template {
} }
/// Render the given template /// Render the given template
public func render(context: Context? = nil, namespace: Namespace? = nil) throws -> String { public func render(context: Context? = nil) throws -> String {
let parser = TokenParser(tokens: tokens, namespace: namespace ?? Namespace()) let context = context ?? Context()
let parser = TokenParser(tokens: tokens, namespace: context.namespace)
let nodes = try parser.parse() let nodes = try parser.parse()
return try renderNodes(nodes, context ?? Context()) return try renderNodes(nodes, context)
} }
} }

View File

@@ -56,11 +56,13 @@ public struct Variable : Equatable, Resolvable {
} }
for bit in lookup() { for bit in lookup() {
current = normalize(current)
if let context = current as? Context { if let context = current as? Context {
current = context[bit] current = context[bit]
} else if let dictionary = resolveDictionary(current) { } else if let dictionary = current as? [String: Any] {
current = dictionary[bit] current = dictionary[bit]
} else if let array = resolveArray(current) { } else if let array = current as? [Any] {
if let index = Int(bit) { if let index = Int(bit) {
current = array[index] current = array[index]
} else if bit == "first" { } else if bit == "first" {
@@ -90,50 +92,42 @@ public func ==(lhs: Variable, rhs: Variable) -> Bool {
} }
func resolveDictionary(current: Any?) -> [String: Any]? {
switch current {
case let dictionary as [String: Any]:
return dictionary
case let dictionary as [String: AnyObject]:
var result: [String: Any] = [:]
for (k, v) in dictionary {
result[k] = v as Any
}
return result
case let dictionary as NSDictionary:
var result: [String: Any] = [:]
for (k, v) in dictionary {
if let k = k as? String {
result[k] = v as Any
}
}
return result
default:
return nil
}
}
func resolveArray(current: Any?) -> [Any]? {
switch current {
case let array as [Any]:
return array
case let array as [AnyObject]:
return array.map { $0 as Any }
case let array as NSArray:
return array.map { $0 as Any }
default:
return nil
}
}
func normalize(current: Any?) -> Any? { func normalize(current: Any?) -> Any? {
if let array = resolveArray(current) { if let current = current as? Normalizable {
return array return current.normalize()
}
if let dictionary = resolveDictionary(current) {
return dictionary
} }
return current return current
} }
protocol Normalizable {
func normalize() -> Any?
}
extension Array : Normalizable {
func normalize() -> Any? {
return map { $0 as Any }
}
}
extension NSArray : Normalizable {
func normalize() -> Any? {
return map { $0 as Any }
}
}
extension Dictionary : Normalizable {
func normalize() -> Any? {
var dictionary: [String: Any] = [:]
for (key, value) in self {
if let key = key as? String {
dictionary[key] = Stencil.normalize(value)
} else if let key = key as? CustomStringConvertible {
dictionary[key.description] = Stencil.normalize(value)
}
}
return dictionary
}
}

View File

@@ -1,6 +1,6 @@
{ {
"name": "Stencil", "name": "Stencil",
"version": "0.5.2", "version": "0.6.0-beta.1",
"summary": "Stencil is a simple and powerful template language for Swift.", "summary": "Stencil is a simple and powerful template language for Swift.",
"homepage": "https://github.com/kylef/Stencil", "homepage": "https://github.com/kylef/Stencil",
"license": { "license": {
@@ -13,7 +13,7 @@
"social_media_url": "http://twitter.com/kylefuller", "social_media_url": "http://twitter.com/kylefuller",
"source": { "source": {
"git": "https://github.com/kylef/Stencil.git", "git": "https://github.com/kylef/Stencil.git",
"tag": "0.5.2" "tag": "0.6.0-beta.1"
}, },
"source_files": [ "source_files": [
"Sources/*.swift" "Sources/*.swift"
@@ -25,16 +25,5 @@
"requires_arc": true, "requires_arc": true,
"dependencies": { "dependencies": {
"PathKit": [ "~> 0.6.0" ] "PathKit": [ "~> 0.6.0" ]
},
"test_specification": {
"source_files": [
"Tests/*.swift",
"Tests/TemplateLoader/*.swift",
"Tests/Nodes/*.swift"
],
"dependencies": {
"Spectre": [ "~> 0.5.0" ],
"PathKit": [ "~> 0.6.0" ]
}
} }
} }

View File

@@ -2,64 +2,61 @@ import Spectre
import Stencil import Stencil
describe("Context") { func testContext() {
var context: Context! describe("Context") {
var context: Context!
$0.before { $0.before {
context = Context(dictionary: ["name": "Kyle"]) context = Context(dictionary: ["name": "Kyle"])
} }
$0.it("allows you to get a value via subscripting") { $0.it("allows you to get a value via subscripting") {
try expect(context["name"] as? String) == "Kyle" try expect(context["name"] as? String) == "Kyle"
} }
$0.it("allows you to set a value via subscripting") { $0.it("allows you to set a value via subscripting") {
context["name"] = "Katie" context["name"] = "Katie"
try expect(context["name"] as? String) == "Katie"
}
$0.it("allows you to remove a value via subscripting") {
context["name"] = nil
try expect(context["name"]).to.beNil()
}
$0.it("allows you to retrieve a value from a parent") {
context.push()
try expect(context["name"] as? String) == "Kyle"
}
$0.it("allows you to override a parent's value") {
context.push()
context["name"] = "Katie"
try expect(context["name"] as? String) == "Katie"
}
$0.it("allows you to pop to restore previous state") {
context.push()
context["name"] = "Katie"
context.pop()
try expect(context["name"] as? String) == "Kyle"
}
$0.it("allows you to push a dictionary onto the stack") {
context.push(["name": "Katie"])
try expect(context["name"] as? String) == "Katie"
}
$0.it("allows you to push a dictionary and run a closure then restoring previous state") {
var didRun = false
try context.push(["name": "Katie"]) {
didRun = true
try expect(context["name"] as? String) == "Katie" try expect(context["name"] as? String) == "Katie"
} }
try expect(didRun).to.beTrue() $0.it("allows you to remove a value via subscripting") {
try expect(context["name"] as? String) == "Kyle" context["name"] = nil
try expect(context["name"]).to.beNil()
}
$0.it("allows you to retrieve a value from a parent") {
try context.push {
try expect(context["name"] as? String) == "Kyle"
}
}
$0.it("allows you to override a parent's value") {
try context.push {
context["name"] = "Katie"
try expect(context["name"] as? String) == "Katie"
}
}
$0.it("allows you to pop to restore previous state") {
context.push {
context["name"] = "Katie"
}
try expect(context["name"] as? String) == "Kyle"
}
$0.it("allows you to push a dictionary and run a closure then restoring previous state") {
var didRun = false
try context.push(["name": "Katie"]) {
didRun = true
try expect(context["name"] as? String) == "Katie"
}
try expect(didRun).to.beTrue()
try expect(context["name"] as? String) == "Kyle"
}
} }
} }

View File

@@ -2,68 +2,69 @@ import Spectre
import Stencil import Stencil
describe("template filters") { func testFilter() {
let context = Context(dictionary: ["name": "Kyle"]) describe("template filters") {
let context: [String: Any] = ["name": "Kyle"]
$0.it("allows you to register a custom filter") { $0.it("allows you to register a custom filter") {
let template = Template(templateString: "{{ name|repeat }}") let template = Template(templateString: "{{ name|repeat }}")
let namespace = Namespace() let namespace = Namespace()
namespace.registerFilter("repeat") { value in namespace.registerFilter("repeat") { value in
if let value = value as? String { if let value = value as? String {
return "\(value) \(value)" return "\(value) \(value)"
}
return nil
} }
return nil let result = try template.render(Context(dictionary: context, namespace: namespace))
try expect(result) == "Kyle Kyle"
} }
let result = try template.render(context, namespace: namespace) $0.it("allows you to register a custom which throws") {
try expect(result) == "Kyle Kyle" let template = Template(templateString: "{{ name|repeat }}")
} let namespace = Namespace()
namespace.registerFilter("repeat") { value in
throw TemplateSyntaxError("No Repeat")
}
$0.it("allows you to register a custom filter") { try expect(try template.render(Context(dictionary: context, namespace: namespace))).toThrow(TemplateSyntaxError("No Repeat"))
let template = Template(templateString: "{{ name|repeat }}")
let namespace = Namespace()
namespace.registerFilter("repeat") { value in
throw TemplateSyntaxError("No Repeat")
} }
try expect(try template.render(context, namespace: namespace)).toThrow(TemplateSyntaxError("No Repeat")) $0.it("allows whitespace in expression") {
let template = Template(templateString: "{{ name | uppercase }}")
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE"
}
} }
$0.it("allows whitespace in expression") {
let template = Template(templateString: "{{ name | uppercase }}") describe("capitalize filter") {
let result = try template.render(Context(dictionary: ["name": "kyle"])) let template = Template(templateString: "{{ name|capitalize }}")
try expect(result) == "KYLE"
} $0.it("capitalizes a string") {
} let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "Kyle"
}
describe("capitalize filter") { }
let template = Template(templateString: "{{ name|capitalize }}")
$0.it("capitalizes a string") { describe("uppercase filter") {
let result = try template.render(Context(dictionary: ["name": "kyle"])) let template = Template(templateString: "{{ name|uppercase }}")
try expect(result) == "Kyle"
} $0.it("transforms a string to be uppercase") {
} let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE"
}
describe("uppercase filter") { }
let template = Template(templateString: "{{ name|uppercase }}")
describe("lowercase filter") {
$0.it("transforms a string to be uppercase") { let template = Template(templateString: "{{ name|lowercase }}")
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE" $0.it("transforms a string to be lowercase") {
} let result = try template.render(Context(dictionary: ["name": "Kyle"]))
} try expect(result) == "kyle"
}
describe("lowercase filter") {
let template = Template(templateString: "{{ name|lowercase }}")
$0.it("transforms a string to be lowercase") {
let result = try template.render(Context(dictionary: ["name": "Kyle"]))
try expect(result) == "kyle"
} }
} }

View File

@@ -2,47 +2,49 @@ import Spectre
import Stencil import Stencil
describe("Lexer") { func testLexer() {
$0.it("can tokenize text") { describe("Lexer") {
let lexer = Lexer(templateString: "Hello World") $0.it("can tokenize text") {
let tokens = lexer.tokenize() let lexer = Lexer(templateString: "Hello World")
let tokens = lexer.tokenize()
try expect(tokens.count) == 1 try expect(tokens.count) == 1
try expect(tokens.first) == Token.Text(value: "Hello World") try expect(tokens.first) == Token.Text(value: "Hello World")
} }
$0.it("can tokenize a comment") { $0.it("can tokenize a comment") {
let lexer = Lexer(templateString: "{# Comment #}") let lexer = Lexer(templateString: "{# Comment #}")
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == (1) try expect(tokens.count) == (1)
try expect(tokens.first) == Token.Comment(value: "Comment") try expect(tokens.first) == Token.Comment(value: "Comment")
} }
$0.it("can tokenize a variable") { $0.it("can tokenize a variable") {
let lexer = Lexer(templateString: "{{ Variable }}") let lexer = Lexer(templateString: "{{ Variable }}")
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 1 try expect(tokens.count) == 1
try expect(tokens.first) == Token.Variable(value: "Variable") try expect(tokens.first) == Token.Variable(value: "Variable")
} }
$0.it("can tokenize a mixture of content") { $0.it("can tokenize a mixture of content") {
let lexer = Lexer(templateString: "My name is {{ name }}.") let lexer = Lexer(templateString: "My name is {{ name }}.")
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 3 try expect(tokens.count) == 3
try expect(tokens[0]) == Token.Text(value: "My name is ") try expect(tokens[0]) == Token.Text(value: "My name is ")
try expect(tokens[1]) == Token.Variable(value: "name") try expect(tokens[1]) == Token.Variable(value: "name")
try expect(tokens[2]) == Token.Text(value: ".") try expect(tokens[2]) == Token.Text(value: ".")
} }
$0.it("can tokenize two variables without being greedy") { $0.it("can tokenize two variables without being greedy") {
let lexer = Lexer(templateString: "{{ thing }}{{ name }}") let lexer = Lexer(templateString: "{{ thing }}{{ name }}")
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 2 try expect(tokens.count) == 2
try expect(tokens[0]) == Token.Variable(value: "thing") try expect(tokens[0]) == Token.Variable(value: "thing")
try expect(tokens[1]) == Token.Variable(value: "name") try expect(tokens[1]) == Token.Variable(value: "name")
}
} }
} }

View File

@@ -3,60 +3,64 @@ import Stencil
import Foundation import Foundation
describe("ForNode") { func testForNode() {
let context = Context(dictionary: [ describe("ForNode") {
"items": [1, 2, 3], let context = Context(dictionary: [
"emptyItems": [Int](), "items": [1, 2, 3],
]) "emptyItems": [Int](),
])
$0.it("renders the given nodes for each item") { $0.it("renders the given nodes for each item") {
let nodes: [NodeType] = [VariableNode(variable: "item")] let nodes: [NodeType] = [VariableNode(variable: "item")]
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: []) let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "123" try expect(try node.render(context)) == "123"
} }
$0.it("renders the given empty nodes when no items found item") { $0.it("renders the given empty nodes when no items found item") {
let nodes: [NodeType] = [VariableNode(variable: "item")] let nodes: [NodeType] = [VariableNode(variable: "item")]
let emptyNodes: [NodeType] = [TextNode(text: "empty")] let emptyNodes: [NodeType] = [TextNode(text: "empty")]
let node = ForNode(variable: "emptyItems", loopVariable: "item", nodes: nodes, emptyNodes: emptyNodes) let node = ForNode(variable: "emptyItems", loopVariable: "item", nodes: nodes, emptyNodes: emptyNodes)
try expect(try node.render(context)) == "empty" try expect(try node.render(context)) == "empty"
} }
$0.it("renders a context variable of type Array<Any>") { $0.it("renders a context variable of type Array<Any>") {
let any_context = Context(dictionary: [ let any_context = Context(dictionary: [
"items": ([1, 2, 3] as [Any]) "items": ([1, 2, 3] as [Any])
]) ])
let nodes: [NodeType] = [VariableNode(variable: "item")] let nodes: [NodeType] = [VariableNode(variable: "item")]
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: []) let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
try expect(try node.render(any_context)) == "123" try expect(try node.render(any_context)) == "123"
} }
$0.it("renders a context variable of type NSArray") { #if os(OSX)
let nsarray_context = Context(dictionary: [ $0.it("renders a context variable of type NSArray") {
let nsarray_context = Context(dictionary: [
"items": NSArray(array: [1, 2, 3]) "items": NSArray(array: [1, 2, 3])
]) ])
let nodes: [NodeType] = [VariableNode(variable: "item")] let nodes: [NodeType] = [VariableNode(variable: "item")]
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: []) let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
try expect(try node.render(nsarray_context)) == "123" try expect(try node.render(nsarray_context)) == "123"
} }
#endif
$0.it("renders the given nodes while providing if the item is first in the context") { $0.it("renders the given nodes while providing if the item is first in the context") {
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.first")] let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.first")]
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: []) let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "1true2false3false" try expect(try node.render(context)) == "1true2false3false"
} }
$0.it("renders the given nodes while providing if the item is last in the context") { $0.it("renders the given nodes while providing if the item is last in the context") {
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.last")] let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.last")]
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: []) let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "1false2false3true" try expect(try node.render(context)) == "1false2false3true"
} }
$0.it("renders the given nodes while providing item counter") { $0.it("renders the given nodes while providing item counter") {
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")] let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.counter")]
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: []) let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "112233" try expect(try node.render(context)) == "112233"
}
} }
} }

View File

@@ -1,113 +1,116 @@
import Spectre import Spectre
import Stencil import Stencil
describe("IfNode") {
$0.describe("parsing") {
$0.it("can parse an if block") {
let tokens = [
Token.Block(value: "if value"),
Token.Text(value: "true"),
Token.Block(value: "else"),
Token.Text(value: "false"),
Token.Block(value: "endif")
]
let parser = TokenParser(tokens: tokens, namespace: Namespace()) func testIfNode() {
let nodes = try parser.parse() describe("IfNode") {
let node = nodes.first as? IfNode $0.describe("parsing") {
let trueNode = node?.trueNodes.first as? TextNode $0.it("can parse an if block") {
let falseNode = node?.falseNodes.first as? TextNode let tokens = [
Token.Block(value: "if value"),
Token.Text(value: "true"),
Token.Block(value: "else"),
Token.Text(value: "false"),
Token.Block(value: "endif")
]
try expect(nodes.count) == 1 let parser = TokenParser(tokens: tokens, namespace: Namespace())
try expect(node?.variable.variable) == "value" let nodes = try parser.parse()
try expect(node?.trueNodes.count) == 1 let node = nodes.first as? IfNode
try expect(trueNode?.text) == "true" let trueNode = node?.trueNodes.first as? TextNode
try expect(node?.falseNodes.count) == 1 let falseNode = node?.falseNodes.first as? TextNode
try expect(falseNode?.text) == "false"
try expect(nodes.count) == 1
try expect(node?.variable.variable) == "value"
try expect(node?.trueNodes.count) == 1
try expect(trueNode?.text) == "true"
try expect(node?.falseNodes.count) == 1
try expect(falseNode?.text) == "false"
}
$0.it("can parse an ifnot block") {
let tokens = [
Token.Block(value: "ifnot value"),
Token.Text(value: "false"),
Token.Block(value: "else"),
Token.Text(value: "true"),
Token.Block(value: "endif")
]
let parser = TokenParser(tokens: tokens, namespace: Namespace())
let nodes = try parser.parse()
let node = nodes.first as? IfNode
let trueNode = node?.trueNodes.first as? TextNode
let falseNode = node?.falseNodes.first as? TextNode
try expect(nodes.count) == 1
try expect(node?.variable.variable) == "value"
try expect(node?.trueNodes.count) == 1
try expect(trueNode?.text) == "true"
try expect(node?.falseNodes.count) == 1
try expect(falseNode?.text) == "false"
}
$0.it("throws an error when parsing an if block without an endif") {
let tokens = [
Token.Block(value: "if value"),
]
let parser = TokenParser(tokens: tokens, namespace: Namespace())
let error = TemplateSyntaxError("`endif` was not found.")
try expect(try parser.parse()).toThrow(error)
}
$0.it("throws an error when parsing an ifnot without an endif") {
let tokens = [
Token.Block(value: "ifnot value"),
]
let parser = TokenParser(tokens: tokens, namespace: Namespace())
let error = TemplateSyntaxError("`endif` was not found.")
try expect(try parser.parse()).toThrow(error)
}
} }
$0.it("can parse an ifnot block") { $0.describe("rendering") {
let tokens = [ let context = Context(dictionary: ["items": true])
Token.Block(value: "ifnot value"),
Token.Text(value: "false"),
Token.Block(value: "else"),
Token.Text(value: "true"),
Token.Block(value: "endif")
]
let parser = TokenParser(tokens: tokens, namespace: Namespace()) $0.it("renders the truth when expression evaluates to true") {
let nodes = try parser.parse() let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
let node = nodes.first as? IfNode try expect(try node.render(context)) == "true"
let trueNode = node?.trueNodes.first as? TextNode }
let falseNode = node?.falseNodes.first as? TextNode
try expect(nodes.count) == 1 $0.it("renders the false when expression evaluates to false") {
try expect(node?.variable.variable) == "value" let node = IfNode(variable: "unknown", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(node?.trueNodes.count) == 1 try expect(try node.render(context)) == "false"
try expect(trueNode?.text) == "true" }
try expect(node?.falseNodes.count) == 1
try expect(falseNode?.text) == "false"
}
$0.it("throws an error when parsing an if block without an endif") { $0.it("renders the truth when array expression is not empty") {
let tokens = [ let items: [[String: Any]] = [["key":"key1","value":42],["key":"key2","value":1337]]
Token.Block(value: "if value"), let arrayContext = Context(dictionary: ["items": [items]])
] let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(arrayContext)) == "true"
}
let parser = TokenParser(tokens: tokens, namespace: Namespace()) $0.it("renders the false when array expression is empty") {
let error = TemplateSyntaxError("`endif` was not found.") let emptyItems = [[String: Any]]()
try expect(try parser.parse()).toThrow(error) let arrayContext = Context(dictionary: ["items": emptyItems])
} let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(arrayContext)) == "false"
}
$0.it("throws an error when parsing an ifnot without an endif") { $0.it("renders the false when dictionary expression is empty") {
let tokens = [ let emptyItems = [String:Any]()
Token.Block(value: "ifnot value"), let arrayContext = Context(dictionary: ["items": emptyItems])
] let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(arrayContext)) == "false"
}
let parser = TokenParser(tokens: tokens, namespace: Namespace()) $0.it("renders the false when Array<Any> variable is empty") {
let error = TemplateSyntaxError("`endif` was not found.") let arrayContext = Context(dictionary: ["items": ([] as [Any])])
try expect(try parser.parse()).toThrow(error) let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
} try expect(try node.render(arrayContext)) == "false"
} }
$0.describe("rendering") {
let context = Context(dictionary: ["items": true])
$0.it("renders the truth when expression evaluates to true") {
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(context)) == "true"
}
$0.it("renders the false when expression evaluates to false") {
let node = IfNode(variable: "unknown", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(context)) == "false"
}
$0.it("renders the truth when array expression is not empty") {
let items: Array<[String:AnyObject]> = [["key":"key1","value":42],["key":"key2","value":1337]]
let arrayContext = Context(dictionary: ["items": [items]])
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(arrayContext)) == "true"
}
$0.it("renders the false when array expression is empty") {
let emptyItems = Array<[String:AnyObject]>()
let arrayContext = Context(dictionary: ["items": emptyItems])
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(arrayContext)) == "false"
}
$0.it("renders the false when dictionary expression is empty") {
let emptyItems = [String:AnyObject]()
let arrayContext = Context(dictionary: ["items": emptyItems])
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(arrayContext)) == "false"
}
$0.it("renders the false when Array<Any> variable is empty") {
let arrayContext = Context(dictionary: ["items": ([] as [Any])])
let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")])
try expect(try node.render(arrayContext)) == "false"
} }
} }
} }

View File

@@ -9,50 +9,52 @@ class ErrorNode : NodeType {
} }
describe("Node") { func testNode() {
let context = Context(dictionary: [ describe("Node") {
"name": "Kyle", let context = Context(dictionary: [
"age": 27, "name": "Kyle",
"items": [1, 2, 3], "age": 27,
]) "items": [1, 2, 3],
])
$0.describe("TextNode") { $0.describe("TextNode") {
$0.it("renders the given text") { $0.it("renders the given text") {
let node = TextNode(text: "Hello World") let node = TextNode(text: "Hello World")
try expect(try node.render(context)) == "Hello World" try expect(try node.render(context)) == "Hello World"
} }
}
$0.describe("VariableNode") {
$0.it("resolves and renders the variable") {
let node = VariableNode(variable: Variable("name"))
try expect(try node.render(context)) == "Kyle"
} }
$0.it("resolves and renders a non string variable") { $0.describe("VariableNode") {
let node = VariableNode(variable: Variable("age")) $0.it("resolves and renders the variable") {
try expect(try node.render(context)) == "27" let node = VariableNode(variable: Variable("name"))
} try expect(try node.render(context)) == "Kyle"
} }
$0.describe("rendering nodes") { $0.it("resolves and renders a non string variable") {
$0.it("renders the nodes") { let node = VariableNode(variable: Variable("age"))
let nodes: [NodeType] = [ try expect(try node.render(context)) == "27"
TextNode(text:"Hello "), }
VariableNode(variable: "name"),
]
try expect(try renderNodes(nodes, context)) == "Hello Kyle"
} }
$0.it("correctly throws a nodes failure") { $0.describe("rendering nodes") {
let nodes: [NodeType] = [ $0.it("renders the nodes") {
TextNode(text:"Hello "), let nodes: [NodeType] = [
VariableNode(variable: "name"), TextNode(text:"Hello "),
ErrorNode(), VariableNode(variable: "name"),
] ]
try expect(try renderNodes(nodes, context)).toThrow(TemplateSyntaxError("Custom Error")) try expect(try renderNodes(nodes, context)) == "Hello Kyle"
}
$0.it("correctly throws a nodes failure") {
let nodes: [NodeType] = [
TextNode(text:"Hello "),
VariableNode(variable: "name"),
ErrorNode(),
]
try expect(try renderNodes(nodes, context)).toThrow(TemplateSyntaxError("Custom Error"))
}
} }
} }
} }

View File

@@ -3,37 +3,41 @@ import Spectre
import Stencil import Stencil
describe("NowNode") { func testNowNode() {
$0.describe("parsing") { #if !os(Linux)
$0.it("parses default format without any now arguments") { describe("NowNode") {
let tokens = [ Token.Block(value: "now") ] $0.describe("parsing") {
let parser = TokenParser(tokens: tokens, namespace: Namespace()) $0.it("parses default format without any now arguments") {
let tokens = [ Token.Block(value: "now") ]
let parser = TokenParser(tokens: tokens, namespace: Namespace())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? NowNode let node = nodes.first as? NowNode
try expect(nodes.count) == 1 try expect(nodes.count) == 1
try expect(node?.format.variable) == "\"yyyy-MM-dd 'at' HH:mm\"" try expect(node?.format.variable) == "\"yyyy-MM-dd 'at' HH:mm\""
}
$0.it("parses now with a format") {
let tokens = [ Token.Block(value: "now \"HH:mm\"") ]
let parser = TokenParser(tokens: tokens, namespace: Namespace())
let nodes = try parser.parse()
let node = nodes.first as? NowNode
try expect(nodes.count) == 1
try expect(node?.format.variable) == "\"HH:mm\""
}
} }
$0.it("parses now with a format") { $0.describe("rendering") {
let tokens = [ Token.Block(value: "now \"HH:mm\"") ] $0.it("renders the date") {
let parser = TokenParser(tokens: tokens, namespace: Namespace()) let node = NowNode(format: Variable("\"yyyy-MM-dd\""))
let nodes = try parser.parse()
let node = nodes.first as? NowNode let formatter = NSDateFormatter()
try expect(nodes.count) == 1 formatter.dateFormat = "yyyy-MM-dd"
try expect(node?.format.variable) == "\"HH:mm\"" let date = formatter.stringFromDate(NSDate())
}
} try expect(try node.render(Context())) == date
}
$0.describe("rendering") {
$0.it("renders the date") {
let node = NowNode(format: Variable("\"yyyy-MM-dd\""))
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.stringFromDate(NSDate())
try expect(try node.render(Context())) == date
} }
} }
#endif
} }

View File

@@ -2,54 +2,61 @@ import Spectre
import Stencil import Stencil
describe("TokenParser") { func testTokenParser() {
$0.it("can parse a text token") { describe("TokenParser") {
let parser = TokenParser(tokens: [ $0.it("can parse a text token") {
Token.Text(value: "Hello World") let parser = TokenParser(tokens: [
], namespace: Namespace()) Token.Text(value: "Hello World")
], namespace: Namespace())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? TextNode let node = nodes.first as? TextNode
try expect(nodes.count) == 1 try expect(nodes.count) == 1
try expect(node?.text) == "Hello World" try expect(node?.text) == "Hello World"
} }
$0.it("can parse a variable token") { $0.it("can parse a variable token") {
let parser = TokenParser(tokens: [ let parser = TokenParser(tokens: [
Token.Variable(value: "'name'") Token.Variable(value: "'name'")
], namespace: Namespace()) ], namespace: Namespace())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? VariableNode let node = nodes.first as? VariableNode
try expect(nodes.count) == 1 try expect(nodes.count) == 1
let result = try node?.render(Context()) let result = try node?.render(Context())
try expect(result) == "name" try expect(result) == "name"
} }
$0.it("can parse a comment token") { $0.it("can parse a comment token") {
let parser = TokenParser(tokens: [ let parser = TokenParser(tokens: [
Token.Comment(value: "Secret stuff!") Token.Comment(value: "Secret stuff!")
], namespace: Namespace()) ], namespace: Namespace())
let nodes = try parser.parse() let nodes = try parser.parse()
try expect(nodes.count) == 0 try expect(nodes.count) == 0
} }
$0.it("can parse a tag token") { $0.it("can parse a tag token") {
let parser = TokenParser(tokens: [ let namespace = Namespace()
Token.Block(value: "now"), namespace.registerSimpleTag("known") { _ in
], namespace: Namespace()) return ""
}
let nodes = try parser.parse() let parser = TokenParser(tokens: [
try expect(nodes.count) == 1 Token.Block(value: "known"),
} ], namespace: namespace)
$0.it("errors when parsing an unknown tag") { let nodes = try parser.parse()
let parser = TokenParser(tokens: [ try expect(nodes.count) == 1
Token.Block(value: "unknown"), }
], namespace: Namespace())
try expect(try parser.parse()).toThrow(TemplateSyntaxError("Unknown template tag 'unknown'")) $0.it("errors when parsing an unknown tag") {
let parser = TokenParser(tokens: [
Token.Block(value: "unknown"),
], namespace: Namespace())
try expect(try parser.parse()).toThrow(TemplateSyntaxError("Unknown template tag 'unknown'"))
}
} }
} }

View File

@@ -9,55 +9,57 @@ class CustomNode : NodeType {
} }
describe("Stencil") { func testStencil() {
$0.it("can render the README example") { describe("Stencil") {
let templateString = "There are {{ articles.count }} articles.\n" + $0.it("can render the README example") {
"\n" + let templateString = "There are {{ articles.count }} articles.\n" +
"{% for article in articles %}" + "\n" +
" - {{ article.title }} by {{ article.author }}.\n" + "{% for article in articles %}" +
"{% endfor %}\n" " - {{ article.title }} by {{ article.author }}.\n" +
"{% endfor %}\n"
let context = Context(dictionary: [ let context = Context(dictionary: [
"articles": [ "articles": [
[ "title": "Migrating from OCUnit to XCTest", "author": "Kyle Fuller" ], [ "title": "Migrating from OCUnit to XCTest", "author": "Kyle Fuller" ],
[ "title": "Memory Management with ARC", "author": "Kyle Fuller" ], [ "title": "Memory Management with ARC", "author": "Kyle Fuller" ],
] ]
]) ])
let template = Template(templateString:templateString) let template = Template(templateString: templateString)
let result = try template.render(context) let result = try template.render(context)
let fixture = "There are 2 articles.\n" + let fixture = "There are 2 articles.\n" +
"\n" + "\n" +
" - Migrating from OCUnit to XCTest by Kyle Fuller.\n" + " - Migrating from OCUnit to XCTest by Kyle Fuller.\n" +
" - Memory Management with ARC by Kyle Fuller.\n" + " - Memory Management with ARC by Kyle Fuller.\n" +
"\n" "\n"
try expect(result) == fixture try expect(result) == fixture
}
$0.it("can render a custom template tag") {
let templateString = "{% custom %}"
let template = Template(templateString: templateString)
let namespace = Namespace()
namespace.registerTag("custom") { parser, token in
return CustomNode()
} }
let result = try template.render(namespace: namespace) $0.it("can render a custom template tag") {
try expect(result) == "Hello World" let templateString = "{% custom %}"
} let template = Template(templateString: templateString)
$0.it("can render a simple custom tag") { let namespace = Namespace()
let templateString = "{% custom %}" namespace.registerTag("custom") { parser, token in
let template = Template(templateString: templateString) return CustomNode()
}
let namespace = Namespace() let result = try template.render(Context(namespace: namespace))
namespace.registerSimpleTag("custom") { context in try expect(result) == "Hello World"
return "Hello World"
} }
try expect(try template.render(namespace: namespace)) == "Hello World" $0.it("can render a simple custom tag") {
let templateString = "{% custom %}"
let template = Template(templateString: templateString)
let namespace = Namespace()
namespace.registerSimpleTag("custom") { context in
return "Hello World"
}
try expect(try template.render(Context(namespace: namespace))) == "Hello World"
}
} }
} }

View File

@@ -3,56 +3,58 @@ import Stencil
import PathKit import PathKit
describe("Include") { func testInclude() {
let path = Path(__FILE__) + ".." + ".." + "Tests" + "fixtures" describe("Include") {
let loader = TemplateLoader(paths: [path]) let path = Path(__FILE__) + ".." + ".." + "fixtures"
let loader = TemplateLoader(paths: [path])
$0.describe("parsing") { $0.describe("parsing") {
$0.it("throws an error when no template is given") { $0.it("throws an error when no template is given") {
let tokens = [ Token.Block(value: "include") ] let tokens = [ Token.Block(value: "include") ]
let parser = TokenParser(tokens: tokens, namespace: Namespace()) let parser = TokenParser(tokens: tokens, namespace: Namespace())
let error = TemplateSyntaxError("'include' tag takes one argument, the template file to be included") let error = TemplateSyntaxError("'include' tag takes one argument, the template file to be included")
try expect(try parser.parse()).toThrow(error) try expect(try parser.parse()).toThrow(error)
} }
$0.it("can parse a valid include block") { $0.it("can parse a valid include block") {
let tokens = [ Token.Block(value: "include \"test.html\"") ] let tokens = [ Token.Block(value: "include \"test.html\"") ]
let parser = TokenParser(tokens: tokens, namespace: Namespace()) let parser = TokenParser(tokens: tokens, namespace: Namespace())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? IncludeNode let node = nodes.first as? IncludeNode
try expect(nodes.count) == 1 try expect(nodes.count) == 1
try expect(node?.templateName) == Variable("\"test.html\"") try expect(node?.templateName) == Variable("\"test.html\"")
}
}
$0.describe("rendering") {
$0.it("throws an error when rendering without a loader") {
let node = IncludeNode(templateName: Variable("\"test.html\""))
do {
try node.render(Context())
} catch {
try expect("\(error)") == "Template loader not in context"
} }
} }
$0.it("throws an error when it cannot find the included template") { $0.describe("rendering") {
let node = IncludeNode(templateName: Variable("\"unknown.html\"")) $0.it("throws an error when rendering without a loader") {
let node = IncludeNode(templateName: Variable("\"test.html\""))
do { do {
try node.render(Context(dictionary: ["loader": loader])) try node.render(Context())
} catch { } catch {
try expect("\(error)".hasPrefix("'unknown.html' template not found")).to.beTrue() try expect("\(error)") == "Template loader not in context"
}
} }
}
$0.it("successfully renders a found included template") { $0.it("throws an error when it cannot find the included template") {
let node = IncludeNode(templateName: Variable("\"test.html\"")) let node = IncludeNode(templateName: Variable("\"unknown.html\""))
let context = Context(dictionary: ["loader":loader, "target": "World"])
let value = try node.render(context) do {
try expect(value) == "Hello World!" try node.render(Context(dictionary: ["loader": loader]))
} catch {
try expect("\(error)".hasPrefix("'unknown.html' template not found")).to.beTrue()
}
}
$0.it("successfully renders a found included template") {
let node = IncludeNode(templateName: Variable("\"test.html\""))
let context = Context(dictionary: ["loader":loader, "target": "World"])
let value = try node.render(context)
try expect(value) == "Hello World!"
}
} }
} }
} }

View File

@@ -3,13 +3,15 @@ import Stencil
import PathKit import PathKit
describe("Inheritence") { func testInheritence() {
let path = Path(__FILE__) + ".." + ".." + "Tests" + "fixtures" describe("Inheritence") {
let loader = TemplateLoader(paths: [path]) let path = Path(__FILE__) + ".." + ".." + "fixtures"
let loader = TemplateLoader(paths: [path])
$0.it("can inherit from another template") { $0.it("can inherit from another template") {
let context = Context(dictionary: ["loader": loader]) let context = Context(dictionary: ["loader": loader])
let template = loader.loadTemplate("child.html") let template = loader.loadTemplate("child.html")
try expect(try template?.render(context)) == "Header\nChild" try expect(try template?.render(context)) == "Header\nChild"
}
} }
} }

View File

@@ -3,21 +3,23 @@ import Stencil
import PathKit import PathKit
describe("TemplateLoader") { func testTemplateLoader() {
let path = Path(__FILE__) + ".." + ".." + "Tests" + "fixtures" describe("TemplateLoader") {
let loader = TemplateLoader(paths: [path]) let path = Path(__FILE__) + ".." + ".." + "Tests" + "fixtures"
let loader = TemplateLoader(paths: [path])
$0.it("returns nil when a template cannot be found") { $0.it("returns nil when a template cannot be found") {
try expect(loader.loadTemplate("unknown.html")).to.beNil() try expect(loader.loadTemplate("unknown.html")).to.beNil()
} }
$0.it("returns nil when an array of templates cannot be found") { $0.it("returns nil when an array of templates cannot be found") {
try expect(loader.loadTemplate(["unknown.html", "unknown2.html"])).to.beNil() try expect(loader.loadTemplate(["unknown.html", "unknown2.html"])).to.beNil()
} }
$0.it("can load a template from a file") { $0.it("can load a template from a file") {
if loader.loadTemplate("test.html") == nil { if loader.loadTemplate("test.html") == nil {
throw failure("didn't find the template") throw failure("didn't find the template")
}
} }
} }
} }

View File

@@ -2,11 +2,13 @@ import Spectre
import Stencil import Stencil
describe("Template") { func testTemplate() {
$0.it("can render a template from a string") { describe("Template") {
let context = Context(dictionary: [ "name": "Kyle" ]) $0.it("can render a template from a string") {
let template = Template(templateString: "Hello World") let context = Context(dictionary: [ "name": "Kyle" ])
let result = try template.render(context) let template = Template(templateString: "Hello World")
try expect(result) == "Hello World" let result = try template.render(context)
try expect(result) == "Hello World"
}
} }
} }

View File

@@ -2,31 +2,33 @@ import Spectre
import Stencil import Stencil
describe("Token") { func testToken() {
$0.it("can split the contents into components") { describe("Token") {
let token = Token.Text(value: "hello world") $0.it("can split the contents into components") {
let components = token.components() let token = Token.Text(value: "hello world")
let components = token.components()
try expect(components.count) == 2 try expect(components.count) == 2
try expect(components[0]) == "hello" try expect(components[0]) == "hello"
try expect(components[1]) == "world" try expect(components[1]) == "world"
} }
$0.it("can split the contents into components with single quoted strings") { $0.it("can split the contents into components with single quoted strings") {
let token = Token.Text(value: "hello 'kyle fuller'") let token = Token.Text(value: "hello 'kyle fuller'")
let components = token.components() let components = token.components()
try expect(components.count) == 2 try expect(components.count) == 2
try expect(components[0]) == "hello" try expect(components[0]) == "hello"
try expect(components[1]) == "'kyle fuller'" try expect(components[1]) == "'kyle fuller'"
} }
$0.it("can split the contents into components with double quoted strings") { $0.it("can split the contents into components with double quoted strings") {
let token = Token.Text(value: "hello \"kyle fuller\"") let token = Token.Text(value: "hello \"kyle fuller\"")
let components = token.components() let components = token.components()
try expect(components.count) == 2 try expect(components.count) == 2
try expect(components[0]) == "hello" try expect(components[0]) == "hello"
try expect(components[1]) == "\"kyle fuller\"" try expect(components[1]) == "\"kyle fuller\""
}
} }
} }

View File

@@ -3,66 +3,75 @@ import Spectre
import Stencil import Stencil
#if os(OSX)
@objc class Object : NSObject { @objc class Object : NSObject {
let title = "Hello World" let title = "Hello World"
} }
#endif
describe("Variable") { func testVariable() {
let context = Context(dictionary: [ describe("Variable") {
"name": "Kyle", let context = Context(dictionary: [
"contacts": ["Katie", "Carlton"], "name": "Kyle",
"profiles": [ "contacts": ["Katie", "Carlton"],
"github": "kylef", "profiles": [
], "github": "kylef",
"object": Object(), ],
]) ])
$0.it("can resolve a string literal with double quotes") { #if os(OSX)
let variable = Variable("\"name\"") context["object"] = Object()
let result = try variable.resolve(context) as? String #endif
try expect(result) == "name"
}
$0.it("can resolve a string literal with single quotes") { $0.it("can resolve a string literal with double quotes") {
let variable = Variable("'name'") let variable = Variable("\"name\"")
let result = try variable.resolve(context) as? String let result = try variable.resolve(context) as? String
try expect(result) == "name" try expect(result) == "name"
} }
$0.it("can resolve a string variable") { $0.it("can resolve a string literal with single quotes") {
let variable = Variable("name") let variable = Variable("'name'")
let result = try variable.resolve(context) as? String let result = try variable.resolve(context) as? String
try expect(result) == "Kyle" try expect(result) == "name"
} }
$0.it("can resolve an item from a dictionary") { $0.it("can resolve a string variable") {
let variable = Variable("profiles.github") let variable = Variable("name")
let result = try variable.resolve(context) as? String let result = try variable.resolve(context) as? String
try expect(result) == "kylef" try expect(result) == "Kyle"
} }
$0.it("can resolve an item from an array via it's index") { $0.it("can resolve an item from a dictionary") {
let variable = Variable("contacts.0") let variable = Variable("profiles.github")
let result = try variable.resolve(context) as? String let result = try variable.resolve(context) as? String
try expect(result) == "Katie" try expect(result) == "kylef"
} }
$0.it("can resolve the first item from an array") { $0.it("can resolve an item from an array via it's index") {
let variable = Variable("contacts.first") let variable = Variable("contacts.0")
let result = try variable.resolve(context) as? String let result = try variable.resolve(context) as? String
try expect(result) == "Katie" try expect(result) == "Katie"
} }
$0.it("can resolve the last item from an array") { $0.it("can resolve the first item from an array") {
let variable = Variable("contacts.last") let variable = Variable("contacts.first")
let result = try variable.resolve(context) as? String let result = try variable.resolve(context) as? String
try expect(result) == "Carlton" try expect(result) == "Katie"
} }
$0.it("can resolve a value via KVO") { $0.it("can resolve the last item from an array") {
let variable = Variable("object.title") let variable = Variable("contacts.last")
let result = try variable.resolve(context) as? String let result = try variable.resolve(context) as? String
try expect(result) == "Hello World" try expect(result) == "Carlton"
}
#if os(OSX)
$0.it("can resolve a value via KVO") {
let variable = Variable("object.title")
let result = try variable.resolve(context) as? String
try expect(result) == "Hello World"
}
#endif
} }
} }

15
Tests/main.swift Normal file
View File

@@ -0,0 +1,15 @@
testContext()
testFilter()
testLexer()
testToken()
testTokenParser()
testTemplateLoader()
testTemplate()
testVariable()
testNode()
testForNode()
testIfNode()
testNowNode()
testInclude()
testInheritence()
testStencil()

View File

@@ -1,21 +0,0 @@
machine:
xcode:
version: "7.0"
environment:
XCODE_SCHEME: NONE
XCODE_PROJECT: NONE
dependencies:
post:
- brew install --HEAD kylef/formulae/conche
test:
override:
- conche test
deployment:
release:
tag: /.*/
commands:
- pod trunk push