-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// StencilSwiftKit UnitTests | ||
// Copyright © 2022 SwiftGen | ||
// MIT Licence | ||
// | ||
|
||
@testable import Stencil | ||
@testable import StencilSwiftKit | ||
import XCTest | ||
|
||
final class ImportNodeTests: XCTestCase { | ||
func testParser() { | ||
let tokens: [Token] = [.block(value: "import \"Common.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("\"Common.stencil\"")) | ||
} | ||
|
||
func testParserFail() { | ||
do { | ||
let tokens: [Token] = [.block(value: "import", at: .unknown)] | ||
|
||
let parser = TokenParser(tokens: tokens, environment: stencilSwiftEnvironment()) | ||
XCTAssertThrowsError(try parser.parse()) | ||
} | ||
} | ||
|
||
func testRenderIsEmpty() throws { | ||
let node = ImportNode(templateName: Variable("\"Common.stencil\"")) | ||
let env = stencilSwiftEnvironment(templates: ["Common.stencil": "Hello world!"]) | ||
let context = Context(dictionary: ["": ""], environment: env) | ||
let output = try node.render(context) | ||
|
||
XCTAssertEqual(output, "") | ||
} | ||
|
||
func testContextModification() throws { | ||
let node = ImportNode(templateName: Variable("\"Common.stencil\"")) | ||
let env = stencilSwiftEnvironment(templates: ["Common.stencil": "{% set x %}hello{% endset %}"]) | ||
let context = Context(dictionary: ["": ""], environment: env) | ||
_ = try node.render(context) | ||
|
||
guard let string = context["x"] as? String else { | ||
XCTFail("Unable to render import token") | ||
return | ||
} | ||
XCTAssertEqual(string, "hello") | ||
} | ||
} |