Skip to content
This repository was archived by the owner on Apr 8, 2018. It is now read-only.

Commit 3c4c589

Browse files
committed
Wrap the compilation process into a single method.
1 parent 42780c0 commit 3c4c589

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

Diff for: MustacheTests/MustacheTests.swift

+21-15
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ import XCTest
1111

1212
class MustacheTests: XCTestCase {
1313
func testExample() {
14-
var lexer = Lexer("Hello {{name}}")
14+
let string = "Hello {{name}}"
1515
do {
16-
let tokens = try lexer.tokenize()
17-
let parser = Parser(tokens: tokens)
18-
let mainOperation = try parser.parse()
19-
let template = try! Template(operation: mainOperation)
16+
let template = try Mustache.compile(string)
2017
let context: DictionaryContext = [
2118
"name": "Stan"
2219
]
@@ -43,12 +40,10 @@ class MustacheTests: XCTestCase {
4340
}
4441

4542
func testExample3() {
46-
var lexer = Lexer("{{#in_ca}}\nWell, {{taxed_value}} dollars, after taxes.\n{{/in_ca}}{{^in_ca}}\nNo tax burden!\n{{/in_ca}}")
43+
let string = "{{#in_ca}}\nWell, {{taxed_value}} dollars, after taxes.\n{{/in_ca}}{{^in_ca}}\nNo tax burden!\n{{/in_ca}}"
4744
do {
48-
let tokens = try lexer.tokenize()
49-
let parser = Parser(tokens: tokens)
50-
let mainOperation = try parser.parse()
51-
let template = try! Template(operation: mainOperation)
45+
let template = try Mustache.compile(string)
46+
5247
let context: DictionaryContext = [
5348
"in_ca": ArrayContext([
5449
DictionaryContext([
@@ -80,12 +75,9 @@ class MustacheTests: XCTestCase {
8075
}
8176

8277
func testExample4() {
83-
var lexer = Lexer(" * {{name}}\n * {{age}}\n * {{company}}\n * {{{company}}}\n * {{&company}}")
78+
let string = " * {{name}}\n * {{age}}\n * {{company}}\n * {{{company}}}\n * {{&company}}"
8479
do {
85-
let tokens = try lexer.tokenize()
86-
let parser = Parser(tokens: tokens)
87-
let mainOperation = try parser.parse()
88-
let template = try! Template(operation: mainOperation)
80+
let template = try Mustache.compile(string)
8981
let context: DictionaryContext = [
9082
"name": "Stan",
9183
"age": "29",
@@ -101,6 +93,20 @@ class MustacheTests: XCTestCase {
10193
// This is an example of a performance test case.
10294
self.measureBlock {
10395
// Put the code you want to measure the time of here.
96+
for _ in 0..<1000 {
97+
let string = " * {{name}}\n * {{age}}\n * {{company}}\n * {{{company}}}\n * {{&company}}"
98+
do {
99+
let template = try Mustache.compile(string)
100+
let context: DictionaryContext = [
101+
"name": "Stan",
102+
"age": "29",
103+
"company": "trifia",
104+
]
105+
_ = template.render(context)
106+
} catch {
107+
XCTFail()
108+
}
109+
}
104110
}
105111
}
106112

Diff for: Sources/Mustache.swift

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ enum Error : ErrorType {
4949
case SyntaxError(String)
5050
}
5151

52-
53-
54-
// The parser
52+
struct Mustache {
53+
static func compile(string: String) throws -> Template {
54+
var lexer = Lexer(string)
55+
let tokens = try lexer.tokenize()
56+
57+
let parser = Parser(tokens: tokens)
58+
let mainOperation = try parser.parse()
59+
60+
return try Template(operation: mainOperation)
61+
}
62+
}

0 commit comments

Comments
 (0)