Skip to content

Commit

Permalink
set correct indention
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Büth committed Jan 9, 2019
1 parent 4268f12 commit 2bf203c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 96 deletions.
40 changes: 20 additions & 20 deletions Sources/StencilSwiftKit/ImportNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@
import Stencil

class ImportNode: NodeType {
var token: Token?
var token: Token?

let templateName: Variable
let templateName: Variable

class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let bits = token.components()
class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let bits = token.components()

guard bits.count == 2 else {
throw TemplateSyntaxError("'import' tag requires one argument, the template file to be imported.")
}

return ImportNode(templateName: Variable(bits[1]))
}

init(templateName: Variable) {
self.templateName = templateName
guard bits.count == 2 else {
throw TemplateSyntaxError("'import' tag requires one argument, the template file to be imported.")
}

func render(_ context: Context) throws -> String {
guard let templateName = try self.templateName.resolve(context) as? String else {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
}
return ImportNode(templateName: Variable(bits[1]))
}

let template = try context.environment.loadTemplate(name: templateName)
init(templateName: Variable) {
self.templateName = templateName
}

_ = try template.render(context.flatten())
return ""
func render(_ context: Context) throws -> String {
guard let templateName = try self.templateName.resolve(context) as? String else {
throw TemplateSyntaxError("'\(self.templateName)' could not be resolved as a string")
}

let template = try context.environment.loadTemplate(name: templateName)

_ = try template.render(context.flatten())
return ""
}
}
152 changes: 76 additions & 76 deletions Tests/StencilSwiftKitTests/ImportNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,91 @@
import XCTest

class ImportNodeTests: XCTestCase {
func testParser() {
let tokens: [Token] = [
.block(value: "import \"Macros.stencil\"", at: .unknown)
]

let parser = TokenParser(tokens: tokens, environment: stencilSwiftEnvironment())
guard let nodes = try? parser.parse(),
let node = nodes.first as? ImportNode else {
XCTFail("Unable to parse tokens")
return
}

XCTAssertEqual(node.templateName.variable, "\"Macros.stencil\"")
func testParser() {
let tokens: [Token] = [
.block(value: "import \"Macros.stencil\"", at: .unknown)
]

let parser = TokenParser(tokens: tokens, environment: stencilSwiftEnvironment())
guard let nodes = try? parser.parse(),
let node = nodes.first as? ImportNode else {
XCTFail("Unable to parse tokens")
return
}

func testParserFail() {
do {
let tokens: [Token] = [
.block(value: "import", at: .unknown)
]
XCTAssertEqual(node.templateName.variable, "\"Macros.stencil\"")
}

let parser = TokenParser(tokens: tokens, environment: stencilSwiftEnvironment())
XCTAssertThrowsError(try parser.parse())
}
}
func testParserFail() {
do {
let tokens: [Token] = [
.block(value: "import", at: .unknown)
]

private final class LoaderMock: Loader {
var loadtemplateCalledWithName: String?
var loadtemplateCalledWithEnvironment: Environment?
var loadTemplateResult: Template?
var loadTemplateError: Error?

func loadTemplate(name: String, environment: Environment) throws -> Template {
loadtemplateCalledWithName = name
loadtemplateCalledWithEnvironment = environment
if let result = loadTemplateResult {
return result
} else if let error = loadTemplateError {
throw error
}
return Template(stringLiteral: "")
}
let parser = TokenParser(tokens: tokens, environment: stencilSwiftEnvironment())
XCTAssertThrowsError(try parser.parse())
}

func testRender() throws {
let loader = LoaderMock()
let environment = Environment(loader: loader, extensions: nil, templateClass: StencilSwiftTemplate.self)

let context = Context(environment: environment)
let node = ImportNode(templateName: Variable("\"Macros.stencil\""))
let output = try node.render(context)

XCTAssertEqual(output, "")
XCTAssertEqual(loader.loadtemplateCalledWithName, "Macros.stencil")
}

private final class LoaderMock: Loader {
var loadtemplateCalledWithName: String?
var loadtemplateCalledWithEnvironment: Environment?
var loadTemplateResult: Template?
var loadTemplateError: Error?

func loadTemplate(name: String, environment: Environment) throws -> Template {
loadtemplateCalledWithName = name
loadtemplateCalledWithEnvironment = environment
if let result = loadTemplateResult {
return result
} else if let error = loadTemplateError {
throw error
}
return Template(stringLiteral: "")
}

func testRenderLoaderFail() {
let loader = LoaderMock()
let environment = Environment(loader: loader, extensions: nil, templateClass: StencilSwiftTemplate.self)

let context = Context(environment: environment)
let node = ImportNode(templateName: Variable("\"Macros.stencil\""))
let someLoaderError = TemplateDoesNotExist(templateNames: ["Macros.stencil"])
loader.loadTemplateError = someLoaderError

XCTAssertThrowsError(try node.render(context), "") { error in
guard error as? TemplateDoesNotExist != nil else {
XCTFail(error.localizedDescription)
return
}
}
}

func testRender() throws {
let loader = LoaderMock()
let environment = Environment(loader: loader, extensions: nil, templateClass: StencilSwiftTemplate.self)

let context = Context(environment: environment)
let node = ImportNode(templateName: Variable("\"Macros.stencil\""))
let output = try node.render(context)

XCTAssertEqual(output, "")
XCTAssertEqual(loader.loadtemplateCalledWithName, "Macros.stencil")
}

func testRenderLoaderFail() {
let loader = LoaderMock()
let environment = Environment(loader: loader, extensions: nil, templateClass: StencilSwiftTemplate.self)

let context = Context(environment: environment)
let node = ImportNode(templateName: Variable("\"Macros.stencil\""))
let someLoaderError = TemplateDoesNotExist(templateNames: ["Macros.stencil"])
loader.loadTemplateError = someLoaderError

XCTAssertThrowsError(try node.render(context), "") { error in
guard error as? TemplateDoesNotExist != nil else {
XCTFail(error.localizedDescription)
return
}
}
}

func testRenderParamterFail() throws {
let loader = LoaderMock()
let environment = Environment(loader: loader, extensions: nil, templateClass: StencilSwiftTemplate.self)
func testRenderParamterFail() throws {
let loader = LoaderMock()
let environment = Environment(loader: loader, extensions: nil, templateClass: StencilSwiftTemplate.self)

let context = Context(environment: environment)
let node = ImportNode(templateName: Variable("a"))
let context = Context(environment: environment)
let node = ImportNode(templateName: Variable("a"))

XCTAssertThrowsError(try node.render(context), "") { error in
guard error as? TemplateSyntaxError != nil else {
XCTFail(error.localizedDescription)
return
}
}
XCTAssertThrowsError(try node.render(context), "") { error in
guard error as? TemplateSyntaxError != nil else {
XCTFail(error.localizedDescription)
return
}
}
}
}

0 comments on commit 2bf203c

Please sign in to comment.