-
Notifications
You must be signed in to change notification settings - Fork 225
/
IncludeSpec.swift
78 lines (66 loc) · 2.74 KB
/
IncludeSpec.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Stencil
// Copyright © 2022 Stencil
// MIT Licence
//
import PathKit
import Spectre
@testable import Stencil
import XCTest
final class IncludeTests: XCTestCase {
private let path = Path(#file as String) + ".." + "fixtures"
private lazy var loader = FileSystemLoader(paths: [path])
private lazy var environment = Environment(loader: loader)
func testParsing() {
it("throws an error when no template is given") {
let tokens: [Token] = [ .block(value: "include", at: .unknown) ]
let parser = TokenParser(tokens: tokens, environment: Environment())
let error = TemplateSyntaxError(reason: """
'include' tag requires one argument, the template file to be included. \
A second optional argument can be used to specify the context that will \
be passed to the included file
""", token: tokens.first)
try expect(try parser.parse()).toThrow(error)
}
it("can parse a valid include block") {
let tokens: [Token] = [ .block(value: "include \"test.html\"", at: .unknown) ]
let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse()
let node = nodes.first as? IncludeNode
try expect(nodes.count) == 1
try expect(node?.templateName) == Variable("\"test.html\"")
}
}
func testRendering() {
it("throws an error when rendering without a loader") {
let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown))
do {
_ = try node.render(Context())
} catch {
try expect("\(error)") == "Template named `test.html` does not exist. No loaders found"
}
}
it("throws an error when it cannot find the included template") {
let node = IncludeNode(templateName: Variable("\"unknown.html\""), token: .block(value: "", at: .unknown))
do {
_ = try node.render(Context(environment: self.environment))
} catch {
try expect("\(error)".hasPrefix("Template named `unknown.html` does not exist in loader")).to.beTrue()
}
}
it("successfully renders a found included template") {
let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown))
let context = Context(dictionary: ["target": "World"], environment: self.environment)
let value = try node.render(context)
try expect(value) == "Hello World!"
}
it("successfully passes context") {
let template = Template(templateString: """
{% include "test.html" child %}
""")
let context = Context(dictionary: ["child": ["target": "World"]], environment: self.environment)
let value = try template.render(context)
try expect(value) == "Hello World!"
}
}
}