[Template] Throw when initialising with non-existant file

This commit is contained in:
Kyle Fuller
2015-10-18 10:53:10 -07:00
parent 4d76fb4e60
commit f0abd34c32
2 changed files with 9 additions and 7 deletions

View File

@@ -8,15 +8,12 @@ public class Template {
/// Create a template with the given name inside the given bundle
public convenience init(named:String, inBundle bundle:NSBundle? = nil) throws {
let url:NSURL
if let bundle = bundle {
url = bundle.URLForResource(named, withExtension: nil)!
let useBundle = bundle ?? NSBundle.mainBundle()
if let url = useBundle.URLForResource(named, withExtension: nil) {
try self.init(URL:url)
} else {
url = NSBundle.mainBundle().URLForResource(named, withExtension: nil)!
throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: nil)
}
try self.init(URL:url)
}
/// Create a template with a file found at the given URL

View File

@@ -20,6 +20,11 @@ class TemplateTests: XCTestCase {
XCTAssertEqual(result, "Hello Kyle!")
}
func testTemplateNamedInBundleThrowsForUnknownFile() {
let testBundle = NSBundle(forClass: self.dynamicType)
let template = try? Template(named: "test2.html", inBundle: testBundle)
XCTAssertNil(template)
}
func testTemplateWithNSURL() {
let testBundle = NSBundle(forClass: self.dynamicType)