Skip to content

Commit

Permalink
Merge pull request #10 from qutheory/vapor-12
Browse files Browse the repository at this point in the history
vapor 12
  • Loading branch information
tanner0101 authored Jun 28, 2016
2 parents 2d3698e + 274c883 commit 53cc79b
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Sources/main.swift
Packages
.build
Resources
*.xcodeproj
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ osx_image: xcode7.3
install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)"
script:
# Build Vapor Mustache
- swift build
# - swift build -c release
- swift build -c release

# Test Vapor Mustache
- swift test
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ let package = Package(
name: "VaporMustache",
dependencies: [
.Package(url: "https://github.com/Zewo/Mustache.git", majorVersion: 0, minor: 6),
.Package(url: "https://github.com/qutheory/vapor.git", majorVersion: 0, minor: 11)
.Package(url: "https://github.com/qutheory/vapor.git", majorVersion: 0, minor: 12)
]
)
1 change: 1 addition & 0 deletions Resources/Views/Includes/test-include-a.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
1 change: 1 addition & 0 deletions Resources/Views/Includes/test-include-b.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
5 changes: 5 additions & 0 deletions Resources/Views/test-view.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{>a}}

Hello, {{name}}

{{>b}}
8 changes: 6 additions & 2 deletions Sources/MustacheRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import Mustache
public class MustacheRenderer: RenderDriver {
static let currentName = "__current"

public enum Error: ErrorProtocol {
case file(String, ErrorProtocol)
}

public var includes: [String: String]

public init(files: [String: String] = [:]) {
public init(files: [String: String] = [:]) throws {
includes = [:]

for (name, file) in files {
Expand All @@ -16,7 +20,7 @@ public class MustacheRenderer: RenderDriver {

includes[name] = String(validatingUTF8: bytes)
} catch {
Log.warning("Could not open file \(file). Error: \(error)")
throw Error.file(file, error)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ public class Provider: Vapor.Provider {
includeFiles.forEach { (name, file) in
files[name] = application.workDir + "Resources/Views/" + file
}

View.renderers[".mustache"] = MustacheRenderer(files: files)

do {
View.renderers[".mustache"] = try MustacheRenderer(files: files)
} catch {
application.log.error("Could not configure Mustache: \(error)")
}
}

}
10 changes: 10 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#if os(Linux)

import XCTest
@testable import VaporMustacheTestSuite

XCTMain([
testCase(ProviderTests.allTests),
])

#endif
39 changes: 39 additions & 0 deletions Tests/VaporMustache/ProviderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import XCTest
@testable import VaporMustache
import Vapor

class ProviderTests: XCTestCase {
static let allTests = [
("testSaveAndFind", testSaveAndFind)
]

func testSaveAndFind() {
let provider = Provider(withIncludes: [
"a": "Includes/test-include-a.mustache",
"b": "Includes/test-include-b.mustache",
])

let app = Application(providers: [provider])

let name = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^*()"

do {
let view = try app.view("test-view.mustache", context: ["name": name])

let response = view.makeResponse()

switch response.body {
case .data(let bytes):
let encoded = String(name.characters.split(separator: "&").joined(separator: "&".characters))
let expected = "a\n\nHello, \(encoded)\n\nb"

let string = try bytes.toString()
XCTAssertEqual(string, expected, "Template did not render properly")
case .chunked(_):
XCTFail("Incorrect body type")
}
} catch {
XCTFail("Could not make view: \(error)")
}
}
}
Empty file.

0 comments on commit 53cc79b

Please sign in to comment.