From f0abd34c320751cdb82b25e66db9b6a782fb27f6 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Sun, 18 Oct 2015 10:53:10 -0700 Subject: [PATCH] [Template] Throw when initialising with non-existant file --- Stencil/Template.swift | 11 ++++------- StencilTests/TemplateTests.swift | 5 +++++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Stencil/Template.swift b/Stencil/Template.swift index 301098d..605f1ae 100644 --- a/Stencil/Template.swift +++ b/Stencil/Template.swift @@ -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 diff --git a/StencilTests/TemplateTests.swift b/StencilTests/TemplateTests.swift index dce68e3..eb37904 100644 --- a/StencilTests/TemplateTests.swift +++ b/StencilTests/TemplateTests.swift @@ -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)