refactor: Use tabs for indent

This commit is contained in:
T. R. Bernstein
2025-09-30 19:02:50 +02:00
parent 6811c71bd6
commit 25d1507159
44 changed files with 5423 additions and 5423 deletions

View File

@@ -1,106 +1,106 @@
/// A container for template variables. /// A container for template variables.
public class Context { public class Context {
var dictionaries: [[String: Any?]] var dictionaries: [[String: Any?]]
/// The context's environment, such as registered extensions, classes, /// The context's environment, such as registered extensions, classes,
public let environment: Environment public let environment: Environment
init(dictionaries: [[String: Any?]], environment: Environment) { init(dictionaries: [[String: Any?]], environment: Environment) {
self.dictionaries = dictionaries self.dictionaries = dictionaries
self.environment = environment self.environment = environment
} }
/// Create a context from a dictionary (and an env.) /// Create a context from a dictionary (and an env.)
/// ///
/// - Parameters: /// - Parameters:
/// - dictionary: The context's data /// - dictionary: The context's data
/// - environment: Environment such as extensions, /// - environment: Environment such as extensions,
public convenience init(dictionary: [String: Any] = [:], environment: Environment? = nil) { public convenience init(dictionary: [String: Any] = [:], environment: Environment? = nil) {
self.init( self.init(
dictionaries: dictionary.isEmpty ? [] : [dictionary], dictionaries: dictionary.isEmpty ? [] : [dictionary],
environment: environment ?? Environment() environment: environment ?? Environment()
) )
} }
/// Access variables in this context by name /// Access variables in this context by name
public subscript(key: String) -> Any? { public subscript(key: String) -> Any? {
/// Retrieves a variable's value, starting at the current context and going upwards /// Retrieves a variable's value, starting at the current context and going upwards
get { get {
for dictionary in Array(dictionaries.reversed()) { for dictionary in Array(dictionaries.reversed()) {
if let value = dictionary[key] { if let value = dictionary[key] {
return value return value
} }
} }
return nil return nil
} }
/// Set a variable in the current context, deleting the variable if it's nil /// Set a variable in the current context, deleting the variable if it's nil
set(value) { set(value) {
if var dictionary = dictionaries.popLast() { if var dictionary = dictionaries.popLast() {
dictionary[key] = value dictionary[key] = value
dictionaries.append(dictionary) dictionaries.append(dictionary)
} }
} }
} }
/// Push a new level into the Context /// Push a new level into the Context
/// ///
/// - Parameters: /// - Parameters:
/// - dictionary: The new level data /// - dictionary: The new level data
fileprivate func push(_ dictionary: [String: Any] = [:]) { fileprivate func push(_ dictionary: [String: Any] = [:]) {
dictionaries.append(dictionary) dictionaries.append(dictionary)
} }
/// Pop the last level off of the Context /// Pop the last level off of the Context
/// ///
/// - returns: The popped level /// - returns: The popped level
// swiftlint:disable:next discouraged_optional_collection // swiftlint:disable:next discouraged_optional_collection
fileprivate func pop() -> [String: Any?]? { fileprivate func pop() -> [String: Any?]? {
dictionaries.popLast() 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
/// ///
/// - Parameters: /// - Parameters:
/// - dictionary: The new level data /// - dictionary: The new level data
/// - closure: The closure to execute /// - closure: The closure to execute
/// - returns: Return value of the closure /// - returns: Return value of the closure
public func push<Result>(dictionary: [String: Any] = [:], closure: (() throws -> Result)) rethrows -> Result { public func push<Result>(dictionary: [String: Any] = [:], closure: (() throws -> Result)) rethrows -> Result {
push(dictionary) push(dictionary)
defer { _ = pop() } defer { _ = pop() }
return try closure() return try closure()
} }
/// Flatten all levels of context data into 1, merging duplicate variables /// Flatten all levels of context data into 1, merging duplicate variables
/// ///
/// - returns: All collected variables /// - returns: All collected variables
public func flatten() -> [String: Any] { public func flatten() -> [String: Any] {
var accumulator: [String: Any] = [:] var accumulator: [String: Any] = [:]
for dictionary in dictionaries { for dictionary in dictionaries {
for (key, value) in dictionary { for (key, value) in dictionary {
if let value = value { if let value = value {
accumulator.updateValue(value, forKey: key) accumulator.updateValue(value, forKey: key)
} }
} }
} }
return accumulator return accumulator
} }
/// Cache result of block by its name in the context top-level, so that it can be later rendered /// Cache result of block by its name in the context top-level, so that it can be later rendered
/// via `{{ block.name }}` /// via `{{ block.name }}`
/// ///
/// - Parameters: /// - Parameters:
/// - name: The name of the stored block /// - name: The name of the stored block
/// - content: The block's rendered content /// - content: The block's rendered content
public func cacheBlock(_ name: String, content: String) { public func cacheBlock(_ name: String, content: String) {
if var block = dictionaries.first?["block"] as? [String: String] { if var block = dictionaries.first?["block"] as? [String: String] {
block[name] = content block[name] = content
dictionaries[0]["block"] = block dictionaries[0]["block"] = block
} else { } else {
dictionaries.insert(["block": [name: content]], at: 0) dictionaries.insert(["block": [name: content]], at: 0)
} }
} }
} }

View File

@@ -1,18 +1,18 @@
/// Marker protocol so we can know which types support `@dynamicMemberLookup`. Add this to your own types that support /// Marker protocol so we can know which types support `@dynamicMemberLookup`. Add this to your own types that support
/// lookup by String. /// lookup by String.
public protocol DynamicMemberLookup { public protocol DynamicMemberLookup {
/// Get a value for a given `String` key /// Get a value for a given `String` key
subscript(dynamicMember member: String) -> Any? { get } subscript(dynamicMember member: String) -> Any? { get }
} }
public extension DynamicMemberLookup where Self: RawRepresentable { public extension DynamicMemberLookup where Self: RawRepresentable {
/// Get a value for a given `String` key /// Get a value for a given `String` key
subscript(dynamicMember member: String) -> Any? { subscript(dynamicMember member: String) -> Any? {
switch member { switch member {
case "rawValue": case "rawValue":
return rawValue return rawValue
default: default:
return nil return nil
} }
} }
} }

View File

@@ -1,84 +1,84 @@
/// Container for environment data, such as registered extensions /// Container for environment data, such as registered extensions
public struct Environment { public struct Environment {
/// The class for loading new templates /// The class for loading new templates
public let templateClass: Template.Type public let templateClass: Template.Type
/// List of registered extensions /// List of registered extensions
public var extensions: [Extension] public var extensions: [Extension]
/// How to handle whitespace /// How to handle whitespace
public var trimBehaviour: TrimBehaviour public var trimBehaviour: TrimBehaviour
/// Mechanism for loading new files /// Mechanism for loading new files
public var loader: Loader? public var loader: Loader?
/// Basic initializer /// Basic initializer
/// ///
/// - Parameters: /// - Parameters:
/// - loader: Mechanism for loading new files /// - loader: Mechanism for loading new files
/// - extensions: List of extension containers /// - extensions: List of extension containers
/// - templateClass: Class for newly loaded templates /// - templateClass: Class for newly loaded templates
/// - trimBehaviour: How to handle whitespace /// - trimBehaviour: How to handle whitespace
public init( public init(
loader: Loader? = nil, loader: Loader? = nil,
extensions: [Extension] = [], extensions: [Extension] = [],
templateClass: Template.Type = Template.self, templateClass: Template.Type = Template.self,
trimBehaviour: TrimBehaviour = .nothing trimBehaviour: TrimBehaviour = .nothing
) { ) {
self.templateClass = templateClass self.templateClass = templateClass
self.loader = loader self.loader = loader
self.extensions = extensions + [DefaultExtension()] self.extensions = extensions + [DefaultExtension()]
self.trimBehaviour = trimBehaviour self.trimBehaviour = trimBehaviour
} }
/// Load a template with the given name /// Load a template with the given name
/// ///
/// - Parameters: /// - Parameters:
/// - name: Name of the template /// - name: Name of the template
/// - returns: Loaded template instance /// - returns: Loaded template instance
public func loadTemplate(name: String) throws -> Template { public func loadTemplate(name: String) throws -> Template {
if let loader = loader { if let loader = loader {
return try loader.loadTemplate(name: name, environment: self) return try loader.loadTemplate(name: name, environment: self)
} else { } else {
throw TemplateDoesNotExist(templateNames: [name], loader: nil) throw TemplateDoesNotExist(templateNames: [name], loader: nil)
} }
} }
/// Load a template with the given names /// Load a template with the given names
/// ///
/// - Parameters: /// - Parameters:
/// - names: Names of the template /// - names: Names of the template
/// - returns: Loaded template instance /// - returns: Loaded template instance
public func loadTemplate(names: [String]) throws -> Template { public func loadTemplate(names: [String]) throws -> Template {
if let loader = loader { if let loader = loader {
return try loader.loadTemplate(names: names, environment: self) return try loader.loadTemplate(names: names, environment: self)
} else { } else {
throw TemplateDoesNotExist(templateNames: names, loader: nil) throw TemplateDoesNotExist(templateNames: names, loader: nil)
} }
} }
/// Render a template with the given name, providing some data /// Render a template with the given name, providing some data
/// ///
/// - Parameters: /// - Parameters:
/// - name: Name of the template /// - name: Name of the template
/// - context: Data for rendering /// - context: Data for rendering
/// - returns: Rendered output /// - returns: Rendered output
public func renderTemplate(name: String, context: [String: Any] = [:]) throws -> String { public func renderTemplate(name: String, context: [String: Any] = [:]) throws -> String {
let template = try loadTemplate(name: name) let template = try loadTemplate(name: name)
return try render(template: template, context: context) return try render(template: template, context: context)
} }
/// Render the given template string, providing some data /// Render the given template string, providing some data
/// ///
/// - Parameters: /// - Parameters:
/// - string: Template string /// - string: Template string
/// - context: Data for rendering /// - context: Data for rendering
/// - returns: Rendered output /// - returns: Rendered output
public func renderTemplate(string: String, context: [String: Any] = [:]) throws -> String { public func renderTemplate(string: String, context: [String: Any] = [:]) throws -> String {
let template = templateClass.init(templateString: string, environment: self) let template = templateClass.init(templateString: string, environment: self)
return try render(template: template, context: context) return try render(template: template, context: context)
} }
func render(template: Template, context: [String: Any]) throws -> String { func render(template: Template, context: [String: Any]) throws -> String {
// update template environment as it can be created from string literal with default environment // update template environment as it can be created from string literal with default environment
template.environment = self template.environment = self
return try template.render(context) return try template.render(context)
} }
} }

View File

@@ -1,81 +1,81 @@
public class TemplateDoesNotExist: Error, CustomStringConvertible { public class TemplateDoesNotExist: Error, CustomStringConvertible {
let templateNames: [String] let templateNames: [String]
let loader: Loader? let loader: Loader?
public init(templateNames: [String], loader: Loader? = nil) { public init(templateNames: [String], loader: Loader? = nil) {
self.templateNames = templateNames self.templateNames = templateNames
self.loader = loader self.loader = loader
} }
public var description: String { public var description: String {
let templates = templateNames.joined(separator: ", ") let templates = templateNames.joined(separator: ", ")
if let loader = loader { if let loader = loader {
return "Template named `\(templates)` does not exist in loader \(loader)" return "Template named `\(templates)` does not exist in loader \(loader)"
} }
return "Template named `\(templates)` does not exist. No loaders found" return "Template named `\(templates)` does not exist. No loaders found"
} }
} }
public struct TemplateSyntaxError: Error, Equatable, CustomStringConvertible { public struct TemplateSyntaxError: Error, Equatable, CustomStringConvertible {
public let reason: String public let reason: String
public var description: String { reason } public var description: String { reason }
public internal(set) var token: Token? public internal(set) var token: Token?
public internal(set) var stackTrace: [Token] public internal(set) var stackTrace: [Token]
public var templateName: String? { token?.sourceMap.filename } public var templateName: String? { token?.sourceMap.filename }
var allTokens: [Token] { var allTokens: [Token] {
stackTrace + (token.map { [$0] } ?? []) stackTrace + (token.map { [$0] } ?? [])
} }
public init(reason: String, token: Token? = nil, stackTrace: [Token] = []) { public init(reason: String, token: Token? = nil, stackTrace: [Token] = []) {
self.reason = reason self.reason = reason
self.stackTrace = stackTrace self.stackTrace = stackTrace
self.token = token self.token = token
} }
public init(_ description: String) { public init(_ description: String) {
self.init(reason: description) self.init(reason: description)
} }
} }
extension Error { extension Error {
func withToken(_ token: Token?) -> Error { func withToken(_ token: Token?) -> Error {
if var error = self as? TemplateSyntaxError { if var error = self as? TemplateSyntaxError {
error.token = error.token ?? token error.token = error.token ?? token
return error return error
} else { } else {
return TemplateSyntaxError(reason: "\(self)", token: token) return TemplateSyntaxError(reason: "\(self)", token: token)
} }
} }
} }
public protocol ErrorReporter: AnyObject { public protocol ErrorReporter: AnyObject {
func renderError(_ error: Error) -> String func renderError(_ error: Error) -> String
} }
open class SimpleErrorReporter: ErrorReporter { open class SimpleErrorReporter: ErrorReporter {
open func renderError(_ error: Error) -> String { open func renderError(_ error: Error) -> String {
guard let templateError = error as? TemplateSyntaxError else { return error.localizedDescription } guard let templateError = error as? TemplateSyntaxError else { return error.localizedDescription }
func describe(token: Token) -> String { func describe(token: Token) -> String {
let templateName = token.sourceMap.filename ?? "" let templateName = token.sourceMap.filename ?? ""
let location = token.sourceMap.location let location = token.sourceMap.location
let highlight = """ let highlight = """
\(String(Array(repeating: " ", count: location.lineOffset)))\ \(String(Array(repeating: " ", count: location.lineOffset)))\
^\(String(Array(repeating: "~", count: max(token.contents.count - 1, 0)))) ^\(String(Array(repeating: "~", count: max(token.contents.count - 1, 0))))
""" """
return """ return """
\(templateName)\(location.lineNumber):\(location.lineOffset): error: \(templateError.reason) \(templateName)\(location.lineNumber):\(location.lineOffset): error: \(templateError.reason)
\(location.content) \(location.content)
\(highlight) \(highlight)
""" """
} }
var descriptions = templateError.stackTrace.reduce(into: []) { $0.append(describe(token: $1)) } var descriptions = templateError.stackTrace.reduce(into: []) { $0.append(describe(token: $1)) }
let description = templateError.token.map(describe(token:)) ?? templateError.reason let description = templateError.token.map(describe(token:)) ?? templateError.reason
descriptions.append(description) descriptions.append(description)
return descriptions.joined(separator: "\n") return descriptions.joined(separator: "\n")
} }
} }

View File

@@ -1,327 +1,327 @@
public protocol Expression: CustomStringConvertible, Resolvable { public protocol Expression: CustomStringConvertible, Resolvable {
func evaluate(context: Context) throws -> Bool func evaluate(context: Context) throws -> Bool
} }
extension Expression { extension Expression {
func resolve(_ context: Context) throws -> Any? { func resolve(_ context: Context) throws -> Any? {
try "\(evaluate(context: context))" try "\(evaluate(context: context))"
} }
} }
protocol InfixOperator: Expression { protocol InfixOperator: Expression {
init(lhs: Expression, rhs: Expression) init(lhs: Expression, rhs: Expression)
} }
protocol PrefixOperator: Expression { protocol PrefixOperator: Expression {
init(expression: Expression) init(expression: Expression)
} }
final class StaticExpression: Expression, CustomStringConvertible { final class StaticExpression: Expression, CustomStringConvertible {
let value: Bool let value: Bool
init(value: Bool) { init(value: Bool) {
self.value = value self.value = value
} }
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
value value
} }
var description: String { var description: String {
"\(value)" "\(value)"
} }
} }
final class VariableExpression: Expression, CustomStringConvertible { final class VariableExpression: Expression, CustomStringConvertible {
let variable: Resolvable let variable: Resolvable
init(variable: Resolvable) { init(variable: Resolvable) {
self.variable = variable self.variable = variable
} }
var description: String { var description: String {
"(variable: \(variable))" "(variable: \(variable))"
} }
func resolve(_ context: Context) throws -> Any? { func resolve(_ context: Context) throws -> Any? {
try variable.resolve(context) try variable.resolve(context)
} }
/// Resolves a variable in the given context as boolean /// Resolves a variable in the given context as boolean
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
let result = try variable.resolve(context) let result = try variable.resolve(context)
var truthy = false var truthy = false
if let result = result as? [Any] { if let result = result as? [Any] {
truthy = !result.isEmpty truthy = !result.isEmpty
} else if let result = result as? [String: Any] { } else if let result = result as? [String: Any] {
truthy = !result.isEmpty truthy = !result.isEmpty
} else if let result = result as? Bool { } else if let result = result as? Bool {
truthy = result truthy = result
} else if let result = result as? String { } else if let result = result as? String {
truthy = !result.isEmpty truthy = !result.isEmpty
} else if let value = result, let result = toNumber(value: value) { } else if let value = result, let result = toNumber(value: value) {
truthy = result > 0 truthy = result > 0
} else if result != nil { } else if result != nil {
truthy = true truthy = true
} }
return truthy return truthy
} }
} }
final class NotExpression: Expression, PrefixOperator, CustomStringConvertible { final class NotExpression: Expression, PrefixOperator, CustomStringConvertible {
let expression: Expression let expression: Expression
init(expression: Expression) { init(expression: Expression) {
self.expression = expression self.expression = expression
} }
var description: String { var description: String {
"not \(expression)" "not \(expression)"
} }
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
try !expression.evaluate(context: context) try !expression.evaluate(context: context)
} }
} }
final class InExpression: Expression, InfixOperator, CustomStringConvertible { final class InExpression: Expression, InfixOperator, CustomStringConvertible {
let lhs: Expression let lhs: Expression
let rhs: Expression let rhs: Expression
init(lhs: Expression, rhs: Expression) { init(lhs: Expression, rhs: Expression) {
self.lhs = lhs self.lhs = lhs
self.rhs = rhs self.rhs = rhs
} }
var description: String { var description: String {
"(\(lhs) in \(rhs))" "(\(lhs) in \(rhs))"
} }
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression { if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression {
let lhsValue = try lhs.variable.resolve(context) let lhsValue = try lhs.variable.resolve(context)
let rhsValue = try rhs.variable.resolve(context) let rhsValue = try rhs.variable.resolve(context)
if let lhs = lhsValue as? AnyHashable, let rhs = rhsValue as? [AnyHashable] { if let lhs = lhsValue as? AnyHashable, let rhs = rhsValue as? [AnyHashable] {
return rhs.contains(lhs) return rhs.contains(lhs)
} else if let lhs = lhsValue as? Int, let rhs = rhsValue as? CountableClosedRange<Int> { } else if let lhs = lhsValue as? Int, let rhs = rhsValue as? CountableClosedRange<Int> {
return rhs.contains(lhs) return rhs.contains(lhs)
} else if let lhs = lhsValue as? Int, let rhs = rhsValue as? CountableRange<Int> { } else if let lhs = lhsValue as? Int, let rhs = rhsValue as? CountableRange<Int> {
return rhs.contains(lhs) return rhs.contains(lhs)
} else if let lhs = lhsValue as? String, let rhs = rhsValue as? String { } else if let lhs = lhsValue as? String, let rhs = rhsValue as? String {
return rhs.contains(lhs) return rhs.contains(lhs)
} else if lhsValue == nil && rhsValue == nil { } else if lhsValue == nil && rhsValue == nil {
return true return true
} }
} }
return false return false
} }
} }
final class OrExpression: Expression, InfixOperator, CustomStringConvertible { final class OrExpression: Expression, InfixOperator, CustomStringConvertible {
let lhs: Expression let lhs: Expression
let rhs: Expression let rhs: Expression
init(lhs: Expression, rhs: Expression) { init(lhs: Expression, rhs: Expression) {
self.lhs = lhs self.lhs = lhs
self.rhs = rhs self.rhs = rhs
} }
var description: String { var description: String {
"(\(lhs) or \(rhs))" "(\(lhs) or \(rhs))"
} }
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
let lhs = try self.lhs.evaluate(context: context) let lhs = try self.lhs.evaluate(context: context)
if lhs { if lhs {
return lhs return lhs
} }
return try rhs.evaluate(context: context) return try rhs.evaluate(context: context)
} }
} }
final class AndExpression: Expression, InfixOperator, CustomStringConvertible { final class AndExpression: Expression, InfixOperator, CustomStringConvertible {
let lhs: Expression let lhs: Expression
let rhs: Expression let rhs: Expression
init(lhs: Expression, rhs: Expression) { init(lhs: Expression, rhs: Expression) {
self.lhs = lhs self.lhs = lhs
self.rhs = rhs self.rhs = rhs
} }
var description: String { var description: String {
"(\(lhs) and \(rhs))" "(\(lhs) and \(rhs))"
} }
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
let lhs = try self.lhs.evaluate(context: context) let lhs = try self.lhs.evaluate(context: context)
if !lhs { if !lhs {
return lhs return lhs
} }
return try rhs.evaluate(context: context) return try rhs.evaluate(context: context)
} }
} }
class EqualityExpression: Expression, InfixOperator, CustomStringConvertible { class EqualityExpression: Expression, InfixOperator, CustomStringConvertible {
let lhs: Expression let lhs: Expression
let rhs: Expression let rhs: Expression
required init(lhs: Expression, rhs: Expression) { required init(lhs: Expression, rhs: Expression) {
self.lhs = lhs self.lhs = lhs
self.rhs = rhs self.rhs = rhs
} }
var description: String { var description: String {
"(\(lhs) == \(rhs))" "(\(lhs) == \(rhs))"
} }
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression { if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression {
let lhsValue = try lhs.variable.resolve(context) let lhsValue = try lhs.variable.resolve(context)
let rhsValue = try rhs.variable.resolve(context) let rhsValue = try rhs.variable.resolve(context)
if let lhs = lhsValue, let rhs = rhsValue { if let lhs = lhsValue, let rhs = rhsValue {
if let lhs = toNumber(value: lhs), let rhs = toNumber(value: rhs) { if let lhs = toNumber(value: lhs), let rhs = toNumber(value: rhs) {
return lhs == rhs return lhs == rhs
} else if let lhs = lhsValue as? String, let rhs = rhsValue as? String { } else if let lhs = lhsValue as? String, let rhs = rhsValue as? String {
return lhs == rhs return lhs == rhs
} else if let lhs = lhsValue as? Bool, let rhs = rhsValue as? Bool { } else if let lhs = lhsValue as? Bool, let rhs = rhsValue as? Bool {
return lhs == rhs return lhs == rhs
} }
} else if lhsValue == nil && rhsValue == nil { } else if lhsValue == nil && rhsValue == nil {
return true return true
} }
} }
return false return false
} }
} }
class NumericExpression: Expression, InfixOperator, CustomStringConvertible { class NumericExpression: Expression, InfixOperator, CustomStringConvertible {
let lhs: Expression let lhs: Expression
let rhs: Expression let rhs: Expression
required init(lhs: Expression, rhs: Expression) { required init(lhs: Expression, rhs: Expression) {
self.lhs = lhs self.lhs = lhs
self.rhs = rhs self.rhs = rhs
} }
var description: String { var description: String {
"(\(lhs) \(symbol) \(rhs))" "(\(lhs) \(symbol) \(rhs))"
} }
func evaluate(context: Context) throws -> Bool { func evaluate(context: Context) throws -> Bool {
if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression { if let lhs = lhs as? VariableExpression, let rhs = rhs as? VariableExpression {
let lhsValue = try lhs.variable.resolve(context) let lhsValue = try lhs.variable.resolve(context)
let rhsValue = try rhs.variable.resolve(context) let rhsValue = try rhs.variable.resolve(context)
if let lhs = lhsValue, let rhs = rhsValue { if let lhs = lhsValue, let rhs = rhsValue {
if let lhs = toNumber(value: lhs), let rhs = toNumber(value: rhs) { if let lhs = toNumber(value: lhs), let rhs = toNumber(value: rhs) {
return compare(lhs: lhs, rhs: rhs) return compare(lhs: lhs, rhs: rhs)
} }
} }
} }
return false return false
} }
var symbol: String { var symbol: String {
"" ""
} }
func compare(lhs: Number, rhs: Number) -> Bool { func compare(lhs: Number, rhs: Number) -> Bool {
false false
} }
} }
class MoreThanExpression: NumericExpression { class MoreThanExpression: NumericExpression {
override var symbol: String { override var symbol: String {
">" ">"
} }
override func compare(lhs: Number, rhs: Number) -> Bool { override func compare(lhs: Number, rhs: Number) -> Bool {
lhs > rhs lhs > rhs
} }
} }
class MoreThanEqualExpression: NumericExpression { class MoreThanEqualExpression: NumericExpression {
override var symbol: String { override var symbol: String {
">=" ">="
} }
override func compare(lhs: Number, rhs: Number) -> Bool { override func compare(lhs: Number, rhs: Number) -> Bool {
lhs >= rhs lhs >= rhs
} }
} }
class LessThanExpression: NumericExpression { class LessThanExpression: NumericExpression {
override var symbol: String { override var symbol: String {
"<" "<"
} }
override func compare(lhs: Number, rhs: Number) -> Bool { override func compare(lhs: Number, rhs: Number) -> Bool {
lhs < rhs lhs < rhs
} }
} }
class LessThanEqualExpression: NumericExpression { class LessThanEqualExpression: NumericExpression {
override var symbol: String { override var symbol: String {
"<=" "<="
} }
override func compare(lhs: Number, rhs: Number) -> Bool { override func compare(lhs: Number, rhs: Number) -> Bool {
lhs <= rhs lhs <= rhs
} }
} }
class InequalityExpression: EqualityExpression { class InequalityExpression: EqualityExpression {
override var description: String { override var description: String {
"(\(lhs) != \(rhs))" "(\(lhs) != \(rhs))"
} }
override func evaluate(context: Context) throws -> Bool { override func evaluate(context: Context) throws -> Bool {
try !super.evaluate(context: context) try !super.evaluate(context: context)
} }
} }
// swiftlint:disable:next cyclomatic_complexity // swiftlint:disable:next cyclomatic_complexity
func toNumber(value: Any) -> Number? { func toNumber(value: Any) -> Number? {
if let value = value as? Float { if let value = value as? Float {
return Number(value) return Number(value)
} else if let value = value as? Double { } else if let value = value as? Double {
return Number(value) return Number(value)
} else if let value = value as? UInt { } else if let value = value as? UInt {
return Number(value) return Number(value)
} else if let value = value as? Int { } else if let value = value as? Int {
return Number(value) return Number(value)
} else if let value = value as? Int8 { } else if let value = value as? Int8 {
return Number(value) return Number(value)
} else if let value = value as? Int16 { } else if let value = value as? Int16 {
return Number(value) return Number(value)
} else if let value = value as? Int32 { } else if let value = value as? Int32 {
return Number(value) return Number(value)
} else if let value = value as? Int64 { } else if let value = value as? Int64 {
return Number(value) return Number(value)
} else if let value = value as? UInt8 { } else if let value = value as? UInt8 {
return Number(value) return Number(value)
} else if let value = value as? UInt16 { } else if let value = value as? UInt16 {
return Number(value) return Number(value)
} else if let value = value as? UInt32 { } else if let value = value as? UInt32 {
return Number(value) return Number(value)
} else if let value = value as? UInt64 { } else if let value = value as? UInt64 {
return Number(value) return Number(value)
} else if let value = value as? Number { } else if let value = value as? Number {
return value return value
} else if let value = value as? Float64 { } else if let value = value as? Float64 {
return Number(value) return Number(value)
} else if let value = value as? Float32 { } else if let value = value as? Float32 {
return Number(value) return Number(value)
} }
return nil return nil
} }

View File

@@ -1,103 +1,103 @@
/// Container for registered tags and filters /// Container for registered tags and filters
open class Extension { open class Extension {
typealias TagParser = (TokenParser, Token) throws -> NodeType typealias TagParser = (TokenParser, Token) throws -> NodeType
var tags = [String: TagParser]() var tags = [String: TagParser]()
var filters = [String: Filter]() var filters = [String: Filter]()
/// Simple initializer /// Simple initializer
public init() { public init() {
} }
/// Registers a new template tag /// Registers a new template tag
public func registerTag(_ name: String, parser: @escaping (TokenParser, Token) throws -> NodeType) { public func registerTag(_ name: String, parser: @escaping (TokenParser, Token) throws -> NodeType) {
tags[name] = parser tags[name] = parser
} }
/// Registers a simple template tag with a name and a handler /// Registers a simple template tag with a name and a handler
public func registerSimpleTag(_ name: String, handler: @escaping (Context) throws -> String) { public func registerSimpleTag(_ name: String, handler: @escaping (Context) throws -> String) {
registerTag(name) { _, token in registerTag(name) { _, token in
SimpleNode(token: token, handler: handler) SimpleNode(token: token, handler: handler)
} }
} }
/// Registers boolean filter with it's negative counterpart /// Registers boolean filter with it's negative counterpart
public func registerFilter(name: String, negativeFilterName: String, filter: @escaping (Any?) throws -> Bool?) { public func registerFilter(name: String, negativeFilterName: String, filter: @escaping (Any?) throws -> Bool?) {
// swiftlint:disable:previous discouraged_optional_boolean // swiftlint:disable:previous discouraged_optional_boolean
filters[name] = .simple(filter) filters[name] = .simple(filter)
filters[negativeFilterName] = .simple { value in filters[negativeFilterName] = .simple { value in
guard let result = try filter(value) else { return nil } guard let result = try filter(value) else { return nil }
return !result return !result
} }
} }
/// Registers a template filter with the given name /// Registers a template filter with the given name
public func registerFilter(_ name: String, filter: @escaping (Any?) throws -> Any?) { public func registerFilter(_ name: String, filter: @escaping (Any?) throws -> Any?) {
filters[name] = .simple(filter) filters[name] = .simple(filter)
} }
/// Registers a template filter with the given name /// Registers a template filter with the given name
public func registerFilter(_ name: String, filter: @escaping (Any?, [Any?]) throws -> Any?) { public func registerFilter(_ name: String, filter: @escaping (Any?, [Any?]) throws -> Any?) {
filters[name] = .arguments { value, args, _ in try filter(value, args) } filters[name] = .arguments { value, args, _ in try filter(value, args) }
} }
/// Registers a template filter with the given name /// Registers a template filter with the given name
public func registerFilter(_ name: String, filter: @escaping (Any?, [Any?], Context) throws -> Any?) { public func registerFilter(_ name: String, filter: @escaping (Any?, [Any?], Context) throws -> Any?) {
filters[name] = .arguments(filter) filters[name] = .arguments(filter)
} }
} }
class DefaultExtension: Extension { class DefaultExtension: Extension {
override init() { override init() {
super.init() super.init()
registerDefaultTags() registerDefaultTags()
registerDefaultFilters() registerDefaultFilters()
} }
fileprivate func registerDefaultTags() { fileprivate func registerDefaultTags() {
registerTag("for", parser: ForNode.parse) registerTag("for", parser: ForNode.parse)
registerTag("break", parser: LoopTerminationNode.parse) registerTag("break", parser: LoopTerminationNode.parse)
registerTag("continue", parser: LoopTerminationNode.parse) registerTag("continue", parser: LoopTerminationNode.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) #if !os(Linux)
registerTag("now", parser: NowNode.parse) registerTag("now", parser: NowNode.parse)
#endif #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)
registerTag("filter", parser: FilterNode.parse) registerTag("filter", parser: FilterNode.parse)
} }
fileprivate func registerDefaultFilters() { fileprivate func registerDefaultFilters() {
registerFilter("default", filter: defaultFilter) registerFilter("default", filter: defaultFilter)
registerFilter("capitalize", filter: capitalise) registerFilter("capitalize", filter: capitalise)
registerFilter("uppercase", filter: uppercase) registerFilter("uppercase", filter: uppercase)
registerFilter("lowercase", filter: lowercase) registerFilter("lowercase", filter: lowercase)
registerFilter("join", filter: joinFilter) registerFilter("join", filter: joinFilter)
registerFilter("split", filter: splitFilter) registerFilter("split", filter: splitFilter)
registerFilter("indent", filter: indentFilter) registerFilter("indent", filter: indentFilter)
registerFilter("filter", filter: filterFilter) registerFilter("filter", filter: filterFilter)
} }
} }
protocol FilterType { protocol FilterType {
func invoke(value: Any?, arguments: [Any?], context: Context) throws -> Any? func invoke(value: Any?, arguments: [Any?], context: Context) throws -> Any?
} }
enum Filter: FilterType { enum Filter: FilterType {
case simple(((Any?) throws -> Any?)) case simple(((Any?) throws -> Any?))
case arguments(((Any?, [Any?], Context) throws -> Any?)) case arguments(((Any?, [Any?], Context) throws -> Any?))
func invoke(value: Any?, arguments: [Any?], context: Context) throws -> Any? { func invoke(value: Any?, arguments: [Any?], context: Context) throws -> Any? {
switch self { switch self {
case let .simple(filter): case let .simple(filter):
if !arguments.isEmpty { if !arguments.isEmpty {
throw TemplateSyntaxError("Can't invoke filter with an argument") throw TemplateSyntaxError("Can't invoke filter with an argument")
} }
return try filter(value) return try filter(value)
case let .arguments(filter): case let .arguments(filter):
return try filter(value, arguments, context) return try filter(value, arguments, context)
} }
} }
} }

View File

@@ -1,36 +1,36 @@
class FilterNode: NodeType { class FilterNode: NodeType {
let resolvable: Resolvable let resolvable: Resolvable
let nodes: [NodeType] let nodes: [NodeType]
let token: Token? let token: Token?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let bits = token.components let bits = token.components
guard bits.count == 2 else { guard bits.count == 2 else {
throw TemplateSyntaxError("'filter' tag takes one argument, the filter expression") throw TemplateSyntaxError("'filter' tag takes one argument, the filter expression")
} }
let blocks = try parser.parse(until(["endfilter"])) let blocks = try parser.parse(until(["endfilter"]))
guard parser.nextToken() != nil else { guard parser.nextToken() != nil else {
throw TemplateSyntaxError("`endfilter` was not found.") throw TemplateSyntaxError("`endfilter` was not found.")
} }
let resolvable = try parser.compileFilter("filter_value|\(bits[1])", containedIn: token) let resolvable = try parser.compileFilter("filter_value|\(bits[1])", containedIn: token)
return FilterNode(nodes: blocks, resolvable: resolvable, token: token) return FilterNode(nodes: blocks, resolvable: resolvable, token: token)
} }
init(nodes: [NodeType], resolvable: Resolvable, token: Token) { init(nodes: [NodeType], resolvable: Resolvable, token: Token) {
self.nodes = nodes self.nodes = nodes
self.resolvable = resolvable self.resolvable = resolvable
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
let value = try renderNodes(nodes, context) let value = try renderNodes(nodes, context)
return try context.push(dictionary: ["filter_value": value]) { return try context.push(dictionary: ["filter_value": value]) {
try VariableNode(variable: resolvable, token: token).render(context) try VariableNode(variable: resolvable, token: token).render(context)
} }
} }
} }

View File

@@ -1,133 +1,133 @@
func capitalise(_ value: Any?) -> Any? { func capitalise(_ value: Any?) -> Any? {
if let array = value as? [Any?] { if let array = value as? [Any?] {
return array.map { stringify($0).capitalized } return array.map { stringify($0).capitalized }
} else { } else {
return stringify(value).capitalized return stringify(value).capitalized
} }
} }
func uppercase(_ value: Any?) -> Any? { func uppercase(_ value: Any?) -> Any? {
if let array = value as? [Any?] { if let array = value as? [Any?] {
return array.map { stringify($0).uppercased() } return array.map { stringify($0).uppercased() }
} else { } else {
return stringify(value).uppercased() return stringify(value).uppercased()
} }
} }
func lowercase(_ value: Any?) -> Any? { func lowercase(_ value: Any?) -> Any? {
if let array = value as? [Any?] { if let array = value as? [Any?] {
return array.map { stringify($0).lowercased() } return array.map { stringify($0).lowercased() }
} else { } else {
return stringify(value).lowercased() return stringify(value).lowercased()
} }
} }
func defaultFilter(value: Any?, arguments: [Any?]) -> Any? { func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
// value can be optional wrapping nil, so this way we check for underlying value // value can be optional wrapping nil, so this way we check for underlying value
if let value = value, String(describing: value) != "nil" { if let value = value, String(describing: value) != "nil" {
return value return value
} }
for argument in arguments { for argument in arguments {
if let argument = argument { if let argument = argument {
return argument return argument
} }
} }
return nil return nil
} }
func joinFilter(value: Any?, arguments: [Any?]) throws -> Any? { func joinFilter(value: Any?, arguments: [Any?]) throws -> Any? {
guard arguments.count < 2 else { guard arguments.count < 2 else {
throw TemplateSyntaxError("'join' filter takes at most one argument") throw TemplateSyntaxError("'join' filter takes at most one argument")
} }
let separator = stringify(arguments.first ?? "") let separator = stringify(arguments.first ?? "")
if let value = value as? [Any] { if let value = value as? [Any] {
return value return value
.map(stringify) .map(stringify)
.joined(separator: separator) .joined(separator: separator)
} }
return value return value
} }
func splitFilter(value: Any?, arguments: [Any?]) throws -> Any? { func splitFilter(value: Any?, arguments: [Any?]) throws -> Any? {
guard arguments.count < 2 else { guard arguments.count < 2 else {
throw TemplateSyntaxError("'split' filter takes at most one argument") throw TemplateSyntaxError("'split' filter takes at most one argument")
} }
let separator = stringify(arguments.first ?? " ") let separator = stringify(arguments.first ?? " ")
if let value = value as? String { if let value = value as? String {
return value.components(separatedBy: separator) return value.components(separatedBy: separator)
} }
return value return value
} }
func indentFilter(value: Any?, arguments: [Any?]) throws -> Any? { func indentFilter(value: Any?, arguments: [Any?]) throws -> Any? {
guard arguments.count <= 3 else { guard arguments.count <= 3 else {
throw TemplateSyntaxError("'indent' filter can take at most 3 arguments") throw TemplateSyntaxError("'indent' filter can take at most 3 arguments")
} }
var indentWidth = 4 var indentWidth = 4
if !arguments.isEmpty { if !arguments.isEmpty {
guard let value = arguments[0] as? Int else { guard let value = arguments[0] as? Int else {
throw TemplateSyntaxError( throw TemplateSyntaxError(
""" """
'indent' filter width argument must be an Integer (\(String(describing: arguments[0]))) 'indent' filter width argument must be an Integer (\(String(describing: arguments[0])))
""" """
) )
} }
indentWidth = value indentWidth = value
} }
var indentationChar = " " var indentationChar = " "
if arguments.count > 1 { if arguments.count > 1 {
guard let value = arguments[1] as? String else { guard let value = arguments[1] as? String else {
throw TemplateSyntaxError( throw TemplateSyntaxError(
""" """
'indent' filter indentation argument must be a String (\(String(describing: arguments[1])) 'indent' filter indentation argument must be a String (\(String(describing: arguments[1]))
""" """
) )
} }
indentationChar = value indentationChar = value
} }
var indentFirst = false var indentFirst = false
if arguments.count > 2 { if arguments.count > 2 {
guard let value = arguments[2] as? Bool else { guard let value = arguments[2] as? Bool else {
throw TemplateSyntaxError("'indent' filter indentFirst argument must be a Bool") throw TemplateSyntaxError("'indent' filter indentFirst argument must be a Bool")
} }
indentFirst = value indentFirst = value
} }
let indentation = [String](repeating: indentationChar, count: indentWidth).joined() let indentation = [String](repeating: indentationChar, count: indentWidth).joined()
return indent(stringify(value), indentation: indentation, indentFirst: indentFirst) return indent(stringify(value), indentation: indentation, indentFirst: indentFirst)
} }
func indent(_ content: String, indentation: String, indentFirst: Bool) -> String { func indent(_ content: String, indentation: String, indentFirst: Bool) -> String {
guard !indentation.isEmpty else { return content } guard !indentation.isEmpty else { return content }
var lines = content.components(separatedBy: .newlines) var lines = content.components(separatedBy: .newlines)
let firstLine = (indentFirst ? indentation : "") + lines.removeFirst() let firstLine = (indentFirst ? indentation : "") + lines.removeFirst()
let result = lines.reduce(into: [firstLine]) { result, line in let result = lines.reduce(into: [firstLine]) { result, line in
result.append(line.isEmpty ? "" : "\(indentation)\(line)") result.append(line.isEmpty ? "" : "\(indentation)\(line)")
} }
return result.joined(separator: "\n") return result.joined(separator: "\n")
} }
func filterFilter(value: Any?, arguments: [Any?], context: Context) throws -> Any? { func filterFilter(value: Any?, arguments: [Any?], context: Context) throws -> Any? {
guard let value = value else { return nil } guard let value = value else { return nil }
guard arguments.count == 1 else { guard arguments.count == 1 else {
throw TemplateSyntaxError("'filter' filter takes one argument") throw TemplateSyntaxError("'filter' filter takes one argument")
} }
let attribute = stringify(arguments[0]) let attribute = stringify(arguments[0])
let expr = try context.environment.compileFilter("$0|\(attribute)") let expr = try context.environment.compileFilter("$0|\(attribute)")
return try context.push(dictionary: ["$0": value]) { return try context.push(dictionary: ["$0": value]) {
try expr.resolve(context) try expr.resolve(context)
} }
} }

View File

@@ -1,274 +1,274 @@
import Foundation import Foundation
class ForNode: NodeType { class ForNode: NodeType {
let resolvable: Resolvable let resolvable: Resolvable
let loopVariables: [String] let loopVariables: [String]
let nodes: [NodeType] let nodes: [NodeType]
let emptyNodes: [NodeType] let emptyNodes: [NodeType]
let `where`: Expression? let `where`: Expression?
let label: String? let label: String?
let token: Token? let token: Token?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
var components = token.components var components = token.components
var label: String? var label: String?
if components.first?.hasSuffix(":") == true { if components.first?.hasSuffix(":") == true {
label = String(components.removeFirst().dropLast()) label = String(components.removeFirst().dropLast())
} }
func hasToken(_ token: String, at index: Int) -> Bool { func hasToken(_ token: String, at index: Int) -> Bool {
components.count > (index + 1) && components[index] == token components.count > (index + 1) && components[index] == token
} }
func endsOrHasToken(_ token: String, at index: Int) -> Bool { func endsOrHasToken(_ token: String, at index: Int) -> Bool {
components.count == index || hasToken(token, at: index) components.count == index || hasToken(token, at: index)
} }
guard hasToken("in", at: 2) && endsOrHasToken("where", at: 4) else { guard hasToken("in", at: 2) && endsOrHasToken("where", at: 4) else {
throw TemplateSyntaxError("'for' statements should use the syntax: `for <x> in <y> [where <condition>]`.") throw TemplateSyntaxError("'for' statements should use the syntax: `for <x> in <y> [where <condition>]`.")
} }
let loopVariables = components[1] let loopVariables = components[1]
.split(separator: ",") .split(separator: ",")
.map(String.init) .map(String.init)
.map { $0.trim(character: " ") } .map { $0.trim(character: " ") }
let resolvable = try parser.compileResolvable(components[3], containedIn: token) let resolvable = try parser.compileResolvable(components[3], containedIn: token)
let `where` = hasToken("where", at: 4) let `where` = hasToken("where", at: 4)
? try parser.compileExpression(components: Array(components.suffix(from: 5)), token: token) ? try parser.compileExpression(components: Array(components.suffix(from: 5)), token: token)
: nil : nil
let forNodes = try parser.parse(until(["endfor", "empty"])) let forNodes = try parser.parse(until(["endfor", "empty"]))
guard let token = parser.nextToken() else { guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endfor` was not found.") throw TemplateSyntaxError("`endfor` was not found.")
} }
var emptyNodes = [NodeType]() var emptyNodes = [NodeType]()
if token.contents == "empty" { if token.contents == "empty" {
emptyNodes = try parser.parse(until(["endfor"])) emptyNodes = try parser.parse(until(["endfor"]))
_ = parser.nextToken() _ = parser.nextToken()
} }
return ForNode( return ForNode(
resolvable: resolvable, resolvable: resolvable,
loopVariables: loopVariables, loopVariables: loopVariables,
nodes: forNodes, nodes: forNodes,
emptyNodes: emptyNodes, emptyNodes: emptyNodes,
where: `where`, where: `where`,
label: label, label: label,
token: token token: token
) )
} }
init( init(
resolvable: Resolvable, resolvable: Resolvable,
loopVariables: [String], loopVariables: [String],
nodes: [NodeType], nodes: [NodeType],
emptyNodes: [NodeType], emptyNodes: [NodeType],
where: Expression? = nil, where: Expression? = nil,
label: String? = nil, label: String? = nil,
token: Token? = nil token: Token? = nil
) { ) {
self.resolvable = resolvable self.resolvable = resolvable
self.loopVariables = loopVariables self.loopVariables = loopVariables
self.nodes = nodes self.nodes = nodes
self.emptyNodes = emptyNodes self.emptyNodes = emptyNodes
self.where = `where` self.where = `where`
self.label = label self.label = label
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
var values = try resolve(context) var values = try resolve(context)
if let `where` = self.where { if let `where` = self.where {
values = try values.filter { item -> Bool in values = try values.filter { item -> Bool in
try push(value: item, context: context) { try push(value: item, context: context) {
try `where`.evaluate(context: context) try `where`.evaluate(context: context)
} }
} }
} }
if !values.isEmpty { if !values.isEmpty {
let count = values.count let count = values.count
var result = "" var result = ""
// collect parent loop contexts // collect parent loop contexts
let parentLoopContexts = (context["forloop"] as? [String: Any])? let parentLoopContexts = (context["forloop"] as? [String: Any])?
.filter { ($1 as? [String: Any])?["label"] != nil } ?? [:] .filter { ($1 as? [String: Any])?["label"] != nil } ?? [:]
for (index, item) in zip(0..., values) { for (index, item) in zip(0..., values) {
var forContext: [String: Any] = [ var forContext: [String: Any] = [
"first": index == 0, "first": index == 0,
"last": index == (count - 1), "last": index == (count - 1),
"counter": index + 1, "counter": index + 1,
"counter0": index, "counter0": index,
"length": count "length": count
] ]
if let label = label { if let label = label {
forContext["label"] = label forContext["label"] = label
forContext[label] = forContext forContext[label] = forContext
} }
forContext.merge(parentLoopContexts) { lhs, _ in lhs } forContext.merge(parentLoopContexts) { lhs, _ in lhs }
var shouldBreak = false var shouldBreak = false
result += try context.push(dictionary: ["forloop": forContext]) { result += try context.push(dictionary: ["forloop": forContext]) {
defer { defer {
// if outer loop should be continued we should break from current loop // if outer loop should be continued we should break from current loop
if let shouldContinueLabel = context[LoopTerminationNode.continueContextKey] as? String { if let shouldContinueLabel = context[LoopTerminationNode.continueContextKey] as? String {
shouldBreak = shouldContinueLabel != label || label == nil shouldBreak = shouldContinueLabel != label || label == nil
} else { } else {
shouldBreak = context[LoopTerminationNode.breakContextKey] != nil shouldBreak = context[LoopTerminationNode.breakContextKey] != nil
} }
} }
return try push(value: item, context: context) { return try push(value: item, context: context) {
try renderNodes(nodes, context) try renderNodes(nodes, context)
} }
} }
if shouldBreak { if shouldBreak {
break break
} }
} }
return result return result
} else { } else {
return try context.push { return try context.push {
try renderNodes(emptyNodes, context) try renderNodes(emptyNodes, context)
} }
} }
} }
private func push<Result>(value: Any, context: Context, closure: () throws -> (Result)) throws -> Result { private func push<Result>(value: Any, context: Context, closure: () throws -> (Result)) throws -> Result {
if loopVariables.isEmpty { if loopVariables.isEmpty {
return try context.push { return try context.push {
try closure() try closure()
} }
} }
let valueMirror = Mirror(reflecting: value) let valueMirror = Mirror(reflecting: value)
if case .tuple? = valueMirror.displayStyle { if case .tuple? = valueMirror.displayStyle {
if loopVariables.count > Int(valueMirror.children.count) { if loopVariables.count > Int(valueMirror.children.count) {
throw TemplateSyntaxError("Tuple '\(value)' has less values than loop variables") throw TemplateSyntaxError("Tuple '\(value)' has less values than loop variables")
} }
var variablesContext = [String: Any]() var variablesContext = [String: Any]()
valueMirror.children.prefix(loopVariables.count).enumerated().forEach { offset, element in valueMirror.children.prefix(loopVariables.count).enumerated().forEach { offset, element in
if loopVariables[offset] != "_" { if loopVariables[offset] != "_" {
variablesContext[loopVariables[offset]] = element.value variablesContext[loopVariables[offset]] = element.value
} }
} }
return try context.push(dictionary: variablesContext) { return try context.push(dictionary: variablesContext) {
try closure() try closure()
} }
} }
return try context.push(dictionary: [loopVariables.first ?? "": value]) { return try context.push(dictionary: [loopVariables.first ?? "": value]) {
try closure() try closure()
} }
} }
private func resolve(_ context: Context) throws -> [Any] { private func resolve(_ context: Context) throws -> [Any] {
let resolved = try resolvable.resolve(context) let resolved = try resolvable.resolve(context)
var values: [Any] var values: [Any]
if let dictionary = resolved as? [String: Any], !dictionary.isEmpty { if let dictionary = resolved as? [String: Any], !dictionary.isEmpty {
values = dictionary.sorted { $0.key < $1.key } values = dictionary.sorted { $0.key < $1.key }
} else if let array = resolved as? [Any] { } else if let array = resolved as? [Any] {
values = array values = array
} else if let range = resolved as? CountableClosedRange<Int> { } else if let range = resolved as? CountableClosedRange<Int> {
values = Array(range) values = Array(range)
} else if let range = resolved as? CountableRange<Int> { } else if let range = resolved as? CountableRange<Int> {
values = Array(range) values = Array(range)
} else if let resolved = resolved { } else if let resolved = resolved {
let mirror = Mirror(reflecting: resolved) let mirror = Mirror(reflecting: resolved)
switch mirror.displayStyle { switch mirror.displayStyle {
case .struct, .tuple: case .struct, .tuple:
values = Array(mirror.children) values = Array(mirror.children)
case .class: case .class:
var children = Array(mirror.children) var children = Array(mirror.children)
var currentMirror: Mirror? = mirror var currentMirror: Mirror? = mirror
while let superclassMirror = currentMirror?.superclassMirror { while let superclassMirror = currentMirror?.superclassMirror {
children.append(contentsOf: superclassMirror.children) children.append(contentsOf: superclassMirror.children)
currentMirror = superclassMirror currentMirror = superclassMirror
} }
values = Array(children) values = Array(children)
default: default:
values = [] values = []
} }
} else { } else {
values = [] values = []
} }
return values return values
} }
} }
struct LoopTerminationNode: NodeType { struct LoopTerminationNode: NodeType {
static let breakContextKey = "_internal_forloop_break" static let breakContextKey = "_internal_forloop_break"
static let continueContextKey = "_internal_forloop_continue" static let continueContextKey = "_internal_forloop_continue"
let name: String let name: String
let label: String? let label: String?
let token: Token? let token: Token?
var contextKey: String { var contextKey: String {
"_internal_forloop_\(name)" "_internal_forloop_\(name)"
} }
private init(name: String, label: String? = nil, token: Token? = nil) { private init(name: String, label: String? = nil, token: Token? = nil) {
self.name = name self.name = name
self.label = label self.label = label
self.token = token self.token = token
} }
static func parse(_ parser: TokenParser, token: Token) throws -> Self { static func parse(_ parser: TokenParser, token: Token) throws -> Self {
let components = token.components let components = token.components
guard components.count <= 2 else { guard components.count <= 2 else {
throw TemplateSyntaxError("'\(token.contents)' can accept only one parameter") throw TemplateSyntaxError("'\(token.contents)' can accept only one parameter")
} }
guard parser.hasOpenedForTag() else { guard parser.hasOpenedForTag() else {
throw TemplateSyntaxError("'\(token.contents)' can be used only inside loop body") throw TemplateSyntaxError("'\(token.contents)' can be used only inside loop body")
} }
return Self(name: components[0], label: components.count == 2 ? components[1] : nil, token: token) return Self(name: components[0], label: components.count == 2 ? components[1] : nil, token: token)
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
let offset = zip(0..., context.dictionaries).reversed().first { _, dictionary in let offset = zip(0..., context.dictionaries).reversed().first { _, dictionary in
guard let forContext = dictionary["forloop"] as? [String: Any], guard let forContext = dictionary["forloop"] as? [String: Any],
dictionary["forloop"] != nil else { return false } dictionary["forloop"] != nil else { return false }
if let label = label { if let label = label {
return label == forContext["label"] as? String return label == forContext["label"] as? String
} else { } else {
return true return true
} }
}?.0 }?.0
if let offset = offset { if let offset = offset {
context.dictionaries[offset][contextKey] = label ?? true context.dictionaries[offset][contextKey] = label ?? true
} else if let label = label { } else if let label = label {
throw TemplateSyntaxError("No loop labeled '\(label)' is currently running") throw TemplateSyntaxError("No loop labeled '\(label)' is currently running")
} else { } else {
throw TemplateSyntaxError("No loop is currently running") throw TemplateSyntaxError("No loop is currently running")
} }
return "" return ""
} }
} }
private extension TokenParser { private extension TokenParser {
func hasOpenedForTag() -> Bool { func hasOpenedForTag() -> Bool {
var openForCount = 0 var openForCount = 0
for parsedToken in parsedTokens.reversed() where parsedToken.kind == .block { for parsedToken in parsedTokens.reversed() where parsedToken.kind == .block {
if parsedToken.components.first == "endfor" { openForCount -= 1 } if parsedToken.components.first == "endfor" { openForCount -= 1 }
if parsedToken.components.first == "for" { openForCount += 1 } if parsedToken.components.first == "for" { openForCount += 1 }
} }
return openForCount > 0 return openForCount > 0
} }
} }

View File

@@ -1,314 +1,314 @@
enum Operator { enum Operator {
case infix(String, Int, InfixOperator.Type) case infix(String, Int, InfixOperator.Type)
case prefix(String, Int, PrefixOperator.Type) case prefix(String, Int, PrefixOperator.Type)
var name: String { var name: String {
switch self { switch self {
case .infix(let name, _, _): case .infix(let name, _, _):
return name return name
case .prefix(let name, _, _): case .prefix(let name, _, _):
return name return name
} }
} }
static let all: [Self] = [ static let all: [Self] = [
.infix("in", 5, InExpression.self), .infix("in", 5, InExpression.self),
.infix("or", 6, OrExpression.self), .infix("or", 6, OrExpression.self),
.infix("and", 7, AndExpression.self), .infix("and", 7, AndExpression.self),
.prefix("not", 8, NotExpression.self), .prefix("not", 8, NotExpression.self),
.infix("==", 10, EqualityExpression.self), .infix("==", 10, EqualityExpression.self),
.infix("!=", 10, InequalityExpression.self), .infix("!=", 10, InequalityExpression.self),
.infix(">", 10, MoreThanExpression.self), .infix(">", 10, MoreThanExpression.self),
.infix(">=", 10, MoreThanEqualExpression.self), .infix(">=", 10, MoreThanEqualExpression.self),
.infix("<", 10, LessThanExpression.self), .infix("<", 10, LessThanExpression.self),
.infix("<=", 10, LessThanEqualExpression.self) .infix("<=", 10, LessThanEqualExpression.self)
] ]
} }
func findOperator(name: String) -> Operator? { func findOperator(name: String) -> Operator? {
for `operator` in Operator.all where `operator`.name == name { for `operator` in Operator.all where `operator`.name == name {
return `operator` return `operator`
} }
return nil return nil
} }
indirect enum IfToken { indirect enum IfToken {
case infix(name: String, bindingPower: Int, operatorType: InfixOperator.Type) case infix(name: String, bindingPower: Int, operatorType: InfixOperator.Type)
case prefix(name: String, bindingPower: Int, operatorType: PrefixOperator.Type) case prefix(name: String, bindingPower: Int, operatorType: PrefixOperator.Type)
case variable(Resolvable) case variable(Resolvable)
case subExpression(Expression) case subExpression(Expression)
case end case end
var bindingPower: Int { var bindingPower: Int {
switch self { switch self {
case .infix(_, let bindingPower, _): case .infix(_, let bindingPower, _):
return bindingPower return bindingPower
case .prefix(_, let bindingPower, _): case .prefix(_, let bindingPower, _):
return bindingPower return bindingPower
case .variable: case .variable:
return 0 return 0
case .subExpression: case .subExpression:
return 0 return 0
case .end: case .end:
return 0 return 0
} }
} }
func nullDenotation(parser: IfExpressionParser) throws -> Expression { func nullDenotation(parser: IfExpressionParser) throws -> Expression {
switch self { switch self {
case .infix(let name, _, _): case .infix(let name, _, _):
throw TemplateSyntaxError("'if' expression error: infix operator '\(name)' doesn't have a left hand side") throw TemplateSyntaxError("'if' expression error: infix operator '\(name)' doesn't have a left hand side")
case .prefix(_, let bindingPower, let operatorType): case .prefix(_, let bindingPower, let operatorType):
let expression = try parser.expression(bindingPower: bindingPower) let expression = try parser.expression(bindingPower: bindingPower)
return operatorType.init(expression: expression) return operatorType.init(expression: expression)
case .variable(let variable): case .variable(let variable):
return VariableExpression(variable: variable) return VariableExpression(variable: variable)
case .subExpression(let expression): case .subExpression(let expression):
return expression return expression
case .end: case .end:
throw TemplateSyntaxError("'if' expression error: end") throw TemplateSyntaxError("'if' expression error: end")
} }
} }
func leftDenotation(left: Expression, parser: IfExpressionParser) throws -> Expression { func leftDenotation(left: Expression, parser: IfExpressionParser) throws -> Expression {
switch self { switch self {
case .infix(_, let bindingPower, let operatorType): case .infix(_, let bindingPower, let operatorType):
let right = try parser.expression(bindingPower: bindingPower) let right = try parser.expression(bindingPower: bindingPower)
return operatorType.init(lhs: left, rhs: right) return operatorType.init(lhs: left, rhs: right)
case .prefix(let name, _, _): case .prefix(let name, _, _):
throw TemplateSyntaxError("'if' expression error: prefix operator '\(name)' was called with a left hand side") throw TemplateSyntaxError("'if' expression error: prefix operator '\(name)' was called with a left hand side")
case .variable(let variable): case .variable(let variable):
throw TemplateSyntaxError("'if' expression error: variable '\(variable)' was called with a left hand side") throw TemplateSyntaxError("'if' expression error: variable '\(variable)' was called with a left hand side")
case .subExpression: case .subExpression:
throw TemplateSyntaxError("'if' expression error: sub expression was called with a left hand side") throw TemplateSyntaxError("'if' expression error: sub expression was called with a left hand side")
case .end: case .end:
throw TemplateSyntaxError("'if' expression error: end") throw TemplateSyntaxError("'if' expression error: end")
} }
} }
var isEnd: Bool { var isEnd: Bool {
switch self { switch self {
case .end: case .end:
return true return true
default: default:
return false return false
} }
} }
} }
final class IfExpressionParser { final class IfExpressionParser {
let tokens: [IfToken] let tokens: [IfToken]
var position: Int = 0 var position: Int = 0
private init(tokens: [IfToken]) { private init(tokens: [IfToken]) {
self.tokens = tokens self.tokens = tokens
} }
static func parser(components: [String], environment: Environment, token: Token) throws -> IfExpressionParser { static func parser(components: [String], environment: Environment, token: Token) throws -> IfExpressionParser {
try IfExpressionParser(components: ArraySlice(components), environment: environment, token: token) try IfExpressionParser(components: ArraySlice(components), environment: environment, token: token)
} }
private init(components: ArraySlice<String>, environment: Environment, token: Token) throws { private init(components: ArraySlice<String>, environment: Environment, token: Token) throws {
var parsedComponents = Set<Int>() var parsedComponents = Set<Int>()
var bracketsBalance = 0 var bracketsBalance = 0
// swiftlint:disable:next closure_body_length // swiftlint:disable:next closure_body_length
self.tokens = try zip(components.indices, components).compactMap { index, component in self.tokens = try zip(components.indices, components).compactMap { index, component in
guard !parsedComponents.contains(index) else { return nil } guard !parsedComponents.contains(index) else { return nil }
if component == "(" { if component == "(" {
bracketsBalance += 1 bracketsBalance += 1
let (expression, parsedCount) = try Self.subExpression( let (expression, parsedCount) = try Self.subExpression(
from: components.suffix(from: index + 1), from: components.suffix(from: index + 1),
environment: environment, environment: environment,
token: token token: token
) )
parsedComponents.formUnion(Set(index...(index + parsedCount))) parsedComponents.formUnion(Set(index...(index + parsedCount)))
return .subExpression(expression) return .subExpression(expression)
} else if component == ")" { } else if component == ")" {
bracketsBalance -= 1 bracketsBalance -= 1
if bracketsBalance < 0 { if bracketsBalance < 0 {
throw TemplateSyntaxError("'if' expression error: missing opening bracket") throw TemplateSyntaxError("'if' expression error: missing opening bracket")
} }
parsedComponents.insert(index) parsedComponents.insert(index)
return nil return nil
} else { } else {
parsedComponents.insert(index) parsedComponents.insert(index)
if let `operator` = findOperator(name: component) { if let `operator` = findOperator(name: component) {
switch `operator` { switch `operator` {
case .infix(let name, let bindingPower, let operatorType): case .infix(let name, let bindingPower, let operatorType):
return .infix(name: name, bindingPower: bindingPower, operatorType: operatorType) return .infix(name: name, bindingPower: bindingPower, operatorType: operatorType)
case .prefix(let name, let bindingPower, let operatorType): case .prefix(let name, let bindingPower, let operatorType):
return .prefix(name: name, bindingPower: bindingPower, operatorType: operatorType) return .prefix(name: name, bindingPower: bindingPower, operatorType: operatorType)
} }
} }
return .variable(try environment.compileResolvable(component, containedIn: token)) return .variable(try environment.compileResolvable(component, containedIn: token))
} }
} }
} }
private static func subExpression( private static func subExpression(
from components: ArraySlice<String>, from components: ArraySlice<String>,
environment: Environment, environment: Environment,
token: Token token: Token
) throws -> (Expression, Int) { ) throws -> (Expression, Int) {
var bracketsBalance = 1 var bracketsBalance = 1
let subComponents = components.prefix { component in let subComponents = components.prefix { component in
if component == "(" { if component == "(" {
bracketsBalance += 1 bracketsBalance += 1
} else if component == ")" { } else if component == ")" {
bracketsBalance -= 1 bracketsBalance -= 1
} }
return bracketsBalance != 0 return bracketsBalance != 0
} }
if bracketsBalance > 0 { if bracketsBalance > 0 {
throw TemplateSyntaxError("'if' expression error: missing closing bracket") throw TemplateSyntaxError("'if' expression error: missing closing bracket")
} }
let expressionParser = try IfExpressionParser(components: subComponents, environment: environment, token: token) let expressionParser = try IfExpressionParser(components: subComponents, environment: environment, token: token)
let expression = try expressionParser.parse() let expression = try expressionParser.parse()
return (expression, subComponents.count) return (expression, subComponents.count)
} }
var currentToken: IfToken { var currentToken: IfToken {
if tokens.count > position { if tokens.count > position {
return tokens[position] return tokens[position]
} }
return .end return .end
} }
var nextToken: IfToken { var nextToken: IfToken {
position += 1 position += 1
return currentToken return currentToken
} }
func parse() throws -> Expression { func parse() throws -> Expression {
let expression = try self.expression() let expression = try self.expression()
if !currentToken.isEnd { if !currentToken.isEnd {
throw TemplateSyntaxError("'if' expression error: dangling token") throw TemplateSyntaxError("'if' expression error: dangling token")
} }
return expression return expression
} }
func expression(bindingPower: Int = 0) throws -> Expression { func expression(bindingPower: Int = 0) throws -> Expression {
var token = currentToken var token = currentToken
position += 1 position += 1
var left = try token.nullDenotation(parser: self) var left = try token.nullDenotation(parser: self)
while bindingPower < currentToken.bindingPower { while bindingPower < currentToken.bindingPower {
token = currentToken token = currentToken
position += 1 position += 1
left = try token.leftDenotation(left: left, parser: self) left = try token.leftDenotation(left: left, parser: self)
} }
return left return left
} }
} }
/// Represents an if condition and the associated nodes when the condition /// Represents an if condition and the associated nodes when the condition
/// evaluates /// evaluates
final class IfCondition { final class IfCondition {
let expression: Expression? let expression: Expression?
let nodes: [NodeType] let nodes: [NodeType]
init(expression: Expression?, nodes: [NodeType]) { init(expression: Expression?, nodes: [NodeType]) {
self.expression = expression self.expression = expression
self.nodes = nodes self.nodes = nodes
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
try context.push { try context.push {
try renderNodes(nodes, context) try renderNodes(nodes, context)
} }
} }
} }
class IfNode: NodeType { class IfNode: NodeType {
let conditions: [IfCondition] let conditions: [IfCondition]
let token: Token? let token: Token?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
var components = token.components var components = token.components
components.removeFirst() components.removeFirst()
let expression = try parser.compileExpression(components: components, token: token) let expression = try parser.compileExpression(components: components, token: token)
let nodes = try parser.parse(until(["endif", "elif", "else"])) let nodes = try parser.parse(until(["endif", "elif", "else"]))
var conditions: [IfCondition] = [ var conditions: [IfCondition] = [
IfCondition(expression: expression, nodes: nodes) IfCondition(expression: expression, nodes: nodes)
] ]
var nextToken = parser.nextToken() var nextToken = parser.nextToken()
while let current = nextToken, current.contents.hasPrefix("elif") { while let current = nextToken, current.contents.hasPrefix("elif") {
var components = current.components var components = current.components
components.removeFirst() components.removeFirst()
let expression = try parser.compileExpression(components: components, token: current) let expression = try parser.compileExpression(components: components, token: current)
let nodes = try parser.parse(until(["endif", "elif", "else"])) let nodes = try parser.parse(until(["endif", "elif", "else"]))
nextToken = parser.nextToken() nextToken = parser.nextToken()
conditions.append(IfCondition(expression: expression, nodes: nodes)) conditions.append(IfCondition(expression: expression, nodes: nodes))
} }
if let current = nextToken, current.contents == "else" { if let current = nextToken, current.contents == "else" {
conditions.append(IfCondition(expression: nil, nodes: try parser.parse(until(["endif"])))) conditions.append(IfCondition(expression: nil, nodes: try parser.parse(until(["endif"]))))
nextToken = parser.nextToken() nextToken = parser.nextToken()
} }
guard let current = nextToken, current.contents == "endif" else { guard let current = nextToken, current.contents == "endif" else {
throw TemplateSyntaxError("`endif` was not found.") throw TemplateSyntaxError("`endif` was not found.")
} }
return IfNode(conditions: conditions, token: token) return IfNode(conditions: conditions, token: token)
} }
class func parse_ifnot(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse_ifnot(_ parser: TokenParser, token: Token) throws -> NodeType {
var components = token.components var components = token.components
guard components.count == 2 else { guard components.count == 2 else {
throw TemplateSyntaxError("'ifnot' statements should use the following syntax 'ifnot condition'.") throw TemplateSyntaxError("'ifnot' statements should use the following syntax 'ifnot condition'.")
} }
components.removeFirst() components.removeFirst()
var trueNodes = [NodeType]() var trueNodes = [NodeType]()
var falseNodes = [NodeType]() var falseNodes = [NodeType]()
let expression = try parser.compileExpression(components: components, token: token) let expression = try parser.compileExpression(components: components, token: token)
falseNodes = try parser.parse(until(["endif", "else"])) falseNodes = try parser.parse(until(["endif", "else"]))
guard let token = parser.nextToken() else { guard let token = parser.nextToken() else {
throw TemplateSyntaxError("`endif` was not found.") throw TemplateSyntaxError("`endif` was not found.")
} }
if token.contents == "else" { if token.contents == "else" {
trueNodes = try parser.parse(until(["endif"])) trueNodes = try parser.parse(until(["endif"]))
_ = parser.nextToken() _ = parser.nextToken()
} }
return IfNode(conditions: [ return IfNode(conditions: [
IfCondition(expression: expression, nodes: trueNodes), IfCondition(expression: expression, nodes: trueNodes),
IfCondition(expression: nil, nodes: falseNodes) IfCondition(expression: nil, nodes: falseNodes)
], token: token) ], token: token)
} }
init(conditions: [IfCondition], token: Token? = nil) { init(conditions: [IfCondition], token: Token? = nil) {
self.conditions = conditions self.conditions = conditions
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
for condition in conditions { for condition in conditions {
if let expression = condition.expression { if let expression = condition.expression {
let truthy = try expression.evaluate(context: context) let truthy = try expression.evaluate(context: context)
if truthy { if truthy {
return try condition.render(context) return try condition.render(context)
} }
} else { } else {
return try condition.render(context) return try condition.render(context)
} }
} }
return "" return ""
} }
} }

View File

@@ -1,48 +1,48 @@
class IncludeNode: NodeType { class IncludeNode: NodeType {
let templateName: Variable let templateName: Variable
let includeContext: String? let includeContext: String?
let token: Token? let token: Token?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let bits = token.components let bits = token.components
guard bits.count == 2 || bits.count == 3 else { guard bits.count == 2 || bits.count == 3 else {
throw TemplateSyntaxError( throw TemplateSyntaxError(
""" """
'include' tag requires one argument, the template file to be included. \ 'include' tag requires one argument, the template file to be included. \
A second optional argument can be used to specify the context that will \ A second optional argument can be used to specify the context that will \
be passed to the included file be passed to the included file
""" """
) )
} }
return IncludeNode(templateName: Variable(bits[1]), includeContext: bits.count == 3 ? bits[2] : nil, token: token) return IncludeNode(templateName: Variable(bits[1]), includeContext: bits.count == 3 ? bits[2] : nil, token: token)
} }
init(templateName: Variable, includeContext: String? = nil, token: Token) { init(templateName: Variable, includeContext: String? = nil, token: Token) {
self.templateName = templateName self.templateName = templateName
self.includeContext = includeContext self.includeContext = includeContext
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
guard let templateName = try self.templateName.resolve(context) as? String else { guard let templateName = try self.templateName.resolve(context) as? String else {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string") throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
} }
let template = try context.environment.loadTemplate(name: templateName) let template = try context.environment.loadTemplate(name: templateName)
do { do {
let subContext = includeContext.flatMap { context[$0] as? [String: Any] } ?? [:] let subContext = includeContext.flatMap { context[$0] as? [String: Any] } ?? [:]
return try context.push(dictionary: subContext) { return try context.push(dictionary: subContext) {
try template.render(context) try template.render(context)
} }
} catch { } catch {
if let error = error as? TemplateSyntaxError { if let error = error as? TemplateSyntaxError {
throw TemplateSyntaxError(reason: error.reason, stackTrace: error.allTokens) throw TemplateSyntaxError(reason: error.reason, stackTrace: error.allTokens)
} else { } else {
throw error throw error
} }
} }
} }
} }

View File

@@ -1,158 +1,158 @@
class BlockContext { class BlockContext {
class var contextKey: String { "block_context" } class var contextKey: String { "block_context" }
// contains mapping of block names to their nodes and templates where they are defined // contains mapping of block names to their nodes and templates where they are defined
var blocks: [String: [BlockNode]] var blocks: [String: [BlockNode]]
init(blocks: [String: BlockNode]) { init(blocks: [String: BlockNode]) {
self.blocks = [:] self.blocks = [:]
blocks.forEach { self.blocks[$0.key] = [$0.value] } blocks.forEach { self.blocks[$0.key] = [$0.value] }
} }
func push(_ block: BlockNode, forKey blockName: String) { func push(_ block: BlockNode, forKey blockName: String) {
if var blocks = blocks[blockName] { if var blocks = blocks[blockName] {
blocks.append(block) blocks.append(block)
self.blocks[blockName] = blocks self.blocks[blockName] = blocks
} else { } else {
self.blocks[blockName] = [block] self.blocks[blockName] = [block]
} }
} }
func pop(_ blockName: String) -> BlockNode? { func pop(_ blockName: String) -> BlockNode? {
if var blocks = blocks[blockName] { if var blocks = blocks[blockName] {
let block = blocks.removeFirst() let block = blocks.removeFirst()
if blocks.isEmpty { if blocks.isEmpty {
self.blocks.removeValue(forKey: blockName) self.blocks.removeValue(forKey: blockName)
} else { } else {
self.blocks[blockName] = blocks self.blocks[blockName] = blocks
} }
return block return block
} else { } else {
return nil return nil
} }
} }
} }
extension Collection { extension Collection {
func any(_ closure: (Iterator.Element) -> Bool) -> Iterator.Element? { func any(_ closure: (Iterator.Element) -> Bool) -> Iterator.Element? {
for element in self where closure(element) { for element in self where closure(element) {
return element return element
} }
return nil return nil
} }
} }
class ExtendsNode: NodeType { class ExtendsNode: NodeType {
let templateName: Variable let templateName: Variable
let blocks: [String: BlockNode] let blocks: [String: BlockNode]
let token: Token? let token: Token?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let bits = token.components let bits = token.components
guard bits.count == 2 else { guard bits.count == 2 else {
throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended") throw TemplateSyntaxError("'extends' takes one argument, the template file to be extended")
} }
let parsedNodes = try parser.parse() let parsedNodes = try parser.parse()
guard (parsedNodes.any { $0 is Self }) == nil else { guard (parsedNodes.any { $0 is Self }) == nil else {
throw TemplateSyntaxError("'extends' cannot appear more than once in the same template") throw TemplateSyntaxError("'extends' cannot appear more than once in the same template")
} }
let blockNodes = parsedNodes.compactMap { $0 as? BlockNode } let blockNodes = parsedNodes.compactMap { $0 as? BlockNode }
let nodes = blockNodes.reduce(into: [String: BlockNode]()) { accumulator, node in let nodes = blockNodes.reduce(into: [String: BlockNode]()) { accumulator, node in
accumulator[node.name] = node accumulator[node.name] = node
} }
return ExtendsNode(templateName: Variable(bits[1]), blocks: nodes, token: token) return ExtendsNode(templateName: Variable(bits[1]), blocks: nodes, token: token)
} }
init(templateName: Variable, blocks: [String: BlockNode], token: Token) { init(templateName: Variable, blocks: [String: BlockNode], token: Token) {
self.templateName = templateName self.templateName = templateName
self.blocks = blocks self.blocks = blocks
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
guard let templateName = try self.templateName.resolve(context) as? String else { guard let templateName = try self.templateName.resolve(context) as? String else {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string") throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
} }
let baseTemplate = try context.environment.loadTemplate(name: templateName) let baseTemplate = try context.environment.loadTemplate(name: templateName)
let blockContext: BlockContext let blockContext: BlockContext
if let currentBlockContext = context[BlockContext.contextKey] as? BlockContext { if let currentBlockContext = context[BlockContext.contextKey] as? BlockContext {
blockContext = currentBlockContext blockContext = currentBlockContext
for (name, block) in blocks { for (name, block) in blocks {
blockContext.push(block, forKey: name) blockContext.push(block, forKey: name)
} }
} else { } else {
blockContext = BlockContext(blocks: blocks) blockContext = BlockContext(blocks: blocks)
} }
do { do {
// pushes base template and renders it's content // pushes base template and renders it's content
// block_context contains all blocks from child templates // block_context contains all blocks from child templates
return try context.push(dictionary: [BlockContext.contextKey: blockContext]) { return try context.push(dictionary: [BlockContext.contextKey: blockContext]) {
try baseTemplate.render(context) try baseTemplate.render(context)
} }
} catch { } catch {
// if error template is already set (see catch in BlockNode) // if error template is already set (see catch in BlockNode)
// and it happend in the same template as current template // and it happend in the same template as current template
// there is no need to wrap it in another error // there is no need to wrap it in another error
if let error = error as? TemplateSyntaxError, error.templateName != token?.sourceMap.filename { if let error = error as? TemplateSyntaxError, error.templateName != token?.sourceMap.filename {
throw TemplateSyntaxError(reason: error.reason, stackTrace: error.allTokens) throw TemplateSyntaxError(reason: error.reason, stackTrace: error.allTokens)
} else { } else {
throw error throw error
} }
} }
} }
} }
class BlockNode: NodeType { class BlockNode: NodeType {
let name: String let name: String
let nodes: [NodeType] let nodes: [NodeType]
let token: Token? let token: Token?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let bits = token.components let bits = token.components
guard bits.count == 2 else { guard bits.count == 2 else {
throw TemplateSyntaxError("'block' tag takes one argument, the block name") throw TemplateSyntaxError("'block' tag takes one argument, the block name")
} }
let blockName = bits[1] let blockName = bits[1]
let nodes = try parser.parse(until(["endblock"])) let nodes = try parser.parse(until(["endblock"]))
_ = parser.nextToken() _ = parser.nextToken()
return BlockNode(name: blockName, nodes: nodes, token: token) return BlockNode(name: blockName, nodes: nodes, token: token)
} }
init(name: String, nodes: [NodeType], token: Token) { init(name: String, nodes: [NodeType], token: Token) {
self.name = name self.name = name
self.nodes = nodes self.nodes = nodes
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.pop(name) { if let blockContext = context[BlockContext.contextKey] as? BlockContext, let child = blockContext.pop(name) {
let childContext: [String: Any] = [ let childContext: [String: Any] = [
BlockContext.contextKey: blockContext, BlockContext.contextKey: blockContext,
"block": ["super": try self.render(context)] "block": ["super": try self.render(context)]
] ]
// render extension node // render extension node
do { do {
return try context.push(dictionary: childContext) { return try context.push(dictionary: childContext) {
try child.render(context) try child.render(context)
} }
} catch { } catch {
throw error.withToken(child.token) throw error.withToken(child.token)
} }
} }
let result = try renderNodes(nodes, context) let result = try renderNodes(nodes, context)
context.cacheBlock(name, content: result) context.cacheBlock(name, content: result)
return result return result
} }
} }

View File

@@ -2,111 +2,111 @@ import Foundation
/// A structure used to represent a template variable, and to resolve it in a given context. /// A structure used to represent a template variable, and to resolve it in a given context.
final class KeyPath { final class KeyPath {
private var components = [String]() private var components = [String]()
private var current = "" private var current = ""
private var partialComponents = [String]() private var partialComponents = [String]()
private var subscriptLevel = 0 private var subscriptLevel = 0
let variable: String let variable: String
let context: Context let context: Context
// Split the keypath string and resolve references if possible // Split the keypath string and resolve references if possible
init(_ variable: String, in context: Context) { init(_ variable: String, in context: Context) {
self.variable = variable self.variable = variable
self.context = context self.context = context
} }
func parse() throws -> [String] { func parse() throws -> [String] {
defer { defer {
components = [] components = []
current = "" current = ""
partialComponents = [] partialComponents = []
subscriptLevel = 0 subscriptLevel = 0
} }
for character in variable { for character in variable {
switch character { switch character {
case "." where subscriptLevel == 0: case "." where subscriptLevel == 0:
try foundSeparator() try foundSeparator()
case "[": case "[":
try openBracket() try openBracket()
case "]": case "]":
try closeBracket() try closeBracket()
default: default:
try addCharacter(character) try addCharacter(character)
} }
} }
try finish() try finish()
return components return components
} }
private func foundSeparator() throws { private func foundSeparator() throws {
if !current.isEmpty { if !current.isEmpty {
partialComponents.append(current) partialComponents.append(current)
} }
guard !partialComponents.isEmpty else { guard !partialComponents.isEmpty else {
throw TemplateSyntaxError("Unexpected '.' in variable '\(variable)'") throw TemplateSyntaxError("Unexpected '.' in variable '\(variable)'")
} }
components += partialComponents components += partialComponents
current = "" current = ""
partialComponents = [] partialComponents = []
} }
// when opening the first bracket, we must have a partial component // when opening the first bracket, we must have a partial component
private func openBracket() throws { private func openBracket() throws {
guard !partialComponents.isEmpty || !current.isEmpty else { guard !partialComponents.isEmpty || !current.isEmpty else {
throw TemplateSyntaxError("Unexpected '[' in variable '\(variable)'") throw TemplateSyntaxError("Unexpected '[' in variable '\(variable)'")
} }
if subscriptLevel > 0 { if subscriptLevel > 0 {
current.append("[") current.append("[")
} else if !current.isEmpty { } else if !current.isEmpty {
partialComponents.append(current) partialComponents.append(current)
current = "" current = ""
} }
subscriptLevel += 1 subscriptLevel += 1
} }
// for a closing bracket at root level, try to resolve the reference // for a closing bracket at root level, try to resolve the reference
private func closeBracket() throws { private func closeBracket() throws {
guard subscriptLevel > 0 else { guard subscriptLevel > 0 else {
throw TemplateSyntaxError("Unbalanced ']' in variable '\(variable)'") throw TemplateSyntaxError("Unbalanced ']' in variable '\(variable)'")
} }
if subscriptLevel > 1 { if subscriptLevel > 1 {
current.append("]") current.append("]")
} else if !current.isEmpty, } else if !current.isEmpty,
let value = try Variable(current).resolve(context) { let value = try Variable(current).resolve(context) {
partialComponents.append("\(value)") partialComponents.append("\(value)")
current = "" current = ""
} else { } else {
throw TemplateSyntaxError("Unable to resolve subscript '\(current)' in variable '\(variable)'") throw TemplateSyntaxError("Unable to resolve subscript '\(current)' in variable '\(variable)'")
} }
subscriptLevel -= 1 subscriptLevel -= 1
} }
private func addCharacter(_ character: Character) throws { private func addCharacter(_ character: Character) throws {
guard partialComponents.isEmpty || subscriptLevel > 0 else { guard partialComponents.isEmpty || subscriptLevel > 0 else {
throw TemplateSyntaxError("Unexpected character '\(character)' in variable '\(variable)'") throw TemplateSyntaxError("Unexpected character '\(character)' in variable '\(variable)'")
} }
current.append(character) current.append(character)
} }
private func finish() throws { private func finish() throws {
// check if we have a last piece // check if we have a last piece
if !current.isEmpty { if !current.isEmpty {
partialComponents.append(current) partialComponents.append(current)
} }
components += partialComponents components += partialComponents
guard subscriptLevel == 0 else { guard subscriptLevel == 0 else {
throw TemplateSyntaxError("Unbalanced subscript brackets in variable '\(variable)'") throw TemplateSyntaxError("Unbalanced subscript brackets in variable '\(variable)'")
} }
} }
} }

View File

@@ -1,57 +1,57 @@
/// Used to lazily set context data. Useful for example if you have some data that requires heavy calculations, and may /// Used to lazily set context data. Useful for example if you have some data that requires heavy calculations, and may
/// not be used in every render possiblity. /// not be used in every render possiblity.
public final class LazyValueWrapper { public final class LazyValueWrapper {
private let closure: (Context) throws -> Any private let closure: (Context) throws -> Any
private let context: Context? private let context: Context?
private var cachedValue: Any? private var cachedValue: Any?
/// Create a wrapper that'll use a **reference** to the current context. /// Create a wrapper that'll use a **reference** to the current context.
/// This means when the closure is evaluated, it'll use the **active** context at that moment. /// This means when the closure is evaluated, it'll use the **active** context at that moment.
/// ///
/// - Parameters: /// - Parameters:
/// - closure: The closure to lazily evaluate /// - closure: The closure to lazily evaluate
public init(closure: @escaping (Context) throws -> Any) { public init(closure: @escaping (Context) throws -> Any) {
self.context = nil self.context = nil
self.closure = closure self.closure = closure
} }
/// Create a wrapper that'll create a **copy** of the current context. /// Create a wrapper that'll create a **copy** of the current context.
/// This means when the closure is evaluated, it'll use the context **as it was** when this wrapper was created. /// This means when the closure is evaluated, it'll use the context **as it was** when this wrapper was created.
/// ///
/// - Parameters: /// - Parameters:
/// - context: The context to use during evaluation /// - context: The context to use during evaluation
/// - closure: The closure to lazily evaluate /// - closure: The closure to lazily evaluate
/// - Note: This will use more memory than the other `init` as it needs to keep a copy of the full context around. /// - Note: This will use more memory than the other `init` as it needs to keep a copy of the full context around.
public init(copying context: Context, closure: @escaping (Context) throws -> Any) { public init(copying context: Context, closure: @escaping (Context) throws -> Any) {
self.context = Context(dictionaries: context.dictionaries, environment: context.environment) self.context = Context(dictionaries: context.dictionaries, environment: context.environment)
self.closure = closure self.closure = closure
} }
/// Shortcut for creating a lazy wrapper when you don't need access to the Stencil context. /// Shortcut for creating a lazy wrapper when you don't need access to the Stencil context.
/// ///
/// - Parameters: /// - Parameters:
/// - closure: The closure to lazily evaluate /// - closure: The closure to lazily evaluate
public init(_ closure: @autoclosure @escaping () throws -> Any) { public init(_ closure: @autoclosure @escaping () throws -> Any) {
self.context = nil self.context = nil
self.closure = { _ in try closure() } self.closure = { _ in try closure() }
} }
} }
extension LazyValueWrapper { extension LazyValueWrapper {
func value(context: Context) throws -> Any { func value(context: Context) throws -> Any {
if let value = cachedValue { if let value = cachedValue {
return value return value
} else { } else {
let value = try closure(self.context ?? context) let value = try closure(self.context ?? context)
cachedValue = value cachedValue = value
return value return value
} }
} }
} }
extension LazyValueWrapper: Resolvable { extension LazyValueWrapper: Resolvable {
public func resolve(_ context: Context) throws -> Any? { public func resolve(_ context: Context) throws -> Any? {
let value = try self.value(context: context) let value = try self.value(context: context)
return try (value as? Resolvable)?.resolve(context) ?? value return try (value as? Resolvable)?.resolve(context) ?? value
} }
} }

View File

@@ -7,251 +7,251 @@ public typealias ContentLocation = (content: String, lineNumber: UInt, lineOffse
// swiftlint:enable large_tuple // swiftlint:enable large_tuple
struct Lexer { struct Lexer {
let templateName: String? let templateName: String?
let templateString: String let templateString: String
let lines: [Line] let lines: [Line]
/// The potential token start characters. In a template these appear after a /// The potential token start characters. In a template these appear after a
/// `{` character, for example `{{`, `{%`, `{#`, ... /// `{` character, for example `{{`, `{%`, `{#`, ...
private static let tokenChars: [Unicode.Scalar] = ["{", "%", "#"] private static let tokenChars: [Unicode.Scalar] = ["{", "%", "#"]
/// The minimum length of a tag /// The minimum length of a tag
private static let tagLength = 2 private static let tagLength = 2
/// The token end characters, corresponding to their token start characters. /// The token end characters, corresponding to their token start characters.
/// For example, a variable token starts with `{{` and ends with `}}` /// For example, a variable token starts with `{{` and ends with `}}`
private static let tokenCharMap: [Unicode.Scalar: Unicode.Scalar] = [ private static let tokenCharMap: [Unicode.Scalar: Unicode.Scalar] = [
"{": "}", "{": "}",
"%": "%", "%": "%",
"#": "#" "#": "#"
] ]
/// Characters controlling whitespace trimming behaviour /// Characters controlling whitespace trimming behaviour
private static let behaviourMap: [Character: WhitespaceBehaviour.Behaviour] = [ private static let behaviourMap: [Character: WhitespaceBehaviour.Behaviour] = [
"+": .keep, "+": .keep,
"-": .trim "-": .trim
] ]
init(templateName: String? = nil, templateString: String) { init(templateName: String? = nil, templateString: String) {
self.templateName = templateName self.templateName = templateName
self.templateString = templateString self.templateString = templateString
self.lines = zip(1..., templateString.components(separatedBy: .newlines)).compactMap { index, line in self.lines = zip(1..., templateString.components(separatedBy: .newlines)).compactMap { index, line in
guard !line.isEmpty, guard !line.isEmpty,
let range = templateString.range(of: line) else { return nil } let range = templateString.range(of: line) else { return nil }
return (content: line, number: UInt(index), range) return (content: line, number: UInt(index), range)
} }
} }
private func behaviour(string: String, tagLength: Int) -> WhitespaceBehaviour { private func behaviour(string: String, tagLength: Int) -> WhitespaceBehaviour {
let leftIndex = string.index(string.startIndex, offsetBy: tagLength, limitedBy: string.endIndex) let leftIndex = string.index(string.startIndex, offsetBy: tagLength, limitedBy: string.endIndex)
let rightIndex = string.index(string.endIndex, offsetBy: -(tagLength + 1), limitedBy: string.startIndex) let rightIndex = string.index(string.endIndex, offsetBy: -(tagLength + 1), limitedBy: string.startIndex)
return WhitespaceBehaviour( return WhitespaceBehaviour(
leading: Self.behaviourMap[leftIndex.map { string[$0] } ?? " "] ?? .unspecified, leading: Self.behaviourMap[leftIndex.map { string[$0] } ?? " "] ?? .unspecified,
trailing: Self.behaviourMap[rightIndex.map { string[$0] } ?? " "] ?? .unspecified trailing: Self.behaviourMap[rightIndex.map { string[$0] } ?? " "] ?? .unspecified
) )
} }
/// Create a token that will be passed on to the parser, with the given /// Create a token that will be passed on to the parser, with the given
/// content and a range. The content will be tested to see if it's a /// content and a range. The content will be tested to see if it's a
/// `variable`, a `block` or a `comment`, otherwise it'll default to a simple /// `variable`, a `block` or a `comment`, otherwise it'll default to a simple
/// `text` token. /// `text` token.
/// ///
/// - Parameters: /// - Parameters:
/// - string: The content string of the token /// - string: The content string of the token
/// - range: The range within the template content, used for smart /// - range: The range within the template content, used for smart
/// error reporting /// error reporting
func createToken(string: String, at range: Range<String.Index>) -> Token { func createToken(string: String, at range: Range<String.Index>) -> Token {
func strip(length: (Int, Int) = (Self.tagLength, Self.tagLength)) -> String { func strip(length: (Int, Int) = (Self.tagLength, Self.tagLength)) -> String {
guard string.count > (length.0 + length.1) else { return "" } guard string.count > (length.0 + length.1) else { return "" }
let trimmed = String(string.dropFirst(length.0).dropLast(length.1)) let trimmed = String(string.dropFirst(length.0).dropLast(length.1))
.components(separatedBy: "\n") .components(separatedBy: "\n")
.filter { !$0.isEmpty } .filter { !$0.isEmpty }
.map { $0.trim(character: " ") } .map { $0.trim(character: " ") }
.joined(separator: " ") .joined(separator: " ")
return trimmed return trimmed
} }
if string.hasPrefix("{{") || string.hasPrefix("{%") || string.hasPrefix("{#") { if string.hasPrefix("{{") || string.hasPrefix("{%") || string.hasPrefix("{#") {
let behaviour = string.hasPrefix("{%") ? behaviour(string: string, tagLength: Self.tagLength) : .unspecified let behaviour = string.hasPrefix("{%") ? behaviour(string: string, tagLength: Self.tagLength) : .unspecified
let stripLengths = ( let stripLengths = (
Self.tagLength + (behaviour.leading != .unspecified ? 1 : 0), Self.tagLength + (behaviour.leading != .unspecified ? 1 : 0),
Self.tagLength + (behaviour.trailing != .unspecified ? 1 : 0) Self.tagLength + (behaviour.trailing != .unspecified ? 1 : 0)
) )
let value = strip(length: stripLengths) let value = strip(length: stripLengths)
let range = templateString.range(of: value, range: range) ?? range let range = templateString.range(of: value, range: range) ?? range
let location = rangeLocation(range) let location = rangeLocation(range)
let sourceMap = SourceMap(filename: templateName, location: location) let sourceMap = SourceMap(filename: templateName, location: location)
if string.hasPrefix("{{") { if string.hasPrefix("{{") {
return .variable(value: value, at: sourceMap) return .variable(value: value, at: sourceMap)
} else if string.hasPrefix("{%") { } else if string.hasPrefix("{%") {
return .block(value: strip(length: stripLengths), at: sourceMap, whitespace: behaviour) return .block(value: strip(length: stripLengths), at: sourceMap, whitespace: behaviour)
} else if string.hasPrefix("{#") { } else if string.hasPrefix("{#") {
return .comment(value: value, at: sourceMap) return .comment(value: value, at: sourceMap)
} }
} }
let location = rangeLocation(range) let location = rangeLocation(range)
let sourceMap = SourceMap(filename: templateName, location: location) let sourceMap = SourceMap(filename: templateName, location: location)
return .text(value: string, at: sourceMap) return .text(value: string, at: sourceMap)
} }
/// Transforms the template into a list of tokens, that will eventually be /// Transforms the template into a list of tokens, that will eventually be
/// passed on to the parser. /// passed on to the parser.
/// ///
/// - Returns: The list of tokens (see `createToken(string: at:)`). /// - Returns: The list of tokens (see `createToken(string: at:)`).
func tokenize() -> [Token] { func tokenize() -> [Token] {
var tokens: [Token] = [] var tokens: [Token] = []
let scanner = Scanner(templateString) let scanner = Scanner(templateString)
while !scanner.isEmpty { while !scanner.isEmpty {
if let (char, text) = scanner.scanForTokenStart(Self.tokenChars) { if let (char, text) = scanner.scanForTokenStart(Self.tokenChars) {
if !text.isEmpty { if !text.isEmpty {
tokens.append(createToken(string: text, at: scanner.range)) tokens.append(createToken(string: text, at: scanner.range))
} }
guard let end = Self.tokenCharMap[char] else { continue } guard let end = Self.tokenCharMap[char] else { continue }
let result = scanner.scanForTokenEnd(end) let result = scanner.scanForTokenEnd(end)
tokens.append(createToken(string: result, at: scanner.range)) tokens.append(createToken(string: result, at: scanner.range))
} else { } else {
tokens.append(createToken(string: scanner.content, at: scanner.range)) tokens.append(createToken(string: scanner.content, at: scanner.range))
scanner.content = "" scanner.content = ""
} }
} }
return tokens return tokens
} }
/// Finds the line matching the given range (for a token) /// Finds the line matching the given range (for a token)
/// ///
/// - Parameter range: The range to search for. /// - Parameter range: The range to search for.
/// - Returns: The content for that line, the line number and offset within /// - Returns: The content for that line, the line number and offset within
/// the line. /// the line.
func rangeLocation(_ range: Range<String.Index>) -> ContentLocation { func rangeLocation(_ range: Range<String.Index>) -> ContentLocation {
guard let line = self.lines.first(where: { $0.range.contains(range.lowerBound) }) else { guard let line = self.lines.first(where: { $0.range.contains(range.lowerBound) }) else {
return ("", 0, 0) return ("", 0, 0)
} }
let offset = templateString.distance(from: line.range.lowerBound, to: range.lowerBound) let offset = templateString.distance(from: line.range.lowerBound, to: range.lowerBound)
return (line.content, line.number, offset) return (line.content, line.number, offset)
} }
} }
class Scanner { class Scanner {
let originalContent: String let originalContent: String
var content: String var content: String
var range: Range<String.UnicodeScalarView.Index> var range: Range<String.UnicodeScalarView.Index>
/// The start delimiter for a token. /// The start delimiter for a token.
private static let tokenStartDelimiter: Unicode.Scalar = "{" private static let tokenStartDelimiter: Unicode.Scalar = "{"
/// And the corresponding end delimiter for a token. /// And the corresponding end delimiter for a token.
private static let tokenEndDelimiter: Unicode.Scalar = "}" private static let tokenEndDelimiter: Unicode.Scalar = "}"
init(_ content: String) { init(_ content: String) {
self.originalContent = content self.originalContent = content
self.content = content self.content = content
range = content.unicodeScalars.startIndex..<content.unicodeScalars.startIndex range = content.unicodeScalars.startIndex..<content.unicodeScalars.startIndex
} }
var isEmpty: Bool { var isEmpty: Bool {
content.isEmpty content.isEmpty
} }
/// Scans for the end of a token, with a specific ending character. If we're /// Scans for the end of a token, with a specific ending character. If we're
/// searching for the end of a block token `%}`, this method receives a `%`. /// searching for the end of a block token `%}`, this method receives a `%`.
/// The scanner will search for that `%` followed by a `}`. /// The scanner will search for that `%` followed by a `}`.
/// ///
/// Note: if the end of a token is found, the `content` and `range` /// Note: if the end of a token is found, the `content` and `range`
/// properties are updated to reflect this. `content` will be set to what /// properties are updated to reflect this. `content` will be set to what
/// remains of the template after the token. `range` will be set to the range /// remains of the template after the token. `range` will be set to the range
/// of the token within the template. /// of the token within the template.
/// ///
/// - Parameter tokenChar: The token end character to search for. /// - Parameter tokenChar: The token end character to search for.
/// - Returns: The content of a token, or "" if no token end was found. /// - Returns: The content of a token, or "" if no token end was found.
func scanForTokenEnd(_ tokenChar: Unicode.Scalar) -> String { func scanForTokenEnd(_ tokenChar: Unicode.Scalar) -> String {
var foundChar = false var foundChar = false
for (index, char) in zip(0..., content.unicodeScalars) { for (index, char) in zip(0..., content.unicodeScalars) {
if foundChar && char == Self.tokenEndDelimiter { if foundChar && char == Self.tokenEndDelimiter {
let result = String(content.unicodeScalars.prefix(index + 1)) let result = String(content.unicodeScalars.prefix(index + 1))
content = String(content.unicodeScalars.dropFirst(index + 1)) content = String(content.unicodeScalars.dropFirst(index + 1))
range = range.upperBound..<originalContent.unicodeScalars.index(range.upperBound, offsetBy: index + 1) range = range.upperBound..<originalContent.unicodeScalars.index(range.upperBound, offsetBy: index + 1)
return result return result
} else { } else {
foundChar = (char == tokenChar) foundChar = (char == tokenChar)
} }
} }
content = "" content = ""
return "" return ""
} }
/// Scans for the start of a token, with a list of potential starting /// Scans for the start of a token, with a list of potential starting
/// characters. To scan for the start of variables (`{{`), blocks (`{%`) and /// characters. To scan for the start of variables (`{{`), blocks (`{%`) and
/// comments (`{#`), this method receives the characters `{`, `%` and `#`. /// comments (`{#`), this method receives the characters `{`, `%` and `#`.
/// The scanner will search for a `{`, followed by one of the search /// The scanner will search for a `{`, followed by one of the search
/// characters. It will give the found character, and the content that came /// characters. It will give the found character, and the content that came
/// before the token. /// before the token.
/// ///
/// Note: if the start of a token is found, the `content` and `range` /// Note: if the start of a token is found, the `content` and `range`
/// properties are updated to reflect this. `content` will be set to what /// properties are updated to reflect this. `content` will be set to what
/// remains of the template starting with the token. `range` will be set to /// remains of the template starting with the token. `range` will be set to
/// the start of the token within the template. /// the start of the token within the template.
/// ///
/// - Parameter tokenChars: List of token start characters to search for. /// - Parameter tokenChars: List of token start characters to search for.
/// - Returns: The found token start character, together with the content /// - Returns: The found token start character, together with the content
/// before the token, or nil of no token start was found. /// before the token, or nil of no token start was found.
func scanForTokenStart(_ tokenChars: [Unicode.Scalar]) -> (Unicode.Scalar, String)? { func scanForTokenStart(_ tokenChars: [Unicode.Scalar]) -> (Unicode.Scalar, String)? {
var foundBrace = false var foundBrace = false
range = range.upperBound..<range.upperBound range = range.upperBound..<range.upperBound
for (index, char) in zip(0..., content.unicodeScalars) { for (index, char) in zip(0..., content.unicodeScalars) {
if foundBrace && tokenChars.contains(char) { if foundBrace && tokenChars.contains(char) {
let result = String(content.unicodeScalars.prefix(index - 1)) let result = String(content.unicodeScalars.prefix(index - 1))
content = String(content.unicodeScalars.dropFirst(index - 1)) content = String(content.unicodeScalars.dropFirst(index - 1))
range = range.upperBound..<originalContent.unicodeScalars.index(range.upperBound, offsetBy: index - 1) range = range.upperBound..<originalContent.unicodeScalars.index(range.upperBound, offsetBy: index - 1)
return (char, result) return (char, result)
} else { } else {
foundBrace = (char == Self.tokenStartDelimiter) foundBrace = (char == Self.tokenStartDelimiter)
} }
} }
return nil return nil
} }
} }
extension String { extension String {
func findFirstNot(character: Character) -> String.Index? { func findFirstNot(character: Character) -> String.Index? {
var index = startIndex var index = startIndex
while index != endIndex { while index != endIndex {
if character != self[index] { if character != self[index] {
return index return index
} }
index = self.index(after: index) index = self.index(after: index)
} }
return nil return nil
} }
func findLastNot(character: Character) -> String.Index? { func findLastNot(character: Character) -> String.Index? {
var index = self.index(before: endIndex) var index = self.index(before: endIndex)
while index != startIndex { while index != startIndex {
if character != self[index] { if character != self[index] {
return self.index(after: index) return self.index(after: index)
} }
index = self.index(before: index) index = self.index(before: index)
} }
return nil return nil
} }
func trim(character: Character) -> String { func trim(character: Character) -> String {
let first = findFirstNot(character: character) ?? startIndex let first = findFirstNot(character: character) ?? startIndex
let last = findLastNot(character: character) ?? endIndex let last = findLastNot(character: character) ?? endIndex
return String(self[first..<last]) return String(self[first..<last])
} }
} }

View File

@@ -3,126 +3,126 @@ import PathKit
/// Type used for loading a template /// Type used for loading a template
public protocol Loader { public protocol Loader {
/// Load a template with the given name /// Load a template with the given name
func loadTemplate(name: String, environment: Environment) throws -> Template func loadTemplate(name: String, environment: Environment) throws -> Template
/// Load a template with the given list of names /// Load a template with the given list of names
func loadTemplate(names: [String], environment: Environment) throws -> Template func loadTemplate(names: [String], environment: Environment) throws -> Template
} }
extension Loader { extension Loader {
/// Default implementation, tries to load the first template that exists from the list of given names /// Default implementation, tries to load the first template that exists from the list of given names
public func loadTemplate(names: [String], environment: Environment) throws -> Template { public func loadTemplate(names: [String], environment: Environment) throws -> Template {
for name in names { for name in names {
do { do {
return try loadTemplate(name: name, environment: environment) return try loadTemplate(name: name, environment: environment)
} catch is TemplateDoesNotExist { } catch is TemplateDoesNotExist {
continue continue
} catch { } catch {
throw error throw error
} }
} }
throw TemplateDoesNotExist(templateNames: names, loader: self) throw TemplateDoesNotExist(templateNames: names, loader: self)
} }
} }
// A class for loading a template from disk // A class for loading a template from disk
public class FileSystemLoader: Loader, CustomStringConvertible { public class FileSystemLoader: Loader, CustomStringConvertible {
public let paths: [Path] public let paths: [Path]
public init(paths: [Path]) { public init(paths: [Path]) {
self.paths = paths self.paths = paths
} }
public init(bundle: [Bundle]) { public init(bundle: [Bundle]) {
self.paths = bundle.compactMap { bundle in self.paths = bundle.compactMap { bundle in
Path(bundle.path) Path(bundle.path)
} }
} }
public var description: String { public var description: String {
"FileSystemLoader(\(paths))" "FileSystemLoader(\(paths))"
} }
public func loadTemplate(name: String, environment: Environment) throws -> Template { public func loadTemplate(name: String, environment: Environment) throws -> Template {
for path in paths { for path in paths {
let templatePath = try path.safeJoin(path: name) let templatePath = try path.safeJoin(path: name)
if !templatePath.exists { if !templatePath.exists {
continue continue
} }
let content: String = try String(contentsOf: templatePath) let content: String = try String(contentsOf: templatePath)
return environment.templateClass.init(templateString: content, environment: environment, name: name) return environment.templateClass.init(templateString: content, environment: environment, name: name)
} }
throw TemplateDoesNotExist(templateNames: [name], loader: self) throw TemplateDoesNotExist(templateNames: [name], loader: self)
} }
public func loadTemplate(names: [String], environment: Environment) throws -> Template { public func loadTemplate(names: [String], environment: Environment) throws -> Template {
for path in paths { for path in paths {
for templateName in names { for templateName in names {
let templatePath = try path.safeJoin(path: templateName) let templatePath = try path.safeJoin(path: templateName)
if templatePath.exists { if templatePath.exists {
let content: String = try String(contentsOf: templatePath) let content: String = try String(contentsOf: templatePath)
return environment.templateClass.init(templateString: content, environment: environment, name: templateName) return environment.templateClass.init(templateString: content, environment: environment, name: templateName)
} }
} }
} }
throw TemplateDoesNotExist(templateNames: names, loader: self) throw TemplateDoesNotExist(templateNames: names, loader: self)
} }
} }
public class DictionaryLoader: Loader { public class DictionaryLoader: Loader {
public let templates: [String: String] public let templates: [String: String]
public init(templates: [String: String]) { public init(templates: [String: String]) {
self.templates = templates self.templates = templates
} }
public func loadTemplate(name: String, environment: Environment) throws -> Template { public func loadTemplate(name: String, environment: Environment) throws -> Template {
if let content = templates[name] { if let content = templates[name] {
return environment.templateClass.init(templateString: content, environment: environment, name: name) return environment.templateClass.init(templateString: content, environment: environment, name: name)
} }
throw TemplateDoesNotExist(templateNames: [name], loader: self) throw TemplateDoesNotExist(templateNames: [name], loader: self)
} }
public func loadTemplate(names: [String], environment: Environment) throws -> Template { public func loadTemplate(names: [String], environment: Environment) throws -> Template {
for name in names { for name in names {
if let content = templates[name] { if let content = templates[name] {
return environment.templateClass.init(templateString: content, environment: environment, name: name) return environment.templateClass.init(templateString: content, environment: environment, name: name)
} }
} }
throw TemplateDoesNotExist(templateNames: names, loader: self) throw TemplateDoesNotExist(templateNames: names, loader: self)
} }
} }
extension Path { extension Path {
func safeJoin(path: String) throws -> Path { func safeJoin(path: String) throws -> Path {
let newPath = self / path let newPath = self / path
if !newPath.string.hasPrefix(self.string) { if !newPath.string.hasPrefix(self.string) {
throw SuspiciousFileOperation(basePath: self, path: newPath) throw SuspiciousFileOperation(basePath: self, path: newPath)
} }
return newPath return newPath
} }
} }
class SuspiciousFileOperation: Error { class SuspiciousFileOperation: Error {
let basePath: Path let basePath: Path
let path: Path let path: Path
init(basePath: Path, path: Path) { init(basePath: Path, path: Path) {
self.basePath = basePath self.basePath = basePath
self.path = path self.path = path
} }
var description: String { var description: String {
"Path `\(path)` is located outside of base path `\(basePath)`" "Path `\(path)` is located outside of base path `\(basePath)`"
} }
} }

View File

@@ -2,184 +2,184 @@ import Foundation
/// Represents a parsed node /// Represents a parsed node
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
/// Reference to this node's token /// Reference to this node's token
var token: Token? { get } var token: Token? { get }
} }
/// 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 {
var result = "" var result = ""
for node in nodes { for node in nodes {
do { do {
result += try node.render(context) result += try node.render(context)
} catch { } catch {
throw error.withToken(node.token) throw error.withToken(node.token)
} }
let shouldBreak = context[LoopTerminationNode.breakContextKey] != nil let shouldBreak = context[LoopTerminationNode.breakContextKey] != nil
let shouldContinue = context[LoopTerminationNode.continueContextKey] != nil let shouldContinue = context[LoopTerminationNode.continueContextKey] != nil
if shouldBreak || shouldContinue { if shouldBreak || shouldContinue {
break break
} }
} }
return result return result
} }
/// Simple node, used for triggering a closure during rendering /// Simple node, used for triggering a closure during rendering
public class SimpleNode: NodeType { public class SimpleNode: NodeType {
public let handler: (Context) throws -> String public let handler: (Context) throws -> String
public let token: Token? public let token: Token?
public init(token: Token, handler: @escaping (Context) throws -> String) { public init(token: Token, handler: @escaping (Context) throws -> String) {
self.token = token self.token = token
self.handler = handler self.handler = handler
} }
public func render(_ context: Context) throws -> String { public func render(_ context: Context) throws -> String {
try handler(context) try handler(context)
} }
} }
/// Represents a block of text, renders the text /// Represents a block of text, renders the text
public class TextNode: NodeType { public class TextNode: NodeType {
public let text: String public let text: String
public let token: Token? public let token: Token?
public let trimBehaviour: TrimBehaviour public let trimBehaviour: TrimBehaviour
public init(text: String, trimBehaviour: TrimBehaviour = .nothing) { public init(text: String, trimBehaviour: TrimBehaviour = .nothing) {
self.text = text self.text = text
self.token = nil self.token = nil
self.trimBehaviour = trimBehaviour self.trimBehaviour = trimBehaviour
} }
public func render(_ context: Context) throws -> String { public func render(_ context: Context) throws -> String {
var string = self.text var string = self.text
if trimBehaviour.leading != .nothing, !string.isEmpty { if trimBehaviour.leading != .nothing, !string.isEmpty {
let range = NSRange(..<string.endIndex, in: string) let range = NSRange(..<string.endIndex, in: string)
string = TrimBehaviour.leadingRegex(trim: trimBehaviour.leading) string = TrimBehaviour.leadingRegex(trim: trimBehaviour.leading)
.stringByReplacingMatches(in: string, options: [], range: range, withTemplate: "") .stringByReplacingMatches(in: string, options: [], range: range, withTemplate: "")
} }
if trimBehaviour.trailing != .nothing, !string.isEmpty { if trimBehaviour.trailing != .nothing, !string.isEmpty {
let range = NSRange(..<string.endIndex, in: string) let range = NSRange(..<string.endIndex, in: string)
string = TrimBehaviour.trailingRegex(trim: trimBehaviour.trailing) string = TrimBehaviour.trailingRegex(trim: trimBehaviour.trailing)
.stringByReplacingMatches(in: string, options: [], range: range, withTemplate: "") .stringByReplacingMatches(in: string, options: [], range: range, withTemplate: "")
} }
return string return string
} }
} }
/// Representing something that can be resolved in a context /// Representing something that can be resolved in a context
public protocol Resolvable { public protocol Resolvable {
/// Try to resolve this with the given context /// Try to resolve this with the given context
func resolve(_ context: Context) throws -> Any? func resolve(_ context: Context) throws -> Any?
} }
/// Represents a variable, renders the variable, may have conditional expressions. /// Represents a variable, renders the variable, may have conditional expressions.
public class VariableNode: NodeType { public class VariableNode: NodeType {
public let variable: Resolvable public let variable: Resolvable
public var token: Token? public var token: Token?
let condition: Expression? let condition: Expression?
let elseExpression: Resolvable? let elseExpression: Resolvable?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let components = token.components let components = token.components
func hasToken(_ token: String, at index: Int) -> Bool { func hasToken(_ token: String, at index: Int) -> Bool {
components.count > (index + 1) && components[index] == token components.count > (index + 1) && components[index] == token
} }
func compileResolvable(_ components: [String], containedIn token: Token) throws -> Resolvable { func compileResolvable(_ components: [String], containedIn token: Token) throws -> Resolvable {
try (try? parser.compileExpression(components: components, token: token)) ?? try (try? parser.compileExpression(components: components, token: token)) ??
parser.compileFilter(components.joined(separator: " "), containedIn: token) parser.compileFilter(components.joined(separator: " "), containedIn: token)
} }
let variable: Resolvable let variable: Resolvable
let condition: Expression? let condition: Expression?
let elseExpression: Resolvable? let elseExpression: Resolvable?
if hasToken("if", at: 1) { if hasToken("if", at: 1) {
variable = try compileResolvable([components[0]], containedIn: token) variable = try compileResolvable([components[0]], containedIn: token)
let components = components.suffix(from: 2) let components = components.suffix(from: 2)
if let elseIndex = components.firstIndex(of: "else") { if let elseIndex = components.firstIndex(of: "else") {
condition = try parser.compileExpression(components: Array(components.prefix(upTo: elseIndex)), token: token) condition = try parser.compileExpression(components: Array(components.prefix(upTo: elseIndex)), token: token)
let elseToken = Array(components.suffix(from: elseIndex.advanced(by: 1))) let elseToken = Array(components.suffix(from: elseIndex.advanced(by: 1)))
elseExpression = try compileResolvable(elseToken, containedIn: token) elseExpression = try compileResolvable(elseToken, containedIn: token)
} else { } else {
condition = try parser.compileExpression(components: Array(components), token: token) condition = try parser.compileExpression(components: Array(components), token: token)
elseExpression = nil elseExpression = nil
} }
} else if !components.isEmpty { } else if !components.isEmpty {
variable = try compileResolvable(components, containedIn: token) variable = try compileResolvable(components, containedIn: token)
condition = nil condition = nil
elseExpression = nil elseExpression = nil
} else { } else {
throw TemplateSyntaxError(reason: "Missing variable name", token: token) throw TemplateSyntaxError(reason: "Missing variable name", token: token)
} }
return VariableNode(variable: variable, token: token, condition: condition, elseExpression: elseExpression) return VariableNode(variable: variable, token: token, condition: condition, elseExpression: elseExpression)
} }
public init(variable: Resolvable, token: Token? = nil) { public init(variable: Resolvable, token: Token? = nil) {
self.variable = variable self.variable = variable
self.token = token self.token = token
self.condition = nil self.condition = nil
self.elseExpression = nil self.elseExpression = nil
} }
init(variable: Resolvable, token: Token? = nil, condition: Expression?, elseExpression: Resolvable?) { init(variable: Resolvable, token: Token? = nil, condition: Expression?, elseExpression: Resolvable?) {
self.variable = variable self.variable = variable
self.token = token self.token = token
self.condition = condition self.condition = condition
self.elseExpression = elseExpression self.elseExpression = elseExpression
} }
public init(variable: String, token: Token? = nil) { public init(variable: String, token: Token? = nil) {
self.variable = Variable(variable) self.variable = Variable(variable)
self.token = token self.token = token
self.condition = nil self.condition = nil
self.elseExpression = nil self.elseExpression = nil
} }
public func render(_ context: Context) throws -> String { public func render(_ context: Context) throws -> String {
if let condition = self.condition, try condition.evaluate(context: context) == false { if let condition = self.condition, try condition.evaluate(context: context) == false {
return try elseExpression?.resolve(context).map(stringify) ?? "" return try elseExpression?.resolve(context).map(stringify) ?? ""
} }
let result = try variable.resolve(context) let result = try variable.resolve(context)
return stringify(result) return stringify(result)
} }
} }
func stringify(_ result: Any?) -> String { func stringify(_ result: Any?) -> String {
if let result = result as? String { if let result = result as? String {
return result return result
} else if let array = result as? [Any?] { } else if let array = result as? [Any?] {
return unwrap(array).description return unwrap(array).description
} else if let result = result as? CustomStringConvertible { } else if let result = result as? CustomStringConvertible {
return result.description return result.description
} else if let result = result as? NSObject { } else if let result = result as? NSObject {
return result.description return result.description
} }
return "" return ""
} }
func unwrap(_ array: [Any?]) -> [Any] { func unwrap(_ array: [Any?]) -> [Any] {
array.map { (item: Any?) -> Any in array.map { (item: Any?) -> Any in
if let item = item { if let item = item {
if let items = item as? [Any?] { if let items = item as? [Any?] {
return unwrap(items) return unwrap(items)
} else { } else {
return item return item
} }
} else { } else {
return item as Any return item as Any
} }
} }
} }

View File

@@ -2,43 +2,43 @@
import Foundation import Foundation
class NowNode: NodeType { class NowNode: NodeType {
let format: Variable let format: Variable
let token: Token? let token: Token?
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType { class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
var format: Variable? var format: Variable?
let components = token.components let components = token.components
guard components.count <= 2 else { guard components.count <= 2 else {
throw TemplateSyntaxError("'now' tags may only have one argument: the format string.") throw TemplateSyntaxError("'now' tags may only have one argument: the format string.")
} }
if components.count == 2 { if components.count == 2 {
format = Variable(components[1]) format = Variable(components[1])
} }
return NowNode(format: format, token: token) return NowNode(format: format, token: token)
} }
init(format: Variable?, token: Token? = nil) { init(format: Variable?, token: Token? = nil) {
self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"") self.format = format ?? Variable("\"yyyy-MM-dd 'at' HH:mm\"")
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
let date = Date() let date = Date()
let format = try self.format.resolve(context) let format = try self.format.resolve(context)
var formatter: DateFormatter var formatter: DateFormatter
if let format = format as? DateFormatter { if let format = format as? DateFormatter {
formatter = format formatter = format
} else if let format = format as? String { } else if let format = format as? String {
formatter = DateFormatter() formatter = DateFormatter()
formatter.dateFormat = format formatter.dateFormat = format
} else { } else {
return "" return ""
} }
return formatter.string(from: date) return formatter.string(from: date)
} }
} }
#endif #endif

View File

@@ -1,272 +1,272 @@
/// Creates a checker that will stop parsing if it encounters a list of tags. /// Creates a checker that will stop parsing if it encounters a list of tags.
/// Useful for example for scanning until a given "end"-node. /// Useful for example for scanning until a given "end"-node.
public func until(_ tags: [String]) -> ((TokenParser, Token) -> Bool) { public func until(_ tags: [String]) -> ((TokenParser, Token) -> Bool) {
{ _, token in { _, token in
if let name = token.components.first { if let name = token.components.first {
for tag in tags where name == tag { for tag in tags where name == tag {
return true return true
} }
} }
return false return false
} }
} }
/// A class for parsing an array of tokens and converts them into a collection of Node's /// A class for parsing an array of tokens and converts them into a collection of Node's
public class TokenParser { public class TokenParser {
/// Parser for finding a kind of node /// Parser for finding a kind of node
public typealias TagParser = (TokenParser, Token) throws -> NodeType public typealias TagParser = (TokenParser, Token) throws -> NodeType
fileprivate var tokens: [Token] fileprivate var tokens: [Token]
fileprivate(set) var parsedTokens: [Token] = [] fileprivate(set) var parsedTokens: [Token] = []
fileprivate let environment: Environment fileprivate let environment: Environment
fileprivate var previousWhiteSpace: WhitespaceBehaviour.Behaviour? fileprivate var previousWhiteSpace: WhitespaceBehaviour.Behaviour?
/// Simple initializer /// Simple initializer
public init(tokens: [Token], environment: Environment) { public init(tokens: [Token], environment: Environment) {
self.tokens = tokens self.tokens = tokens
self.environment = environment self.environment = environment
} }
/// Parse the given tokens into nodes /// Parse the given tokens into nodes
public func parse() throws -> [NodeType] { public func parse() throws -> [NodeType] {
try parse(nil) try parse(nil)
} }
/// Parse nodes until a specific "something" is detected, determined by the provided closure. /// Parse nodes until a specific "something" is detected, determined by the provided closure.
/// Combine this with the `until(:)` function above to scan nodes until a given token. /// Combine this with the `until(:)` function above to scan nodes until a given token.
public func parse(_ parseUntil: ((_ parser: TokenParser, _ token: Token) -> (Bool))?) throws -> [NodeType] { public func parse(_ parseUntil: ((_ parser: TokenParser, _ token: Token) -> (Bool))?) throws -> [NodeType] {
var nodes = [NodeType]() var nodes = [NodeType]()
while !tokens.isEmpty { while !tokens.isEmpty {
guard let token = nextToken() else { break } guard let token = nextToken() else { break }
switch token.kind { switch token.kind {
case .text: case .text:
nodes.append(TextNode(text: token.contents, trimBehaviour: trimBehaviour)) nodes.append(TextNode(text: token.contents, trimBehaviour: trimBehaviour))
case .variable: case .variable:
previousWhiteSpace = nil previousWhiteSpace = nil
try nodes.append(VariableNode.parse(self, token: token)) try nodes.append(VariableNode.parse(self, token: token))
case .block: case .block:
previousWhiteSpace = token.whitespace?.trailing previousWhiteSpace = token.whitespace?.trailing
if let parseUntil = parseUntil, parseUntil(self, token) { if let parseUntil = parseUntil, parseUntil(self, token) {
prependToken(token) prependToken(token)
return nodes return nodes
} }
if var tag = token.components.first { if var tag = token.components.first {
do { do {
// special case for labeled tags (such as for loops) // special case for labeled tags (such as for loops)
if tag.hasSuffix(":") && token.components.count >= 2 { if tag.hasSuffix(":") && token.components.count >= 2 {
tag = token.components[1] tag = token.components[1]
} }
let parser = try environment.findTag(name: tag) let parser = try environment.findTag(name: tag)
let node = try parser(self, token) let node = try parser(self, token)
nodes.append(node) nodes.append(node)
} catch { } catch {
throw error.withToken(token) throw error.withToken(token)
} }
} }
case .comment: case .comment:
previousWhiteSpace = nil previousWhiteSpace = nil
continue continue
} }
} }
return nodes return nodes
} }
/// Pop the next token (returning it) /// Pop the next token (returning it)
public func nextToken() -> Token? { public func nextToken() -> Token? {
if !tokens.isEmpty { if !tokens.isEmpty {
let nextToken = tokens.remove(at: 0) let nextToken = tokens.remove(at: 0)
parsedTokens.append(nextToken) parsedTokens.append(nextToken)
return nextToken return nextToken
} }
return nil return nil
} }
func peekWhitespace() -> WhitespaceBehaviour.Behaviour? { func peekWhitespace() -> WhitespaceBehaviour.Behaviour? {
tokens.first?.whitespace?.leading tokens.first?.whitespace?.leading
} }
/// Insert a token /// Insert a token
public func prependToken(_ token: Token) { public func prependToken(_ token: Token) {
tokens.insert(token, at: 0) tokens.insert(token, at: 0)
if parsedTokens.last == token { if parsedTokens.last == token {
parsedTokens.removeLast() parsedTokens.removeLast()
} }
} }
/// Create filter expression from a string contained in provided token /// Create filter expression from a string contained in provided token
public func compileFilter(_ filterToken: String, containedIn token: Token) throws -> Resolvable { public func compileFilter(_ filterToken: String, containedIn token: Token) throws -> Resolvable {
try environment.compileFilter(filterToken, containedIn: token) try environment.compileFilter(filterToken, containedIn: token)
} }
/// Create boolean expression from components contained in provided token /// Create boolean expression from components contained in provided token
public func compileExpression(components: [String], token: Token) throws -> Expression { public func compileExpression(components: [String], token: Token) throws -> Expression {
try environment.compileExpression(components: components, containedIn: token) try environment.compileExpression(components: components, containedIn: token)
} }
/// Create resolvable (i.e. range variable or filter expression) from a string contained in provided token /// Create resolvable (i.e. range variable or filter expression) from a string contained in provided token
public func compileResolvable(_ token: String, containedIn containingToken: Token) throws -> Resolvable { public func compileResolvable(_ token: String, containedIn containingToken: Token) throws -> Resolvable {
try environment.compileResolvable(token, containedIn: containingToken) try environment.compileResolvable(token, containedIn: containingToken)
} }
private var trimBehaviour: TrimBehaviour { private var trimBehaviour: TrimBehaviour {
var behaviour: TrimBehaviour = .nothing var behaviour: TrimBehaviour = .nothing
if let leading = previousWhiteSpace { if let leading = previousWhiteSpace {
if leading == .unspecified { if leading == .unspecified {
behaviour.leading = environment.trimBehaviour.trailing behaviour.leading = environment.trimBehaviour.trailing
} else { } else {
behaviour.leading = leading == .trim ? .whitespaceAndNewLines : .nothing behaviour.leading = leading == .trim ? .whitespaceAndNewLines : .nothing
} }
} }
if let trailing = peekWhitespace() { if let trailing = peekWhitespace() {
if trailing == .unspecified { if trailing == .unspecified {
behaviour.trailing = environment.trimBehaviour.leading behaviour.trailing = environment.trimBehaviour.leading
} else { } else {
behaviour.trailing = trailing == .trim ? .whitespaceAndNewLines : .nothing behaviour.trailing = trailing == .trim ? .whitespaceAndNewLines : .nothing
} }
} }
return behaviour return behaviour
} }
} }
extension Environment { extension Environment {
func findTag(name: String) throws -> Extension.TagParser { func findTag(name: String) throws -> Extension.TagParser {
for ext in extensions { for ext in extensions {
if let filter = ext.tags[name] { if let filter = ext.tags[name] {
return filter return filter
} }
} }
throw TemplateSyntaxError("Unknown template tag '\(name)'") throw TemplateSyntaxError("Unknown template tag '\(name)'")
} }
func findFilter(_ name: String) throws -> FilterType { func findFilter(_ name: String) throws -> FilterType {
for ext in extensions { for ext in extensions {
if let filter = ext.filters[name] { if let filter = ext.filters[name] {
return filter return filter
} }
} }
let suggestedFilters = self.suggestedFilters(for: name) let suggestedFilters = self.suggestedFilters(for: name)
if suggestedFilters.isEmpty { if suggestedFilters.isEmpty {
throw TemplateSyntaxError("Unknown filter '\(name)'.") throw TemplateSyntaxError("Unknown filter '\(name)'.")
} else { } else {
throw TemplateSyntaxError( throw TemplateSyntaxError(
""" """
Unknown filter '\(name)'. \ Unknown filter '\(name)'. \
Found similar filters: \(suggestedFilters.map { "'\($0)'" }.joined(separator: ", ")). Found similar filters: \(suggestedFilters.map { "'\($0)'" }.joined(separator: ", ")).
""" """
) )
} }
} }
private func suggestedFilters(for name: String) -> [String] { private func suggestedFilters(for name: String) -> [String] {
let allFilters = extensions.flatMap { $0.filters.keys } let allFilters = extensions.flatMap { $0.filters.keys }
let filtersWithDistance = allFilters let filtersWithDistance = allFilters
.map { (filterName: $0, distance: $0.levenshteinDistance(name)) } .map { (filterName: $0, distance: $0.levenshteinDistance(name)) }
// do not suggest filters which names are shorter than the distance // do not suggest filters which names are shorter than the distance
.filter { $0.filterName.count > $0.distance } .filter { $0.filterName.count > $0.distance }
guard let minDistance = filtersWithDistance.min(by: { $0.distance < $1.distance })?.distance else { guard let minDistance = filtersWithDistance.min(by: { $0.distance < $1.distance })?.distance else {
return [] return []
} }
// suggest all filters with the same distance // suggest all filters with the same distance
return filtersWithDistance.filter { $0.distance == minDistance }.map { $0.filterName } return filtersWithDistance.filter { $0.distance == minDistance }.map { $0.filterName }
} }
/// Create filter expression from a string /// Create filter expression from a string
public func compileFilter(_ token: String) throws -> Resolvable { public func compileFilter(_ token: String) throws -> Resolvable {
try FilterExpression(token: token, environment: self) try FilterExpression(token: token, environment: self)
} }
/// Create filter expression from a string contained in provided token /// Create filter expression from a string contained in provided token
public func compileFilter(_ filterToken: String, containedIn containingToken: Token) throws -> Resolvable { public func compileFilter(_ filterToken: String, containedIn containingToken: Token) throws -> Resolvable {
do { do {
return try FilterExpression(token: filterToken, environment: self) return try FilterExpression(token: filterToken, environment: self)
} catch { } catch {
guard var syntaxError = error as? TemplateSyntaxError, syntaxError.token == nil else { guard var syntaxError = error as? TemplateSyntaxError, syntaxError.token == nil else {
throw error throw error
} }
// find offset of filter in the containing token so that only filter is highligted, not the whole token // find offset of filter in the containing token so that only filter is highligted, not the whole token
if let filterTokenRange = containingToken.contents.range(of: filterToken) { if let filterTokenRange = containingToken.contents.range(of: filterToken) {
var location = containingToken.sourceMap.location var location = containingToken.sourceMap.location
location.lineOffset += containingToken.contents.distance( location.lineOffset += containingToken.contents.distance(
from: containingToken.contents.startIndex, from: containingToken.contents.startIndex,
to: filterTokenRange.lowerBound to: filterTokenRange.lowerBound
) )
syntaxError.token = .variable( syntaxError.token = .variable(
value: filterToken, value: filterToken,
at: SourceMap(filename: containingToken.sourceMap.filename, location: location) at: SourceMap(filename: containingToken.sourceMap.filename, location: location)
) )
} else { } else {
syntaxError.token = containingToken syntaxError.token = containingToken
} }
throw syntaxError throw syntaxError
} }
} }
/// Create resolvable (i.e. range variable or filter expression) from a string /// Create resolvable (i.e. range variable or filter expression) from a string
public func compileResolvable(_ token: String) throws -> Resolvable { public func compileResolvable(_ token: String) throws -> Resolvable {
try RangeVariable(token, environment: self) try RangeVariable(token, environment: self)
?? compileFilter(token) ?? compileFilter(token)
} }
/// Create resolvable (i.e. range variable or filter expression) from a string contained in provided token /// Create resolvable (i.e. range variable or filter expression) from a string contained in provided token
public func compileResolvable(_ token: String, containedIn containingToken: Token) throws -> Resolvable { public func compileResolvable(_ token: String, containedIn containingToken: Token) throws -> Resolvable {
try RangeVariable(token, environment: self, containedIn: containingToken) try RangeVariable(token, environment: self, containedIn: containingToken)
?? compileFilter(token, containedIn: containingToken) ?? compileFilter(token, containedIn: containingToken)
} }
/// Create boolean expression from components contained in provided token /// Create boolean expression from components contained in provided token
public func compileExpression(components: [String], containedIn token: Token) throws -> Expression { public func compileExpression(components: [String], containedIn token: Token) throws -> Expression {
try IfExpressionParser.parser(components: components, environment: self, token: token).parse() try IfExpressionParser.parser(components: components, environment: self, token: token).parse()
} }
} }
// https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows // https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows
extension String { extension String {
subscript(_ index: Int) -> Character { subscript(_ index: Int) -> Character {
self[self.index(self.startIndex, offsetBy: index)] self[self.index(self.startIndex, offsetBy: index)]
} }
func levenshteinDistance(_ target: String) -> Int { func levenshteinDistance(_ target: String) -> Int {
// create two work vectors of integer distances // create two work vectors of integer distances
var last, current: [Int] var last, current: [Int]
// initialize v0 (the previous row of distances) // initialize v0 (the previous row of distances)
// this row is A[0][i]: edit distance for an empty s // this row is A[0][i]: edit distance for an empty s
// the distance is just the number of characters to delete from t // the distance is just the number of characters to delete from t
last = [Int](0...target.count) last = [Int](0...target.count)
current = [Int](repeating: 0, count: target.count + 1) current = [Int](repeating: 0, count: target.count + 1)
for selfIndex in 0..<self.count { for selfIndex in 0..<self.count {
// calculate v1 (current row distances) from the previous row v0 // calculate v1 (current row distances) from the previous row v0
// first element of v1 is A[i+1][0] // first element of v1 is A[i+1][0]
// edit distance is delete (i+1) chars from s to match empty t // edit distance is delete (i+1) chars from s to match empty t
current[0] = selfIndex + 1 current[0] = selfIndex + 1
// use formula to fill in the rest of the row // use formula to fill in the rest of the row
for targetIndex in 0..<target.count { for targetIndex in 0..<target.count {
current[targetIndex + 1] = Swift.min( current[targetIndex + 1] = Swift.min(
last[targetIndex + 1] + 1, last[targetIndex + 1] + 1,
current[targetIndex] + 1, current[targetIndex] + 1,
last[targetIndex] + (self[selfIndex] == target[targetIndex] ? 0 : 1) last[targetIndex] + (self[selfIndex] == target[targetIndex] ? 0 : 1)
) )
} }
// copy v1 (current row) to v0 (previous row) for next iteration // copy v1 (current row) to v0 (previous row) for next iteration
last = current last = current
} }
return current[target.count] return current[target.count]
} }
} }

View File

@@ -8,80 +8,80 @@ let NSFileNoSuchFileError = 4
/// A class representing a template /// A class representing a template
open class Template: ExpressibleByStringLiteral { open class Template: ExpressibleByStringLiteral {
let templateString: String let templateString: String
var environment: Environment var environment: Environment
/// The list of parsed (lexed) tokens /// The list of parsed (lexed) tokens
public let tokens: [Token] public let tokens: [Token]
/// The name of the loaded Template if the Template was loaded from a Loader /// The name of the loaded Template if the Template was loaded from a Loader
public let name: String? public let name: String?
/// Create a template with a template string /// Create a template with a template string
public required init(templateString: String, environment: Environment? = nil, name: String? = nil) { public required init(templateString: String, environment: Environment? = nil, name: String? = nil) {
self.environment = environment ?? Environment() self.environment = environment ?? Environment()
self.name = name self.name = name
self.templateString = templateString self.templateString = templateString
let lexer = Lexer(templateName: name, templateString: templateString) let lexer = Lexer(templateName: name, templateString: templateString)
tokens = lexer.tokenize() tokens = lexer.tokenize()
} }
/// Create a template with the given name inside the given bundle /// Create a template with the given name inside the given bundle
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead") @available(*, deprecated, message: "Use Environment/FileSystemLoader instead")
public convenience init(named: String, inBundle bundle: Bundle? = nil) throws { public convenience init(named: String, inBundle bundle: Bundle? = nil) throws {
let useBundle = bundle ?? Bundle.main let useBundle = bundle ?? Bundle.main
guard let url = useBundle.url(forResource: named, withExtension: nil) else { guard let url = useBundle.url(forResource: named, withExtension: nil) else {
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil) throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil)
} }
try self.init(URL: url) try self.init(URL: url)
} }
/// Create a template with a file found at the given URL /// Create a template with a file found at the given URL
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead") @available(*, deprecated, message: "Use Environment/FileSystemLoader instead")
public convenience init(URL: Foundation.URL) throws { public convenience init(URL: Foundation.URL) throws {
guard let path = Path(url: URL) else { guard let path = Path(url: URL) else {
throw TemplateDoesNotExist(templateNames: [URL.lastPathComponent]) throw TemplateDoesNotExist(templateNames: [URL.lastPathComponent])
} }
try self.init(path: path) try self.init(path: path)
} }
/// Create a template with a file found at the given path /// Create a template with a file found at the given path
@available(*, deprecated, message: "Use Environment/FileSystemLoader instead") @available(*, deprecated, message: "Use Environment/FileSystemLoader instead")
public convenience init(path: Path, environment: Environment? = nil, name: String? = nil) throws { public convenience init(path: Path, environment: Environment? = nil, name: String? = nil) throws {
let value = try String(contentsOf: path) let value = try String(contentsOf: path)
self.init(templateString: value, environment: environment, name: name) self.init(templateString: value, environment: environment, name: name)
} }
// MARK: ExpressibleByStringLiteral // MARK: ExpressibleByStringLiteral
// Create a templaVte with a template string literal // Create a templaVte with a template string literal
public required convenience init(stringLiteral value: String) { public required convenience init(stringLiteral value: String) {
self.init(templateString: value) self.init(templateString: value)
} }
// Create a template with a template string literal // Create a template with a template string literal
public required convenience init(extendedGraphemeClusterLiteral value: StringLiteralType) { public required convenience init(extendedGraphemeClusterLiteral value: StringLiteralType) {
self.init(stringLiteral: value) self.init(stringLiteral: value)
} }
// Create a template with a template string literal // Create a template with a template string literal
public required convenience init(unicodeScalarLiteral value: StringLiteralType) { public required convenience init(unicodeScalarLiteral value: StringLiteralType) {
self.init(stringLiteral: value) self.init(stringLiteral: value)
} }
/// Render the given template with a context /// Render the given template with a context
public func render(_ context: Context) throws -> String { public func render(_ context: Context) throws -> String {
let context = context let context = context
let parser = TokenParser(tokens: tokens, environment: context.environment) let parser = TokenParser(tokens: tokens, environment: context.environment)
let nodes = try parser.parse() let nodes = try parser.parse()
return try renderNodes(nodes, context) return try renderNodes(nodes, context)
} }
/// Render the given template /// Render the given template
// swiftlint:disable:next discouraged_optional_collection // swiftlint:disable:next discouraged_optional_collection
open func render(_ dictionary: [String: Any]? = nil) throws -> String { open func render(_ dictionary: [String: Any]? = nil) throws -> String {
try render(Context(dictionary: dictionary ?? [:], environment: environment)) try render(Context(dictionary: dictionary ?? [:], environment: environment))
} }
} }

View File

@@ -1,81 +1,81 @@
import Foundation import Foundation
extension String { extension String {
/// Split a string by a separator leaving quoted phrases together /// Split a string by a separator leaving quoted phrases together
func smartSplit(separator: Character = " ") -> [String] { func smartSplit(separator: Character = " ") -> [String] {
var word = "" var word = ""
var components: [String] = [] var components: [String] = []
var separate: Character = separator var separate: Character = separator
var singleQuoteCount = 0 var singleQuoteCount = 0
var doubleQuoteCount = 0 var doubleQuoteCount = 0
for character in self { for character in self {
if character == "'" { if character == "'" {
singleQuoteCount += 1 singleQuoteCount += 1
} else if character == "\"" { } else if character == "\"" {
doubleQuoteCount += 1 doubleQuoteCount += 1
} }
if character == separate { if character == separate {
if separate != separator { if separate != separator {
word.append(separate) word.append(separate)
} else if (singleQuoteCount.isMultiple(of: 2) || doubleQuoteCount.isMultiple(of: 2)) && !word.isEmpty { } else if (singleQuoteCount.isMultiple(of: 2) || doubleQuoteCount.isMultiple(of: 2)) && !word.isEmpty {
appendWord(word, to: &components) appendWord(word, to: &components)
word = "" word = ""
} }
separate = separator separate = separator
} else { } else {
if separate == separator && (character == "'" || character == "\"") { if separate == separator && (character == "'" || character == "\"") {
separate = character separate = character
} }
word.append(character) word.append(character)
} }
} }
if !word.isEmpty { if !word.isEmpty {
appendWord(word, to: &components) appendWord(word, to: &components)
} }
return components return components
} }
private func appendWord(_ word: String, to components: inout [String]) { private func appendWord(_ word: String, to components: inout [String]) {
let specialCharacters = ",|:" let specialCharacters = ",|:"
if !components.isEmpty { if !components.isEmpty {
if let precedingChar = components.last?.last, specialCharacters.contains(precedingChar) { if let precedingChar = components.last?.last, specialCharacters.contains(precedingChar) {
// special case for labeled for-loops // special case for labeled for-loops
if components.count == 1 && word == "for" { if components.count == 1 && word == "for" {
components.append(word) components.append(word)
} else { } else {
components[components.count - 1] += word components[components.count - 1] += word
} }
} else if specialCharacters.contains(word) { } else if specialCharacters.contains(word) {
components[components.count - 1] += word components[components.count - 1] += word
} else if word != "(" && word.first == "(" || word != ")" && word.first == ")" { } else if word != "(" && word.first == "(" || word != ")" && word.first == ")" {
components.append(String(word.prefix(1))) components.append(String(word.prefix(1)))
appendWord(String(word.dropFirst()), to: &components) appendWord(String(word.dropFirst()), to: &components)
} else if word != "(" && word.last == "(" || word != ")" && word.last == ")" { } else if word != "(" && word.last == "(" || word != ")" && word.last == ")" {
appendWord(String(word.dropLast()), to: &components) appendWord(String(word.dropLast()), to: &components)
components.append(String(word.suffix(1))) components.append(String(word.suffix(1)))
} else { } else {
components.append(word) components.append(word)
} }
} else { } else {
components.append(word) components.append(word)
} }
} }
} }
public struct SourceMap: Equatable { public struct SourceMap: Equatable {
public let filename: String? public let filename: String?
public let location: ContentLocation public let location: ContentLocation
init(filename: String? = nil, location: ContentLocation = ("", 0, 0)) { init(filename: String? = nil, location: ContentLocation = ("", 0, 0)) {
self.filename = filename self.filename = filename
self.location = location self.location = location
} }
static let unknown = Self() static let unknown = Self()
@@ -85,70 +85,70 @@ public struct SourceMap: Equatable {
} }
public struct WhitespaceBehaviour: Equatable { public struct WhitespaceBehaviour: Equatable {
public enum Behaviour { public enum Behaviour {
case unspecified case unspecified
case trim case trim
case keep case keep
} }
let leading: Behaviour let leading: Behaviour
let trailing: Behaviour let trailing: Behaviour
public static let unspecified = Self(leading: .unspecified, trailing: .unspecified) public static let unspecified = Self(leading: .unspecified, trailing: .unspecified)
} }
public class Token: Equatable { public class Token: Equatable {
public enum Kind: Equatable { public enum Kind: Equatable {
/// A token representing a piece of text. /// A token representing a piece of text.
case text case text
/// A token representing a variable. /// A token representing a variable.
case variable case variable
/// A token representing a comment. /// A token representing a comment.
case comment case comment
/// A token representing a template block. /// A token representing a template block.
case block case block
} }
public let contents: String public let contents: String
public let kind: Kind public let kind: Kind
public let sourceMap: SourceMap public let sourceMap: SourceMap
public var whitespace: WhitespaceBehaviour? public var whitespace: WhitespaceBehaviour?
/// Returns the underlying value as an array seperated by spaces /// Returns the underlying value as an array seperated by spaces
public private(set) lazy var components: [String] = self.contents.smartSplit() public private(set) lazy var components: [String] = self.contents.smartSplit()
init(contents: String, kind: Kind, sourceMap: SourceMap, whitespace: WhitespaceBehaviour? = nil) { init(contents: String, kind: Kind, sourceMap: SourceMap, whitespace: WhitespaceBehaviour? = nil) {
self.contents = contents self.contents = contents
self.kind = kind self.kind = kind
self.sourceMap = sourceMap self.sourceMap = sourceMap
self.whitespace = whitespace self.whitespace = whitespace
} }
/// A token representing a piece of text. /// A token representing a piece of text.
public static func text(value: String, at sourceMap: SourceMap) -> Token { public static func text(value: String, at sourceMap: SourceMap) -> Token {
Token(contents: value, kind: .text, sourceMap: sourceMap) Token(contents: value, kind: .text, sourceMap: sourceMap)
} }
/// A token representing a variable. /// A token representing a variable.
public static func variable(value: String, at sourceMap: SourceMap) -> Token { public static func variable(value: String, at sourceMap: SourceMap) -> Token {
Token(contents: value, kind: .variable, sourceMap: sourceMap) Token(contents: value, kind: .variable, sourceMap: sourceMap)
} }
/// A token representing a comment. /// A token representing a comment.
public static func comment(value: String, at sourceMap: SourceMap) -> Token { public static func comment(value: String, at sourceMap: SourceMap) -> Token {
Token(contents: value, kind: .comment, sourceMap: sourceMap) Token(contents: value, kind: .comment, sourceMap: sourceMap)
} }
/// A token representing a template block. /// A token representing a template block.
public static func block( public static func block(
value: String, value: String,
at sourceMap: SourceMap, at sourceMap: SourceMap,
whitespace: WhitespaceBehaviour = .unspecified whitespace: WhitespaceBehaviour = .unspecified
) -> Token { ) -> Token {
Token(contents: value, kind: .block, sourceMap: sourceMap, whitespace: whitespace) Token(contents: value, kind: .block, sourceMap: sourceMap, whitespace: whitespace)
} }
public static func == (lhs: Token, rhs: Token) -> Bool { public static func == (lhs: Token, rhs: Token) -> Bool {
lhs.contents == rhs.contents && lhs.kind == rhs.kind && lhs.sourceMap == rhs.sourceMap lhs.contents == rhs.contents && lhs.kind == rhs.kind && lhs.sourceMap == rhs.sourceMap
} }
} }

View File

@@ -1,75 +1,75 @@
import Foundation import Foundation
public struct TrimBehaviour: Equatable { public struct TrimBehaviour: Equatable {
var leading: Trim var leading: Trim
var trailing: Trim var trailing: Trim
public enum Trim { public enum Trim {
/// nothing /// nothing
case nothing case nothing
/// tabs and spaces /// tabs and spaces
case whitespace case whitespace
/// tabs and spaces and a single new line /// tabs and spaces and a single new line
case whitespaceAndOneNewLine case whitespaceAndOneNewLine
/// all tabs spaces and newlines /// all tabs spaces and newlines
case whitespaceAndNewLines case whitespaceAndNewLines
} }
public init(leading: Trim, trailing: Trim) { public init(leading: Trim, trailing: Trim) {
self.leading = leading self.leading = leading
self.trailing = trailing self.trailing = trailing
} }
/// doesn't touch newlines /// doesn't touch newlines
public static let nothing = Self(leading: .nothing, trailing: .nothing) public static let nothing = Self(leading: .nothing, trailing: .nothing)
/// removes whitespace before a block and whitespace and a single newline after a block /// removes whitespace before a block and whitespace and a single newline after a block
public static let smart = Self(leading: .whitespace, trailing: .whitespaceAndOneNewLine) public static let smart = Self(leading: .whitespace, trailing: .whitespaceAndOneNewLine)
/// removes all whitespace and newlines before and after a block /// removes all whitespace and newlines before and after a block
public static let all = Self(leading: .whitespaceAndNewLines, trailing: .whitespaceAndNewLines) public static let all = Self(leading: .whitespaceAndNewLines, trailing: .whitespaceAndNewLines)
static func leadingRegex(trim: Trim) -> NSRegularExpression { static func leadingRegex(trim: Trim) -> NSRegularExpression {
switch trim { switch trim {
case .nothing: case .nothing:
fatalError("No RegularExpression for none") fatalError("No RegularExpression for none")
case .whitespace: case .whitespace:
return Self.leadingWhitespace return Self.leadingWhitespace
case .whitespaceAndOneNewLine: case .whitespaceAndOneNewLine:
return Self.leadingWhitespaceAndOneNewLine return Self.leadingWhitespaceAndOneNewLine
case .whitespaceAndNewLines: case .whitespaceAndNewLines:
return Self.leadingWhitespaceAndNewlines return Self.leadingWhitespaceAndNewlines
} }
} }
static func trailingRegex(trim: Trim) -> NSRegularExpression { static func trailingRegex(trim: Trim) -> NSRegularExpression {
switch trim { switch trim {
case .nothing: case .nothing:
fatalError("No RegularExpression for none") fatalError("No RegularExpression for none")
case .whitespace: case .whitespace:
return Self.trailingWhitespace return Self.trailingWhitespace
case .whitespaceAndOneNewLine: case .whitespaceAndOneNewLine:
return Self.trailingWhitespaceAndOneNewLine return Self.trailingWhitespaceAndOneNewLine
case .whitespaceAndNewLines: case .whitespaceAndNewLines:
return Self.trailingWhitespaceAndNewLines return Self.trailingWhitespaceAndNewLines
} }
} }
// swiftlint:disable:next force_try // swiftlint:disable:next force_try
private static let leadingWhitespaceAndNewlines = try! NSRegularExpression(pattern: "^\\s+") private static let leadingWhitespaceAndNewlines = try! NSRegularExpression(pattern: "^\\s+")
// swiftlint:disable:next force_try // swiftlint:disable:next force_try
private static let trailingWhitespaceAndNewLines = try! NSRegularExpression(pattern: "\\s+$") private static let trailingWhitespaceAndNewLines = try! NSRegularExpression(pattern: "\\s+$")
// swiftlint:disable:next force_try // swiftlint:disable:next force_try
private static let leadingWhitespaceAndOneNewLine = try! NSRegularExpression(pattern: "^[ \t]*\n") private static let leadingWhitespaceAndOneNewLine = try! NSRegularExpression(pattern: "^[ \t]*\n")
// swiftlint:disable:next force_try // swiftlint:disable:next force_try
private static let trailingWhitespaceAndOneNewLine = try! NSRegularExpression(pattern: "\n[ \t]*$") private static let trailingWhitespaceAndOneNewLine = try! NSRegularExpression(pattern: "\n[ \t]*$")
// swiftlint:disable:next force_try // swiftlint:disable:next force_try
private static let leadingWhitespace = try! NSRegularExpression(pattern: "^[ \t]*") private static let leadingWhitespace = try! NSRegularExpression(pattern: "^[ \t]*")
// swiftlint:disable:next force_try // swiftlint:disable:next force_try
private static let trailingWhitespace = try! NSRegularExpression(pattern: "[ \t]*$") private static let trailingWhitespace = try! NSRegularExpression(pattern: "[ \t]*$")
} }

View File

@@ -3,151 +3,151 @@ import Foundation
typealias Number = Float typealias Number = Float
class FilterExpression: Resolvable { class FilterExpression: Resolvable {
let filters: [(FilterType, [Variable])] let filters: [(FilterType, [Variable])]
let variable: Variable let variable: Variable
init(token: String, environment: Environment) throws { init(token: String, environment: Environment) throws {
let bits = token.smartSplit(separator: "|").map { String($0).trim(character: " ") } let bits = token.smartSplit(separator: "|").map { String($0).trim(character: " ") }
if bits.isEmpty { if bits.isEmpty {
throw TemplateSyntaxError("Variable tags must include at least 1 argument") throw TemplateSyntaxError("Variable tags must include at least 1 argument")
} }
variable = Variable(bits[0]) variable = Variable(bits[0])
let filterBits = bits[bits.indices.suffix(from: 1)] let filterBits = bits[bits.indices.suffix(from: 1)]
do { do {
filters = try filterBits.map { bit in filters = try filterBits.map { bit in
let (name, arguments) = parseFilterComponents(token: bit) let (name, arguments) = parseFilterComponents(token: bit)
let filter = try environment.findFilter(name) let filter = try environment.findFilter(name)
return (filter, arguments) return (filter, arguments)
} }
} catch { } catch {
filters = [] filters = []
throw error throw error
} }
} }
func resolve(_ context: Context) throws -> Any? { func resolve(_ context: Context) throws -> Any? {
let result = try variable.resolve(context) let result = try variable.resolve(context)
return try filters.reduce(result) { value, filter in return try filters.reduce(result) { value, filter in
let arguments = try filter.1.map { try $0.resolve(context) } let arguments = try filter.1.map { try $0.resolve(context) }
return try filter.0.invoke(value: value, arguments: arguments, context: context) return try filter.0.invoke(value: value, arguments: arguments, context: context)
} }
} }
} }
/// A structure used to represent a template variable, and to resolve it in a given context. /// A structure used to represent a template variable, and to resolve it in a given context.
public struct Variable: Equatable, Resolvable { public struct Variable: Equatable, Resolvable {
public let variable: String public let variable: String
/// Create a variable with a string representing the variable /// Create a variable with a string representing the variable
public init(_ variable: String) { public init(_ variable: String) {
self.variable = variable self.variable = variable
} }
/// Resolve the variable in the given context /// Resolve the variable in the given context
public func resolve(_ context: Context) throws -> Any? { public func resolve(_ context: Context) throws -> Any? {
if variable.count > 1 && if variable.count > 1 &&
((variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\""))) { ((variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\""))) {
// String literal // String literal
return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)]) return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)])
} }
// Number literal // Number literal
if let int = Int(variable) { if let int = Int(variable) {
return int return int
} }
if let number = Number(variable) { if let number = Number(variable) {
return number return number
} }
// Boolean literal // Boolean literal
if let bool = Bool(variable) { if let bool = Bool(variable) {
return bool return bool
} }
var current: Any? = context var current: Any? = context
for bit in try lookup(context) { for bit in try lookup(context) {
current = resolve(bit: bit, context: current) current = resolve(bit: bit, context: current)
if current == nil { if current == nil {
return nil return nil
} else if let lazyCurrent = current as? LazyValueWrapper { } else if let lazyCurrent = current as? LazyValueWrapper {
current = try lazyCurrent.value(context: context) current = try lazyCurrent.value(context: context)
} }
} }
if let resolvable = current as? Resolvable { if let resolvable = current as? Resolvable {
current = try resolvable.resolve(context) current = try resolvable.resolve(context)
} else if let node = current as? NodeType { } else if let node = current as? NodeType {
current = try node.render(context) current = try node.render(context)
} }
return normalize(current) return normalize(current)
} }
// Split the lookup string and resolve references if possible // Split the lookup string and resolve references if possible
private func lookup(_ context: Context) throws -> [String] { private func lookup(_ context: Context) throws -> [String] {
let keyPath = KeyPath(variable, in: context) let keyPath = KeyPath(variable, in: context)
return try keyPath.parse() return try keyPath.parse()
} }
// Try to resolve a partial keypath for the given context // Try to resolve a partial keypath for the given context
private func resolve(bit: String, context: Any?) -> Any? { private func resolve(bit: String, context: Any?) -> Any? {
let context = normalize(context) let context = normalize(context)
if let context = context as? Context { if let context = context as? Context {
return context[bit] return context[bit]
} else if let dictionary = context as? [String: Any] { } else if let dictionary = context as? [String: Any] {
return resolve(bit: bit, dictionary: dictionary) return resolve(bit: bit, dictionary: dictionary)
} else if let array = context as? [Any] { } else if let array = context as? [Any] {
return resolve(bit: bit, collection: array) return resolve(bit: bit, collection: array)
} else if let string = context as? String { } else if let string = context as? String {
return resolve(bit: bit, collection: string) return resolve(bit: bit, collection: string)
} else if let value = context as? DynamicMemberLookup { } else if let value = context as? DynamicMemberLookup {
return value[dynamicMember: bit] return value[dynamicMember: bit]
} else if let object = context as? NSObject { // NSKeyValueCoding } else if let object = context as? NSObject { // NSKeyValueCoding
#if canImport(ObjectiveC) #if canImport(ObjectiveC)
if object.responds(to: Selector(bit)) { if object.responds(to: Selector(bit)) {
return object.value(forKey: bit) return object.value(forKey: bit)
} }
#else #else
return nil return nil
#endif #endif
} else if let value = context { } else if let value = context {
return Mirror(reflecting: value).getValue(for: bit) return Mirror(reflecting: value).getValue(for: bit)
} }
return nil return nil
} }
// Try to resolve a partial keypath for the given dictionary // Try to resolve a partial keypath for the given dictionary
private func resolve(bit: String, dictionary: [String: Any]) -> Any? { private func resolve(bit: String, dictionary: [String: Any]) -> Any? {
if bit == "count" { if bit == "count" {
return dictionary.count return dictionary.count
} else { } else {
return dictionary[bit] return dictionary[bit]
} }
} }
// Try to resolve a partial keypath for the given collection // Try to resolve a partial keypath for the given collection
private func resolve<T: Collection>(bit: String, collection: T) -> Any? { private func resolve<T: Collection>(bit: String, collection: T) -> Any? {
if let index = Int(bit) { if let index = Int(bit) {
if index >= 0 && index < collection.count { if index >= 0 && index < collection.count {
return collection[collection.index(collection.startIndex, offsetBy: index)] return collection[collection.index(collection.startIndex, offsetBy: index)]
} else { } else {
return nil return nil
} }
} else if bit == "first" { } else if bit == "first" {
return collection.first return collection.first
} else if bit == "last" { } else if bit == "last" {
return collection[collection.index(collection.endIndex, offsetBy: -1)] return collection[collection.index(collection.endIndex, offsetBy: -1)]
} else if bit == "count" { } else if bit == "count" {
return collection.count return collection.count
} else { } else {
return nil return nil
} }
} }
} }
/// A structure used to represet range of two integer values expressed as `from...to`. /// A structure used to represet range of two integer values expressed as `from...to`.
@@ -155,131 +155,131 @@ public struct Variable: Equatable, Resolvable {
/// Rendering this variable produces array from range `from...to`. /// Rendering this variable produces array from range `from...to`.
/// If `from` is more than `to` array will contain values of reversed range. /// If `from` is more than `to` array will contain values of reversed range.
public struct RangeVariable: Resolvable { public struct RangeVariable: Resolvable {
public let from: Resolvable public let from: Resolvable
// swiftlint:disable:next identifier_name // swiftlint:disable:next identifier_name
public let to: Resolvable public let to: Resolvable
public init?(_ token: String, environment: Environment) throws { public init?(_ token: String, environment: Environment) throws {
let components = token.components(separatedBy: "...") let components = token.components(separatedBy: "...")
guard components.count == 2 else { guard components.count == 2 else {
return nil return nil
} }
self.from = try environment.compileFilter(components[0]) self.from = try environment.compileFilter(components[0])
self.to = try environment.compileFilter(components[1]) self.to = try environment.compileFilter(components[1])
} }
public init?(_ token: String, environment: Environment, containedIn containingToken: Token) throws { public init?(_ token: String, environment: Environment, containedIn containingToken: Token) throws {
let components = token.components(separatedBy: "...") let components = token.components(separatedBy: "...")
guard components.count == 2 else { guard components.count == 2 else {
return nil return nil
} }
self.from = try environment.compileFilter(components[0], containedIn: containingToken) self.from = try environment.compileFilter(components[0], containedIn: containingToken)
self.to = try environment.compileFilter(components[1], containedIn: containingToken) self.to = try environment.compileFilter(components[1], containedIn: containingToken)
} }
public func resolve(_ context: Context) throws -> Any? { public func resolve(_ context: Context) throws -> Any? {
let lowerResolved = try from.resolve(context) let lowerResolved = try from.resolve(context)
let upperResolved = try to.resolve(context) let upperResolved = try to.resolve(context)
guard let lower = lowerResolved.flatMap(toNumber(value:)).flatMap(Int.init) else { guard let lower = lowerResolved.flatMap(toNumber(value:)).flatMap(Int.init) else {
throw TemplateSyntaxError("'from' value is not an Integer (\(lowerResolved ?? "nil"))") throw TemplateSyntaxError("'from' value is not an Integer (\(lowerResolved ?? "nil"))")
} }
guard let upper = upperResolved.flatMap(toNumber(value:)).flatMap(Int.init) else { guard let upper = upperResolved.flatMap(toNumber(value:)).flatMap(Int.init) else {
throw TemplateSyntaxError("'to' value is not an Integer (\(upperResolved ?? "nil") )") throw TemplateSyntaxError("'to' value is not an Integer (\(upperResolved ?? "nil") )")
} }
let range = min(lower, upper)...max(lower, upper) let range = min(lower, upper)...max(lower, upper)
return lower > upper ? Array(range.reversed()) : Array(range) return lower > upper ? Array(range.reversed()) : Array(range)
} }
} }
func normalize(_ current: Any?) -> Any? { func normalize(_ current: Any?) -> Any? {
if let current = current as? Normalizable { if let current = current as? Normalizable {
return current.normalize() return current.normalize()
} }
return current return current
} }
protocol Normalizable { protocol Normalizable {
func normalize() -> Any? func normalize() -> Any?
} }
extension Array: Normalizable { extension Array: Normalizable {
func normalize() -> Any? { func normalize() -> Any? {
map { $0 as Any } map { $0 as Any }
} }
} }
// swiftlint:disable:next legacy_objc_type // swiftlint:disable:next legacy_objc_type
extension NSArray: Normalizable { extension NSArray: Normalizable {
func normalize() -> Any? { func normalize() -> Any? {
map { $0 as Any } map { $0 as Any }
} }
} }
extension Dictionary: Normalizable { extension Dictionary: Normalizable {
func normalize() -> Any? { func normalize() -> Any? {
var dictionary: [String: Any] = [:] var dictionary: [String: Any] = [:]
for (key, value) in self { for (key, value) in self {
if let key = key as? String { if let key = key as? String {
dictionary[key] = Stencil.normalize(value) dictionary[key] = Stencil.normalize(value)
} else if let key = key as? CustomStringConvertible { } else if let key = key as? CustomStringConvertible {
dictionary[key.description] = Stencil.normalize(value) dictionary[key.description] = Stencil.normalize(value)
} }
} }
return dictionary return dictionary
} }
} }
func parseFilterComponents(token: String) -> (String, [Variable]) { func parseFilterComponents(token: String) -> (String, [Variable]) {
var components = token.smartSplit(separator: ":") var components = token.smartSplit(separator: ":")
let name = components.removeFirst().trim(character: " ") let name = components.removeFirst().trim(character: " ")
let variables = components let variables = components
.joined(separator: ":") .joined(separator: ":")
.smartSplit(separator: ",") .smartSplit(separator: ",")
.map { Variable($0.trim(character: " ")) } .map { Variable($0.trim(character: " ")) }
return (name, variables) return (name, variables)
} }
extension Mirror { extension Mirror {
func getValue(for key: String) -> Any? { func getValue(for key: String) -> Any? {
let result = descendant(key) ?? Int(key).flatMap { descendant($0) } let result = descendant(key) ?? Int(key).flatMap { descendant($0) }
if result == nil { if result == nil {
// go through inheritance chain to reach superclass properties // go through inheritance chain to reach superclass properties
return superclassMirror?.getValue(for: key) return superclassMirror?.getValue(for: key)
} else if let result = result { } else if let result = result {
guard String(describing: result) != "nil" else { guard String(describing: result) != "nil" else {
// mirror returns non-nil value even for nil-containing properties // mirror returns non-nil value even for nil-containing properties
// so we have to check if its value is actually nil or not // so we have to check if its value is actually nil or not
return nil return nil
} }
if let result = (result as? AnyOptional)?.wrapped { if let result = (result as? AnyOptional)?.wrapped {
return result return result
} else { } else {
return result return result
} }
} }
return result return result
} }
} }
protocol AnyOptional { protocol AnyOptional {
var wrapped: Any? { get } var wrapped: Any? { get }
} }
extension Optional: AnyOptional { extension Optional: AnyOptional {
var wrapped: Any? { var wrapped: Any? {
switch self { switch self {
case let .some(value): case let .some(value):
return value return value
case .none: case .none:
return nil return nil
} }
} }
} }

View File

@@ -3,158 +3,158 @@ import Spectre
import XCTest import XCTest
final class ContextTests: XCTestCase { final class ContextTests: XCTestCase {
func testContextSubscripting() { func testContextSubscripting() {
describe("Context Subscripting") { test in describe("Context Subscripting") { test in
var context = Context() var context = Context()
test.before { test.before {
context = Context(dictionary: ["name": "Kyle"]) context = Context(dictionary: ["name": "Kyle"])
} }
test.it("allows you to get a value via subscripting") { test.it("allows you to get a value via subscripting") {
try expect(context["name"] as? String) == "Kyle" try expect(context["name"] as? String) == "Kyle"
} }
test.it("allows you to set a value via subscripting") { test.it("allows you to set a value via subscripting") {
context["name"] = "Katie" context["name"] = "Katie"
try expect(context["name"] as? String) == "Katie" try expect(context["name"] as? String) == "Katie"
} }
test.it("allows you to remove a value via subscripting") { test.it("allows you to remove a value via subscripting") {
context["name"] = nil context["name"] = nil
try expect(context["name"]).to.beNil() try expect(context["name"]).to.beNil()
} }
test.it("allows you to retrieve a value from a parent") { test.it("allows you to retrieve a value from a parent") {
try context.push { try context.push {
try expect(context["name"] as? String) == "Kyle" try expect(context["name"] as? String) == "Kyle"
} }
} }
test.it("allows you to override a parent's value") { test.it("allows you to override a parent's value") {
try context.push { try context.push {
context["name"] = "Katie" context["name"] = "Katie"
try expect(context["name"] as? String) == "Katie" try expect(context["name"] as? String) == "Katie"
} }
} }
} }
} }
func testContextRestoration() { func testContextRestoration() {
describe("Context Restoration") { test in describe("Context Restoration") { test in
var context = Context() var context = Context()
test.before { test.before {
context = Context(dictionary: ["name": "Kyle"]) context = Context(dictionary: ["name": "Kyle"])
} }
test.it("allows you to pop to restore previous state") { test.it("allows you to pop to restore previous state") {
context.push { context.push {
context["name"] = "Katie" context["name"] = "Katie"
} }
try expect(context["name"] as? String) == "Kyle" try expect(context["name"] as? String) == "Kyle"
} }
test.it("allows you to remove a parent's value in a level") { test.it("allows you to remove a parent's value in a level") {
try context.push { try context.push {
context["name"] = nil context["name"] = nil
try expect(context["name"]).to.beNil() try expect(context["name"]).to.beNil()
} }
try expect(context["name"] as? String) == "Kyle" try expect(context["name"] as? String) == "Kyle"
} }
test.it("allows you to push a dictionary and run a closure then restoring previous state") { test.it("allows you to push a dictionary and run a closure then restoring previous state") {
var didRun = false var didRun = false
try context.push(dictionary: ["name": "Katie"]) { try context.push(dictionary: ["name": "Katie"]) {
didRun = true didRun = true
try expect(context["name"] as? String) == "Katie" try expect(context["name"] as? String) == "Katie"
} }
try expect(didRun).to.beTrue() try expect(didRun).to.beTrue()
try expect(context["name"] as? String) == "Kyle" try expect(context["name"] as? String) == "Kyle"
} }
test.it("allows you to flatten the context contents") { test.it("allows you to flatten the context contents") {
try context.push(dictionary: ["test": "abc"]) { try context.push(dictionary: ["test": "abc"]) {
let flattened = context.flatten() let flattened = context.flatten()
try expect(flattened.count) == 2 try expect(flattened.count) == 2
try expect(flattened["name"] as? String) == "Kyle" try expect(flattened["name"] as? String) == "Kyle"
try expect(flattened["test"] as? String) == "abc" try expect(flattened["test"] as? String) == "abc"
} }
} }
} }
} }
func testContextLazyEvaluation() { func testContextLazyEvaluation() {
let ticker = Ticker() let ticker = Ticker()
var context = Context() var context = Context()
var wrapper = LazyValueWrapper("") var wrapper = LazyValueWrapper("")
describe("Lazy evaluation") { test in describe("Lazy evaluation") { test in
test.before { test.before {
ticker.count = 0 ticker.count = 0
wrapper = LazyValueWrapper(ticker.tick()) wrapper = LazyValueWrapper(ticker.tick())
context = Context(dictionary: ["name": wrapper]) context = Context(dictionary: ["name": wrapper])
} }
test.it("Evaluates lazy data") { test.it("Evaluates lazy data") {
let template = Template(templateString: "{{ name }}") let template = Template(templateString: "{{ name }}")
let result = try template.render(context) let result = try template.render(context)
try expect(result) == "Kyle" try expect(result) == "Kyle"
try expect(ticker.count) == 1 try expect(ticker.count) == 1
} }
test.it("Evaluates lazy only once") { test.it("Evaluates lazy only once") {
let template = Template(templateString: "{{ name }}{{ name }}") let template = Template(templateString: "{{ name }}{{ name }}")
let result = try template.render(context) let result = try template.render(context)
try expect(result) == "KyleKyle" try expect(result) == "KyleKyle"
try expect(ticker.count) == 1 try expect(ticker.count) == 1
} }
test.it("Does not evaluate lazy data when not used") { test.it("Does not evaluate lazy data when not used") {
let template = Template(templateString: "{{ 'Katie' }}") let template = Template(templateString: "{{ 'Katie' }}")
let result = try template.render(context) let result = try template.render(context)
try expect(result) == "Katie" try expect(result) == "Katie"
try expect(ticker.count) == 0 try expect(ticker.count) == 0
} }
} }
} }
func testContextLazyAccessTypes() { func testContextLazyAccessTypes() {
it("Supports evaluation via context reference") { it("Supports evaluation via context reference") {
let context = Context(dictionary: ["name": "Kyle"]) let context = Context(dictionary: ["name": "Kyle"])
context["alias"] = LazyValueWrapper { $0["name"] ?? "" } context["alias"] = LazyValueWrapper { $0["name"] ?? "" }
let template = Template(templateString: "{{ alias }}") let template = Template(templateString: "{{ alias }}")
try context.push(dictionary: ["name": "Katie"]) { try context.push(dictionary: ["name": "Katie"]) {
let result = try template.render(context) let result = try template.render(context)
try expect(result) == "Katie" try expect(result) == "Katie"
} }
} }
it("Supports evaluation via context copy") { it("Supports evaluation via context copy") {
let context = Context(dictionary: ["name": "Kyle"]) let context = Context(dictionary: ["name": "Kyle"])
context["alias"] = LazyValueWrapper(copying: context) { $0["name"] ?? "" } context["alias"] = LazyValueWrapper(copying: context) { $0["name"] ?? "" }
let template = Template(templateString: "{{ alias }}") let template = Template(templateString: "{{ alias }}")
try context.push(dictionary: ["name": "Katie"]) { try context.push(dictionary: ["name": "Katie"]) {
let result = try template.render(context) let result = try template.render(context)
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
} }
} }
} }
// MARK: - Helpers // MARK: - Helpers
private final class Ticker { private final class Ticker {
var count: Int = 0 var count: Int = 0
func tick() -> String { func tick() -> String {
count += 1 count += 1
return "Kyle" return "Kyle"
} }
} }

View File

@@ -4,122 +4,122 @@ import Spectre
import XCTest import XCTest
final class EnvironmentBaseAndChildTemplateTests: XCTestCase { final class EnvironmentBaseAndChildTemplateTests: XCTestCase {
private var environment = Environment(loader: ExampleLoader()) private var environment = Environment(loader: ExampleLoader())
private var childTemplate: Template = "" private var childTemplate: Template = ""
private var baseTemplate: Template = "" private var baseTemplate: Template = ""
override func setUp() { override func setUp() {
super.setUp() super.setUp()
let path = Path(#file as String)! / ".." / "fixtures" let path = Path(#file as String)! / ".." / "fixtures"
let loader = FileSystemLoader(paths: [path]) let loader = FileSystemLoader(paths: [path])
environment = Environment(loader: loader) environment = Environment(loader: loader)
childTemplate = "" childTemplate = ""
baseTemplate = "" baseTemplate = ""
} }
override func tearDown() { override func tearDown() {
super.tearDown() super.tearDown()
} }
func testSyntaxErrorInBaseTemplate() throws { func testSyntaxErrorInBaseTemplate() throws {
childTemplate = try environment.loadTemplate(name: "invalid-child-super.html") childTemplate = try environment.loadTemplate(name: "invalid-child-super.html")
baseTemplate = try environment.loadTemplate(name: "invalid-base.html") baseTemplate = try environment.loadTemplate(name: "invalid-base.html")
try expectError( try expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
childToken: "extends \"invalid-base.html\"", childToken: "extends \"invalid-base.html\"",
baseToken: "target|unknown" baseToken: "target|unknown"
) )
} }
func testRuntimeErrorInBaseTemplate() throws { func testRuntimeErrorInBaseTemplate() throws {
let filterExtension = Extension() let filterExtension = Extension()
filterExtension.registerFilter("unknown") { (_: Any?) in filterExtension.registerFilter("unknown") { (_: Any?) in
throw TemplateSyntaxError("filter error") throw TemplateSyntaxError("filter error")
} }
environment.extensions += [filterExtension] environment.extensions += [filterExtension]
childTemplate = try environment.loadTemplate(name: "invalid-child-super.html") childTemplate = try environment.loadTemplate(name: "invalid-child-super.html")
baseTemplate = try environment.loadTemplate(name: "invalid-base.html") baseTemplate = try environment.loadTemplate(name: "invalid-base.html")
try expectError( try expectError(
reason: "filter error", reason: "filter error",
childToken: "extends \"invalid-base.html\"", childToken: "extends \"invalid-base.html\"",
baseToken: "target|unknown" baseToken: "target|unknown"
) )
} }
func testSyntaxErrorInChildTemplate() throws { func testSyntaxErrorInChildTemplate() throws {
childTemplate = Template( childTemplate = Template(
templateString: """ templateString: """
{% extends "base.html" %} {% extends "base.html" %}
{% block body %}Child {{ target|unknown }}{% endblock %} {% block body %}Child {{ target|unknown }}{% endblock %}
""", """,
environment: environment, environment: environment,
name: nil name: nil
) )
try expectError( try expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
childToken: "target|unknown", childToken: "target|unknown",
baseToken: nil baseToken: nil
) )
} }
func testRuntimeErrorInChildTemplate() throws { func testRuntimeErrorInChildTemplate() throws {
let filterExtension = Extension() let filterExtension = Extension()
filterExtension.registerFilter("unknown") { (_: Any?) in filterExtension.registerFilter("unknown") { (_: Any?) in
throw TemplateSyntaxError("filter error") throw TemplateSyntaxError("filter error")
} }
environment.extensions += [filterExtension] environment.extensions += [filterExtension]
childTemplate = Template( childTemplate = Template(
templateString: """ templateString: """
{% extends "base.html" %} {% extends "base.html" %}
{% block body %}Child {{ target|unknown }}{% endblock %} {% block body %}Child {{ target|unknown }}{% endblock %}
""", """,
environment: environment, environment: environment,
name: nil name: nil
) )
try expectError( try expectError(
reason: "filter error", reason: "filter error",
childToken: "target|unknown", childToken: "target|unknown",
baseToken: nil baseToken: nil
) )
} }
private func expectError( private func expectError(
reason: String, reason: String,
childToken: String, childToken: String,
baseToken: String?, baseToken: String?,
file: String = #file, file: String = #file,
line: Int = #line, line: Int = #line,
function: String = #function function: String = #function
) throws { ) throws {
var expectedError = expectedSyntaxError(token: childToken, template: childTemplate, description: reason) var expectedError = expectedSyntaxError(token: childToken, template: childTemplate, description: reason)
if let baseToken = baseToken { if let baseToken = baseToken {
expectedError.stackTrace = [ expectedError.stackTrace = [
expectedSyntaxError( expectedSyntaxError(
token: baseToken, token: baseToken,
template: baseTemplate, template: baseTemplate,
description: reason description: reason
).token ).token
].compactMap { $0 } ].compactMap { $0 }
} }
let error = try expect( let error = try expect(
self.environment.render(template: self.childTemplate, context: ["target": "World"]), self.environment.render(template: self.childTemplate, context: ["target": "World"]),
file: file, file: file,
line: line, line: line,
function: function function: function
).toThrow() as TemplateSyntaxError ).toThrow() as TemplateSyntaxError
let reporter = SimpleErrorReporter() let reporter = SimpleErrorReporter()
try expect( try expect(
reporter.renderError(error), reporter.renderError(error),
file: file, file: file,
line: line, line: line,
function: function function: function
) == reporter.renderError(expectedError) ) == reporter.renderError(expectedError)
} }
} }

View File

@@ -4,85 +4,85 @@ import Spectre
import XCTest import XCTest
final class EnvironmentIncludeTemplateTests: XCTestCase { final class EnvironmentIncludeTemplateTests: XCTestCase {
private var environment = Environment(loader: ExampleLoader()) private var environment = Environment(loader: ExampleLoader())
private var template: Template = "" private var template: Template = ""
private var includedTemplate: Template = "" private var includedTemplate: Template = ""
override func setUp() { override func setUp() {
super.setUp() super.setUp()
let path = Path(#file as String)! / ".." / "fixtures" let path = Path(#file as String)! / ".." / "fixtures"
let loader = FileSystemLoader(paths: [path]) let loader = FileSystemLoader(paths: [path])
environment = Environment(loader: loader) environment = Environment(loader: loader)
template = "" template = ""
includedTemplate = "" includedTemplate = ""
} }
override func tearDown() { override func tearDown() {
super.tearDown() super.tearDown()
} }
func testSyntaxError() throws { func testSyntaxError() throws {
template = Template(templateString: """ template = Template(templateString: """
{% include "invalid-include.html" %} {% include "invalid-include.html" %}
""", environment: environment) """, environment: environment)
includedTemplate = try environment.loadTemplate(name: "invalid-include.html") includedTemplate = try environment.loadTemplate(name: "invalid-include.html")
try expectError( try expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: #"include "invalid-include.html""#, token: #"include "invalid-include.html""#,
includedToken: "target|unknown" includedToken: "target|unknown"
) )
} }
func testRuntimeError() throws { func testRuntimeError() throws {
let filterExtension = Extension() let filterExtension = Extension()
filterExtension.registerFilter("unknown") { (_: Any?) in filterExtension.registerFilter("unknown") { (_: Any?) in
throw TemplateSyntaxError("filter error") throw TemplateSyntaxError("filter error")
} }
environment.extensions += [filterExtension] environment.extensions += [filterExtension]
template = Template(templateString: """ template = Template(templateString: """
{% include "invalid-include.html" %} {% include "invalid-include.html" %}
""", environment: environment) """, environment: environment)
includedTemplate = try environment.loadTemplate(name: "invalid-include.html") includedTemplate = try environment.loadTemplate(name: "invalid-include.html")
try expectError( try expectError(
reason: "filter error", reason: "filter error",
token: "include \"invalid-include.html\"", token: "include \"invalid-include.html\"",
includedToken: "target|unknown" includedToken: "target|unknown"
) )
} }
private func expectError( private func expectError(
reason: String, reason: String,
token: String, token: String,
includedToken: String, includedToken: String,
file: String = #file, file: String = #file,
line: Int = #line, line: Int = #line,
function: String = #function function: String = #function
) throws { ) throws {
var expectedError = expectedSyntaxError(token: token, template: template, description: reason) var expectedError = expectedSyntaxError(token: token, template: template, description: reason)
expectedError.stackTrace = [ expectedError.stackTrace = [
expectedSyntaxError( expectedSyntaxError(
token: includedToken, token: includedToken,
template: includedTemplate, template: includedTemplate,
description: reason description: reason
).token ).token
].compactMap { $0 } ].compactMap { $0 }
let error = try expect( let error = try expect(
self.environment.render(template: self.template, context: ["target": "World"]), self.environment.render(template: self.template, context: ["target": "World"]),
file: file, file: file,
line: line, line: line,
function: function function: function
).toThrow() as TemplateSyntaxError ).toThrow() as TemplateSyntaxError
let reporter = SimpleErrorReporter() let reporter = SimpleErrorReporter()
try expect( try expect(
reporter.renderError(error), reporter.renderError(error),
file: file, file: file,
line: line, line: line,
function: function function: function
) == reporter.renderError(expectedError) ) == reporter.renderError(expectedError)
} }
} }

View File

@@ -3,218 +3,218 @@ import Spectre
import XCTest import XCTest
final class EnvironmentTests: XCTestCase { final class EnvironmentTests: XCTestCase {
private var environment = Environment(loader: ExampleLoader()) private var environment = Environment(loader: ExampleLoader())
private var template: Template = "" private var template: Template = ""
override func setUp() { override func setUp() {
super.setUp() super.setUp()
let errorExtension = Extension() let errorExtension = Extension()
errorExtension.registerFilter("throw") { (_: Any?) in errorExtension.registerFilter("throw") { (_: Any?) in
throw TemplateSyntaxError("filter error") throw TemplateSyntaxError("filter error")
} }
errorExtension.registerSimpleTag("simpletag") { _ in errorExtension.registerSimpleTag("simpletag") { _ in
throw TemplateSyntaxError("simpletag error") throw TemplateSyntaxError("simpletag error")
} }
errorExtension.registerTag("customtag") { _, token in errorExtension.registerTag("customtag") { _, token in
ErrorNode(token: token) ErrorNode(token: token)
} }
environment = Environment(loader: ExampleLoader()) environment = Environment(loader: ExampleLoader())
environment.extensions += [errorExtension] environment.extensions += [errorExtension]
template = "" template = ""
} }
override func tearDown() { override func tearDown() {
super.tearDown() super.tearDown()
} }
func testLoading() { func testLoading() {
it("can load a template from a name") { it("can load a template from a name") {
let template = try self.environment.loadTemplate(name: "example.html") let template = try self.environment.loadTemplate(name: "example.html")
try expect(template.name) == "example.html" try expect(template.name) == "example.html"
} }
it("can load a template from a names") { it("can load a template from a names") {
let template = try self.environment.loadTemplate(names: ["first.html", "example.html"]) let template = try self.environment.loadTemplate(names: ["first.html", "example.html"])
try expect(template.name) == "example.html" try expect(template.name) == "example.html"
} }
} }
func testRendering() { func testRendering() {
it("can render a template from a string") { it("can render a template from a string") {
let result = try self.environment.renderTemplate(string: "Hello World") let result = try self.environment.renderTemplate(string: "Hello World")
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
it("can render a template from a file") { it("can render a template from a file") {
let result = try self.environment.renderTemplate(name: "example.html") let result = try self.environment.renderTemplate(name: "example.html")
try expect(result) == "Hello World!" try expect(result) == "Hello World!"
} }
it("allows you to provide a custom template class") { it("allows you to provide a custom template class") {
let environment = Environment(loader: ExampleLoader(), templateClass: CustomTemplate.self) let environment = Environment(loader: ExampleLoader(), templateClass: CustomTemplate.self)
let result = try environment.renderTemplate(string: "Hello World") let result = try environment.renderTemplate(string: "Hello World")
try expect(result) == "here" try expect(result) == "here"
} }
} }
func testSyntaxError() { func testSyntaxError() {
it("reports syntax error on invalid for tag syntax") { it("reports syntax error on invalid for tag syntax") {
self.template = "Hello {% for name in %}{{ name }}, {% endfor %}!" self.template = "Hello {% for name in %}{{ name }}, {% endfor %}!"
try self.expectError( try self.expectError(
reason: "'for' statements should use the syntax: `for <x> in <y> [where <condition>]`.", reason: "'for' statements should use the syntax: `for <x> in <y> [where <condition>]`.",
token: "for name in" token: "for name in"
) )
} }
it("reports syntax error on missing endfor") { it("reports syntax error on missing endfor") {
self.template = "{% for name in names %}{{ name }}" self.template = "{% for name in names %}{{ name }}"
try self.expectError(reason: "`endfor` was not found.", token: "for name in names") try self.expectError(reason: "`endfor` was not found.", token: "for name in names")
} }
it("reports syntax error on unknown tag") { it("reports syntax error on unknown tag") {
self.template = "{% for name in names %}{{ name }}{% end %}" self.template = "{% for name in names %}{{ name }}{% end %}"
try self.expectError(reason: "Unknown template tag 'end'", token: "end") try self.expectError(reason: "Unknown template tag 'end'", token: "end")
} }
} }
func testUnknownFilter() { func testUnknownFilter() {
it("reports syntax error in for tag") { it("reports syntax error in for tag") {
self.template = "{% for name in names|unknown %}{{ name }}{% endfor %}" self.template = "{% for name in names|unknown %}{{ name }}{% endfor %}"
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: "names|unknown" token: "names|unknown"
) )
} }
it("reports syntax error in for-where tag") { it("reports syntax error in for-where tag") {
self.template = "{% for name in names where name|unknown %}{{ name }}{% endfor %}" self.template = "{% for name in names where name|unknown %}{{ name }}{% endfor %}"
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: "name|unknown" token: "name|unknown"
) )
} }
it("reports syntax error in if tag") { it("reports syntax error in if tag") {
self.template = "{% if name|unknown %}{{ name }}{% endif %}" self.template = "{% if name|unknown %}{{ name }}{% endif %}"
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: "name|unknown" token: "name|unknown"
) )
} }
it("reports syntax error in elif tag") { it("reports syntax error in elif tag") {
self.template = "{% if name %}{{ name }}{% elif name|unknown %}{% endif %}" self.template = "{% if name %}{{ name }}{% elif name|unknown %}{% endif %}"
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: "name|unknown" token: "name|unknown"
) )
} }
it("reports syntax error in ifnot tag") { it("reports syntax error in ifnot tag") {
self.template = "{% ifnot name|unknown %}{{ name }}{% endif %}" self.template = "{% ifnot name|unknown %}{{ name }}{% endif %}"
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: "name|unknown" token: "name|unknown"
) )
} }
it("reports syntax error in filter tag") { it("reports syntax error in filter tag") {
self.template = "{% filter unknown %}Text{% endfilter %}" self.template = "{% filter unknown %}Text{% endfilter %}"
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: "filter unknown" token: "filter unknown"
) )
} }
it("reports syntax error in variable tag") { it("reports syntax error in variable tag") {
self.template = "{{ name|unknown }}" self.template = "{{ name|unknown }}"
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.", reason: "Unknown filter 'unknown'. Found similar filters: 'uppercase'.",
token: "name|unknown" token: "name|unknown"
) )
} }
it("reports error in variable tag") { it("reports error in variable tag") {
self.template = "{{ }}" self.template = "{{ }}"
try self.expectError(reason: "Missing variable name", token: " ") try self.expectError(reason: "Missing variable name", token: " ")
} }
} }
func testRenderingError() { func testRenderingError() {
it("reports rendering error in variable filter") { it("reports rendering error in variable filter") {
self.template = Template(templateString: "{{ name|throw }}", environment: self.environment) self.template = Template(templateString: "{{ name|throw }}", environment: self.environment)
try self.expectError(reason: "filter error", token: "name|throw") try self.expectError(reason: "filter error", token: "name|throw")
} }
it("reports rendering error in filter tag") { it("reports rendering error in filter tag") {
self.template = Template(templateString: "{% filter throw %}Test{% endfilter %}", environment: self.environment) self.template = Template(templateString: "{% filter throw %}Test{% endfilter %}", environment: self.environment)
try self.expectError(reason: "filter error", token: "filter throw") try self.expectError(reason: "filter error", token: "filter throw")
} }
it("reports rendering error in simple tag") { it("reports rendering error in simple tag") {
self.template = Template(templateString: "{% simpletag %}", environment: self.environment) self.template = Template(templateString: "{% simpletag %}", environment: self.environment)
try self.expectError(reason: "simpletag error", token: "simpletag") try self.expectError(reason: "simpletag error", token: "simpletag")
} }
it("reports passing argument to simple filter") { it("reports passing argument to simple filter") {
self.template = "{{ name|uppercase:5 }}" self.template = "{{ name|uppercase:5 }}"
try self.expectError(reason: "Can't invoke filter with an argument", token: "name|uppercase:5") try self.expectError(reason: "Can't invoke filter with an argument", token: "name|uppercase:5")
} }
it("reports rendering error in custom tag") { it("reports rendering error in custom tag") {
self.template = Template(templateString: "{% customtag %}", environment: self.environment) self.template = Template(templateString: "{% customtag %}", environment: self.environment)
try self.expectError(reason: "Custom Error", token: "customtag") try self.expectError(reason: "Custom Error", token: "customtag")
} }
it("reports rendering error in for body") { it("reports rendering error in for body") {
self.template = Template(templateString: """ self.template = Template(templateString: """
{% for name in names %}{% customtag %}{% endfor %} {% for name in names %}{% customtag %}{% endfor %}
""", environment: self.environment) """, environment: self.environment)
try self.expectError(reason: "Custom Error", token: "customtag") try self.expectError(reason: "Custom Error", token: "customtag")
} }
it("reports rendering error in block") { it("reports rendering error in block") {
self.template = Template( self.template = Template(
templateString: "{% block some %}{% customtag %}{% endblock %}", templateString: "{% block some %}{% customtag %}{% endblock %}",
environment: self.environment environment: self.environment
) )
try self.expectError(reason: "Custom Error", token: "customtag") try self.expectError(reason: "Custom Error", token: "customtag")
} }
} }
private func expectError( private func expectError(
reason: String, reason: String,
token: String, token: String,
file: String = #file, file: String = #file,
line: Int = #line, line: Int = #line,
function: String = #function function: String = #function
) throws { ) throws {
let expectedError = expectedSyntaxError(token: token, template: template, description: reason) let expectedError = expectedSyntaxError(token: token, template: template, description: reason)
let error = try expect( let error = try expect(
self.environment.render(template: self.template, context: ["names": ["Bob", "Alice"], "name": "Bob"]), self.environment.render(template: self.template, context: ["names": ["Bob", "Alice"], "name": "Bob"]),
file: file, file: file,
line: line, line: line,
function: function function: function
).toThrow() as TemplateSyntaxError ).toThrow() as TemplateSyntaxError
let reporter = SimpleErrorReporter() let reporter = SimpleErrorReporter()
try expect( try expect(
reporter.renderError(error), reporter.renderError(error),
file: file, file: file,
line: line, line: line,
function: function function: function
) == reporter.renderError(expectedError) ) == reporter.renderError(expectedError)
} }
} }
// MARK: - Helpers // MARK: - Helpers
private class CustomTemplate: Template { private class CustomTemplate: Template {
// swiftlint:disable discouraged_optional_collection // swiftlint:disable discouraged_optional_collection
override func render(_ dictionary: [String: Any]? = nil) throws -> String { override func render(_ dictionary: [String: Any]? = nil) throws -> String {
"here" "here"
} }
} }

View File

@@ -3,353 +3,353 @@ import Spectre
import XCTest import XCTest
final class ExpressionsTests: XCTestCase { final class ExpressionsTests: XCTestCase {
private let parser = TokenParser(tokens: [], environment: Environment()) private let parser = TokenParser(tokens: [], environment: Environment())
private func makeExpression(_ components: [String]) -> Stencil.Expression { private func makeExpression(_ components: [String]) -> Stencil.Expression {
do { do {
let parser = try IfExpressionParser.parser( let parser = try IfExpressionParser.parser(
components: components, components: components,
environment: Environment(), environment: Environment(),
token: .text(value: "", at: .unknown) token: .text(value: "", at: .unknown)
) )
return try parser.parse() return try parser.parse()
} catch { } catch {
fatalError(error.localizedDescription) fatalError(error.localizedDescription)
} }
} }
func testTrueExpressions() { func testTrueExpressions() {
let expression = VariableExpression(variable: Variable("value")) let expression = VariableExpression(variable: Variable("value"))
it("evaluates to true when value is not nil") { it("evaluates to true when value is not nil") {
let context = Context(dictionary: ["value": "known"]) let context = Context(dictionary: ["value": "known"])
try expect(try expression.evaluate(context: context)).to.beTrue() try expect(try expression.evaluate(context: context)).to.beTrue()
} }
it("evaluates to true when array variable is not empty") { it("evaluates to true when array variable is not empty") {
let items: [[String: Any]] = [["key": "key1", "value": 42], ["key": "key2", "value": 1_337]] let items: [[String: Any]] = [["key": "key1", "value": 42], ["key": "key2", "value": 1_337]]
let context = Context(dictionary: ["value": [items]]) let context = Context(dictionary: ["value": [items]])
try expect(try expression.evaluate(context: context)).to.beTrue() try expect(try expression.evaluate(context: context)).to.beTrue()
} }
it("evaluates to false when dictionary value is empty") { it("evaluates to false when dictionary value is empty") {
let emptyItems = [String: Any]() let emptyItems = [String: Any]()
let context = Context(dictionary: ["value": emptyItems]) let context = Context(dictionary: ["value": emptyItems])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to true when integer value is above 0") { it("evaluates to true when integer value is above 0") {
let context = Context(dictionary: ["value": 1]) let context = Context(dictionary: ["value": 1])
try expect(try expression.evaluate(context: context)).to.beTrue() try expect(try expression.evaluate(context: context)).to.beTrue()
} }
it("evaluates to true with string") { it("evaluates to true with string") {
let context = Context(dictionary: ["value": "test"]) let context = Context(dictionary: ["value": "test"])
try expect(try expression.evaluate(context: context)).to.beTrue() try expect(try expression.evaluate(context: context)).to.beTrue()
} }
it("evaluates to true when float value is above 0") { it("evaluates to true when float value is above 0") {
let context = Context(dictionary: ["value": Float(0.5)]) let context = Context(dictionary: ["value": Float(0.5)])
try expect(try expression.evaluate(context: context)).to.beTrue() try expect(try expression.evaluate(context: context)).to.beTrue()
} }
it("evaluates to true when double value is above 0") { it("evaluates to true when double value is above 0") {
let context = Context(dictionary: ["value": Double(0.5)]) let context = Context(dictionary: ["value": Double(0.5)])
try expect(try expression.evaluate(context: context)).to.beTrue() try expect(try expression.evaluate(context: context)).to.beTrue()
} }
} }
func testFalseExpressions() { func testFalseExpressions() {
let expression = VariableExpression(variable: Variable("value")) let expression = VariableExpression(variable: Variable("value"))
it("evaluates to false when value is unset") { it("evaluates to false when value is unset") {
let context = Context() let context = Context()
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to false when array value is empty") { it("evaluates to false when array value is empty") {
let emptyItems = [[String: Any]]() let emptyItems = [[String: Any]]()
let context = Context(dictionary: ["value": emptyItems]) let context = Context(dictionary: ["value": emptyItems])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to false when dictionary value is empty") { it("evaluates to false when dictionary value is empty") {
let emptyItems = [String: Any]() let emptyItems = [String: Any]()
let context = Context(dictionary: ["value": emptyItems]) let context = Context(dictionary: ["value": emptyItems])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to false when Array<Any> value is empty") { it("evaluates to false when Array<Any> value is empty") {
let context = Context(dictionary: ["value": ([] as [Any])]) let context = Context(dictionary: ["value": ([] as [Any])])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to false when empty string") { it("evaluates to false when empty string") {
let context = Context(dictionary: ["value": ""]) let context = Context(dictionary: ["value": ""])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to false when integer value is below 0 or below") { it("evaluates to false when integer value is below 0 or below") {
let context = Context(dictionary: ["value": 0]) let context = Context(dictionary: ["value": 0])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
let negativeContext = Context(dictionary: ["value": -1]) let negativeContext = Context(dictionary: ["value": -1])
try expect(try expression.evaluate(context: negativeContext)).to.beFalse() try expect(try expression.evaluate(context: negativeContext)).to.beFalse()
} }
it("evaluates to false when float is 0 or below") { it("evaluates to false when float is 0 or below") {
let context = Context(dictionary: ["value": Float(0)]) let context = Context(dictionary: ["value": Float(0)])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to false when double is 0 or below") { it("evaluates to false when double is 0 or below") {
let context = Context(dictionary: ["value": Double(0)]) let context = Context(dictionary: ["value": Double(0)])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
it("evaluates to false when uint is 0") { it("evaluates to false when uint is 0") {
let context = Context(dictionary: ["value": UInt(0)]) let context = Context(dictionary: ["value": UInt(0)])
try expect(try expression.evaluate(context: context)).to.beFalse() try expect(try expression.evaluate(context: context)).to.beFalse()
} }
} }
func testNotExpression() { func testNotExpression() {
it("returns truthy for positive expressions") { it("returns truthy for positive expressions") {
let expression = NotExpression(expression: VariableExpression(variable: Variable("true"))) let expression = NotExpression(expression: VariableExpression(variable: Variable("true")))
try expect(expression.evaluate(context: Context())).to.beFalse() try expect(expression.evaluate(context: Context())).to.beFalse()
} }
it("returns falsy for negative expressions") { it("returns falsy for negative expressions") {
let expression = NotExpression(expression: VariableExpression(variable: Variable("false"))) let expression = NotExpression(expression: VariableExpression(variable: Variable("false")))
try expect(expression.evaluate(context: Context())).to.beTrue() try expect(expression.evaluate(context: Context())).to.beTrue()
} }
} }
func testExpressionParsing() { func testExpressionParsing() {
it("can parse a variable expression") { it("can parse a variable expression") {
let expression = self.makeExpression(["value"]) let expression = self.makeExpression(["value"])
try expect(expression.evaluate(context: Context())).to.beFalse() try expect(expression.evaluate(context: Context())).to.beFalse()
try expect(expression.evaluate(context: Context(dictionary: ["value": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["value": true]))).to.beTrue()
} }
it("can parse a not expression") { it("can parse a not expression") {
let expression = self.makeExpression(["not", "value"]) let expression = self.makeExpression(["not", "value"])
try expect(expression.evaluate(context: Context())).to.beTrue() try expect(expression.evaluate(context: Context())).to.beTrue()
try expect(expression.evaluate(context: Context(dictionary: ["value": true]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["value": true]))).to.beFalse()
} }
} }
func testAndExpression() { func testAndExpression() {
let expression = makeExpression(["lhs", "and", "rhs"]) let expression = makeExpression(["lhs", "and", "rhs"])
it("evaluates to false with lhs false") { it("evaluates to false with lhs false") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": true]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": true]))).to.beFalse()
} }
it("evaluates to false with rhs false") { it("evaluates to false with rhs false") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beFalse()
} }
it("evaluates to false with lhs and rhs false") { it("evaluates to false with lhs and rhs false") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": false]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": false]))).to.beFalse()
} }
it("evaluates to true with lhs and rhs true") { it("evaluates to true with lhs and rhs true") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue()
} }
} }
func testOrExpression() { func testOrExpression() {
let expression = makeExpression(["lhs", "or", "rhs"]) let expression = makeExpression(["lhs", "or", "rhs"])
it("evaluates to true with lhs true") { it("evaluates to true with lhs true") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beTrue()
} }
it("evaluates to true with rhs true") { it("evaluates to true with rhs true") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": true]))).to.beTrue()
} }
it("evaluates to true with lhs and rhs true") { it("evaluates to true with lhs and rhs true") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue()
} }
it("evaluates to false with lhs and rhs false") { it("evaluates to false with lhs and rhs false") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": false]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": false, "rhs": false]))).to.beFalse()
} }
} }
func testEqualityExpression() { func testEqualityExpression() {
let expression = makeExpression(["lhs", "==", "rhs"]) let expression = makeExpression(["lhs", "==", "rhs"])
it("evaluates to true with equal lhs/rhs") { it("evaluates to true with equal lhs/rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "a"]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "a"]))).to.beTrue()
} }
it("evaluates to false with non equal lhs/rhs") { it("evaluates to false with non equal lhs/rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beFalse()
} }
it("evaluates to true with nils") { it("evaluates to true with nils") {
try expect(expression.evaluate(context: Context(dictionary: [:]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: [:]))).to.beTrue()
} }
it("evaluates to true with numbers") { it("evaluates to true with numbers") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.0]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.0]))).to.beTrue()
} }
it("evaluates to false with non equal numbers") { it("evaluates to false with non equal numbers") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.1]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.1]))).to.beFalse()
} }
it("evaluates to true with booleans") { it("evaluates to true with booleans") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue()
} }
it("evaluates to false with falsy booleans") { it("evaluates to false with falsy booleans") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": false]))).to.beFalse()
} }
it("evaluates to false with different types") { it("evaluates to false with different types") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": 1]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": 1]))).to.beFalse()
} }
} }
func testInequalityExpression() { func testInequalityExpression() {
let expression = makeExpression(["lhs", "!=", "rhs"]) let expression = makeExpression(["lhs", "!=", "rhs"])
it("evaluates to true with inequal lhs/rhs") { it("evaluates to true with inequal lhs/rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beTrue()
} }
it("evaluates to false with equal lhs/rhs") { it("evaluates to false with equal lhs/rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "b", "rhs": "b"]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "b", "rhs": "b"]))).to.beFalse()
} }
} }
func testMoreThanExpression() { func testMoreThanExpression() {
let expression = makeExpression(["lhs", ">", "rhs"]) let expression = makeExpression(["lhs", ">", "rhs"])
it("evaluates to true with lhs > rhs") { it("evaluates to true with lhs > rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 4]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 4]))).to.beTrue()
} }
it("evaluates to false with lhs == rhs") { it("evaluates to false with lhs == rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.0]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.0]))).to.beFalse()
} }
} }
func testMoreThanEqualExpression() { func testMoreThanEqualExpression() {
let expression = makeExpression(["lhs", ">=", "rhs"]) let expression = makeExpression(["lhs", ">=", "rhs"])
it("evaluates to true with lhs == rhs") { it("evaluates to true with lhs == rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5]))).to.beTrue()
} }
it("evaluates to false with lhs < rhs") { it("evaluates to false with lhs < rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.1]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.1]))).to.beFalse()
} }
} }
func testLessThanExpression() { func testLessThanExpression() {
let expression = makeExpression(["lhs", "<", "rhs"]) let expression = makeExpression(["lhs", "<", "rhs"])
it("evaluates to true with lhs < rhs") { it("evaluates to true with lhs < rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 4, "rhs": 4.5]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 4, "rhs": 4.5]))).to.beTrue()
} }
it("evaluates to false with lhs == rhs") { it("evaluates to false with lhs == rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.0]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5.0]))).to.beFalse()
} }
} }
func testLessThanEqualExpression() { func testLessThanEqualExpression() {
let expression = makeExpression(["lhs", "<=", "rhs"]) let expression = makeExpression(["lhs", "<=", "rhs"])
it("evaluates to true with lhs == rhs") { it("evaluates to true with lhs == rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.0, "rhs": 5]))).to.beTrue()
} }
it("evaluates to false with lhs > rhs") { it("evaluates to false with lhs > rhs") {
try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.1, "rhs": 5.0]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": 5.1, "rhs": 5.0]))).to.beFalse()
} }
} }
func testMultipleExpressions() { func testMultipleExpressions() {
let expression = makeExpression(["one", "or", "two", "and", "not", "three"]) let expression = makeExpression(["one", "or", "two", "and", "not", "three"])
it("evaluates to true with one") { it("evaluates to true with one") {
try expect(expression.evaluate(context: Context(dictionary: ["one": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["one": true]))).to.beTrue()
} }
it("evaluates to true with one and three") { it("evaluates to true with one and three") {
try expect(expression.evaluate(context: Context(dictionary: ["one": true, "three": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["one": true, "three": true]))).to.beTrue()
} }
it("evaluates to true with two") { it("evaluates to true with two") {
try expect(expression.evaluate(context: Context(dictionary: ["two": true]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["two": true]))).to.beTrue()
} }
it("evaluates to false with two and three") { it("evaluates to false with two and three") {
try expect(expression.evaluate(context: Context(dictionary: ["two": true, "three": true]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["two": true, "three": true]))).to.beFalse()
} }
it("evaluates to false with two and three") { it("evaluates to false with two and three") {
try expect(expression.evaluate(context: Context(dictionary: ["two": true, "three": true]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["two": true, "three": true]))).to.beFalse()
} }
it("evaluates to false with nothing") { it("evaluates to false with nothing") {
try expect(expression.evaluate(context: Context())).to.beFalse() try expect(expression.evaluate(context: Context())).to.beFalse()
} }
} }
func testTrueInExpression() throws { func testTrueInExpression() throws {
let expression = makeExpression(["lhs", "in", "rhs"]) let expression = makeExpression(["lhs", "in", "rhs"])
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": 1, "lhs": 1,
"rhs": [1, 2, 3] "rhs": [1, 2, 3]
]))).to.beTrue() ]))).to.beTrue()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": "a", "lhs": "a",
"rhs": ["a", "b", "c"] "rhs": ["a", "b", "c"]
]))).to.beTrue() ]))).to.beTrue()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": "a", "lhs": "a",
"rhs": "abc" "rhs": "abc"
]))).to.beTrue() ]))).to.beTrue()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": 1, "lhs": 1,
"rhs": 1...3 "rhs": 1...3
]))).to.beTrue() ]))).to.beTrue()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": 1, "lhs": 1,
"rhs": 1..<3 "rhs": 1..<3
]))).to.beTrue() ]))).to.beTrue()
} }
func testFalseInExpression() throws { func testFalseInExpression() throws {
let expression = makeExpression(["lhs", "in", "rhs"]) let expression = makeExpression(["lhs", "in", "rhs"])
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": 1, "lhs": 1,
"rhs": [2, 3, 4] "rhs": [2, 3, 4]
]))).to.beFalse() ]))).to.beFalse()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": "a", "lhs": "a",
"rhs": ["b", "c", "d"] "rhs": ["b", "c", "d"]
]))).to.beFalse() ]))).to.beFalse()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": "a", "lhs": "a",
"rhs": "bcd" "rhs": "bcd"
]))).to.beFalse() ]))).to.beFalse()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": 4, "lhs": 4,
"rhs": 1...3 "rhs": 1...3
]))).to.beFalse() ]))).to.beFalse()
try expect(expression.evaluate(context: Context(dictionary: [ try expect(expression.evaluate(context: Context(dictionary: [
"lhs": 3, "lhs": 3,
"rhs": 1..<3 "rhs": 1..<3
]))).to.beFalse() ]))).to.beFalse()
} }
} }

View File

@@ -3,380 +3,380 @@ import Spectre
import XCTest import XCTest
final class FilterTests: XCTestCase { final class FilterTests: XCTestCase {
func testRegistration() { func testRegistration() {
let context: [String: Any] = ["name": "Kyle"] let context: [String: Any] = ["name": "Kyle"]
it("allows you to register a custom filter") { it("allows you to register a custom filter") {
let template = Template(templateString: "{{ name|repeat }}") let template = Template(templateString: "{{ name|repeat }}")
let repeatExtension = Extension() let repeatExtension = Extension()
repeatExtension.registerFilter("repeat") { (value: Any?) in repeatExtension.registerFilter("repeat") { (value: Any?) 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( let result = try template.render(Context(
dictionary: context, dictionary: context,
environment: Environment(extensions: [repeatExtension]) environment: Environment(extensions: [repeatExtension])
)) ))
try expect(result) == "Kyle Kyle" try expect(result) == "Kyle Kyle"
} }
it("allows you to register boolean filters") { it("allows you to register boolean filters") {
let repeatExtension = Extension() let repeatExtension = Extension()
repeatExtension.registerFilter(name: "isPositive", negativeFilterName: "isNotPositive") { (value: Any?) in repeatExtension.registerFilter(name: "isPositive", negativeFilterName: "isNotPositive") { (value: Any?) in
if let value = value as? Int { if let value = value as? Int {
return value > 0 return value > 0
} }
return nil return nil
} }
let result = try Template(templateString: "{{ value|isPositive }}") let result = try Template(templateString: "{{ value|isPositive }}")
.render(Context(dictionary: ["value": 1], environment: Environment(extensions: [repeatExtension]))) .render(Context(dictionary: ["value": 1], environment: Environment(extensions: [repeatExtension])))
try expect(result) == "true" try expect(result) == "true"
let negativeResult = try Template(templateString: "{{ value|isNotPositive }}") let negativeResult = try Template(templateString: "{{ value|isNotPositive }}")
.render(Context(dictionary: ["value": -1], environment: Environment(extensions: [repeatExtension]))) .render(Context(dictionary: ["value": -1], environment: Environment(extensions: [repeatExtension])))
try expect(negativeResult) == "true" try expect(negativeResult) == "true"
} }
it("allows you to register a custom which throws") { it("allows you to register a custom which throws") {
let template = Template(templateString: "{{ name|repeat }}") let template = Template(templateString: "{{ name|repeat }}")
let repeatExtension = Extension() let repeatExtension = Extension()
repeatExtension.registerFilter("repeat") { (_: Any?) in repeatExtension.registerFilter("repeat") { (_: Any?) in
throw TemplateSyntaxError("No Repeat") throw TemplateSyntaxError("No Repeat")
} }
let context = Context(dictionary: context, environment: Environment(extensions: [repeatExtension])) let context = Context(dictionary: context, environment: Environment(extensions: [repeatExtension]))
try expect(try template.render(context)) try expect(try template.render(context))
.toThrow(TemplateSyntaxError(reason: "No Repeat", token: template.tokens.first)) .toThrow(TemplateSyntaxError(reason: "No Repeat", token: template.tokens.first))
} }
it("throws when you pass arguments to simple filter") { it("throws when you pass arguments to simple filter") {
let template = Template(templateString: "{{ name|uppercase:5 }}") let template = Template(templateString: "{{ name|uppercase:5 }}")
try expect(try template.render(Context(dictionary: ["name": "kyle"]))).toThrow() try expect(try template.render(Context(dictionary: ["name": "kyle"]))).toThrow()
} }
} }
func testRegistrationOverrideDefault() throws { func testRegistrationOverrideDefault() throws {
let template = Template(templateString: "{{ name|join }}") let template = Template(templateString: "{{ name|join }}")
let context: [String: Any] = ["name": "Kyle"] let context: [String: Any] = ["name": "Kyle"]
let repeatExtension = Extension() let repeatExtension = Extension()
repeatExtension.registerFilter("join") { (_: Any?) in repeatExtension.registerFilter("join") { (_: Any?) in
"joined" "joined"
} }
let result = try template.render(Context( let result = try template.render(Context(
dictionary: context, dictionary: context,
environment: Environment(extensions: [repeatExtension]) environment: Environment(extensions: [repeatExtension])
)) ))
try expect(result) == "joined" try expect(result) == "joined"
} }
func testRegistrationWithArguments() { func testRegistrationWithArguments() {
let context: [String: Any] = ["name": "Kyle"] let context: [String: Any] = ["name": "Kyle"]
it("allows you to register a custom filter which accepts single argument") { it("allows you to register a custom filter which accepts single argument") {
let template = Template(templateString: """ let template = Template(templateString: """
{{ name|repeat:'value1, "value2"' }} {{ name|repeat:'value1, "value2"' }}
""") """)
let repeatExtension = Extension() let repeatExtension = Extension()
repeatExtension.registerFilter("repeat") { value, arguments in repeatExtension.registerFilter("repeat") { value, arguments in
guard let value = value, guard let value = value,
let argument = arguments.first else { return nil } let argument = arguments.first else { return nil }
return "\(value) \(value) with args \(argument ?? "")" return "\(value) \(value) with args \(argument ?? "")"
} }
let result = try template.render(Context( let result = try template.render(Context(
dictionary: context, dictionary: context,
environment: Environment(extensions: [repeatExtension]) environment: Environment(extensions: [repeatExtension])
)) ))
try expect(result) == """ try expect(result) == """
Kyle Kyle with args value1, "value2" Kyle Kyle with args value1, "value2"
""" """
} }
it("allows you to register a custom filter which accepts several arguments") { it("allows you to register a custom filter which accepts several arguments") {
let template = Template(templateString: """ let template = Template(templateString: """
{{ name|repeat:'value"1"',"value'2'",'(key, value)' }} {{ name|repeat:'value"1"',"value'2'",'(key, value)' }}
""") """)
let repeatExtension = Extension() let repeatExtension = Extension()
repeatExtension.registerFilter("repeat") { value, arguments in repeatExtension.registerFilter("repeat") { value, arguments in
guard let value = value else { return nil } guard let value = value else { return nil }
let args = arguments.compactMap { $0 } let args = arguments.compactMap { $0 }
return "\(value) \(value) with args 0: \(args[0]), 1: \(args[1]), 2: \(args[2])" return "\(value) \(value) with args 0: \(args[0]), 1: \(args[1]), 2: \(args[2])"
} }
let result = try template.render(Context( let result = try template.render(Context(
dictionary: context, dictionary: context,
environment: Environment(extensions: [repeatExtension]) environment: Environment(extensions: [repeatExtension])
)) ))
try expect(result) == """ try expect(result) == """
Kyle Kyle with args 0: value"1", 1: value'2', 2: (key, value) Kyle Kyle with args 0: value"1", 1: value'2', 2: (key, value)
""" """
} }
it("allows whitespace in expression") { it("allows whitespace in expression") {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value | join : ", " }} {{ value | join : ", " }}
""") """)
let result = try template.render(Context(dictionary: ["value": ["One", "Two"]])) let result = try template.render(Context(dictionary: ["value": ["One", "Two"]]))
try expect(result) == "One, Two" try expect(result) == "One, Two"
} }
} }
func testStringFilters() { func testStringFilters() {
it("transforms a string to be capitalized") { it("transforms a string to be capitalized") {
let template = Template(templateString: "{{ name|capitalize }}") let template = Template(templateString: "{{ name|capitalize }}")
let result = try template.render(Context(dictionary: ["name": "kyle"])) let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
it("transforms a string to be uppercase") { it("transforms a string to be uppercase") {
let template = Template(templateString: "{{ name|uppercase }}") let template = Template(templateString: "{{ name|uppercase }}")
let result = try template.render(Context(dictionary: ["name": "kyle"])) let result = try template.render(Context(dictionary: ["name": "kyle"]))
try expect(result) == "KYLE" try expect(result) == "KYLE"
} }
it("transforms a string to be lowercase") { it("transforms a string to be lowercase") {
let template = Template(templateString: "{{ name|lowercase }}") let template = Template(templateString: "{{ name|lowercase }}")
let result = try template.render(Context(dictionary: ["name": "Kyle"])) let result = try template.render(Context(dictionary: ["name": "Kyle"]))
try expect(result) == "kyle" try expect(result) == "kyle"
} }
} }
func testStringFiltersWithArrays() { func testStringFiltersWithArrays() {
it("transforms a string to be capitalized") { it("transforms a string to be capitalized") {
let template = Template(templateString: "{{ names|capitalize }}") let template = Template(templateString: "{{ names|capitalize }}")
let result = try template.render(Context(dictionary: ["names": ["kyle", "kyle"]])) let result = try template.render(Context(dictionary: ["names": ["kyle", "kyle"]]))
try expect(result) == """ try expect(result) == """
["Kyle", "Kyle"] ["Kyle", "Kyle"]
""" """
} }
it("transforms a string to be uppercase") { it("transforms a string to be uppercase") {
let template = Template(templateString: "{{ names|uppercase }}") let template = Template(templateString: "{{ names|uppercase }}")
let result = try template.render(Context(dictionary: ["names": ["kyle", "kyle"]])) let result = try template.render(Context(dictionary: ["names": ["kyle", "kyle"]]))
try expect(result) == """ try expect(result) == """
["KYLE", "KYLE"] ["KYLE", "KYLE"]
""" """
} }
it("transforms a string to be lowercase") { it("transforms a string to be lowercase") {
let template = Template(templateString: "{{ names|lowercase }}") let template = Template(templateString: "{{ names|lowercase }}")
let result = try template.render(Context(dictionary: ["names": ["Kyle", "Kyle"]])) let result = try template.render(Context(dictionary: ["names": ["Kyle", "Kyle"]]))
try expect(result) == """ try expect(result) == """
["kyle", "kyle"] ["kyle", "kyle"]
""" """
} }
} }
func testDefaultFilter() { func testDefaultFilter() {
let template = Template(templateString: """ let template = Template(templateString: """
Hello {{ name|default:"World" }} Hello {{ name|default:"World" }}
""") """)
it("shows the variable value") { it("shows the variable value") {
let result = try template.render(Context(dictionary: ["name": "Kyle"])) let result = try template.render(Context(dictionary: ["name": "Kyle"]))
try expect(result) == "Hello Kyle" try expect(result) == "Hello Kyle"
} }
it("shows the default value") { it("shows the default value") {
let result = try template.render(Context(dictionary: [:])) let result = try template.render(Context(dictionary: [:]))
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
it("supports multiple defaults") { it("supports multiple defaults") {
let template = Template(templateString: """ let template = Template(templateString: """
Hello {{ name|default:a,b,c,"World" }} Hello {{ name|default:a,b,c,"World" }}
""") """)
let result = try template.render(Context(dictionary: [:])) let result = try template.render(Context(dictionary: [:]))
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
it("can use int as default") { it("can use int as default") {
let template = Template(templateString: "{{ value|default:1 }}") let template = Template(templateString: "{{ value|default:1 }}")
let result = try template.render(Context(dictionary: [:])) let result = try template.render(Context(dictionary: [:]))
try expect(result) == "1" try expect(result) == "1"
} }
it("can use float as default") { it("can use float as default") {
let template = Template(templateString: "{{ value|default:1.5 }}") let template = Template(templateString: "{{ value|default:1.5 }}")
let result = try template.render(Context(dictionary: [:])) let result = try template.render(Context(dictionary: [:]))
try expect(result) == "1.5" try expect(result) == "1.5"
} }
it("checks for underlying nil value correctly") { it("checks for underlying nil value correctly") {
let template = Template(templateString: """ let template = Template(templateString: """
Hello {{ user.name|default:"anonymous" }} Hello {{ user.name|default:"anonymous" }}
""") """)
let nilName: String? = nil let nilName: String? = nil
let user: [String: Any?] = ["name": nilName] let user: [String: Any?] = ["name": nilName]
let result = try template.render(Context(dictionary: ["user": user])) let result = try template.render(Context(dictionary: ["user": user]))
try expect(result) == "Hello anonymous" try expect(result) == "Hello anonymous"
} }
} }
func testJoinFilter() { func testJoinFilter() {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|join:", " }} {{ value|join:", " }}
""") """)
it("joins a collection of strings") { it("joins a collection of strings") {
let result = try template.render(Context(dictionary: ["value": ["One", "Two"]])) let result = try template.render(Context(dictionary: ["value": ["One", "Two"]]))
try expect(result) == "One, Two" try expect(result) == "One, Two"
} }
it("joins a mixed-type collection") { it("joins a mixed-type collection") {
let result = try template.render(Context(dictionary: ["value": ["One", 2, true, 10.5, "Five"]])) let result = try template.render(Context(dictionary: ["value": ["One", 2, true, 10.5, "Five"]]))
try expect(result) == "One, 2, true, 10.5, Five" try expect(result) == "One, 2, true, 10.5, Five"
} }
it("can join by non string") { it("can join by non string") {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|join:separator }} {{ value|join:separator }}
""") """)
let result = try template.render(Context(dictionary: ["value": ["One", "Two"], "separator": true])) let result = try template.render(Context(dictionary: ["value": ["One", "Two"], "separator": true]))
try expect(result) == "OnetrueTwo" try expect(result) == "OnetrueTwo"
} }
it("can join without arguments") { it("can join without arguments") {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|join }} {{ value|join }}
""") """)
let result = try template.render(Context(dictionary: ["value": ["One", "Two"]])) let result = try template.render(Context(dictionary: ["value": ["One", "Two"]]))
try expect(result) == "OneTwo" try expect(result) == "OneTwo"
} }
} }
func testSplitFilter() { func testSplitFilter() {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|split:", " }} {{ value|split:", " }}
""") """)
it("split a string into array") { it("split a string into array") {
let result = try template.render(Context(dictionary: ["value": "One, Two"])) let result = try template.render(Context(dictionary: ["value": "One, Two"]))
try expect(result) == """ try expect(result) == """
["One", "Two"] ["One", "Two"]
""" """
} }
it("can split without arguments") { it("can split without arguments") {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|split }} {{ value|split }}
""") """)
let result = try template.render(Context(dictionary: ["value": "One, Two"])) let result = try template.render(Context(dictionary: ["value": "One, Two"]))
try expect(result) == """ try expect(result) == """
["One,", "Two"] ["One,", "Two"]
""" """
} }
} }
func testFilterSuggestion() { func testFilterSuggestion() {
it("made for unknown filter") { it("made for unknown filter") {
let template = Template(templateString: "{{ value|unknownFilter }}") let template = Template(templateString: "{{ value|unknownFilter }}")
let filterExtension = Extension() let filterExtension = Extension()
filterExtension.registerFilter("knownFilter") { value, _ in value } filterExtension.registerFilter("knownFilter") { value, _ in value }
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknownFilter'. Found similar filters: 'knownFilter'.", reason: "Unknown filter 'unknownFilter'. Found similar filters: 'knownFilter'.",
token: "value|unknownFilter", token: "value|unknownFilter",
template: template, template: template,
extension: filterExtension extension: filterExtension
) )
} }
it("made for multiple similar filters") { it("made for multiple similar filters") {
let template = Template(templateString: "{{ value|lowerFirst }}") let template = Template(templateString: "{{ value|lowerFirst }}")
let filterExtension = Extension() let filterExtension = Extension()
filterExtension.registerFilter("lowerFirstWord") { value, _ in value } filterExtension.registerFilter("lowerFirstWord") { value, _ in value }
filterExtension.registerFilter("lowerFirstLetter") { value, _ in value } filterExtension.registerFilter("lowerFirstLetter") { value, _ in value }
try self.expectError( try self.expectError(
reason: "Unknown filter 'lowerFirst'. Found similar filters: 'lowerFirstWord', 'lowercase'.", reason: "Unknown filter 'lowerFirst'. Found similar filters: 'lowerFirstWord', 'lowercase'.",
token: "value|lowerFirst", token: "value|lowerFirst",
template: template, template: template,
extension: filterExtension extension: filterExtension
) )
} }
it("not made when can't find similar filter") { it("not made when can't find similar filter") {
let template = Template(templateString: "{{ value|unknownFilter }}") let template = Template(templateString: "{{ value|unknownFilter }}")
let filterExtension = Extension() let filterExtension = Extension()
filterExtension.registerFilter("lowerFirstWord") { value, _ in value } filterExtension.registerFilter("lowerFirstWord") { value, _ in value }
try self.expectError( try self.expectError(
reason: "Unknown filter 'unknownFilter'. Found similar filters: 'lowerFirstWord'.", reason: "Unknown filter 'unknownFilter'. Found similar filters: 'lowerFirstWord'.",
token: "value|unknownFilter", token: "value|unknownFilter",
template: template, template: template,
extension: filterExtension extension: filterExtension
) )
} }
} }
func testIndentContent() throws { func testIndentContent() throws {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|indent:2 }} {{ value|indent:2 }}
""") """)
let result = try template.render(Context(dictionary: [ let result = try template.render(Context(dictionary: [
"value": """ "value": """
One One
Two Two
""" """
])) ]))
try expect(result) == """ try expect(result) == """
One One
Two Two
""" """
} }
func testIndentWithArbitraryCharacter() throws { func testIndentWithArbitraryCharacter() throws {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|indent:2,"\t" }} {{ value|indent:2,"\t" }}
""") """)
let result = try template.render(Context(dictionary: [ let result = try template.render(Context(dictionary: [
"value": """ "value": """
One One
Two Two
""" """
])) ]))
try expect(result) == """ try expect(result) == """
One One
\t\tTwo \t\tTwo
""" """
} }
func testIndentFirstLine() throws { func testIndentFirstLine() throws {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|indent:2," ",true }} {{ value|indent:2," ",true }}
""") """)
let result = try template.render(Context(dictionary: [ let result = try template.render(Context(dictionary: [
"value": """ "value": """
One One
Two Two
""" """
])) ]))
// swiftlint:disable indentation_width // swiftlint:disable indentation_width
try expect(result) == """ try expect(result) == """
One One
Two Two
""" """
// swiftlint:enable indentation_width // swiftlint:enable indentation_width
} }
func testIndentNotEmptyLines() throws { func testIndentNotEmptyLines() throws {
let template = Template(templateString: """ let template = Template(templateString: """
{{ value|indent }} {{ value|indent }}
""") """)
let result = try template.render(Context(dictionary: [ let result = try template.render(Context(dictionary: [
"value": """ "value": """
One One
@@ -384,9 +384,9 @@ final class FilterTests: XCTestCase {
""" """
])) ]))
// swiftlint:disable indentation_width // swiftlint:disable indentation_width
try expect(result) == """ try expect(result) == """
One One
@@ -394,64 +394,64 @@ final class FilterTests: XCTestCase {
""" """
// swiftlint:enable indentation_width // swiftlint:enable indentation_width
} }
func testDynamicFilters() throws { func testDynamicFilters() throws {
it("can apply dynamic filter") { it("can apply dynamic filter") {
let template = Template(templateString: "{{ name|filter:somefilter }}") let template = Template(templateString: "{{ name|filter:somefilter }}")
let result = try template.render(Context(dictionary: ["name": "Jhon", "somefilter": "uppercase"])) let result = try template.render(Context(dictionary: ["name": "Jhon", "somefilter": "uppercase"]))
try expect(result) == "JHON" try expect(result) == "JHON"
} }
it("can apply dynamic filter on array") { it("can apply dynamic filter on array") {
let template = Template(templateString: "{{ values|filter:joinfilter }}") let template = Template(templateString: "{{ values|filter:joinfilter }}")
let result = try template.render(Context(dictionary: ["values": [1, 2, 3], "joinfilter": "join:\", \""])) let result = try template.render(Context(dictionary: ["values": [1, 2, 3], "joinfilter": "join:\", \""]))
try expect(result) == "1, 2, 3" try expect(result) == "1, 2, 3"
} }
it("throws on unknown dynamic filter") { it("throws on unknown dynamic filter") {
let template = Template(templateString: "{{ values|filter:unknown }}") let template = Template(templateString: "{{ values|filter:unknown }}")
let context = Context(dictionary: ["values": [1, 2, 3], "unknown": "absurd"]) let context = Context(dictionary: ["values": [1, 2, 3], "unknown": "absurd"])
try expect(try template.render(context)).toThrow() try expect(try template.render(context)).toThrow()
} }
} }
private func expectError( private func expectError(
reason: String, reason: String,
token: String, token: String,
template: Template, template: Template,
extension: Extension, extension: Extension,
file: String = #file, file: String = #file,
line: Int = #line, line: Int = #line,
function: String = #function function: String = #function
) throws { ) throws {
guard let range = template.templateString.range(of: token) else { guard let range = template.templateString.range(of: token) else {
fatalError("Can't find '\(token)' in '\(template)'") fatalError("Can't find '\(token)' in '\(template)'")
} }
let environment = Environment(extensions: [`extension`]) let environment = Environment(extensions: [`extension`])
let expectedError: Error = { let expectedError: Error = {
let lexer = Lexer(templateString: template.templateString) let lexer = Lexer(templateString: template.templateString)
let location = lexer.rangeLocation(range) let location = lexer.rangeLocation(range)
let sourceMap = SourceMap(filename: template.name, location: location) let sourceMap = SourceMap(filename: template.name, location: location)
let token = Token.block(value: token, at: sourceMap) let token = Token.block(value: token, at: sourceMap)
return TemplateSyntaxError(reason: reason, token: token, stackTrace: []) return TemplateSyntaxError(reason: reason, token: token, stackTrace: [])
}() }()
let error = try expect( let error = try expect(
environment.render(template: template, context: [:]), environment.render(template: template, context: [:]),
file: file, file: file,
line: line, line: line,
function: function function: function
).toThrow() as TemplateSyntaxError ).toThrow() as TemplateSyntaxError
let reporter = SimpleErrorReporter() let reporter = SimpleErrorReporter()
try expect( try expect(
reporter.renderError(error), reporter.renderError(error),
file: file, file: file,
line: line, line: line,
function: function function: function
) == reporter.renderError(expectedError) ) == reporter.renderError(expectedError)
} }
} }

View File

@@ -3,52 +3,52 @@ import Stencil
import XCTest import XCTest
final class FilterTagTests: XCTestCase { final class FilterTagTests: XCTestCase {
func testFilterTag() { func testFilterTag() {
it("allows you to use a filter") { it("allows you to use a filter") {
let template = Template(templateString: "{% filter uppercase %}Test{% endfilter %}") let template = Template(templateString: "{% filter uppercase %}Test{% endfilter %}")
let result = try template.render() let result = try template.render()
try expect(result) == "TEST" try expect(result) == "TEST"
} }
it("allows you to chain filters") { it("allows you to chain filters") {
let template = Template(templateString: "{% filter lowercase|capitalize %}TEST{% endfilter %}") let template = Template(templateString: "{% filter lowercase|capitalize %}TEST{% endfilter %}")
let result = try template.render() let result = try template.render()
try expect(result) == "Test" try expect(result) == "Test"
} }
it("errors without a filter") { it("errors without a filter") {
let template = Template(templateString: "Some {% filter %}Test{% endfilter %}") let template = Template(templateString: "Some {% filter %}Test{% endfilter %}")
try expect(try template.render()).toThrow() try expect(try template.render()).toThrow()
} }
it("can render filters with arguments") { it("can render filters with arguments") {
let ext = Extension() let ext = Extension()
ext.registerFilter("split") { value, args in ext.registerFilter("split") { value, args in
guard let value = value as? String, guard let value = value as? String,
let argument = args.first as? String else { return value } let argument = args.first as? String else { return value }
return value.components(separatedBy: argument) return value.components(separatedBy: argument)
} }
let env = Environment(extensions: [ext]) let env = Environment(extensions: [ext])
let result = try env.renderTemplate(string: """ let result = try env.renderTemplate(string: """
{% filter split:","|join:";" %}{{ items|join:"," }}{% endfilter %} {% filter split:","|join:";" %}{{ items|join:"," }}{% endfilter %}
""", context: ["items": [1, 2]]) """, context: ["items": [1, 2]])
try expect(result) == "1;2" try expect(result) == "1;2"
} }
it("can render filters with quote as an argument") { it("can render filters with quote as an argument") {
let ext = Extension() let ext = Extension()
ext.registerFilter("replace") { value, args in ext.registerFilter("replace") { value, args in
guard let value = value as? String, guard let value = value as? String,
args.count == 2, args.count == 2,
let search = args.first as? String, let search = args.first as? String,
let replacement = args.last as? String else { return value } let replacement = args.last as? String else { return value }
return value.replacingOccurrences(of: search, with: replacement) return value.replacingOccurrences(of: search, with: replacement)
} }
let env = Environment(extensions: [ext]) let env = Environment(extensions: [ext])
let result = try env.renderTemplate(string: """ let result = try env.renderTemplate(string: """
{% filter replace:'"',"" %}{{ items|join:"," }}{% endfilter %} {% filter replace:'"',"" %}{{ items|join:"," }}{% endfilter %}
""", context: ["items": ["\"1\"", "\"2\""]]) """, context: ["items": ["\"1\"", "\"2\""]])
try expect(result) == "1,2" try expect(result) == "1,2"
} }
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -4,60 +4,60 @@ import Spectre
import XCTest import XCTest
extension Expectation { extension Expectation {
@discardableResult @discardableResult
func toThrow<E: Error>() throws -> E { func toThrow<E: Error>() throws -> E {
var thrownError: Error? var thrownError: Error?
do { do {
_ = try expression() _ = try expression()
} catch { } catch {
thrownError = error thrownError = error
} }
if let thrownError = thrownError { if let thrownError = thrownError {
if let thrownError = thrownError as? E { if let thrownError = thrownError as? E {
return thrownError return thrownError
} else { } else {
throw failure("\(thrownError) is not \(T.self)") throw failure("\(thrownError) is not \(T.self)")
} }
} else { } else {
throw failure("expression did not throw an error") throw failure("expression did not throw an error")
} }
} }
} }
extension XCTestCase { extension XCTestCase {
func expectedSyntaxError(token: String, template: Template, description: String) -> TemplateSyntaxError { func expectedSyntaxError(token: String, template: Template, description: String) -> TemplateSyntaxError {
guard let range = template.templateString.range(of: token) else { guard let range = template.templateString.range(of: token) else {
fatalError("Can't find '\(token)' in '\(template)'") fatalError("Can't find '\(token)' in '\(template)'")
} }
let lexer = Lexer(templateString: template.templateString) let lexer = Lexer(templateString: template.templateString)
let location = lexer.rangeLocation(range) let location = lexer.rangeLocation(range)
let sourceMap = SourceMap(filename: template.name, location: location) let sourceMap = SourceMap(filename: template.name, location: location)
let token = Token.block(value: token, at: sourceMap) let token = Token.block(value: token, at: sourceMap)
return TemplateSyntaxError(reason: description, token: token, stackTrace: []) return TemplateSyntaxError(reason: description, token: token, stackTrace: [])
} }
} }
// MARK: - Test Types // MARK: - Test Types
class ExampleLoader: Loader { class ExampleLoader: Loader {
func loadTemplate(name: String, environment: Environment) throws -> Template { func loadTemplate(name: String, environment: Environment) throws -> Template {
if name == "example.html" { if name == "example.html" {
return Template(templateString: "Hello World!", environment: environment, name: name) return Template(templateString: "Hello World!", environment: environment, name: name)
} }
throw TemplateDoesNotExist(templateNames: [name], loader: self) throw TemplateDoesNotExist(templateNames: [name], loader: self)
} }
} }
class ErrorNode: NodeType { class ErrorNode: NodeType {
let token: Token? let token: Token?
init(token: Token? = nil) { init(token: Token? = nil) {
self.token = token self.token = token
} }
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
throw TemplateSyntaxError("Custom Error") throw TemplateSyntaxError("Custom Error")
} }
} }

View File

@@ -3,288 +3,288 @@ import Spectre
import XCTest import XCTest
final class IfNodeTests: XCTestCase { final class IfNodeTests: XCTestCase {
func testParseIf() { func testParseIf() {
it("can parse an if block") { it("can parse an if block") {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if value", at: .unknown), .block(value: "if value", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? IfNode let node = nodes.first as? IfNode
let conditions = node?.conditions let conditions = node?.conditions
try expect(conditions?.count) == 1 try expect(conditions?.count) == 1
try expect(conditions?[0].nodes.count) == 1 try expect(conditions?[0].nodes.count) == 1
let trueNode = conditions?[0].nodes.first as? TextNode let trueNode = conditions?[0].nodes.first as? TextNode
try expect(trueNode?.text) == "true" try expect(trueNode?.text) == "true"
} }
it("can parse an if with complex expression") { it("can parse an if with complex expression") {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: """ .block(value: """
if value == \"test\" and (not name or not (name and surname) or( some )and other ) if value == \"test\" and (not name or not (name and surname) or( some )and other )
""", at: .unknown), """, at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
try expect(nodes.first is IfNode).beTrue() try expect(nodes.first is IfNode).beTrue()
} }
} }
func testParseIfWithElse() throws { func testParseIfWithElse() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if value", at: .unknown), .block(value: "if value", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "else", at: .unknown), .block(value: "else", at: .unknown),
.text(value: "false", at: .unknown), .text(value: "false", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? IfNode let node = nodes.first as? IfNode
let conditions = node?.conditions let conditions = node?.conditions
try expect(conditions?.count) == 2 try expect(conditions?.count) == 2
try expect(conditions?[0].nodes.count) == 1 try expect(conditions?[0].nodes.count) == 1
let trueNode = conditions?[0].nodes.first as? TextNode let trueNode = conditions?[0].nodes.first as? TextNode
try expect(trueNode?.text) == "true" try expect(trueNode?.text) == "true"
try expect(conditions?[1].nodes.count) == 1 try expect(conditions?[1].nodes.count) == 1
let falseNode = conditions?[1].nodes.first as? TextNode let falseNode = conditions?[1].nodes.first as? TextNode
try expect(falseNode?.text) == "false" try expect(falseNode?.text) == "false"
} }
func testParseIfWithElif() throws { func testParseIfWithElif() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if value", at: .unknown), .block(value: "if value", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "elif something", at: .unknown), .block(value: "elif something", at: .unknown),
.text(value: "some", at: .unknown), .text(value: "some", at: .unknown),
.block(value: "else", at: .unknown), .block(value: "else", at: .unknown),
.text(value: "false", at: .unknown), .text(value: "false", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? IfNode let node = nodes.first as? IfNode
let conditions = node?.conditions let conditions = node?.conditions
try expect(conditions?.count) == 3 try expect(conditions?.count) == 3
try expect(conditions?[0].nodes.count) == 1 try expect(conditions?[0].nodes.count) == 1
let trueNode = conditions?[0].nodes.first as? TextNode let trueNode = conditions?[0].nodes.first as? TextNode
try expect(trueNode?.text) == "true" try expect(trueNode?.text) == "true"
try expect(conditions?[1].nodes.count) == 1 try expect(conditions?[1].nodes.count) == 1
let elifNode = conditions?[1].nodes.first as? TextNode let elifNode = conditions?[1].nodes.first as? TextNode
try expect(elifNode?.text) == "some" try expect(elifNode?.text) == "some"
try expect(conditions?[2].nodes.count) == 1 try expect(conditions?[2].nodes.count) == 1
let falseNode = conditions?[2].nodes.first as? TextNode let falseNode = conditions?[2].nodes.first as? TextNode
try expect(falseNode?.text) == "false" try expect(falseNode?.text) == "false"
} }
func testParseIfWithElifWithoutElse() throws { func testParseIfWithElifWithoutElse() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if value", at: .unknown), .block(value: "if value", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "elif something", at: .unknown), .block(value: "elif something", at: .unknown),
.text(value: "some", at: .unknown), .text(value: "some", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? IfNode let node = nodes.first as? IfNode
let conditions = node?.conditions let conditions = node?.conditions
try expect(conditions?.count) == 2 try expect(conditions?.count) == 2
try expect(conditions?[0].nodes.count) == 1 try expect(conditions?[0].nodes.count) == 1
let trueNode = conditions?[0].nodes.first as? TextNode let trueNode = conditions?[0].nodes.first as? TextNode
try expect(trueNode?.text) == "true" try expect(trueNode?.text) == "true"
try expect(conditions?[1].nodes.count) == 1 try expect(conditions?[1].nodes.count) == 1
let elifNode = conditions?[1].nodes.first as? TextNode let elifNode = conditions?[1].nodes.first as? TextNode
try expect(elifNode?.text) == "some" try expect(elifNode?.text) == "some"
} }
func testParseMultipleElif() throws { func testParseMultipleElif() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if value", at: .unknown), .block(value: "if value", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "elif something1", at: .unknown), .block(value: "elif something1", at: .unknown),
.text(value: "some1", at: .unknown), .text(value: "some1", at: .unknown),
.block(value: "elif something2", at: .unknown), .block(value: "elif something2", at: .unknown),
.text(value: "some2", at: .unknown), .text(value: "some2", at: .unknown),
.block(value: "else", at: .unknown), .block(value: "else", at: .unknown),
.text(value: "false", at: .unknown), .text(value: "false", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? IfNode let node = nodes.first as? IfNode
let conditions = node?.conditions let conditions = node?.conditions
try expect(conditions?.count) == 4 try expect(conditions?.count) == 4
try expect(conditions?[0].nodes.count) == 1 try expect(conditions?[0].nodes.count) == 1
let trueNode = conditions?[0].nodes.first as? TextNode let trueNode = conditions?[0].nodes.first as? TextNode
try expect(trueNode?.text) == "true" try expect(trueNode?.text) == "true"
try expect(conditions?[1].nodes.count) == 1 try expect(conditions?[1].nodes.count) == 1
let elifNode = conditions?[1].nodes.first as? TextNode let elifNode = conditions?[1].nodes.first as? TextNode
try expect(elifNode?.text) == "some1" try expect(elifNode?.text) == "some1"
try expect(conditions?[2].nodes.count) == 1 try expect(conditions?[2].nodes.count) == 1
let elif2Node = conditions?[2].nodes.first as? TextNode let elif2Node = conditions?[2].nodes.first as? TextNode
try expect(elif2Node?.text) == "some2" try expect(elif2Node?.text) == "some2"
try expect(conditions?[3].nodes.count) == 1 try expect(conditions?[3].nodes.count) == 1
let falseNode = conditions?[3].nodes.first as? TextNode let falseNode = conditions?[3].nodes.first as? TextNode
try expect(falseNode?.text) == "false" try expect(falseNode?.text) == "false"
} }
func testParseIfnot() throws { func testParseIfnot() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "ifnot value", at: .unknown), .block(value: "ifnot value", at: .unknown),
.text(value: "false", at: .unknown), .text(value: "false", at: .unknown),
.block(value: "else", at: .unknown), .block(value: "else", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let node = nodes.first as? IfNode let node = nodes.first as? IfNode
let conditions = node?.conditions let conditions = node?.conditions
try expect(conditions?.count) == 2 try expect(conditions?.count) == 2
try expect(conditions?[0].nodes.count) == 1 try expect(conditions?[0].nodes.count) == 1
let trueNode = conditions?[0].nodes.first as? TextNode let trueNode = conditions?[0].nodes.first as? TextNode
try expect(trueNode?.text) == "true" try expect(trueNode?.text) == "true"
try expect(conditions?[1].nodes.count) == 1 try expect(conditions?[1].nodes.count) == 1
let falseNode = conditions?[1].nodes.first as? TextNode let falseNode = conditions?[1].nodes.first as? TextNode
try expect(falseNode?.text) == "false" try expect(falseNode?.text) == "false"
} }
func testParsingErrors() { func testParsingErrors() {
it("throws an error when parsing an if block without an endif") { it("throws an error when parsing an if block without an endif") {
let tokens: [Token] = [.block(value: "if value", at: .unknown)] let tokens: [Token] = [.block(value: "if value", at: .unknown)]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let error = TemplateSyntaxError(reason: "`endif` was not found.", token: tokens.first) let error = TemplateSyntaxError(reason: "`endif` was not found.", token: tokens.first)
try expect(try parser.parse()).toThrow(error) try expect(try parser.parse()).toThrow(error)
} }
it("throws an error when parsing an ifnot without an endif") { it("throws an error when parsing an ifnot without an endif") {
let tokens: [Token] = [.block(value: "ifnot value", at: .unknown)] let tokens: [Token] = [.block(value: "ifnot value", at: .unknown)]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let error = TemplateSyntaxError(reason: "`endif` was not found.", token: tokens.first) let error = TemplateSyntaxError(reason: "`endif` was not found.", token: tokens.first)
try expect(try parser.parse()).toThrow(error) try expect(try parser.parse()).toThrow(error)
} }
} }
func testRendering() { func testRendering() {
it("renders a true expression") { it("renders a true expression") {
let node = IfNode(conditions: [ let node = IfNode(conditions: [
IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "1")]), IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "2")]), IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "2")]),
IfCondition(expression: nil, nodes: [TextNode(text: "3")]) IfCondition(expression: nil, nodes: [TextNode(text: "3")])
]) ])
try expect(try node.render(Context())) == "1" try expect(try node.render(Context())) == "1"
} }
it("renders the first true expression") { it("renders the first true expression") {
let node = IfNode(conditions: [ let node = IfNode(conditions: [
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]), IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "2")]), IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "2")]),
IfCondition(expression: nil, nodes: [TextNode(text: "3")]) IfCondition(expression: nil, nodes: [TextNode(text: "3")])
]) ])
try expect(try node.render(Context())) == "2" try expect(try node.render(Context())) == "2"
} }
it("renders the empty expression when other conditions are falsy") { it("renders the empty expression when other conditions are falsy") {
let node = IfNode(conditions: [ let node = IfNode(conditions: [
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]), IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "2")]), IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "2")]),
IfCondition(expression: nil, nodes: [TextNode(text: "3")]) IfCondition(expression: nil, nodes: [TextNode(text: "3")])
]) ])
try expect(try node.render(Context())) == "3" try expect(try node.render(Context())) == "3"
} }
it("renders empty when no truthy conditions") { it("renders empty when no truthy conditions") {
let node = IfNode(conditions: [ let node = IfNode(conditions: [
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]), IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "2")]) IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "2")])
]) ])
try expect(try node.render(Context())) == "" try expect(try node.render(Context())) == ""
} }
} }
func testSupportVariableFilters() throws { func testSupportVariableFilters() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if value|uppercase == \"TEST\"", at: .unknown), .block(value: "if value|uppercase == \"TEST\"", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let result = try renderNodes(nodes, Context(dictionary: ["value": "test"])) let result = try renderNodes(nodes, Context(dictionary: ["value": "test"]))
try expect(result) == "true" try expect(result) == "true"
} }
func testEvaluatesNilAsFalse() throws { func testEvaluatesNilAsFalse() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if instance.value", at: .unknown), .block(value: "if instance.value", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
let result = try renderNodes(nodes, Context(dictionary: ["instance": SomeType()])) let result = try renderNodes(nodes, Context(dictionary: ["instance": SomeType()]))
try expect(result) == "" try expect(result) == ""
} }
func testSupportsRangeVariables() throws { func testSupportsRangeVariables() throws {
let tokens: [Token] = [ let tokens: [Token] = [
.block(value: "if value in 1...3", at: .unknown), .block(value: "if value in 1...3", at: .unknown),
.text(value: "true", at: .unknown), .text(value: "true", at: .unknown),
.block(value: "else", at: .unknown), .block(value: "else", at: .unknown),
.text(value: "false", at: .unknown), .text(value: "false", at: .unknown),
.block(value: "endif", at: .unknown) .block(value: "endif", at: .unknown)
] ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
try expect(renderNodes(nodes, Context(dictionary: ["value": 3]))) == "true" try expect(renderNodes(nodes, Context(dictionary: ["value": 3]))) == "true"
try expect(renderNodes(nodes, Context(dictionary: ["value": 4]))) == "false" try expect(renderNodes(nodes, Context(dictionary: ["value": 4]))) == "false"
} }
} }
// MARK: - Helpers // MARK: - Helpers
private struct SomeType { private struct SomeType {
let value: String? = nil let value: String? = nil
} }

View File

@@ -4,69 +4,69 @@ import Spectre
import XCTest import XCTest
final class IncludeTests: XCTestCase { final class IncludeTests: XCTestCase {
private let path = Path(#file as String)! / ".." / "fixtures" private let path = Path(#file as String)! / ".." / "fixtures"
private lazy var loader = FileSystemLoader(paths: [path]) private lazy var loader = FileSystemLoader(paths: [path])
private lazy var environment = Environment(loader: loader) private lazy var environment = Environment(loader: loader)
func testParsing() { func testParsing() {
it("throws an error when no template is given") { it("throws an error when no template is given") {
let tokens: [Token] = [ .block(value: "include", at: .unknown) ] let tokens: [Token] = [ .block(value: "include", at: .unknown) ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
let error = TemplateSyntaxError(reason: """ let error = TemplateSyntaxError(reason: """
'include' tag requires one argument, the template file to be included. \ 'include' tag requires one argument, the template file to be included. \
A second optional argument can be used to specify the context that will \ A second optional argument can be used to specify the context that will \
be passed to the included file be passed to the included file
""", token: tokens.first) """, token: tokens.first)
try expect(try parser.parse()).toThrow(error) try expect(try parser.parse()).toThrow(error)
} }
it("can parse a valid include block") { it("can parse a valid include block") {
let tokens: [Token] = [ .block(value: "include \"test.html\"", at: .unknown) ] let tokens: [Token] = [ .block(value: "include \"test.html\"", at: .unknown) ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
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\"")
} }
} }
func testRendering() { func testRendering() {
it("throws an error when rendering without a loader") { it("throws an error when rendering without a loader") {
let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown)) let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown))
do { do {
_ = try node.render(Context()) _ = try node.render(Context())
} catch { } catch {
try expect("\(error)") == "Template named `test.html` does not exist. No loaders found" try expect("\(error)") == "Template named `test.html` does not exist. No loaders found"
} }
} }
it("throws an error when it cannot find the included template") { it("throws an error when it cannot find the included template") {
let node = IncludeNode(templateName: Variable("\"unknown.html\""), token: .block(value: "", at: .unknown)) let node = IncludeNode(templateName: Variable("\"unknown.html\""), token: .block(value: "", at: .unknown))
do { do {
_ = try node.render(Context(environment: self.environment)) _ = try node.render(Context(environment: self.environment))
} catch { } catch {
try expect("\(error)".hasPrefix("Template named `unknown.html` does not exist in loader")).to.beTrue() try expect("\(error)".hasPrefix("Template named `unknown.html` does not exist in loader")).to.beTrue()
} }
} }
it("successfully renders a found included template") { it("successfully renders a found included template") {
let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown)) let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown))
let context = Context(dictionary: ["target": "World"], environment: self.environment) let context = Context(dictionary: ["target": "World"], environment: self.environment)
let value = try node.render(context) let value = try node.render(context)
try expect(value) == "Hello World!" try expect(value) == "Hello World!"
} }
it("successfully passes context") { it("successfully passes context") {
let template = Template(templateString: """ let template = Template(templateString: """
{% include "test.html" child %} {% include "test.html" child %}
""") """)
let context = Context(dictionary: ["child": ["target": "World"]], environment: self.environment) let context = Context(dictionary: ["child": ["target": "World"]], environment: self.environment)
let value = try template.render(context) let value = try template.render(context)
try expect(value) == "Hello World!" try expect(value) == "Hello World!"
} }
} }
} }

View File

@@ -4,70 +4,70 @@ import Stencil
import XCTest import XCTest
final class InheritanceTests: XCTestCase { final class InheritanceTests: XCTestCase {
private let path = Path(#file as String)! / ".." / "fixtures" private let path = Path(#file as String)! / ".." / "fixtures"
private lazy var loader = FileSystemLoader(paths: [path]) private lazy var loader = FileSystemLoader(paths: [path])
private lazy var environment = Environment(loader: loader) private lazy var environment = Environment(loader: loader)
func testInheritance() { func testInheritance() {
it("can inherit from another template") { it("can inherit from another template") {
let template = try self.environment.loadTemplate(name: "child.html") let template = try self.environment.loadTemplate(name: "child.html")
try expect(try template.render()) == """ try expect(try template.render()) == """
Super_Header Child_Header Super_Header Child_Header
Child_Body Child_Body
""" """
} }
it("can inherit from another template inheriting from another template") { it("can inherit from another template inheriting from another template") {
let template = try self.environment.loadTemplate(name: "child-child.html") let template = try self.environment.loadTemplate(name: "child-child.html")
try expect(try template.render()) == """ try expect(try template.render()) == """
Super_Header Child_Header Child_Child_Header Super_Header Child_Header Child_Child_Header
Child_Body Child_Body
""" """
} }
it("can inherit from a template that calls a super block") { it("can inherit from a template that calls a super block") {
let template = try self.environment.loadTemplate(name: "child-super.html") let template = try self.environment.loadTemplate(name: "child-super.html")
try expect(try template.render()) == """ try expect(try template.render()) == """
Header Header
Child_Body Child_Body
""" """
} }
it("can render block.super in if tag") { it("can render block.super in if tag") {
let template = try self.environment.loadTemplate(name: "if-block-child.html") let template = try self.environment.loadTemplate(name: "if-block-child.html")
try expect(try template.render(["sort": "new"])) == """ try expect(try template.render(["sort": "new"])) == """
Title - Nieuwste spellen Title - Nieuwste spellen
""" """
try expect(try template.render(["sort": "upcoming"])) == """ try expect(try template.render(["sort": "upcoming"])) == """
Title - Binnenkort op de agenda Title - Binnenkort op de agenda
""" """
try expect(try template.render(["sort": "near-me"])) == """ try expect(try template.render(["sort": "near-me"])) == """
Title - In mijn buurt Title - In mijn buurt
""" """
} }
} }
func testInheritanceCache() { func testInheritanceCache() {
it("can call block twice") { it("can call block twice") {
let template: Template = "{% block repeat %}Block{% endblock %}{{ block.repeat }}" let template: Template = "{% block repeat %}Block{% endblock %}{{ block.repeat }}"
try expect(try template.render()) == "BlockBlock" try expect(try template.render()) == "BlockBlock"
} }
it("renders child content when calling block twice in base template") { it("renders child content when calling block twice in base template") {
let template = try self.environment.loadTemplate(name: "child-repeat.html") let template = try self.environment.loadTemplate(name: "child-repeat.html")
try expect(try template.render()) == """ try expect(try template.render()) == """
Super_Header Child_Header Super_Header Child_Header
Child_Body Child_Body
Repeat Repeat
Super_Header Child_Header Super_Header Child_Header
Child_Body Child_Body
""" """
} }
} }
} }

View File

@@ -4,86 +4,86 @@ import Spectre
import XCTest import XCTest
final class LexerTests: XCTestCase { final class LexerTests: XCTestCase {
func testText() throws { func testText() throws {
let lexer = Lexer(templateString: "Hello World") let lexer = Lexer(templateString: "Hello World")
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 1 try expect(tokens.count) == 1
try expect(tokens.first) == .text(value: "Hello World", at: makeSourceMap("Hello World", for: lexer)) try expect(tokens.first) == .text(value: "Hello World", at: makeSourceMap("Hello World", for: lexer))
} }
func testComment() throws { func testComment() throws {
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) == .comment(value: "Comment", at: makeSourceMap("Comment", for: lexer)) try expect(tokens.first) == .comment(value: "Comment", at: makeSourceMap("Comment", for: lexer))
} }
func testVariable() throws { func testVariable() throws {
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) == .variable(value: "Variable", at: makeSourceMap("Variable", for: lexer)) try expect(tokens.first) == .variable(value: "Variable", at: makeSourceMap("Variable", for: lexer))
} }
func testTokenWithoutSpaces() throws { func testTokenWithoutSpaces() throws {
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) == .variable(value: "Variable", at: makeSourceMap("Variable", for: lexer)) try expect(tokens.first) == .variable(value: "Variable", at: makeSourceMap("Variable", for: lexer))
} }
func testUnclosedTag() throws { func testUnclosedTag() throws {
let templateString = "{{ thing" let templateString = "{{ thing"
let lexer = Lexer(templateString: templateString) let lexer = Lexer(templateString: templateString)
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 1 try expect(tokens.count) == 1
try expect(tokens.first) == .text(value: "", at: makeSourceMap("{{ thing", for: lexer)) try expect(tokens.first) == .text(value: "", at: makeSourceMap("{{ thing", for: lexer))
} }
func testContentMixture() throws { func testContentMixture() throws {
let templateString = "My name is {{ myname }}." let templateString = "My name is {{ myname }}."
let lexer = Lexer(templateString: templateString) let lexer = Lexer(templateString: templateString)
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 3 try expect(tokens.count) == 3
try expect(tokens[0]) == .text(value: "My name is ", at: makeSourceMap("My name is ", for: lexer)) try expect(tokens[0]) == .text(value: "My name is ", at: makeSourceMap("My name is ", for: lexer))
try expect(tokens[1]) == .variable(value: "myname", at: makeSourceMap("myname", for: lexer)) try expect(tokens[1]) == .variable(value: "myname", at: makeSourceMap("myname", for: lexer))
try expect(tokens[2]) == .text(value: ".", at: makeSourceMap(".", for: lexer)) try expect(tokens[2]) == .text(value: ".", at: makeSourceMap(".", for: lexer))
} }
func testVariablesWithoutBeingGreedy() throws { func testVariablesWithoutBeingGreedy() throws {
let templateString = "{{ thing }}{{ name }}" let templateString = "{{ thing }}{{ name }}"
let lexer = Lexer(templateString: templateString) let lexer = Lexer(templateString: templateString)
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 2 try expect(tokens.count) == 2
try expect(tokens[0]) == .variable(value: "thing", at: makeSourceMap("thing", for: lexer)) try expect(tokens[0]) == .variable(value: "thing", at: makeSourceMap("thing", for: lexer))
try expect(tokens[1]) == .variable(value: "name", at: makeSourceMap("name", for: lexer)) try expect(tokens[1]) == .variable(value: "name", at: makeSourceMap("name", for: lexer))
} }
func testUnclosedBlock() throws { func testUnclosedBlock() throws {
let lexer = Lexer(templateString: "{%}") let lexer = Lexer(templateString: "{%}")
_ = lexer.tokenize() _ = lexer.tokenize()
} }
func testTokenizeIncorrectSyntaxWithoutCrashing() throws { func testTokenizeIncorrectSyntaxWithoutCrashing() throws {
let lexer = Lexer(templateString: "func some() {{% if %}") let lexer = Lexer(templateString: "func some() {{% if %}")
_ = lexer.tokenize() _ = lexer.tokenize()
} }
func testEmptyVariable() throws { func testEmptyVariable() throws {
let lexer = Lexer(templateString: "{{}}") let lexer = Lexer(templateString: "{{}}")
_ = lexer.tokenize() _ = lexer.tokenize()
} }
func testNewlines() throws { func testNewlines() throws {
// swiftlint:disable indentation_width // swiftlint:disable indentation_width
let templateString = """ let templateString = """
My name is {% My name is {%
if name if name
and and
@@ -93,69 +93,69 @@ final class LexerTests: XCTestCase {
}}{% }}{%
endif %}. endif %}.
""" """
// swiftlint:enable indentation_width // swiftlint:enable indentation_width
let lexer = Lexer(templateString: templateString) let lexer = Lexer(templateString: templateString)
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 5 try expect(tokens.count) == 5
try expect(tokens[0]) == .text(value: "My name is ", at: makeSourceMap("My name is", for: lexer)) try expect(tokens[0]) == .text(value: "My name is ", at: makeSourceMap("My name is", for: lexer))
try expect(tokens[1]) == .block(value: "if name and name", at: makeSourceMap("{%", for: lexer)) try expect(tokens[1]) == .block(value: "if name and name", at: makeSourceMap("{%", for: lexer))
try expect(tokens[2]) == .variable(value: "name", at: makeSourceMap("name", for: lexer, options: .backwards)) try expect(tokens[2]) == .variable(value: "name", at: makeSourceMap("name", for: lexer, options: .backwards))
try expect(tokens[3]) == .block(value: "endif", at: makeSourceMap("endif", for: lexer)) try expect(tokens[3]) == .block(value: "endif", at: makeSourceMap("endif", for: lexer))
try expect(tokens[4]) == .text(value: ".", at: makeSourceMap(".", for: lexer)) try expect(tokens[4]) == .text(value: ".", at: makeSourceMap(".", for: lexer))
} }
func testTrimSymbols() throws { func testTrimSymbols() throws {
let fBlock = "if hello" let fBlock = "if hello"
let sBlock = "ta da" let sBlock = "ta da"
let lexer = Lexer(templateString: "{%+ \(fBlock) -%}{% \(sBlock) -%}") let lexer = Lexer(templateString: "{%+ \(fBlock) -%}{% \(sBlock) -%}")
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
let behaviours = ( let behaviours = (
WhitespaceBehaviour(leading: .keep, trailing: .trim), WhitespaceBehaviour(leading: .keep, trailing: .trim),
WhitespaceBehaviour(leading: .unspecified, trailing: .trim) WhitespaceBehaviour(leading: .unspecified, trailing: .trim)
) )
try expect(tokens.count) == 2 try expect(tokens.count) == 2
try expect(tokens[0]) == .block(value: fBlock, at: makeSourceMap(fBlock, for: lexer), whitespace: behaviours.0) try expect(tokens[0]) == .block(value: fBlock, at: makeSourceMap(fBlock, for: lexer), whitespace: behaviours.0)
try expect(tokens[1]) == .block(value: sBlock, at: makeSourceMap(sBlock, for: lexer), whitespace: behaviours.1) try expect(tokens[1]) == .block(value: sBlock, at: makeSourceMap(sBlock, for: lexer), whitespace: behaviours.1)
} }
func testEscapeSequence() throws { func testEscapeSequence() throws {
let templateString = "class Some {{ '{' }}{% if true %}{{ stuff }}{% endif %}" let templateString = "class Some {{ '{' }}{% if true %}{{ stuff }}{% endif %}"
let lexer = Lexer(templateString: templateString) let lexer = Lexer(templateString: templateString)
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 5 try expect(tokens.count) == 5
try expect(tokens[0]) == .text(value: "class Some ", at: makeSourceMap("class Some ", for: lexer)) try expect(tokens[0]) == .text(value: "class Some ", at: makeSourceMap("class Some ", for: lexer))
try expect(tokens[1]) == .variable(value: "'{'", at: makeSourceMap("'{'", for: lexer)) try expect(tokens[1]) == .variable(value: "'{'", at: makeSourceMap("'{'", for: lexer))
try expect(tokens[2]) == .block(value: "if true", at: makeSourceMap("if true", for: lexer)) try expect(tokens[2]) == .block(value: "if true", at: makeSourceMap("if true", for: lexer))
try expect(tokens[3]) == .variable(value: "stuff", at: makeSourceMap("stuff", for: lexer)) try expect(tokens[3]) == .variable(value: "stuff", at: makeSourceMap("stuff", for: lexer))
try expect(tokens[4]) == .block(value: "endif", at: makeSourceMap("endif", for: lexer)) try expect(tokens[4]) == .block(value: "endif", at: makeSourceMap("endif", for: lexer))
} }
func testPerformance() throws { func testPerformance() throws {
let path = Path(#file as String)! / ".." / "fixtures" / "huge.html" let path = Path(#file as String)! / ".." / "fixtures" / "huge.html"
let content: String = try NSString(contentsOfFile: path.string, encoding: String.Encoding.utf8.rawValue).substring(from: 0) as String let content: String = try NSString(contentsOfFile: path.string, encoding: String.Encoding.utf8.rawValue).substring(from: 0) as String
measure { measure {
let lexer = Lexer(templateString: content) let lexer = Lexer(templateString: content)
_ = lexer.tokenize() _ = lexer.tokenize()
} }
} }
func testCombiningDiaeresis() throws { func testCombiningDiaeresis() throws {
// the symbol "ü" in the `templateString` is unusually encoded as 0x75 0xCC 0x88 (LATIN SMALL LETTER U + COMBINING // the symbol "ü" in the `templateString` is unusually encoded as 0x75 0xCC 0x88 (LATIN SMALL LETTER U + COMBINING
// DIAERESIS) instead of 0xC3 0xBC (LATIN SMALL LETTER U WITH DIAERESIS) // DIAERESIS) instead of 0xC3 0xBC (LATIN SMALL LETTER U WITH DIAERESIS)
let templateString = "\n{% if test %}ü{% endif %}\n{% if ü %}ü{% endif %}\n" let templateString = "\n{% if test %}ü{% endif %}\n{% if ü %}ü{% endif %}\n"
let lexer = Lexer(templateString: templateString) let lexer = Lexer(templateString: templateString)
let tokens = lexer.tokenize() let tokens = lexer.tokenize()
try expect(tokens.count) == 9 try expect(tokens.count) == 9
assert(tokens[1].contents == "if test") assert(tokens[1].contents == "if test")
} }
private func makeSourceMap(_ token: String, for lexer: Lexer, options: String.CompareOptions = []) -> SourceMap { private func makeSourceMap(_ token: String, for lexer: Lexer, options: String.CompareOptions = []) -> SourceMap {
guard let range = lexer.templateString.range(of: token, options: options) else { fatalError("Token not found") } guard let range = lexer.templateString.range(of: token, options: options) else { fatalError("Token not found") }
return SourceMap(location: lexer.rangeLocation(range)) return SourceMap(location: lexer.rangeLocation(range))
} }
} }

View File

@@ -4,52 +4,52 @@ import Stencil
import XCTest import XCTest
final class TemplateLoaderTests: XCTestCase { final class TemplateLoaderTests: XCTestCase {
func testFileSystemLoader() { func testFileSystemLoader() {
let path = Path(#file as String)! / ".." / "fixtures" let path = Path(#file as String)! / ".." / "fixtures"
let loader = FileSystemLoader(paths: [path]) let loader = FileSystemLoader(paths: [path])
let environment = Environment(loader: loader) let environment = Environment(loader: loader)
it("errors when a template cannot be found") { it("errors when a template cannot be found") {
try expect(try environment.loadTemplate(name: "unknown.html")).toThrow() try expect(try environment.loadTemplate(name: "unknown.html")).toThrow()
} }
it("errors when an array of templates cannot be found") { it("errors when an array of templates cannot be found") {
try expect(try environment.loadTemplate(names: ["unknown.html", "unknown2.html"])).toThrow() try expect(try environment.loadTemplate(names: ["unknown.html", "unknown2.html"])).toThrow()
} }
it("can load a template from a file") { it("can load a template from a file") {
_ = try environment.loadTemplate(name: "test.html") _ = try environment.loadTemplate(name: "test.html")
} }
it("errors when loading absolute file outside of the selected path") { it("errors when loading absolute file outside of the selected path") {
try expect(try environment.loadTemplate(name: "/etc/hosts")).toThrow() try expect(try environment.loadTemplate(name: "/etc/hosts")).toThrow()
} }
it("errors when loading relative file outside of the selected path") { it("errors when loading relative file outside of the selected path") {
try expect(try environment.loadTemplate(name: "../LoaderSpec.swift")).toThrow() try expect(try environment.loadTemplate(name: "../LoaderSpec.swift")).toThrow()
} }
} }
func testDictionaryLoader() { func testDictionaryLoader() {
let loader = DictionaryLoader(templates: [ let loader = DictionaryLoader(templates: [
"index.html": "Hello World" "index.html": "Hello World"
]) ])
let environment = Environment(loader: loader) let environment = Environment(loader: loader)
it("errors when a template cannot be found") { it("errors when a template cannot be found") {
try expect(try environment.loadTemplate(name: "unknown.html")).toThrow() try expect(try environment.loadTemplate(name: "unknown.html")).toThrow()
} }
it("errors when an array of templates cannot be found") { it("errors when an array of templates cannot be found") {
try expect(try environment.loadTemplate(names: ["unknown.html", "unknown2.html"])).toThrow() try expect(try environment.loadTemplate(names: ["unknown.html", "unknown2.html"])).toThrow()
} }
it("can load a template from a known templates") { it("can load a template from a known templates") {
_ = try environment.loadTemplate(name: "index.html") _ = try environment.loadTemplate(name: "index.html")
} }
it("can load a known template from a collection of templates") { it("can load a known template from a collection of templates") {
_ = try environment.loadTemplate(names: ["unknown.html", "index.html"]) _ = try environment.loadTemplate(names: ["unknown.html", "index.html"])
} }
} }
} }

View File

@@ -3,109 +3,109 @@ import Spectre
import XCTest import XCTest
final class NodeTests: XCTestCase { final class NodeTests: XCTestCase {
private let context = Context(dictionary: [ private let context = Context(dictionary: [
"name": "Kyle", "name": "Kyle",
"age": 27, "age": 27,
"items": [1, 2, 3] "items": [1, 2, 3]
]) ])
func testTextNode() { func testTextNode() {
it("renders the given text") { it("renders the given text") {
let node = TextNode(text: "Hello World") let node = TextNode(text: "Hello World")
try expect(try node.render(self.context)) == "Hello World" try expect(try node.render(self.context)) == "Hello World"
} }
it("Trims leading whitespace") { it("Trims leading whitespace") {
let text = " \n Some text " let text = " \n Some text "
let trimBehaviour = TrimBehaviour(leading: .whitespace, trailing: .nothing) let trimBehaviour = TrimBehaviour(leading: .whitespace, trailing: .nothing)
let node = TextNode(text: text, trimBehaviour: trimBehaviour) let node = TextNode(text: text, trimBehaviour: trimBehaviour)
try expect(try node.render(self.context)) == "\n Some text " try expect(try node.render(self.context)) == "\n Some text "
} }
it("Trims leading whitespace and one newline") { it("Trims leading whitespace and one newline") {
let text = "\n\n Some text " let text = "\n\n Some text "
let trimBehaviour = TrimBehaviour(leading: .whitespaceAndOneNewLine, trailing: .nothing) let trimBehaviour = TrimBehaviour(leading: .whitespaceAndOneNewLine, trailing: .nothing)
let node = TextNode(text: text, trimBehaviour: trimBehaviour) let node = TextNode(text: text, trimBehaviour: trimBehaviour)
try expect(try node.render(self.context)) == "\n Some text " try expect(try node.render(self.context)) == "\n Some text "
} }
it("Trims leading whitespace and one newline") { it("Trims leading whitespace and one newline") {
let text = "\n\n Some text " let text = "\n\n Some text "
let trimBehaviour = TrimBehaviour(leading: .whitespaceAndNewLines, trailing: .nothing) let trimBehaviour = TrimBehaviour(leading: .whitespaceAndNewLines, trailing: .nothing)
let node = TextNode(text: text, trimBehaviour: trimBehaviour) let node = TextNode(text: text, trimBehaviour: trimBehaviour)
try expect(try node.render(self.context)) == "Some text " try expect(try node.render(self.context)) == "Some text "
} }
it("Trims trailing whitespace") { it("Trims trailing whitespace") {
let text = " Some text \n" let text = " Some text \n"
let trimBehaviour = TrimBehaviour(leading: .nothing, trailing: .whitespace) let trimBehaviour = TrimBehaviour(leading: .nothing, trailing: .whitespace)
let node = TextNode(text: text, trimBehaviour: trimBehaviour) let node = TextNode(text: text, trimBehaviour: trimBehaviour)
try expect(try node.render(self.context)) == " Some text\n" try expect(try node.render(self.context)) == " Some text\n"
} }
it("Trims trailing whitespace and one newline") { it("Trims trailing whitespace and one newline") {
let text = " Some text \n \n " let text = " Some text \n \n "
let trimBehaviour = TrimBehaviour(leading: .nothing, trailing: .whitespaceAndOneNewLine) let trimBehaviour = TrimBehaviour(leading: .nothing, trailing: .whitespaceAndOneNewLine)
let node = TextNode(text: text, trimBehaviour: trimBehaviour) let node = TextNode(text: text, trimBehaviour: trimBehaviour)
try expect(try node.render(self.context)) == " Some text \n " try expect(try node.render(self.context)) == " Some text \n "
} }
it("Trims trailing whitespace and newlines") { it("Trims trailing whitespace and newlines") {
let text = " Some text \n \n " let text = " Some text \n \n "
let trimBehaviour = TrimBehaviour(leading: .nothing, trailing: .whitespaceAndNewLines) let trimBehaviour = TrimBehaviour(leading: .nothing, trailing: .whitespaceAndNewLines)
let node = TextNode(text: text, trimBehaviour: trimBehaviour) let node = TextNode(text: text, trimBehaviour: trimBehaviour)
try expect(try node.render(self.context)) == " Some text" try expect(try node.render(self.context)) == " Some text"
} }
it("Trims all whitespace") { it("Trims all whitespace") {
let text = " \n \nSome text \n " let text = " \n \nSome text \n "
let trimBehaviour = TrimBehaviour(leading: .whitespaceAndNewLines, trailing: .whitespaceAndNewLines) let trimBehaviour = TrimBehaviour(leading: .whitespaceAndNewLines, trailing: .whitespaceAndNewLines)
let node = TextNode(text: text, trimBehaviour: trimBehaviour) let node = TextNode(text: text, trimBehaviour: trimBehaviour)
try expect(try node.render(self.context)) == "Some text" try expect(try node.render(self.context)) == "Some text"
} }
} }
func testVariableNode() { func testVariableNode() {
it("resolves and renders the variable") { it("resolves and renders the variable") {
let node = VariableNode(variable: Variable("name")) let node = VariableNode(variable: Variable("name"))
try expect(try node.render(self.context)) == "Kyle" try expect(try node.render(self.context)) == "Kyle"
} }
it("resolves and renders a non string variable") { it("resolves and renders a non string variable") {
let node = VariableNode(variable: Variable("age")) let node = VariableNode(variable: Variable("age"))
try expect(try node.render(self.context)) == "27" try expect(try node.render(self.context)) == "27"
} }
} }
func testRendering() { func testRendering() {
it("renders the nodes") { it("renders the nodes") {
let nodes: [NodeType] = [ let nodes: [NodeType] = [
TextNode(text: "Hello "), TextNode(text: "Hello "),
VariableNode(variable: "name") VariableNode(variable: "name")
] ]
try expect(try renderNodes(nodes, self.context)) == "Hello Kyle" try expect(try renderNodes(nodes, self.context)) == "Hello Kyle"
} }
it("correctly throws a nodes failure") { it("correctly throws a nodes failure") {
let nodes: [NodeType] = [ let nodes: [NodeType] = [
TextNode(text: "Hello "), TextNode(text: "Hello "),
VariableNode(variable: "name"), VariableNode(variable: "name"),
ErrorNode() ErrorNode()
] ]
try expect(try renderNodes(nodes, self.context)).toThrow(TemplateSyntaxError("Custom Error")) try expect(try renderNodes(nodes, self.context)).toThrow(TemplateSyntaxError("Custom Error"))
} }
} }
func testRenderingBooleans() { func testRenderingBooleans() {
it("can render true & false") { it("can render true & false") {
try expect(Template(templateString: "{{ true }}").render()) == "true" try expect(Template(templateString: "{{ true }}").render()) == "true"
try expect(Template(templateString: "{{ false }}").render()) == "false" try expect(Template(templateString: "{{ false }}").render()) == "false"
} }
it("can resolve variable") { it("can resolve variable") {
let template = Template(templateString: "{{ value == \"known\" }}") let template = Template(templateString: "{{ value == \"known\" }}")
try expect(template.render(["value": "known"])) == "true" try expect(template.render(["value": "known"])) == "true"
try expect(template.render(["value": "unknown"])) == "false" try expect(template.render(["value": "unknown"])) == "false"
} }
it("can render a boolean expression") { it("can render a boolean expression") {
try expect(Template(templateString: "{{ 1 > 0 }}").render()) == "true" try expect(Template(templateString: "{{ 1 > 0 }}").render()) == "true"
try expect(Template(templateString: "{{ 1 == 2 }}").render()) == "false" try expect(Template(templateString: "{{ 1 == 2 }}").render()) == "false"
} }
} }
} }

View File

@@ -3,48 +3,48 @@ import Spectre
import XCTest import XCTest
final class NowNodeTests: XCTestCase { final class NowNodeTests: XCTestCase {
func testParsing() { func testParsing() {
it("parses default format without any now arguments") { it("parses default format without any now arguments") {
#if os(Linux) #if os(Linux)
throw skip() throw skip()
#else #else
let tokens: [Token] = [ .block(value: "now", at: .unknown) ] let tokens: [Token] = [ .block(value: "now", at: .unknown) ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
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\""
#endif #endif
} }
it("parses now with a format") { it("parses now with a format") {
#if os(Linux) #if os(Linux)
throw skip() throw skip()
#else #else
let tokens: [Token] = [ .block(value: "now \"HH:mm\"", at: .unknown) ] let tokens: [Token] = [ .block(value: "now \"HH:mm\"", at: .unknown) ]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
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) == "\"HH:mm\"" try expect(node?.format.variable) == "\"HH:mm\""
#endif #endif
} }
} }
func testRendering() { func testRendering() {
it("renders the date") { it("renders the date") {
#if os(Linux) #if os(Linux)
throw skip() throw skip()
#else #else
let node = NowNode(format: Variable("\"yyyy-MM-dd\"")) let node = NowNode(format: Variable("\"yyyy-MM-dd\""))
let formatter = DateFormatter() let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd" formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.string(from: Date()) let date = formatter.string(from: Date())
try expect(try node.render(Context())) == date try expect(try node.render(Context())) == date
#endif #endif
} }
} }
} }

View File

@@ -3,77 +3,77 @@ import Spectre
import XCTest import XCTest
final class TokenParserTests: XCTestCase { final class TokenParserTests: XCTestCase {
func testTextToken() throws { func testTextToken() throws {
let parser = TokenParser(tokens: [ let parser = TokenParser(tokens: [
.text(value: "Hello World", at: .unknown) .text(value: "Hello World", at: .unknown)
], environment: Environment()) ], environment: Environment())
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"
} }
func testVariableToken() throws { func testVariableToken() throws {
let parser = TokenParser(tokens: [ let parser = TokenParser(tokens: [
.variable(value: "'name'", at: .unknown) .variable(value: "'name'", at: .unknown)
], environment: Environment()) ], environment: Environment())
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"
} }
func testCommentToken() throws { func testCommentToken() throws {
let parser = TokenParser(tokens: [ let parser = TokenParser(tokens: [
.comment(value: "Secret stuff!", at: .unknown) .comment(value: "Secret stuff!", at: .unknown)
], environment: Environment()) ], environment: Environment())
let nodes = try parser.parse() let nodes = try parser.parse()
try expect(nodes.count) == 0 try expect(nodes.count) == 0
} }
func testTagToken() throws { func testTagToken() throws {
let simpleExtension = Extension() let simpleExtension = Extension()
simpleExtension.registerSimpleTag("known") { _ in simpleExtension.registerSimpleTag("known") { _ in
"" ""
} }
let parser = TokenParser(tokens: [ let parser = TokenParser(tokens: [
.block(value: "known", at: .unknown) .block(value: "known", at: .unknown)
], environment: Environment(extensions: [simpleExtension])) ], environment: Environment(extensions: [simpleExtension]))
let nodes = try parser.parse() let nodes = try parser.parse()
try expect(nodes.count) == 1 try expect(nodes.count) == 1
} }
func testErrorUnknownTag() throws { func testErrorUnknownTag() throws {
let tokens: [Token] = [.block(value: "unknown", at: .unknown)] let tokens: [Token] = [.block(value: "unknown", at: .unknown)]
let parser = TokenParser(tokens: tokens, environment: Environment()) let parser = TokenParser(tokens: tokens, environment: Environment())
try expect(try parser.parse()).toThrow(TemplateSyntaxError( try expect(try parser.parse()).toThrow(TemplateSyntaxError(
reason: "Unknown template tag 'unknown'", reason: "Unknown template tag 'unknown'",
token: tokens.first token: tokens.first
)) ))
} }
func testTransformWhitespaceBehaviourToTrimBehaviour() throws { func testTransformWhitespaceBehaviourToTrimBehaviour() throws {
let simpleExtension = Extension() let simpleExtension = Extension()
simpleExtension.registerSimpleTag("known") { _ in "" } simpleExtension.registerSimpleTag("known") { _ in "" }
let parser = TokenParser(tokens: [ let parser = TokenParser(tokens: [
.block(value: "known", at: .unknown, whitespace: WhitespaceBehaviour(leading: .unspecified, trailing: .trim)), .block(value: "known", at: .unknown, whitespace: WhitespaceBehaviour(leading: .unspecified, trailing: .trim)),
.text(value: " \nSome text ", at: .unknown), .text(value: " \nSome text ", at: .unknown),
.block(value: "known", at: .unknown, whitespace: WhitespaceBehaviour(leading: .keep, trailing: .trim)) .block(value: "known", at: .unknown, whitespace: WhitespaceBehaviour(leading: .keep, trailing: .trim))
], environment: Environment(extensions: [simpleExtension])) ], environment: Environment(extensions: [simpleExtension]))
let nodes = try parser.parse() let nodes = try parser.parse()
try expect(nodes.count) == 3 try expect(nodes.count) == 3
let textNode = nodes[1] as? TextNode let textNode = nodes[1] as? TextNode
try expect(textNode?.text) == " \nSome text " try expect(textNode?.text) == " \nSome text "
try expect(textNode?.trimBehaviour) == TrimBehaviour(leading: .whitespaceAndNewLines, trailing: .nothing) try expect(textNode?.trimBehaviour) == TrimBehaviour(leading: .whitespaceAndNewLines, trailing: .nothing)
} }
} }

View File

@@ -3,68 +3,68 @@ import Stencil
import XCTest import XCTest
final class StencilTests: XCTestCase { final class StencilTests: XCTestCase {
private lazy var environment: Environment = { private lazy var environment: Environment = {
let exampleExtension = Extension() let exampleExtension = Extension()
exampleExtension.registerSimpleTag("simpletag") { _ in exampleExtension.registerSimpleTag("simpletag") { _ in
"Hello World" "Hello World"
} }
exampleExtension.registerTag("customtag") { _, token in exampleExtension.registerTag("customtag") { _, token in
CustomNode(token: token) CustomNode(token: token)
} }
return Environment(extensions: [exampleExtension]) return Environment(extensions: [exampleExtension])
}() }()
func testStencil() { func testStencil() {
it("can render the README example") { it("can render the README example") {
let templateString = """ let templateString = """
There are {{ articles.count }} articles. There are {{ articles.count }} articles.
{% for article in articles %}\ {% for article in articles %}\
- {{ article.title }} by {{ article.author }}. - {{ article.title }} by {{ article.author }}.
{% endfor %} {% endfor %}
""" """
let context = [ let context = [
"articles": [ "articles": [
Article(title: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"), Article(title: "Migrating from OCUnit to XCTest", author: "Kyle Fuller"),
Article(title: "Memory Management with ARC", author: "Kyle Fuller") Article(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)
try expect(result) == """ try expect(result) == """
There are 2 articles. There are 2 articles.
- Migrating from OCUnit to XCTest by Kyle Fuller. - Migrating from OCUnit to XCTest by Kyle Fuller.
- Memory Management with ARC by Kyle Fuller. - Memory Management with ARC by Kyle Fuller.
""" """
} }
it("can render a custom template tag") { it("can render a custom template tag") {
let result = try self.environment.renderTemplate(string: "{% customtag %}") let result = try self.environment.renderTemplate(string: "{% customtag %}")
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
it("can render a simple custom tag") { it("can render a simple custom tag") {
let result = try self.environment.renderTemplate(string: "{% simpletag %}") let result = try self.environment.renderTemplate(string: "{% simpletag %}")
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
} }
} }
// MARK: - Helpers // MARK: - Helpers
private struct CustomNode: NodeType { private struct CustomNode: NodeType {
let token: Token? let token: Token?
func render(_ context: Context) throws -> String { func render(_ context: Context) throws -> String {
"Hello World" "Hello World"
} }
} }
private struct Article { private struct Article {
let title: String let title: String
let author: String let author: String
} }

View File

@@ -3,17 +3,17 @@ import Spectre
import XCTest import XCTest
final class TemplateTests: XCTestCase { final class TemplateTests: XCTestCase {
func testTemplate() { func testTemplate() {
it("can render a template from a string") { it("can render a template from a string") {
let template = Template(templateString: "Hello World") let template = Template(templateString: "Hello World")
let result = try template.render([ "name": "Kyle" ]) let result = try template.render([ "name": "Kyle" ])
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
it("can render a template from a string literal") { it("can render a template from a string literal") {
let template: Template = "Hello World" let template: Template = "Hello World"
let result = try template.render([ "name": "Kyle" ]) let result = try template.render([ "name": "Kyle" ])
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
} }
} }

View File

@@ -3,32 +3,32 @@ import Spectre
import XCTest import XCTest
final class TokenTests: XCTestCase { final class TokenTests: XCTestCase {
func testToken() { func testToken() {
it("can split the contents into components") { it("can split the contents into components") {
let token = Token.text(value: "hello world", at: .unknown) let token = Token.text(value: "hello world", at: .unknown)
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]) == "world" try expect(components[1]) == "world"
} }
it("can split the contents into components with single quoted strings") { it("can split the contents into components with single quoted strings") {
let token = Token.text(value: "hello 'kyle fuller'", at: .unknown) let token = Token.text(value: "hello 'kyle fuller'", at: .unknown)
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'"
} }
it("can split the contents into components with double quoted strings") { it("can split the contents into components with double quoted strings") {
let token = Token.text(value: "hello \"kyle fuller\"", at: .unknown) let token = Token.text(value: "hello \"kyle fuller\"", at: .unknown)
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,357 +3,357 @@ import Spectre
import XCTest import XCTest
final class VariableTests: XCTestCase { final class VariableTests: XCTestCase {
private let context: Context = { private let context: Context = {
let ext = Extension() let ext = Extension()
ext.registerFilter("incr") { arg in ext.registerFilter("incr") { arg in
(arg.flatMap { toNumber(value: $0) } ?? 0) + 1 (arg.flatMap { toNumber(value: $0) } ?? 0) + 1
} }
let environment = Environment(extensions: [ext]) let environment = Environment(extensions: [ext])
var context = Context(dictionary: [ var context = Context(dictionary: [
"name": "Kyle", "name": "Kyle",
"contacts": ["Katie", "Carlton"], "contacts": ["Katie", "Carlton"],
"profiles": [ "profiles": [
"github": "kylef" "github": "kylef"
], ],
"counter": [ "counter": [
"count": "kylef" "count": "kylef"
], ],
"article": Article(author: Person(name: "Kyle")), "article": Article(author: Person(name: "Kyle")),
"blog": Blog(), "blog": Blog(),
"tuple": (one: 1, two: 2), "tuple": (one: 1, two: 2),
"dynamic": [ "dynamic": [
"enum": DynamicEnum.someValue, "enum": DynamicEnum.someValue,
"struct": DynamicStruct() "struct": DynamicStruct()
] ]
], environment: environment) ], environment: environment)
#if os(OSX) #if os(OSX)
context["object"] = Object() context["object"] = Object()
#endif #endif
return context return context
}() }()
func testLiterals() { func testLiterals() {
it("can resolve a string literal with double quotes") { it("can resolve a string literal with double quotes") {
let variable = Variable("\"name\"") let variable = Variable("\"name\"")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "name" try expect(result) == "name"
} }
it("can resolve a string literal with one double quote") { it("can resolve a string literal with one double quote") {
let variable = Variable("\"") let variable = Variable("\"")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result).to.beNil() try expect(result).to.beNil()
} }
it("can resolve a string literal with single quotes") { it("can resolve a string literal with single quotes") {
let variable = Variable("'name'") let variable = Variable("'name'")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "name" try expect(result) == "name"
} }
it("can resolve a string literal with one single quote") { it("can resolve a string literal with one single quote") {
let variable = Variable("'") let variable = Variable("'")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result).to.beNil() try expect(result).to.beNil()
} }
it("can resolve an integer literal") { it("can resolve an integer literal") {
let variable = Variable("5") let variable = Variable("5")
let result = try variable.resolve(self.context) as? Int let result = try variable.resolve(self.context) as? Int
try expect(result) == 5 try expect(result) == 5
} }
it("can resolve an float literal") { it("can resolve an float literal") {
let variable = Variable("3.14") let variable = Variable("3.14")
let result = try variable.resolve(self.context) as? Number let result = try variable.resolve(self.context) as? Number
try expect(result) == 3.14 try expect(result) == 3.14
} }
it("can resolve boolean literal") { it("can resolve boolean literal") {
try expect(Variable("true").resolve(self.context) as? Bool) == true try expect(Variable("true").resolve(self.context) as? Bool) == true
try expect(Variable("false").resolve(self.context) as? Bool) == false try expect(Variable("false").resolve(self.context) as? Bool) == false
try expect(Variable("0").resolve(self.context) as? Int) == 0 try expect(Variable("0").resolve(self.context) as? Int) == 0
try expect(Variable("1").resolve(self.context) as? Int) == 1 try expect(Variable("1").resolve(self.context) as? Int) == 1
} }
} }
func testVariable() { func testVariable() {
it("can resolve a string variable") { it("can resolve a string variable") {
let variable = Variable("name") let variable = Variable("name")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
} }
func testDictionary() { func testDictionary() {
it("can resolve an item from a dictionary") { it("can resolve an item from a dictionary") {
let variable = Variable("profiles.github") let variable = Variable("profiles.github")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "kylef" try expect(result) == "kylef"
} }
it("can get the count of a dictionary") { it("can get the count of a dictionary") {
let variable = Variable("profiles.count") let variable = Variable("profiles.count")
let result = try variable.resolve(self.context) as? Int let result = try variable.resolve(self.context) as? Int
try expect(result) == 1 try expect(result) == 1
} }
} }
func testArray() { func testArray() {
it("can resolve an item from an array via it's index") { 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(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Katie" try expect(result) == "Katie"
let variable1 = Variable("contacts.1") let variable1 = Variable("contacts.1")
let result1 = try variable1.resolve(self.context) as? String let result1 = try variable1.resolve(self.context) as? String
try expect(result1) == "Carlton" try expect(result1) == "Carlton"
} }
it("can resolve an item from an array via unknown index") { it("can resolve an item from an array via unknown index") {
let variable = Variable("contacts.5") let variable = Variable("contacts.5")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result).to.beNil() try expect(result).to.beNil()
let variable1 = Variable("contacts.-5") let variable1 = Variable("contacts.-5")
let result1 = try variable1.resolve(self.context) as? String let result1 = try variable1.resolve(self.context) as? String
try expect(result1).to.beNil() try expect(result1).to.beNil()
} }
it("can resolve the first item from an array") { it("can resolve the first item from an array") {
let variable = Variable("contacts.first") let variable = Variable("contacts.first")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Katie" try expect(result) == "Katie"
} }
it("can resolve the last item from an array") { it("can resolve the last item from an array") {
let variable = Variable("contacts.last") let variable = Variable("contacts.last")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Carlton" try expect(result) == "Carlton"
} }
} }
func testDynamicMemberLookup() { func testDynamicMemberLookup() {
it("can resolve dynamic member lookup") { it("can resolve dynamic member lookup") {
let variable = Variable("dynamic.struct.test") let variable = Variable("dynamic.struct.test")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "this is a dynamic response" try expect(result) == "this is a dynamic response"
} }
it("can resolve dynamic enum rawValue") { it("can resolve dynamic enum rawValue") {
let variable = Variable("dynamic.enum.rawValue") let variable = Variable("dynamic.enum.rawValue")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "this is raw value" try expect(result) == "this is raw value"
} }
} }
func testReflection() { func testReflection() {
it("can resolve a property with reflection") { it("can resolve a property with reflection") {
let variable = Variable("article.author.name") let variable = Variable("article.author.name")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
it("can resolve a value via reflection") { it("can resolve a value via reflection") {
let variable = Variable("blog.articles.0.author.name") let variable = Variable("blog.articles.0.author.name")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
it("can resolve a superclass value via reflection") { it("can resolve a superclass value via reflection") {
let variable = Variable("blog.url") let variable = Variable("blog.url")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "blog.com" try expect(result) == "blog.com"
} }
it("can resolve optional variable property using reflection") { it("can resolve optional variable property using reflection") {
let variable = Variable("blog.featuring.author.name") let variable = Variable("blog.featuring.author.name")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Jhon" try expect(result) == "Jhon"
} }
} }
func testKVO() { func testKVO() {
#if os(OSX) #if os(OSX)
it("can resolve a value via KVO") { it("can resolve a value via KVO") {
let variable = Variable("object.title") let variable = Variable("object.title")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Hello World" try expect(result) == "Hello World"
} }
it("can resolve a superclass value via KVO") { it("can resolve a superclass value via KVO") {
let variable = Variable("object.name") let variable = Variable("object.name")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Foo" try expect(result) == "Foo"
} }
it("does not crash on KVO") { it("does not crash on KVO") {
let variable = Variable("object.fullname") let variable = Variable("object.fullname")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result).to.beNil() try expect(result).to.beNil()
} }
#endif #endif
} }
func testTuple() { func testTuple() {
it("can resolve tuple by index") { it("can resolve tuple by index") {
let variable = Variable("tuple.0") let variable = Variable("tuple.0")
let result = try variable.resolve(self.context) as? Int let result = try variable.resolve(self.context) as? Int
try expect(result) == 1 try expect(result) == 1
} }
it("can resolve tuple by label") { it("can resolve tuple by label") {
let variable = Variable("tuple.two") let variable = Variable("tuple.two")
let result = try variable.resolve(self.context) as? Int let result = try variable.resolve(self.context) as? Int
try expect(result) == 2 try expect(result) == 2
} }
} }
func testOptional() { func testOptional() {
it("does not render Optional") { it("does not render Optional") {
var array: [Any?] = [1, nil] var array: [Any?] = [1, nil]
array.append(array) array.append(array)
let context = Context(dictionary: ["values": array]) let context = Context(dictionary: ["values": array])
try expect(VariableNode(variable: "values").render(context)) == "[1, nil, [1, nil]]" try expect(VariableNode(variable: "values").render(context)) == "[1, nil, [1, nil]]"
try expect(VariableNode(variable: "values.1").render(context)) == "" try expect(VariableNode(variable: "values.1").render(context)) == ""
} }
} }
func testSubscripting() { func testSubscripting() {
it("can resolve a property subscript via reflection") { it("can resolve a property subscript via reflection") {
try self.context.push(dictionary: ["property": "name"]) { try self.context.push(dictionary: ["property": "name"]) {
let variable = Variable("article.author[property]") let variable = Variable("article.author[property]")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
} }
it("can subscript an array with a valid index") { it("can subscript an array with a valid index") {
try self.context.push(dictionary: ["property": 0]) { try self.context.push(dictionary: ["property": 0]) {
let variable = Variable("contacts[property]") let variable = Variable("contacts[property]")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Katie" try expect(result) == "Katie"
} }
} }
it("can subscript an array with an unknown index") { it("can subscript an array with an unknown index") {
try self.context.push(dictionary: ["property": 5]) { try self.context.push(dictionary: ["property": 5]) {
let variable = Variable("contacts[property]") let variable = Variable("contacts[property]")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result).to.beNil() try expect(result).to.beNil()
} }
} }
#if os(OSX) #if os(OSX)
it("can resolve a subscript via KVO") { it("can resolve a subscript via KVO") {
try self.context.push(dictionary: ["property": "name"]) { try self.context.push(dictionary: ["property": "name"]) {
let variable = Variable("object[property]") let variable = Variable("object[property]")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Foo" try expect(result) == "Foo"
} }
} }
#endif #endif
it("can resolve an optional subscript via reflection") { it("can resolve an optional subscript via reflection") {
try self.context.push(dictionary: ["property": "featuring"]) { try self.context.push(dictionary: ["property": "featuring"]) {
let variable = Variable("blog[property].author.name") let variable = Variable("blog[property].author.name")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Jhon" try expect(result) == "Jhon"
} }
} }
} }
func testMultipleSubscripting() { func testMultipleSubscripting() {
it("can resolve multiple subscripts") { it("can resolve multiple subscripts") {
try self.context.push(dictionary: [ try self.context.push(dictionary: [
"prop1": "articles", "prop1": "articles",
"prop2": 0, "prop2": 0,
"prop3": "name" "prop3": "name"
]) { ]) {
let variable = Variable("blog[prop1][prop2].author[prop3]") let variable = Variable("blog[prop1][prop2].author[prop3]")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
} }
it("can resolve nested subscripts") { it("can resolve nested subscripts") {
try self.context.push(dictionary: [ try self.context.push(dictionary: [
"prop1": "prop2", "prop1": "prop2",
"ref": ["prop2": "name"] "ref": ["prop2": "name"]
]) { ]) {
let variable = Variable("article.author[ref[prop1]]") let variable = Variable("article.author[ref[prop1]]")
let result = try variable.resolve(self.context) as? String let result = try variable.resolve(self.context) as? String
try expect(result) == "Kyle" try expect(result) == "Kyle"
} }
} }
it("throws for invalid keypath syntax") { it("throws for invalid keypath syntax") {
try self.context.push(dictionary: ["prop": "name"]) { try self.context.push(dictionary: ["prop": "name"]) {
let samples = [ let samples = [
".", ".",
"..", "..",
".test", ".test",
"test..test", "test..test",
"[prop]", "[prop]",
"article.author[prop", "article.author[prop",
"article.author[[prop]", "article.author[[prop]",
"article.author[prop]]", "article.author[prop]]",
"article.author[]", "article.author[]",
"article.author[[]]", "article.author[[]]",
"article.author[prop][]", "article.author[prop][]",
"article.author[prop]comments", "article.author[prop]comments",
"article.author[.]" "article.author[.]"
] ]
for lookup in samples { for lookup in samples {
let variable = Variable(lookup) let variable = Variable(lookup)
try expect(variable.resolve(self.context)).toThrow() try expect(variable.resolve(self.context)).toThrow()
} }
} }
} }
} }
func testRangeVariable() { func testRangeVariable() {
func makeVariable(_ token: String) throws -> RangeVariable? { func makeVariable(_ token: String) throws -> RangeVariable? {
let token = Token.variable(value: token, at: .unknown) let token = Token.variable(value: token, at: .unknown)
return try RangeVariable(token.contents, environment: context.environment, containedIn: token) return try RangeVariable(token.contents, environment: context.environment, containedIn: token)
} }
it("can resolve closed range as array") { it("can resolve closed range as array") {
let result = try makeVariable("1...3")?.resolve(self.context) as? [Int] let result = try makeVariable("1...3")?.resolve(self.context) as? [Int]
try expect(result) == [1, 2, 3] try expect(result) == [1, 2, 3]
} }
it("can resolve decreasing closed range as reversed array") { it("can resolve decreasing closed range as reversed array") {
let result = try makeVariable("3...1")?.resolve(self.context) as? [Int] let result = try makeVariable("3...1")?.resolve(self.context) as? [Int]
try expect(result) == [3, 2, 1] try expect(result) == [3, 2, 1]
} }
it("can use filter on range variables") { it("can use filter on range variables") {
let result = try makeVariable("1|incr...3|incr")?.resolve(self.context) as? [Int] let result = try makeVariable("1|incr...3|incr")?.resolve(self.context) as? [Int]
try expect(result) == [2, 3, 4] try expect(result) == [2, 3, 4]
} }
it("throws when left value is not int") { it("throws when left value is not int") {
let template: Template = "{% for i in k...j %}{{ i }}{% endfor %}" let template: Template = "{% for i in k...j %}{{ i }}{% endfor %}"
try expect(try template.render(Context(dictionary: ["j": 3, "k": "1"]))).toThrow() try expect(try template.render(Context(dictionary: ["j": 3, "k": "1"]))).toThrow()
} }
it("throws when right value is not int") { it("throws when right value is not int") {
let variable = try makeVariable("k...j") let variable = try makeVariable("k...j")
try expect(try variable?.resolve(Context(dictionary: ["j": "3", "k": 1]))).toThrow() try expect(try variable?.resolve(Context(dictionary: ["j": "3", "k": 1]))).toThrow()
} }
it("throws is left range value is missing") { it("throws is left range value is missing") {
try expect(makeVariable("...1")).toThrow() try expect(makeVariable("...1")).toThrow()
} }
it("throws is right range value is missing") { it("throws is right range value is missing") {
try expect(makeVariable("1...")).toThrow() try expect(makeVariable("1...")).toThrow()
} }
} }
} }
// MARK: - Helpers // MARK: - Helpers
@@ -361,38 +361,38 @@ final class VariableTests: XCTestCase {
#if os(OSX) #if os(OSX)
@objc @objc
class Superclass: NSObject { class Superclass: NSObject {
@objc let name = "Foo" @objc let name = "Foo"
} }
@objc @objc
class Object: Superclass { class Object: Superclass {
@objc let title = "Hello World" @objc let title = "Hello World"
} }
#endif #endif
private struct Person { private struct Person {
let name: String let name: String
} }
private struct Article { private struct Article {
let author: Person let author: Person
} }
private class WebSite { private class WebSite {
let url: String = "blog.com" let url: String = "blog.com"
} }
private class Blog: WebSite { private class Blog: WebSite {
let articles: [Article] = [Article(author: Person(name: "Kyle"))] let articles: [Article] = [Article(author: Person(name: "Kyle"))]
let featuring: Article? = Article(author: Person(name: "Jhon")) let featuring: Article? = Article(author: Person(name: "Jhon"))
} }
@dynamicMemberLookup @dynamicMemberLookup
private struct DynamicStruct: DynamicMemberLookup { private struct DynamicStruct: DynamicMemberLookup {
subscript(dynamicMember member: String) -> Any? { subscript(dynamicMember member: String) -> Any? {
member == "test" ? "this is a dynamic response" : nil member == "test" ? "this is a dynamic response" : nil
} }
} }
private enum DynamicEnum: String, DynamicMemberLookup { private enum DynamicEnum: String, DynamicMemberLookup {
case someValue = "this is raw value" case someValue = "this is raw value"
} }