Use spectre-build for tests
This commit is contained in:
@@ -1 +1 @@
|
|||||||
2.2-dev
|
DEVELOPMENT-SNAPSHOT-2016-01-25-a
|
||||||
|
|||||||
8
.travis.yml
Normal file
8
.travis.yml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
os:
|
||||||
|
- osx
|
||||||
|
osx_image: xcode7.2
|
||||||
|
install:
|
||||||
|
- curl -sL https://gist.github.com/kylef/5c0475ff02b7c7671d2a/raw/621ef9b29bbb852fdfd2e10ed147b321d792c1e4/swiftenv-install.sh | bash
|
||||||
|
script:
|
||||||
|
- . ~/.swiftenv/init
|
||||||
|
- make test
|
||||||
7
Makefile
Normal file
7
Makefile
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
stencil:
|
||||||
|
@echo "Building Stencil"
|
||||||
|
@swift build
|
||||||
|
|
||||||
|
test: stencil
|
||||||
|
@echo "Running Tests"
|
||||||
|
@.build/debug/spectre-build
|
||||||
@@ -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),
|
||||||
|
]
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Stencil
|
# Stencil
|
||||||
|
|
||||||
[](https://circleci.com/gh/kylef/Stencil)
|
[](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
|
||||||
|
|||||||
@@ -2,64 +2,66 @@ 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") {
|
||||||
|
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(didRun).to.beTrue()
|
||||||
|
try expect(context["name"] as? String) == "Kyle"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 = Context(dictionary: ["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, 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, 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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,60 +3,62 @@ 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") {
|
$0.it("renders a context variable of type NSArray") {
|
||||||
let nsarray_context = Context(dictionary: [
|
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"
|
||||||
}
|
}
|
||||||
|
|
||||||
$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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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: Array<[String:AnyObject]> = [["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 = Array<[String:AnyObject]>()
|
||||||
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:AnyObject]()
|
||||||
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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,37 +3,39 @@ import Spectre
|
|||||||
import Stencil
|
import Stencil
|
||||||
|
|
||||||
|
|
||||||
describe("NowNode") {
|
func testNowNode() {
|
||||||
$0.describe("parsing") {
|
describe("NowNode") {
|
||||||
$0.it("parses default format without any now arguments") {
|
$0.describe("parsing") {
|
||||||
let tokens = [ Token.Block(value: "now") ]
|
$0.it("parses default format without any now arguments") {
|
||||||
let parser = TokenParser(tokens: tokens, namespace: Namespace())
|
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
|
|
||||||
try expect(nodes.count) == 1
|
|
||||||
try expect(node?.format.variable) == "\"HH:mm\""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$0.describe("rendering") {
|
let formatter = NSDateFormatter()
|
||||||
$0.it("renders the date") {
|
formatter.dateFormat = "yyyy-MM-dd"
|
||||||
let node = NowNode(format: Variable("\"yyyy-MM-dd\""))
|
let date = formatter.stringFromDate(NSDate())
|
||||||
|
|
||||||
let formatter = NSDateFormatter()
|
try expect(try node.render(Context())) == date
|
||||||
formatter.dateFormat = "yyyy-MM-dd"
|
}
|
||||||
let date = formatter.stringFromDate(NSDate())
|
|
||||||
|
|
||||||
try expect(try node.render(Context())) == date
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,54 +2,56 @@ 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 parser = TokenParser(tokens: [
|
||||||
Token.Block(value: "now"),
|
Token.Block(value: "now"),
|
||||||
], namespace: Namespace())
|
], namespace: Namespace())
|
||||||
|
|
||||||
let nodes = try parser.parse()
|
let nodes = try parser.parse()
|
||||||
try expect(nodes.count) == 1
|
try expect(nodes.count) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("errors when parsing an unknown tag") {
|
$0.it("errors when parsing an unknown tag") {
|
||||||
let parser = TokenParser(tokens: [
|
let parser = TokenParser(tokens: [
|
||||||
Token.Block(value: "unknown"),
|
Token.Block(value: "unknown"),
|
||||||
], namespace: Namespace())
|
], namespace: Namespace())
|
||||||
|
|
||||||
try expect(try parser.parse()).toThrow(TemplateSyntaxError("Unknown template tag 'unknown'"))
|
try expect(try parser.parse()).toThrow(TemplateSyntaxError("Unknown template tag 'unknown'"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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(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(namespace: namespace)) == "Hello World"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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!"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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\""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,61 +8,63 @@ import Stencil
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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(),
|
],
|
||||||
])
|
"object": Object(),
|
||||||
|
])
|
||||||
|
|
||||||
$0.it("can resolve a string literal with double 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 literal with single quotes") {
|
$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) == "name"
|
try expect(result) == "name"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can resolve a string variable") {
|
$0.it("can resolve a string variable") {
|
||||||
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) == "Kyle"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can resolve an item from a dictionary") {
|
$0.it("can resolve an item from a dictionary") {
|
||||||
let variable = Variable("profiles.github")
|
let variable = Variable("profiles.github")
|
||||||
let result = try variable.resolve(context) as? String
|
let result = try variable.resolve(context) as? String
|
||||||
try expect(result) == "kylef"
|
try expect(result) == "kylef"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can resolve an item from an array via it's index") {
|
$0.it("can resolve an item from an array via it's index") {
|
||||||
let variable = Variable("contacts.0")
|
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 first item from an array") {
|
$0.it("can resolve the first item from an array") {
|
||||||
let variable = Variable("contacts.first")
|
let variable = Variable("contacts.first")
|
||||||
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 last item from an array") {
|
||||||
let variable = Variable("contacts.last")
|
let variable = Variable("contacts.last")
|
||||||
let result = try variable.resolve(context) as? String
|
let result = try variable.resolve(context) as? String
|
||||||
try expect(result) == "Carlton"
|
try expect(result) == "Carlton"
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("can resolve a value via KVO") {
|
$0.it("can resolve a value via KVO") {
|
||||||
let variable = Variable("object.title")
|
let variable = Variable("object.title")
|
||||||
let result = try variable.resolve(context) as? String
|
let result = try variable.resolve(context) as? String
|
||||||
try expect(result) == "Hello World"
|
try expect(result) == "Hello World"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
Tests/main.swift
Normal file
15
Tests/main.swift
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
testContext()
|
||||||
|
testFilter()
|
||||||
|
testLexer()
|
||||||
|
testToken()
|
||||||
|
testTokenParser()
|
||||||
|
testTemplateLoader()
|
||||||
|
testTemplate()
|
||||||
|
testVariable()
|
||||||
|
testNode()
|
||||||
|
testForNode()
|
||||||
|
testIfNode()
|
||||||
|
testNowNode()
|
||||||
|
testInclude()
|
||||||
|
testInheritence()
|
||||||
|
testStencil()
|
||||||
21
circle.yml
21
circle.yml
@@ -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
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user