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

@@ -4,5 +4,8 @@ let package = Package(
name: "Stencil",
dependencies: [
.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
[![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
syntax similar to Django and Mustache. If you're familiar with these, you will
@@ -25,8 +25,8 @@ let context = Context(dictionary: [
])
do {
let template = Template(named: "template.stencil")
let rendered = template.render(context)
let template = try Template(named: "template.stencil")
let rendered = try template.render(context)
print(rendered)
} catch {
print("Failed to render template \(error)")
@@ -73,8 +73,8 @@ following lookup:
For example, if `people` was an array:
```html+django
There are {{ people.count }} people, {{ people.first }} is first person.
Followed by {{ people.1 }}.
There are {{ people.count }} people. {{ people.first }} is the first person,
followed by {{ people.1 }}.
```
#### Filters
@@ -142,7 +142,7 @@ A for loop allows you to iterate over an array found by variable lookup.
{% for item in items %}
{{ item }}
{% empty %}
There we're no items.
There were no items.
{% endfor %}
```

View File

@@ -1,15 +1,17 @@
/// A container for template variables.
public class Context {
var dictionaries:[[String: Any]]
var dictionaries: [[String: Any]]
let namespace: Namespace
/// Initialise a Context with a dictionary
public init(dictionary:[String: Any]) {
/// Initialise a Context with an optional dictionary and optional namespace
public init(dictionary: [String: Any]? = nil, namespace: Namespace = Namespace()) {
if let dictionary = dictionary {
dictionaries = [dictionary]
} else {
dictionaries = []
}
/// Initialise an empty Context
public init() {
dictionaries = []
self.namespace = namespace
}
public subscript(key: String) -> Any? {
@@ -35,20 +37,19 @@ public class 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 ?? [:])
}
/// Pop the last level off of the Context
public func pop() -> [String: Any]? {
private func pop() -> [String: Any]? {
return dictionaries.popLast()
}
/// 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 {
push(dictionary)
let result = try closure()
pop()
return result
defer { pop() }
return try closure()
}
}

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)
context.push([BlockContext.contextKey: blockContext])
let result = try template.render(context)
context.pop()
return result
return try context.push([BlockContext.contextKey: blockContext]) {
return try template.render(context)
}
}
}

View File

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

View File

@@ -1,5 +1,6 @@
import Foundation
public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertible {
public let description:String
@@ -8,15 +9,18 @@ public struct TemplateSyntaxError : ErrorType, Equatable, CustomStringConvertibl
}
}
public func ==(lhs:TemplateSyntaxError, rhs:TemplateSyntaxError) -> Bool {
return lhs.description == rhs.description
}
public protocol NodeType {
/// Render the node in the given context
func render(context:Context) throws -> String
}
/// Render the collection of nodes in the given context
public func renderNodes(nodes:[NodeType], _ context:Context) throws -> String {
return try nodes.map { try $0.render(context) }.joinWithSeparator("")
@@ -34,6 +38,7 @@ public class SimpleNode : NodeType {
}
}
public class TextNode : NodeType {
public let text:String
@@ -46,10 +51,12 @@ public class TextNode : NodeType {
}
}
public protocol Resolvable {
func resolve(context: Context) throws -> Any?
}
public class VariableNode : NodeType {
public let variable: Resolvable
@@ -75,187 +82,3 @@ public class VariableNode : NodeType {
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
public func render(context: Context? = nil, namespace: Namespace? = nil) throws -> String {
let parser = TokenParser(tokens: tokens, namespace: namespace ?? Namespace())
public func render(context: Context? = nil) throws -> String {
let context = context ?? Context()
let parser = TokenParser(tokens: tokens, namespace: context.namespace)
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() {
current = normalize(current)
if let context = current as? Context {
current = context[bit]
} else if let dictionary = resolveDictionary(current) {
} else if let dictionary = current as? [String: Any] {
current = dictionary[bit]
} else if let array = resolveArray(current) {
} else if let array = current as? [Any] {
if let index = Int(bit) {
current = array[index]
} 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? {
if let array = resolveArray(current) {
return array
}
if let dictionary = resolveDictionary(current) {
return dictionary
if let current = current as? Normalizable {
return current.normalize()
}
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",
"version": "0.5.2",
"version": "0.6.0-beta.1",
"summary": "Stencil is a simple and powerful template language for Swift.",
"homepage": "https://github.com/kylef/Stencil",
"license": {
@@ -13,7 +13,7 @@
"social_media_url": "http://twitter.com/kylefuller",
"source": {
"git": "https://github.com/kylef/Stencil.git",
"tag": "0.5.2"
"tag": "0.6.0-beta.1"
},
"source_files": [
"Sources/*.swift"
@@ -25,16 +25,5 @@
"requires_arc": true,
"dependencies": {
"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,7 +2,8 @@ import Spectre
import Stencil
describe("Context") {
func testContext() {
describe("Context") {
var context: Context!
$0.before {
@@ -26,29 +27,24 @@ describe("Context") {
}
$0.it("allows you to retrieve a value from a parent") {
context.push()
try context.push {
try expect(context["name"] as? String) == "Kyle"
}
}
$0.it("allows you to override a parent's value") {
context.push()
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.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"
try expect(context["name"] as? String) == "Kyle"
}
$0.it("allows you to push a dictionary and run a closure then restoring previous state") {
@@ -62,4 +58,5 @@ describe("Context") {
try expect(didRun).to.beTrue()
try expect(context["name"] as? String) == "Kyle"
}
}
}

View File

@@ -2,8 +2,9 @@ import Spectre
import Stencil
describe("template filters") {
let context = Context(dictionary: ["name": "Kyle"])
func testFilter() {
describe("template filters") {
let context: [String: Any] = ["name": "Kyle"]
$0.it("allows you to register a custom filter") {
let template = Template(templateString: "{{ name|repeat }}")
@@ -17,18 +18,18 @@ describe("template filters") {
return nil
}
let result = try template.render(context, namespace: namespace)
let result = try template.render(Context(dictionary: context, namespace: namespace))
try expect(result) == "Kyle Kyle"
}
$0.it("allows you to register a custom filter") {
$0.it("allows you to register a custom which throws") {
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"))
try expect(try template.render(Context(dictionary: context, namespace: namespace))).toThrow(TemplateSyntaxError("No Repeat"))
}
$0.it("allows whitespace in expression") {
@@ -36,34 +37,34 @@ describe("template filters") {
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE"
}
}
}
describe("capitalize filter") {
describe("capitalize filter") {
let template = Template(templateString: "{{ name|capitalize }}")
$0.it("capitalizes a string") {
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "Kyle"
}
}
}
describe("uppercase filter") {
describe("uppercase filter") {
let template = Template(templateString: "{{ name|uppercase }}")
$0.it("transforms a string to be uppercase") {
let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE"
}
}
}
describe("lowercase filter") {
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,7 +2,8 @@ import Spectre
import Stencil
describe("Lexer") {
func testLexer() {
describe("Lexer") {
$0.it("can tokenize text") {
let lexer = Lexer(templateString: "Hello World")
let tokens = lexer.tokenize()
@@ -45,4 +46,5 @@ describe("Lexer") {
try expect(tokens[0]) == Token.Variable(value: "thing")
try expect(tokens[1]) == Token.Variable(value: "name")
}
}
}

View File

@@ -3,7 +3,8 @@ import Stencil
import Foundation
describe("ForNode") {
func testForNode() {
describe("ForNode") {
let context = Context(dictionary: [
"items": [1, 2, 3],
"emptyItems": [Int](),
@@ -32,6 +33,7 @@ describe("ForNode") {
try expect(try node.render(any_context)) == "123"
}
#if os(OSX)
$0.it("renders a context variable of type NSArray") {
let nsarray_context = Context(dictionary: [
"items": NSArray(array: [1, 2, 3])
@@ -41,6 +43,7 @@ describe("ForNode") {
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
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") {
let nodes: [NodeType] = [VariableNode(variable: "item"), VariableNode(variable: "forloop.first")]
@@ -59,4 +62,5 @@ describe("ForNode") {
let node = ForNode(variable: "items", loopVariable: "item", nodes: nodes, emptyNodes: [])
try expect(try node.render(context)) == "112233"
}
}
}

View File

@@ -1,7 +1,9 @@
import Spectre
import Stencil
describe("IfNode") {
func testIfNode() {
describe("IfNode") {
$0.describe("parsing") {
$0.it("can parse an if block") {
let tokens = [
@@ -84,21 +86,21 @@ describe("IfNode") {
}
$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 items: [[String: Any]] = [["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 emptyItems = [[String: Any]]()
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 emptyItems = [String:Any]()
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"
@@ -110,4 +112,5 @@ describe("IfNode") {
try expect(try node.render(arrayContext)) == "false"
}
}
}
}

View File

@@ -9,7 +9,8 @@ class ErrorNode : NodeType {
}
describe("Node") {
func testNode() {
describe("Node") {
let context = Context(dictionary: [
"name": "Kyle",
"age": 27,
@@ -55,4 +56,5 @@ describe("Node") {
try expect(try renderNodes(nodes, context)).toThrow(TemplateSyntaxError("Custom Error"))
}
}
}
}

View File

@@ -3,7 +3,9 @@ import Spectre
import Stencil
describe("NowNode") {
func testNowNode() {
#if !os(Linux)
describe("NowNode") {
$0.describe("parsing") {
$0.it("parses default format without any now arguments") {
let tokens = [ Token.Block(value: "now") ]
@@ -36,4 +38,6 @@ describe("NowNode") {
try expect(try node.render(Context())) == date
}
}
}
#endif
}

View File

@@ -2,7 +2,8 @@ import Spectre
import Stencil
describe("TokenParser") {
func testTokenParser() {
describe("TokenParser") {
$0.it("can parse a text token") {
let parser = TokenParser(tokens: [
Token.Text(value: "Hello World")
@@ -37,9 +38,14 @@ describe("TokenParser") {
}
$0.it("can parse a tag token") {
let namespace = Namespace()
namespace.registerSimpleTag("known") { _ in
return ""
}
let parser = TokenParser(tokens: [
Token.Block(value: "now"),
], namespace: Namespace())
Token.Block(value: "known"),
], namespace: namespace)
let nodes = try parser.parse()
try expect(nodes.count) == 1
@@ -52,4 +58,5 @@ describe("TokenParser") {
try expect(try parser.parse()).toThrow(TemplateSyntaxError("Unknown template tag 'unknown'"))
}
}
}

View File

@@ -9,7 +9,8 @@ class CustomNode : NodeType {
}
describe("Stencil") {
func testStencil() {
describe("Stencil") {
$0.it("can render the README example") {
let templateString = "There are {{ articles.count }} articles.\n" +
"\n" +
@@ -24,7 +25,7 @@ describe("Stencil") {
]
])
let template = Template(templateString:templateString)
let template = Template(templateString: templateString)
let result = try template.render(context)
let fixture = "There are 2 articles.\n" +
@@ -45,7 +46,7 @@ describe("Stencil") {
return CustomNode()
}
let result = try template.render(namespace: namespace)
let result = try template.render(Context(namespace: namespace))
try expect(result) == "Hello World"
}
@@ -58,6 +59,7 @@ describe("Stencil") {
return "Hello World"
}
try expect(try template.render(namespace: namespace)) == "Hello World"
try expect(try template.render(Context(namespace: namespace))) == "Hello World"
}
}
}

View File

@@ -3,8 +3,9 @@ import Stencil
import PathKit
describe("Include") {
let path = Path(__FILE__) + ".." + ".." + "Tests" + "fixtures"
func testInclude() {
describe("Include") {
let path = Path(__FILE__) + ".." + ".." + "fixtures"
let loader = TemplateLoader(paths: [path])
$0.describe("parsing") {
@@ -55,4 +56,5 @@ describe("Include") {
try expect(value) == "Hello World!"
}
}
}
}

View File

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

View File

@@ -3,7 +3,8 @@ import Stencil
import PathKit
describe("TemplateLoader") {
func testTemplateLoader() {
describe("TemplateLoader") {
let path = Path(__FILE__) + ".." + ".." + "Tests" + "fixtures"
let loader = TemplateLoader(paths: [path])
@@ -20,4 +21,5 @@ describe("TemplateLoader") {
throw failure("didn't find the template")
}
}
}
}

View File

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

View File

@@ -2,7 +2,8 @@ import Spectre
import Stencil
describe("Token") {
func testToken() {
describe("Token") {
$0.it("can split the contents into components") {
let token = Token.Text(value: "hello world")
let components = token.components()
@@ -29,4 +30,5 @@ describe("Token") {
try expect(components[0]) == "hello"
try expect(components[1]) == "\"kyle fuller\""
}
}
}

View File

@@ -3,21 +3,27 @@ import Spectre
import Stencil
#if os(OSX)
@objc class Object : NSObject {
let title = "Hello World"
}
#endif
describe("Variable") {
func testVariable() {
describe("Variable") {
let context = Context(dictionary: [
"name": "Kyle",
"contacts": ["Katie", "Carlton"],
"profiles": [
"github": "kylef",
],
"object": Object(),
])
#if os(OSX)
context["object"] = Object()
#endif
$0.it("can resolve a string literal with double quotes") {
let variable = Variable("\"name\"")
let result = try variable.resolve(context) as? String
@@ -60,9 +66,12 @@ describe("Variable") {
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